Skip to content

Commit 575033c

Browse files
committed
Also disallow ref/ref mut overriding the binding mode
1 parent 4107322 commit 575033c

File tree

6 files changed

+67
-20
lines changed

6 files changed

+67
-20
lines changed

compiler/rustc_hir_typeck/src/pat.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -691,7 +691,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
691691

692692
BindingMode(def_br, Mutability::Mut)
693693
} else {
694-
// `mut` resets binding mode on edition <= 2021
694+
// `mut` resets the binding mode on edition <= 2021
695695
*self
696696
.typeck_results
697697
.borrow_mut()
@@ -702,7 +702,18 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
702702
}
703703
}
704704
BindingMode(ByRef::No, mutbl) => BindingMode(def_br, mutbl),
705-
BindingMode(ByRef::Yes(_), _) => user_bind_annot,
705+
BindingMode(ByRef::Yes(_), _) => {
706+
if matches!(def_br, ByRef::Yes(_)) {
707+
// `ref`/`ref mut` overrides the binding mode on edition <= 2021
708+
*self
709+
.typeck_results
710+
.borrow_mut()
711+
.rust_2024_migration_desugared_pats_mut()
712+
.entry(pat_info.top_info.hir_id)
713+
.or_default() |= pat.span.at_least_rust_2024();
714+
}
715+
user_bind_annot
716+
}
706717
};
707718

708719
if bm.0 == ByRef::Yes(Mutability::Mut)

