Skip to content

Commit 528d45a

Browse files
Feature gate
1 parent e0da13f commit 528d45a

File tree

9 files changed

+76
-9
lines changed

9 files changed

+76
-9
lines changed

compiler/rustc_ast_passes/src/feature_gate.rs

+1
Original file line numberDiff line numberDiff line change
@@ -565,6 +565,7 @@ pub fn check_crate(krate: &ast::Crate, sess: &Session, features: &Features) {
565565
gate_all!(unnamed_fields, "unnamed fields are not yet fully implemented");
566566
gate_all!(fn_delegation, "functions delegation is not yet fully implemented");
567567
gate_all!(postfix_match, "postfix match is experimental");
568+
gate_all!(mut_ref, "mutable by-reference bindings are experimental");
568569

569570
if !visitor.features.never_patterns {
570571
if let Some(spans) = spans.get(&sym::never_patterns) {

compiler/rustc_feature/src/unstable.rs

+2
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,8 @@ declare_features! (
529529
(unstable, more_qualified_paths, "1.54.0", Some(86935)),
530530
/// Allows the `#[must_not_suspend]` attribute.
531531
(unstable, must_not_suspend, "1.57.0", Some(83310)),
532+
/// Allows `mut ref` and `mut ref mut` identifier patterns.
533+
(incomplete, mut_ref, "CURRENT_RUSTC_VERSION", Some(123076)),
532534
/// Allows using `#[naked]` on functions.
533535
(unstable, naked_functions, "1.9.0", Some(90957)),
534536
/// Allows specifying the as-needed link modifier

compiler/rustc_parse/src/parser/pat.rs

+4
Original file line numberDiff line numberDiff line change
@@ -779,6 +779,10 @@ impl<'a> Parser<'a> {
779779
self.ban_mut_general_pat(mut_span, &pat, changed_any_binding);
780780
}
781781

782+
if matches!(pat.kind, PatKind::Ident(BindingAnnotation(ByRef::Yes(_), Mutability::Mut), ..))
783+
{
784+
self.psess.gated_spans.gate(sym::mut_ref, pat.span);
785+
}
782786
Ok(pat.into_inner().kind)
783787
}
784788

compiler/rustc_span/src/symbol.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1185,6 +1185,7 @@ symbols! {
11851185
multiple_supertrait_upcastable,
11861186
must_not_suspend,
11871187
must_use,
1188+
mut_ref,
11881189
naked,
11891190
naked_functions,
11901191
name,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
fn main() {
2+
let mut ref x = 10; //~ ERROR [E0658]
3+
x = &11;
4+
let ref mut y = 12;
5+
*y = 13;
6+
let mut ref mut z = 14; //~ ERROR [E0658]
7+
z = &mut 15;
8+
9+
#[cfg(FALSE)]
10+
let mut ref x = 10; //~ ERROR [E0658]
11+
#[cfg(FALSE)]
12+
let mut ref mut y = 10; //~ ERROR [E0658]
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
error[E0658]: mutable by-reference bindings are experimental
2+
--> $DIR/feature-gate-mut-ref.rs:2:17
3+
|
4+
LL | let mut ref x = 10;
5+
| ^
6+
|
7+
= note: see issue #123076 <https://github.com/rust-lang/rust/issues/123076> for more information
8+
= help: add `#![feature(mut_ref)]` 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]: mutable by-reference bindings are experimental
12+
--> $DIR/feature-gate-mut-ref.rs:6:21
13+
|
14+
LL | let mut ref mut z = 14;
15+
| ^
16+
|
17+
= note: see issue #123076 <https://github.com/rust-lang/rust/issues/123076> for more information
18+
= help: add `#![feature(mut_ref)]` 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[E0658]: mutable by-reference bindings are experimental
22+
--> $DIR/feature-gate-mut-ref.rs:10:17
23+
|
24+
LL | let mut ref x = 10;
25+
| ^
26+
|
27+
= note: see issue #123076 <https://github.com/rust-lang/rust/issues/123076> for more information
28+
= help: add `#![feature(mut_ref)]` to the crate attributes to enable
29+
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
30+
31+
error[E0658]: mutable by-reference bindings are experimental
32+
--> $DIR/feature-gate-mut-ref.rs:12:21
33+
|
34+
LL | let mut ref mut y = 10;
35+
| ^
36+
|
37+
= note: see issue #123076 <https://github.com/rust-lang/rust/issues/123076> for more information
38+
= help: add `#![feature(mut_ref)]` to the crate attributes to enable
39+
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
40+
41+
error: aborting due to 4 previous errors
42+
43+
For more information about this error, try `rustc --explain E0658`.

tests/ui/mut/mut-ref.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//@ check-pass
2-
2+
#![allow(incomplete_features)]
3+
#![feature(mut_ref)]
34
fn main() {
45
let mut ref x = 10;
56
x = &11;

tests/ui/match/mut-ref-mut-2021.rs renamed to tests/ui/pattern/mut-ref-mut-2021.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
//@ edition: 2021
2+
#![allow(incomplete_features)]
3+
#![feature(mut_ref)]
24

35
struct Foo(u8);
46

tests/ui/match/mut-ref-mut-2021.stderr renamed to tests/ui/pattern/mut-ref-mut-2021.stderr

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0384]: cannot assign twice to immutable variable `a`
2-
--> $DIR/mut-ref-mut-2021.rs:7:5
2+
--> $DIR/mut-ref-mut-2021.rs:9:5
33
|
44
LL | let Foo(a) = Foo(0);
55
| -
@@ -10,55 +10,55 @@ LL | a = 42;
1010
| ^^^^^^ cannot assign twice to immutable variable
1111

1212
error[E0384]: cannot assign twice to immutable variable `a`
13-
--> $DIR/mut-ref-mut-2021.rs:13:5
13+
--> $DIR/mut-ref-mut-2021.rs:15:5
1414
|
1515
LL | let Foo(ref a) = Foo(0);
1616
| ----- first assignment to `a`
1717
LL | a = &42;
1818
| ^^^^^^^ cannot assign twice to immutable variable
1919

2020
error[E0384]: cannot assign twice to immutable variable `a`
21-
--> $DIR/mut-ref-mut-2021.rs:19:5
21+
--> $DIR/mut-ref-mut-2021.rs:21:5
2222
|
2323
LL | let Foo(ref mut a) = Foo(0);
2424
| --------- first assignment to `a`
2525
LL | a = &mut 42;
2626
| ^^^^^^^^^^^ cannot assign twice to immutable variable
2727

2828
error[E0384]: cannot assign twice to immutable variable `a`
29-
--> $DIR/mut-ref-mut-2021.rs:25:5
29+
--> $DIR/mut-ref-mut-2021.rs:27:5
3030
|
3131
LL | let Foo(a) = &Foo(0);
3232
| - first assignment to `a`
3333
LL | a = &42;
3434
| ^^^^^^^ cannot assign twice to immutable variable
3535

3636
error[E0384]: cannot assign twice to immutable variable `a`
37-
--> $DIR/mut-ref-mut-2021.rs:31:5
37+
--> $DIR/mut-ref-mut-2021.rs:33:5
3838
|
3939
LL | let Foo(ref a) = &Foo(0);
4040
| ----- first assignment to `a`
4141
LL | a = &42;
4242
| ^^^^^^^ cannot assign twice to immutable variable
4343

4444
error[E0384]: cannot assign twice to immutable variable `a`
45-
--> $DIR/mut-ref-mut-2021.rs:37:5
45+
--> $DIR/mut-ref-mut-2021.rs:39:5
4646
|
4747
LL | let Foo(a) = &mut Foo(0);
4848
| - first assignment to `a`
4949
LL | a = &mut 42;
5050
| ^^^^^^^^^^^ cannot assign twice to immutable variable
5151

5252
error[E0384]: cannot assign twice to immutable variable `a`
53-
--> $DIR/mut-ref-mut-2021.rs:43:5
53+
--> $DIR/mut-ref-mut-2021.rs:45:5
5454
|
5555
LL | let Foo(ref a) = &mut Foo(0);
5656
| ----- first assignment to `a`
5757
LL | a = &42;
5858
| ^^^^^^^ cannot assign twice to immutable variable
5959

6060
error[E0384]: cannot assign twice to immutable variable `a`
61-
--> $DIR/mut-ref-mut-2021.rs:49:5
61+
--> $DIR/mut-ref-mut-2021.rs:51:5
6262
|
6363
LL | let Foo(ref mut a) = &mut Foo(0);
6464
| --------- first assignment to `a`

0 commit comments

Comments
 (0)