Feature-gate mut ref patterns in struct field shorthand.

This commit is contained in:
Zachary S 2026-01-13 12:57:04 -06:00
parent cd6c412838
commit f809e332d8
3 changed files with 18 additions and 2 deletions

View file

@ -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 {

View file

@ -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]
}

View file

@ -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 <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 5 previous errors
For more information about this error, try `rustc --explain E0658`.