Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Feature-gate mut ref patterns in struct field shorthand.
  • Loading branch information
zachs18 committed Jan 14, 2026
commit f809e332d895bc80d28d8d2cb10297a3fe9ca4f2
6 changes: 6 additions & 0 deletions compiler/rustc_parse/src/parser/pat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/feature-gates/feature-gate-mut-ref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
}
12 changes: 11 additions & 1 deletion tests/ui/feature-gates/feature-gate-mut-ref.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Loading