Skip to content

Commit 29e56a8

Browse files
authored
Unrolled build for #151102
Rollup merge of #151102 - zachs18:mut-ref-in-struct-pattern-shorthand-gate, r=JonathanBrouwer Feature-gate `mut ref` patterns in struct pattern field shorthand Tracking issue for `mut_ref` (and other parts of Match Ergonomics 2024): #123076 #123080 introduced `mut ref`[^1] patterns (for by-reference bindings where the binding itself is mutable), feature-gated behind the `mut_ref` feature, except for in struct pattern shorthand, where the feature gating was missing. Thus, `mut ref` patterns in struct pattern shorthand has been unintentionally stable for ~18 months (since 1.79.0 ([compiler explorer](https://rust.godbolt.org/z/4WTrvhboT))). This PR adds feature-gating for `mut ref` patterns in struct pattern shorthand. Since this is reverting an accidental stabilization, this probably needs a crater run and a T-lang FCP? Some alternative possibilities: * Do nothing (let the inconsistency exist until `feature(mut_ref)` is stabilized) * Document the existing behavior * Do a FCW instead of fully feature-gating * Stabilize `feature(mut_ref)` CC #123080 (comment) CC @Nadrieril [^1]: everything in this description also applies analogously to `mut ref mut` patterns.
2 parents 905b926 + f809e33 commit 29e56a8

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

compiler/rustc_parse/src/parser/pat.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1749,6 +1749,12 @@ impl<'a> Parser<'a> {
17491749
hi = self.prev_token.span;
17501750
let ann = BindingMode(by_ref, mutability);
17511751
let fieldpat = self.mk_pat_ident(boxed_span.to(hi), ann, fieldname);
1752+
if matches!(
1753+
fieldpat.kind,
1754+
PatKind::Ident(BindingMode(ByRef::Yes(..), Mutability::Mut), ..)
1755+
) {
1756+
self.psess.gated_spans.gate(sym::mut_ref, fieldpat.span);
1757+
}
17521758
let subpat = if is_box {
17531759
self.mk_pat(lo.to(hi), PatKind::Box(Box::new(fieldpat)))
17541760
} else {

tests/ui/feature-gates/feature-gate-mut-ref.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,7 @@ fn main() {
1010
let mut ref x = 10; //~ ERROR [E0658]
1111
#[cfg(false)]
1212
let mut ref mut y = 10; //~ ERROR [E0658]
13+
14+
struct Foo { x: i32 }
15+
let Foo { mut ref x } = Foo { x: 10 }; //~ ERROR [E0658]
1316
}

tests/ui/feature-gates/feature-gate-mut-ref.stderr

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,16 @@ LL | let mut ref mut y = 10;
3838
= help: add `#![feature(mut_ref)]` to the crate attributes to enable
3939
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
4040

41-
error: aborting due to 4 previous errors
41+
error[E0658]: mutable by-reference bindings are experimental
42+
--> $DIR/feature-gate-mut-ref.rs:15:15
43+
|
44+
LL | let Foo { mut ref x } = Foo { x: 10 };
45+
| ^^^^^^^^^
46+
|
47+
= note: see issue #123076 <https://github.com/rust-lang/rust/issues/123076> for more information
48+
= help: add `#![feature(mut_ref)]` to the crate attributes to enable
49+
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
50+
51+
error: aborting due to 5 previous errors
4252

4353
For more information about this error, try `rustc --explain E0658`.

0 commit comments

Comments
 (0)