Also disallow ref/ref mut overriding the binding mode

This commit is contained in:
Nadrieril 2024-10-07 23:39:33 +02:00
parent 4107322766
commit 575033c50c
6 changed files with 67 additions and 20 deletions

View File

@ -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()
@ -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)

View File

@ -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);

View File

@ -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);

View File

@ -21,7 +21,23 @@ 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);
| -^^^^^^
@ -29,7 +45,7 @@ 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);
| -^^^^^^^^^^
@ -37,7 +53,7 @@ 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);
| -^^^^^^
@ -45,7 +61,7 @@ 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);
| -^^^^^^^^^^
@ -53,7 +69,7 @@ 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) {
| -^^^^^^^
@ -61,7 +77,7 @@ 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) {
| -^^^^^^^^^^^
@ -69,7 +85,7 @@ 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) {
| -^^^^^^^
@ -77,7 +93,7 @@ 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))) {
| ^^^^^^^^^^^^^^^^^^^^^^^^
@ -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 };
| ^^^^^^^^^^^^^^^^^^^^^^
@ -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 };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -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) } =
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -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

View File

@ -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!()

View File

@ -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
|
@ -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`.