Skip to content

Commit

Permalink
Feature gate
Browse files Browse the repository at this point in the history
  • Loading branch information
Jules-Bertholet committed Mar 26, 2024
1 parent 7aa8c6d commit fc0bc1d
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 9 deletions.
1 change: 1 addition & 0 deletions compiler/rustc_ast_passes/src/feature_gate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,7 @@ pub fn check_crate(krate: &ast::Crate, sess: &Session, features: &Features) {
gate_all!(unnamed_fields, "unnamed fields are not yet fully implemented");
gate_all!(fn_delegation, "functions delegation is not yet fully implemented");
gate_all!(postfix_match, "postfix match is experimental");
gate_all!(mut_ref, "mutable by-reference bindings are experimental");

if !visitor.features.never_patterns {
if let Some(spans) = spans.get(&sym::never_patterns) {
Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_feature/src/unstable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,8 @@ declare_features! (
(unstable, more_qualified_paths, "1.54.0", Some(86935)),
/// Allows the `#[must_not_suspend]` attribute.
(unstable, must_not_suspend, "1.57.0", Some(83310)),
/// Allows `mut ref` and `mut ref mut` identifier patterns.
(unstable, mut_ref, "CURRENT_RUSTC_VERSION", Some(123076)),
/// Allows using `#[naked]` on functions.
(unstable, naked_functions, "1.9.0", Some(90957)),
/// Allows specifying the as-needed link modifier
Expand Down
4 changes: 4 additions & 0 deletions compiler/rustc_parse/src/parser/pat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -779,6 +779,10 @@ impl<'a> Parser<'a> {
self.ban_mut_general_pat(mut_span, &pat, changed_any_binding);
}

if matches!(pat.kind, PatKind::Ident(BindingAnnotation(ByRef::Yes(_), Mutability::Mut), ..))
{
self.psess.gated_spans.gate(sym::mut_ref, pat.span);
}
Ok(pat.into_inner().kind)
}

Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_span/src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1184,6 +1184,7 @@ symbols! {
multiple_supertrait_upcastable,
must_not_suspend,
must_use,
mut_ref,
naked,
naked_functions,
name,
Expand Down
13 changes: 13 additions & 0 deletions tests/ui/feature-gates/feature-gate-mut-ref.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
fn main() {
let mut ref x = 10; //~ ERROR [E0658]
x = &11;
let ref mut y = 12;
*y = 13;
let mut ref mut z = 14; //~ ERROR [E0658]
z = &mut 15;

#[cfg(FALSE)]
let mut ref x = 10; //~ ERROR [E0658]
#[cfg(FALSE)]
let mut ref mut y = 10; //~ ERROR [E0658]
}
43 changes: 43 additions & 0 deletions tests/ui/feature-gates/feature-gate-mut-ref.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
error[E0658]: mutable by-reference bindings are experimental
--> $DIR/feature-gate-mut-ref.rs:2:17
|
LL | let mut ref x = 10;
| ^
|
= note: see issue #123076 <https://github.com/rust-lang/rust/issues/123076> for more information
= help: add `#![feature(mut_ref)]` to the crate attributes to enable
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date

error[E0658]: mutable by-reference bindings are experimental
--> $DIR/feature-gate-mut-ref.rs:6:21
|
LL | let mut ref mut z = 14;
| ^
|
= note: see issue #123076 <https://github.com/rust-lang/rust/issues/123076> for more information
= help: add `#![feature(mut_ref)]` to the crate attributes to enable
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date

error[E0658]: mutable by-reference bindings are experimental
--> $DIR/feature-gate-mut-ref.rs:10:17
|
LL | let mut ref x = 10;
| ^
|
= note: see issue #123076 <https://github.com/rust-lang/rust/issues/123076> for more information
= help: add `#![feature(mut_ref)]` to the crate attributes to enable
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date

error[E0658]: mutable by-reference bindings are experimental
--> $DIR/feature-gate-mut-ref.rs:12:21
|
LL | let mut ref mut y = 10;
| ^
|
= note: see issue #123076 <https://github.com/rust-lang/rust/issues/123076> for more information
= help: add `#![feature(mut_ref)]` to the crate attributes to enable
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date

error: aborting due to 4 previous errors

For more information about this error, try `rustc --explain E0658`.
1 change: 1 addition & 0 deletions tests/ui/match/mut-ref-mut-2021.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
//@ edition: 2021
#![feature(mut_ref)]

struct Foo(u8);

Expand Down
16 changes: 8 additions & 8 deletions tests/ui/match/mut-ref-mut-2021.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error[E0384]: cannot assign twice to immutable variable `a`
--> $DIR/mut-ref-mut-2021.rs:7:5
--> $DIR/mut-ref-mut-2021.rs:8:5
|
LL | let Foo(a) = Foo(0);
| -
Expand All @@ -10,55 +10,55 @@ LL | a = 42;
| ^^^^^^ cannot assign twice to immutable variable

error[E0384]: cannot assign twice to immutable variable `a`
--> $DIR/mut-ref-mut-2021.rs:13:5
--> $DIR/mut-ref-mut-2021.rs:14:5
|
LL | let Foo(ref a) = Foo(0);
| ----- first assignment to `a`
LL | a = &42;
| ^^^^^^^ cannot assign twice to immutable variable

error[E0384]: cannot assign twice to immutable variable `a`
--> $DIR/mut-ref-mut-2021.rs:19:5
--> $DIR/mut-ref-mut-2021.rs:20:5
|
LL | let Foo(ref mut a) = Foo(0);
| --------- first assignment to `a`
LL | a = &mut 42;
| ^^^^^^^^^^^ cannot assign twice to immutable variable

error[E0384]: cannot assign twice to immutable variable `a`
--> $DIR/mut-ref-mut-2021.rs:25:5
--> $DIR/mut-ref-mut-2021.rs:26:5
|
LL | let Foo(a) = &Foo(0);
| - first assignment to `a`
LL | a = &42;
| ^^^^^^^ cannot assign twice to immutable variable

error[E0384]: cannot assign twice to immutable variable `a`
--> $DIR/mut-ref-mut-2021.rs:31:5
--> $DIR/mut-ref-mut-2021.rs:32:5
|
LL | let Foo(ref a) = &Foo(0);
| ----- first assignment to `a`
LL | a = &42;
| ^^^^^^^ cannot assign twice to immutable variable

error[E0384]: cannot assign twice to immutable variable `a`
--> $DIR/mut-ref-mut-2021.rs:37:5
--> $DIR/mut-ref-mut-2021.rs:38:5
|
LL | let Foo(a) = &mut Foo(0);
| - first assignment to `a`
LL | a = &mut 42;
| ^^^^^^^^^^^ cannot assign twice to immutable variable

error[E0384]: cannot assign twice to immutable variable `a`
--> $DIR/mut-ref-mut-2021.rs:43:5
--> $DIR/mut-ref-mut-2021.rs:44:5
|
LL | let Foo(ref a) = &mut Foo(0);
| ----- first assignment to `a`
LL | a = &42;
| ^^^^^^^ cannot assign twice to immutable variable

error[E0384]: cannot assign twice to immutable variable `a`
--> $DIR/mut-ref-mut-2021.rs:49:5
--> $DIR/mut-ref-mut-2021.rs:50:5
|
LL | let Foo(ref mut a) = &mut Foo(0);
| --------- first assignment to `a`
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/mut/mut-ref.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//@ check-pass

#![feature(mut_ref)]
fn main() {
let mut ref x = 10;
x = &11;
Expand Down

0 comments on commit fc0bc1d

Please sign in to comment.