Skip to content

Commit

Permalink
Also disallow ref/ref mut overriding the binding mode
Browse files Browse the repository at this point in the history
  • Loading branch information
Nadrieril committed Oct 7, 2024
1 parent 4107322 commit 575033c
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 20 deletions.
15 changes: 13 additions & 2 deletions compiler/rustc_hir_typeck/src/pat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -691,7 +691,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {

BindingMode(def_br, Mutability::Mut)
} else {
// `mut` resets binding mode on edition <= 2021
// `mut` resets the binding mode on edition <= 2021
*self
.typeck_results
.borrow_mut()
Expand All @@ -702,7 +702,18 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
}
}
BindingMode(ByRef::No, mutbl) => BindingMode(def_br, mutbl),
BindingMode(ByRef::Yes(_), _) => user_bind_annot,
BindingMode(ByRef::Yes(_), _) => {
if matches!(def_br, ByRef::Yes(_)) {
// `ref`/`ref mut` overrides the binding mode on edition <= 2021
*self
.typeck_results
.borrow_mut()
.rust_2024_migration_desugared_pats_mut()
.entry(pat_info.top_info.hir_id)
.or_default() |= pat.span.at_least_rust_2024();
}
user_bind_annot
}
};

if bm.0 == ByRef::Yes(Mutability::Mut)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,12 @@ fn main() {
//~^ ERROR: the semantics of this pattern will change in edition 2024
assert_type_eq(x, 0u8);

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

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

let &Foo(x) = &Foo(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,11 @@ fn main() {
assert_type_eq(x, 0u8);

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

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

let &Foo(x) = &Foo(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,63 +21,79 @@ LL | let Foo(mut x) = &mut Foo(0);
| help: desugar the match ergonomics: `&mut`

error: the semantics of this pattern will change in edition 2024
--> $DIR/migration_lint.rs:51:9
--> $DIR/migration_lint.rs:33:9
|
LL | let Foo(ref x) = &Foo(0);
| -^^^^^^^^^
| |
| help: desugar the match ergonomics: `&`

error: the semantics of this pattern will change in edition 2024
--> $DIR/migration_lint.rs:37:9
|
LL | let Foo(ref x) = &mut Foo(0);
| -^^^^^^^^^
| |
| help: desugar the match ergonomics: `&mut`

error: the semantics of this pattern will change in edition 2024
--> $DIR/migration_lint.rs:53:9
|
LL | let Foo(&x) = &Foo(&0);
| -^^^^^^
| |
| help: desugar the match ergonomics: `&`

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

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

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

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

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

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

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

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

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

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

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

error: aborting due to 14 previous errors
error: aborting due to 16 previous errors

Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ test_pat_on_type![Foo { f: (&x,) }: Foo]; //~ ERROR mismatched types
test_pat_on_type![Foo { f: (&x,) }: &mut Foo]; //~ ERROR mismatched types
test_pat_on_type![Foo { f: &(x,) }: &Foo]; //~ ERROR patterns are not allowed to reset the default binding mode
test_pat_on_type![(mut x,): &(T,)]; //~ ERROR patterns are not allowed to reset the default binding mode
test_pat_on_type![(ref x,): &(T,)];
test_pat_on_type![(ref mut x,): &mut (T,)];
test_pat_on_type![(ref x,): &(T,)]; //~ ERROR patterns are not allowed to reset the default binding mode
test_pat_on_type![(ref mut x,): &mut (T,)]; //~ ERROR patterns are not allowed to reset the default binding mode

fn get<X>() -> X {
unimplemented!()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,22 @@ LL | test_pat_on_type![(mut x,): &(T,)];
| |
| help: desugar the match ergonomics: `&`

error: patterns are not allowed to reset the default binding mode in rust 2024
--> $DIR/min_match_ergonomics_fail.rs:37:19
|
LL | test_pat_on_type![(ref x,): &(T,)];
| -^^^^^^^
| |
| help: desugar the match ergonomics: `&`

error: patterns are not allowed to reset the default binding mode in rust 2024
--> $DIR/min_match_ergonomics_fail.rs:38:19
|
LL | test_pat_on_type![(ref mut x,): &mut (T,)];
| -^^^^^^^^^^^
| |
| help: desugar the match ergonomics: `&mut`

error: patterns are not allowed to reset the default binding mode in rust 2024
--> $DIR/min_match_ergonomics_fail.rs:47:9
|
Expand All @@ -139,6 +155,6 @@ LL | (&x,) => x,
| |
| help: desugar the match ergonomics: `&`

error: aborting due to 11 previous errors
error: aborting due to 13 previous errors

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

0 comments on commit 575033c

Please sign in to comment.