Skip to content

Commit 71f9f9a

Browse files
Rollup merge of rust-lang#155939 - scrabsha:view-types/feature-gate, r=nikomatsakis
Add feature gate for view_types experiment
2 parents fe86e5f + 77fb8d1 commit 71f9f9a

6 files changed

Lines changed: 74 additions & 1 deletion

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_feature/src/unstable.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -750,6 +750,8 @@ declare_features! (
750750
(internal, unsized_fn_params, "1.49.0", Some(48055)),
751751
/// Allows using the `#[used(linker)]` (or `#[used(compiler)]`) attribute.
752752
(unstable, used_with_arg, "1.60.0", Some(93798)),
753+
/// Allows view types.
754+
(unstable, view_types, "CURRENT_RUSTC_VERSION", Some(155938)),
753755
/// Target features on wasm.
754756
(unstable, wasm_target_feature, "1.30.0", Some(150260)),
755757
/// Allows use of attributes in `where` clauses.

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 }),

compiler/rustc_span/src/symbol.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2232,6 +2232,7 @@ symbols! {
22322232
verbatim,
22332233
version,
22342234
vfp2,
2235+
view_types,
22352236
vis,
22362237
visible_private_types,
22372238
volatile,
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
struct Foo {
2+
a: usize,
3+
b: usize,
4+
}
5+
6+
fn bar(a: &mut Foo.{ a }, b: &mut Foo.{ b }) {
7+
//~^ ERROR view types are experimental
8+
//~| ERROR view types are experimental
9+
a.a += 1;
10+
b.b += 1;
11+
}
12+
13+
fn main() {
14+
let mut foo = Foo { a: 0, b: 0 };
15+
bar(&mut foo, &mut foo);
16+
//~^ ERROR cannot borrow `foo` as mutable more than once at a time
17+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
error[E0658]: view types are experimental
2+
--> $DIR/feature-gate-view-types.rs:6:19
3+
|
4+
LL | fn bar(a: &mut Foo.{ a }, b: &mut Foo.{ b }) {
5+
| ^^^^^^
6+
|
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
10+
11+
error[E0658]: view types are experimental
12+
--> $DIR/feature-gate-view-types.rs:6:38
13+
|
14+
LL | fn bar(a: &mut Foo.{ a }, b: &mut Foo.{ b }) {
15+
| ^^^^^^
16+
|
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
20+
21+
error[E0499]: cannot borrow `foo` as mutable more than once at a time
22+
--> $DIR/feature-gate-view-types.rs:15:19
23+
|
24+
LL | bar(&mut foo, &mut foo);
25+
| --- -------- ^^^^^^^^ second mutable borrow occurs here
26+
| | |
27+
| | first mutable borrow occurs here
28+
| first borrow later used by call
29+
30+
error: aborting due to 3 previous errors
31+
32+
Some errors have detailed explanations: E0499, E0658.
33+
For more information about an error, try `rustc --explain E0499`.

0 commit comments

Comments
 (0)