From cd6c41283838023ee3b22709106d666869ec9a6f Mon Sep 17 00:00:00 2001 From: Zachary S Date: Tue, 13 Jan 2026 22:40:12 -0600 Subject: [PATCH 1/2] Add test for feature-gating `mut ref` patterns in struct field shorthand. --- tests/ui/feature-gates/feature-gate-mut-ref.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/ui/feature-gates/feature-gate-mut-ref.rs b/tests/ui/feature-gates/feature-gate-mut-ref.rs index 752ae35d8a9ae..eac2a59002e28 100644 --- a/tests/ui/feature-gates/feature-gate-mut-ref.rs +++ b/tests/ui/feature-gates/feature-gate-mut-ref.rs @@ -10,4 +10,7 @@ fn main() { let mut ref x = 10; //~ ERROR [E0658] #[cfg(false)] let mut ref mut y = 10; //~ ERROR [E0658] + + struct Foo { x: i32 } + let Foo { mut ref x } = Foo { x: 10 }; } From f809e332d895bc80d28d8d2cb10297a3fe9ca4f2 Mon Sep 17 00:00:00 2001 From: Zachary S Date: Tue, 13 Jan 2026 12:57:04 -0600 Subject: [PATCH 2/2] Feature-gate `mut ref` patterns in struct field shorthand. --- compiler/rustc_parse/src/parser/pat.rs | 6 ++++++ tests/ui/feature-gates/feature-gate-mut-ref.rs | 2 +- tests/ui/feature-gates/feature-gate-mut-ref.stderr | 12 +++++++++++- 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/compiler/rustc_parse/src/parser/pat.rs b/compiler/rustc_parse/src/parser/pat.rs index d7f3a36122e59..f3e12d48a6a56 100644 --- a/compiler/rustc_parse/src/parser/pat.rs +++ b/compiler/rustc_parse/src/parser/pat.rs @@ -1750,6 +1750,12 @@ impl<'a> Parser<'a> { hi = self.prev_token.span; let ann = BindingMode(by_ref, mutability); let fieldpat = self.mk_pat_ident(boxed_span.to(hi), ann, fieldname); + if matches!( + fieldpat.kind, + PatKind::Ident(BindingMode(ByRef::Yes(..), Mutability::Mut), ..) + ) { + self.psess.gated_spans.gate(sym::mut_ref, fieldpat.span); + } let subpat = if is_box { self.mk_pat(lo.to(hi), PatKind::Box(Box::new(fieldpat))) } else { diff --git a/tests/ui/feature-gates/feature-gate-mut-ref.rs b/tests/ui/feature-gates/feature-gate-mut-ref.rs index eac2a59002e28..74b1fba5dfea2 100644 --- a/tests/ui/feature-gates/feature-gate-mut-ref.rs +++ b/tests/ui/feature-gates/feature-gate-mut-ref.rs @@ -12,5 +12,5 @@ fn main() { let mut ref mut y = 10; //~ ERROR [E0658] struct Foo { x: i32 } - let Foo { mut ref x } = Foo { x: 10 }; + let Foo { mut ref x } = Foo { x: 10 }; //~ ERROR [E0658] } diff --git a/tests/ui/feature-gates/feature-gate-mut-ref.stderr b/tests/ui/feature-gates/feature-gate-mut-ref.stderr index d3eb674e92dec..921ff878bf4d8 100644 --- a/tests/ui/feature-gates/feature-gate-mut-ref.stderr +++ b/tests/ui/feature-gates/feature-gate-mut-ref.stderr @@ -38,6 +38,16 @@ LL | let mut ref mut y = 10; = 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 +error[E0658]: mutable by-reference bindings are experimental + --> $DIR/feature-gate-mut-ref.rs:15:15 + | +LL | let Foo { mut ref x } = Foo { x: 10 }; + | ^^^^^^^^^ + | + = note: see issue #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 5 previous errors For more information about this error, try `rustc --explain E0658`.