tests/ui/pattern/rfc-3627-match-ergonomics-2024/migration_lint.fixed

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,12 @@ fn main() {
3030
//~^ ERROR: the semantics of this pattern will change in edition 2024
3131
assert_type_eq(x, 0u8);
3232

33-
let Foo(ref x) = &Foo(0);
33+
let &Foo(ref x) = &Foo(0);
34+
//~^ ERROR: the semantics of this pattern will change in edition 2024
3435
assert_type_eq(x, &0u8);
3536

36-
let Foo(ref x) = &mut Foo(0);
37+
let &mut Foo(ref x) = &mut Foo(0);
38+
//~^ ERROR: the semantics of this pattern will change in edition 2024
3739
assert_type_eq(x, &0u8);
3840

3941
let &Foo(x) = &Foo(0);

tests/ui/pattern/rfc-3627-match-ergonomics-2024/migration_lint.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,11 @@ fn main() {
3131
assert_type_eq(x, 0u8);
3232

3333
let Foo(ref x) = &Foo(0);
34+
//~^ ERROR: the semantics of this pattern will change in edition 2024
3435
assert_type_eq(x, &0u8);
3536

3637
let Foo(ref x) = &mut Foo(0);
38+
//~^ ERROR: the semantics of this pattern will change in edition 2024
3739
assert_type_eq(x, &0u8);
3840

3941
let &Foo(x) = &Foo(0);

tests/ui/pattern/rfc-3627-match-ergonomics-2024/migration_lint.stderr

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,63 +21,79 @@ LL | let Foo(mut x) = &mut Foo(0);
2121
| help: desugar the match ergonomics: `&mut`
2222

2323
error: the semantics of this pattern will change in edition 2024
24-
--> $DIR/migration_lint.rs:51:9
24+
--> $DIR/migration_lint.rs:33:9
25+
|
26+
LL | let Foo(ref x) = &Foo(0);
27+
| -^^^^^^^^^
28+
| |
29+
| help: desugar the match ergonomics: `&`
30+
31+
error: the semantics of this pattern will change in edition 2024
32+
--> $DIR/migration_lint.rs:37:9
33+
|
34+
LL | let Foo(ref x) = &mut Foo(0);
35+
| -^^^^^^^^^
36+
| |
37+
| help: desugar the match ergonomics: `&mut`
38+
39+
error: the semantics of this pattern will change in edition 2024
40+
--> $DIR/migration_lint.rs:53:9
2541
|
2642
LL | let Foo(&x) = &Foo(&0);
2743
| -^^^^^^
2844
| |
2945
| help: desugar the match ergonomics: `&`
3046

3147
error: the semantics of this pattern will change in edition 2024
32-
--> $DIR/migration_lint.rs:55:9
48+
--> $DIR/migration_lint.rs:57:9
3349
|
3450
LL | let Foo(&mut x) = &Foo(&mut 0);
3551
| -^^^^^^^^^^
3652
| |
3753
| help: desugar the match ergonomics: `&`
3854

3955
error: the semantics of this pattern will change in edition 2024
40-
--> $DIR/migration_lint.rs:59:9
56+
--> $DIR/migration_lint.rs:61:9
4157
|
4258
LL | let Foo(&x) = &mut Foo(&0);
4359
| -^^^^^^
4460
| |
4561
| help: desugar the match ergonomics: `&mut`
4662

4763
error: the semantics of this pattern will change in edition 2024
48-
--> $DIR/migration_lint.rs:63:9
64+
--> $DIR/migration_lint.rs:65:9
4965
|
5066
LL | let Foo(&mut x) = &mut Foo(&mut 0);
5167
| -^^^^^^^^^^
5268
| |
5369
| help: desugar the match ergonomics: `&mut`
5470

5571
error: the semantics of this pattern will change in edition 2024
56-
--> $DIR/migration_lint.rs:71:12
72+
--> $DIR/migration_lint.rs:73:12
5773
|
5874
LL | if let Some(&x) = &&&&&Some(&0u8) {
5975
| -^^^^^^^
6076
| |
6177
| help: desugar the match ergonomics: `&&&&&`
6278

6379
error: the semantics of this pattern will change in edition 2024
64-
--> $DIR/migration_lint.rs:76:12
80+
--> $DIR/migration_lint.rs:78:12
6581
|
6682
LL | if let Some(&mut x) = &&&&&Some(&mut 0u8) {
6783
| -^^^^^^^^^^^
6884
| |
6985
| help: desugar the match ergonomics: `&&&&&`
7086

7187
error: the semantics of this pattern will change in edition 2024
72-
--> $DIR/migration_lint.rs:81:12
88+
--> $DIR/migration_lint.rs:83:12
7389
|
7490
LL | if let Some(&x) = &&&&&mut Some(&0u8) {
7591
| -^^^^^^^
7692
| |
7793
| help: desugar the match ergonomics: `&&&&&mut`
7894

7995
error: the semantics of this pattern will change in edition 2024
80-
--> $DIR/migration_lint.rs:86:12
96+
--> $DIR/migration_lint.rs:88:12
8197
|
8298
LL | if let Some(&mut Some(Some(x))) = &mut Some(&mut Some(&mut Some(0u8))) {
8399
| ^^^^^^^^^^^^^^^^^^^^^^^^
@@ -88,7 +104,7 @@ LL | if let &mut Some(&mut Some(&mut Some(ref mut x))) = &mut Some(&mut Some
88104
| ++++ ++++ +++++++
89105

90106
error: the semantics of this pattern will change in edition 2024
91-
--> $DIR/migration_lint.rs:97:9
107+
--> $DIR/migration_lint.rs:99:9
92108
|
93109
LL | let Struct { a, mut b, c } = &Struct { a: 0, b: 0, c: 0 };
94110
| ^^^^^^^^^^^^^^^^^^^^^^
@@ -99,7 +115,7 @@ LL | let &Struct { ref a, mut b, ref c } = &Struct { a: 0, b: 0, c: 0 };
99115
| + +++ +++
100116

101117
error: the semantics of this pattern will change in edition 2024
102-
--> $DIR/migration_lint.rs:102:9
118+
--> $DIR/migration_lint.rs:104:9
103119
|
104120
LL | let Struct { a: &a, b, ref c } = &Struct { a: &0, b: &0, c: &0 };
105121
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -110,7 +126,7 @@ LL | let &Struct { a: &a, ref b, ref c } = &Struct { a: &0, b: &0, c: &0 };
110126
| + +++
111127

112128
error: the semantics of this pattern will change in edition 2024
113-
--> $DIR/migration_lint.rs:108:12
129+
--> $DIR/migration_lint.rs:110:12
114130
|
115131
LL | if let Struct { a: &Some(a), b: Some(&b), c: Some(c) } =
116132
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -121,12 +137,12 @@ LL | if let &Struct { a: &Some(a), b: &Some(&b), c: &Some(ref c) } =
121137
| + + + +++
122138

123139
error: patterns are not allowed to reset the default binding mode in rust 2024
124-
--> $DIR/migration_lint.rs:120:9
140+
--> $DIR/migration_lint.rs:122:9
125141
|
126142
LL | (Some(mut x), migration_lint_macros::mixed_edition_pat!(y)) => {
127143
| -^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
128144
| |
129145
| help: desugar the match ergonomics: `&`
130146

131-
error: aborting due to 14 previous errors
147+
error: aborting due to 16 previous errors
132148

tests/ui/pattern/rfc-3627-match-ergonomics-2024/min_match_ergonomics_fail.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ test_pat_on_type![Foo { f: (&x,) }: Foo]; //~ ERROR mismatched types
3434
test_pat_on_type![Foo { f: (&x,) }: &mut Foo]; //~ ERROR mismatched types
3535
test_pat_on_type![Foo { f: &(x,) }: &Foo]; //~ ERROR patterns are not allowed to reset the default binding mode
3636
test_pat_on_type![(mut x,): &(T,)]; //~ ERROR patterns are not allowed to reset the default binding mode
37-
test_pat_on_type![(ref x,): &(T,)];
38-
test_pat_on_type![(ref mut x,): &mut (T,)];
37+
test_pat_on_type![(ref x,): &(T,)]; //~ ERROR patterns are not allowed to reset the default binding mode
38+
test_pat_on_type![(ref mut x,): &mut (T,)]; //~ ERROR patterns are not allowed to reset the default binding mode
3939

4040
fn get<X>() -> X {
4141
unimplemented!()

tests/ui/pattern/rfc-3627-match-ergonomics-2024/min_match_ergonomics_fail.stderr

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,22 @@ LL | test_pat_on_type![(mut x,): &(T,)];
131131
| |
132132
| help: desugar the match ergonomics: `&`
133133

134+
error: patterns are not allowed to reset the default binding mode in rust 2024
135+
--> $DIR/min_match_ergonomics_fail.rs:37:19
136+
|
137+
LL | test_pat_on_type![(ref x,): &(T,)];
138+
| -^^^^^^^
139+
| |
140+
| help: desugar the match ergonomics: `&`
141+
142+
error: patterns are not allowed to reset the default binding mode in rust 2024
143+
--> $DIR/min_match_ergonomics_fail.rs:38:19
144+
|
145+
LL | test_pat_on_type![(ref mut x,): &mut (T,)];
146+
| -^^^^^^^^^^^
147+
| |
148+
| help: desugar the match ergonomics: `&mut`
149+
134150
error: patterns are not allowed to reset the default binding mode in rust 2024
135151
--> $DIR/min_match_ergonomics_fail.rs:47:9
136152
|
@@ -139,6 +155,6 @@ LL | (&x,) => x,
139155
| |
140156
| help: desugar the match ergonomics: `&`
141157

142-
error: aborting due to 11 previous errors
158+
error: aborting due to 13 previous errors
143159

144160
For more information about this error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)