Skip to content

Commit 77fb8d1

Browse files
committed
Parse view type syntax, mark it as unstable
1 parent d3992fa commit 77fb8d1

4 files changed

Lines changed: 45 additions & 40 deletions

File tree

compiler/rustc_ast_passes/src/feature_gate.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,7 @@ pub fn check_crate(krate: &ast::Crate, sess: &Session, features: &Features) {
510510
gate_all!(try_blocks_heterogeneous, "`try bikeshed` expression is experimental");
511511
gate_all!(unsafe_binders, "unsafe binder types are experimental");
512512
gate_all!(unsafe_fields, "`unsafe` fields are experimental");
513+
gate_all!(view_types, "view types are experimental");
513514
gate_all!(where_clause_attrs, "attributes in `where` clause are unstable");
514515
gate_all!(yeet_expr, "`do yeet` expression is experimental");
515516
// tidy-alphabetical-end

compiler/rustc_parse/src/parser/ty.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use crate::errors::{
1919
NeedPlusAfterTraitObjectLifetime, NestedCVariadicType, ReturnTypesUseThinArrow,
2020
};
2121
use crate::parser::item::FrontMatterParsingMode;
22-
use crate::parser::{FnContext, FnParseMode};
22+
use crate::parser::{ExpTokenPair, FnContext, FnParseMode};
2323
use crate::{exp, maybe_recover_from_interpolated_ty_qpath};
2424

2525
/// Signals whether parsing a type should allow `+`.
@@ -768,6 +768,25 @@ impl<'a> Parser<'a> {
768768
self.bump_with((dyn_tok, dyn_tok_sp));
769769
}
770770
let ty = self.parse_ty_no_plus()?;
771+
if self.token == TokenKind::Dot && self.look_ahead(1, |t| t.kind == TokenKind::OpenBrace) {
772+
// & [mut] <type> . { <fields> }
773+
// ^
774+
// we are here
775+
let view_start_span = self.token.span;
776+
self.bump();
777+
let fields = self
778+
.parse_delim_comma_seq(
779+
ExpTokenPair { tok: TokenKind::OpenBrace, token_type: TokenType::OpenBrace },
780+
ExpTokenPair { tok: TokenKind::CloseBrace, token_type: TokenType::CloseBrace },
781+
|p| p.parse_ident(),
782+
)?
783+
.0;
784+
// FIXME(scrabsha): actually propagate field view in the AST.
785+
let _ = fields;
786+
let view_end_span = self.prev_token.span;
787+
let span = view_start_span.to(view_end_span);
788+
self.psess.gated_spans.gate(sym::view_types, span);
789+
}
771790
Ok(match pinned {
772791
Pinnedness::Not => TyKind::Ref(opt_lifetime, MutTy { ty, mutbl }),
773792
Pinnedness::Pinned => TyKind::PinnedRef(opt_lifetime, MutTy { ty, mutbl }),
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
1-
//@ known-bug: #155938
2-
31
struct Foo {
42
a: usize,
53
b: usize,
64
}
75

86
fn bar(a: &mut Foo.{ a }, b: &mut Foo.{ b }) {
7+
//~^ ERROR view types are experimental
8+
//~| ERROR view types are experimental
99
a.a += 1;
1010
b.b += 1;
1111
}
1212

1313
fn main() {
1414
let mut foo = Foo { a: 0, b: 0 };
1515
bar(&mut foo, &mut foo);
16+
//~^ ERROR cannot borrow `foo` as mutable more than once at a time
1617
}
Lines changed: 21 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,33 @@
1-
error: expected parameter name, found `{`
2-
--> $DIR/feature-gate-view-types.rs:8:20
1+
error[E0658]: view types are experimental
2+
--> $DIR/feature-gate-view-types.rs:6:19
33
|
44
LL | fn bar(a: &mut Foo.{ a }, b: &mut Foo.{ b }) {
5-
| ^ expected parameter name
6-
7-
error: expected one of `!`, `(`, `)`, `,`, `::`, or `<`, found `.`
8-
--> $DIR/feature-gate-view-types.rs:8:19
5+
| ^^^^^^
96
|
10-
LL | fn bar(a: &mut Foo.{ a }, b: &mut Foo.{ b }) {
11-
| ^
12-
| |
13-
| expected one of `!`, `(`, `)`, `,`, `::`, or `<`
14-
| help: missing `,`
7+
= note: see issue #155938 <https://github.com/rust-lang/rust/issues/155938> for more information
8+
= help: add `#![feature(view_types)]` to the crate attributes to enable
9+
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
1510

16-
error: expected parameter name, found `{`
17-
--> $DIR/feature-gate-view-types.rs:8:39
11+
error[E0658]: view types are experimental
12+
--> $DIR/feature-gate-view-types.rs:6:38
1813
|
1914
LL | fn bar(a: &mut Foo.{ a }, b: &mut Foo.{ b }) {
20-
| ^ expected parameter name
21-
22-
error: expected one of `!`, `(`, `)`, `,`, `::`, or `<`, found `.`
23-
--> $DIR/feature-gate-view-types.rs:8:38
15+
| ^^^^^^
2416
|
25-
LL | fn bar(a: &mut Foo.{ a }, b: &mut Foo.{ b }) {
26-
| ^
27-
| |
28-
| expected one of `!`, `(`, `)`, `,`, `::`, or `<`
29-
| help: missing `,`
17+
= note: see issue #155938 <https://github.com/rust-lang/rust/issues/155938> for more information
18+
= help: add `#![feature(view_types)]` to the crate attributes to enable
19+
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
3020

31-
error[E0061]: this function takes 4 arguments but 2 arguments were supplied
32-
--> $DIR/feature-gate-view-types.rs:15:5
21+
error[E0499]: cannot borrow `foo` as mutable more than once at a time
22+
--> $DIR/feature-gate-view-types.rs:15:19
3323
|
3424
LL | bar(&mut foo, &mut foo);
35-
| ^^^-------------------- two arguments are missing
36-
|
37-
note: function defined here
38-
--> $DIR/feature-gate-view-types.rs:8:4
39-
|
40-
LL | fn bar(a: &mut Foo.{ a }, b: &mut Foo.{ b }) {
41-
| ^^^ -----------------
42-
help: provide the arguments
43-
|
44-
LL | bar(&mut foo, &mut foo, /* &mut Foo */, /* _ */);
45-
| +++++++++++++++++++++++++
25+
| --- -------- ^^^^^^^^ second mutable borrow occurs here
26+
| | |
27+
| | first mutable borrow occurs here
28+
| first borrow later used by call
4629

47-
error: aborting due to 5 previous errors
30+
error: aborting due to 3 previous errors
4831

49-
For more information about this error, try `rustc --explain E0061`.
32+
Some errors have detailed explanations: E0499, E0658.
33+
For more information about an error, try `rustc --explain E0499`.

0 commit comments

Comments
 (0)