Skip to content

Commit

Permalink
add more pattern migration tests
Browse files Browse the repository at this point in the history
Most of these are meant to test possible future improvements, but since
they cover cases the existing test suite didn't, I figure including them
now may be helpful.
  • Loading branch information
dianne committed Feb 6, 2025
1 parent 8dcdb3e commit f1e4d94
Show file tree
Hide file tree
Showing 3 changed files with 400 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -150,4 +150,93 @@ fn main() {
let &[&(_)] = &[&0];
//~^ ERROR: reference patterns may only be written when the default binding mode is `move` in Rust 2024
//~| WARN: this changes meaning in Rust 2024

// NB: Most of the following tests are for possible future improvements to migration suggestions

// Test removing multiple binding modifiers.
let Struct { a, b, c } = &Struct { a: 0, b: 0, c: 0 };
//~^ ERROR: binding modifiers may only be written when the default binding mode is `move` in Rust 2024
//~| WARN: this changes meaning in Rust 2024
assert_type_eq(a, &0u32);
assert_type_eq(c, &0u32);

// Test that we don't change bindings' modes when removing binding modifiers.
let &mut Struct { ref a, ref mut b, ref mut c } = &mut Struct { a: 0, b: 0, c: 0 };
//~^ ERROR: binding modifiers may only be written when the default binding mode is `move` in Rust 2024
//~| WARN: this changes meaning in Rust 2024
assert_type_eq(a, &0u32);
assert_type_eq(b, &mut 0u32);
assert_type_eq(c, &mut 0u32);

// Test removing multiple reference patterns of various mutabilities, plus a binding modifier.
let &mut &Struct { a: &[ref a], b: &mut [&[ref b]], ref c } = &mut &Struct { a: &[0], b: &mut [&[0]], c: 0 };
//~^ ERROR: reference patterns may only be written when the default binding mode is `move` in Rust 2024
//~| WARN: this changes meaning in Rust 2024
assert_type_eq(a, &0u32);
assert_type_eq(b, &0u32);
assert_type_eq(c, &0u32);

// Test that we don't change bindings' types when removing reference patterns.
let &Foo(&ref a) = &Foo(&0);
//~^ ERROR: reference patterns may only be written when the default binding mode is `move` in Rust 2024
//~| WARN: this changes meaning in Rust 2024
assert_type_eq(a, &0u32);

// Test that we don't change bindings' modes when adding reference paterns (caught early).
let &(&a, ref b, &[ref c], &mut [&mut (ref d, &[ref e])]) = &(&0, 0, &[0], &mut [&mut (0, &[0])]);
//~^ ERROR: reference patterns may only be written when the default binding mode is `move` in Rust 2024
//~| WARN: this changes meaning in Rust 2024
assert_type_eq(a, 0u32);
assert_type_eq(b, &0u32);
assert_type_eq(c, &0u32);
assert_type_eq(d, &0u32);
assert_type_eq(e, &0u32);

// Test that we don't change bindings' modes when adding reference patterns (caught late).
let &(ref a, &mut [ref b], &[mut c]) = &(0, &mut [0], &[0]);
//~^ ERROR: binding modifiers may only be written when the default binding mode is `move` in Rust 2024
//~| WARN: this changes meaning in Rust 2024
assert_type_eq(a, &0u32);
assert_type_eq(b, &0u32);
assert_type_eq(c, 0u32);

// Test featuring both additions and removals.
let &(&a, &mut (ref b, &[ref c])) = &(&0, &mut (0, &[0]));
//~^ ERROR: reference patterns may only be written when the default binding mode is `move` in Rust 2024
//~| WARN: this changes meaning in Rust 2024
assert_type_eq(a, 0u32);
assert_type_eq(b, &0u32);
assert_type_eq(c, &0u32);

// Test that bindings' subpatterns' modes are updated properly.
let &[mut a @ ref b] = &[0];
//~^ ERROR: binding modifiers may only be written when the default binding mode is `move` in Rust 2024
//~| WARN: this changes meaning in Rust 2024
assert_type_eq(a, 0u32);
assert_type_eq(b, &0u32);

// Test that bindings' subpatterns' modes are checked properly.
let &[ref a @ mut b] = &[0];
//~^ ERROR: binding modifiers may only be written when the default binding mode is `move` in Rust 2024
//~| WARN: this changes meaning in Rust 2024
assert_type_eq(a, &0u32);
assert_type_eq(b, 0u32);

// Test that we respect bindings' subpatterns' types when rewriting `&ref x` to `x`.
let [&Foo(&ref a @ ref b), &Foo(&ref c @ d)] = [&Foo(&0); 2];
//~^ ERROR: reference patterns may only be written when the default binding mode is `move` in Rust 2024
//~| WARN: this changes meaning in Rust 2024
assert_type_eq(a, &0u32);
assert_type_eq(b, &0u32);
assert_type_eq(c, &0u32);
assert_type_eq(d, 0u32);

// Test that we respect bindings' subpatterns' modes when rewriting `&ref x` to `x`.
let [&Foo(&ref a @ [ref b]), &Foo(&ref c @ [d])] = [&Foo(&[0]); 2];
//~^ ERROR: reference patterns may only be written when the default binding mode is `move` in Rust 2024
//~| WARN: this changes meaning in Rust 2024
assert_type_eq(a, &[0u32]);
assert_type_eq(b, &0u32);
assert_type_eq(c, &[0u32]);
assert_type_eq(d, 0u32);
}
89 changes: 89 additions & 0 deletions tests/ui/pattern/rfc-3627-match-ergonomics-2024/migration_lint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,4 +150,93 @@ fn main() {
let [&(_)] = &[&0];
//~^ ERROR: reference patterns may only be written when the default binding mode is `move` in Rust 2024
//~| WARN: this changes meaning in Rust 2024

// NB: Most of the following tests are for possible future improvements to migration suggestions

// Test removing multiple binding modifiers.
let Struct { ref a, ref b, c } = &Struct { a: 0, b: 0, c: 0 };
//~^ ERROR: binding modifiers may only be written when the default binding mode is `move` in Rust 2024
//~| WARN: this changes meaning in Rust 2024
assert_type_eq(a, &0u32);
assert_type_eq(c, &0u32);

// Test that we don't change bindings' modes when removing binding modifiers.
let Struct { ref a, ref mut b, c } = &mut Struct { a: 0, b: 0, c: 0 };
//~^ ERROR: binding modifiers may only be written when the default binding mode is `move` in Rust 2024
//~| WARN: this changes meaning in Rust 2024
assert_type_eq(a, &0u32);
assert_type_eq(b, &mut 0u32);
assert_type_eq(c, &mut 0u32);

// Test removing multiple reference patterns of various mutabilities, plus a binding modifier.
let Struct { a: &[ref a], b: &mut [[b]], c } = &mut &Struct { a: &[0], b: &mut [&[0]], c: 0 };
//~^ ERROR: reference patterns may only be written when the default binding mode is `move` in Rust 2024
//~| WARN: this changes meaning in Rust 2024
assert_type_eq(a, &0u32);
assert_type_eq(b, &0u32);
assert_type_eq(c, &0u32);

// Test that we don't change bindings' types when removing reference patterns.
let Foo(&ref a) = &Foo(&0);
//~^ ERROR: reference patterns may only be written when the default binding mode is `move` in Rust 2024
//~| WARN: this changes meaning in Rust 2024
assert_type_eq(a, &0u32);

// Test that we don't change bindings' modes when adding reference paterns (caught early).
let (&a, b, [c], [(d, [e])]) = &(&0, 0, &[0], &mut [&mut (0, &[0])]);
//~^ ERROR: reference patterns may only be written when the default binding mode is `move` in Rust 2024
//~| WARN: this changes meaning in Rust 2024
assert_type_eq(a, 0u32);
assert_type_eq(b, &0u32);
assert_type_eq(c, &0u32);
assert_type_eq(d, &0u32);
assert_type_eq(e, &0u32);

// Test that we don't change bindings' modes when adding reference patterns (caught late).
let (a, [b], [mut c]) = &(0, &mut [0], &[0]);
//~^ ERROR: binding modifiers may only be written when the default binding mode is `move` in Rust 2024
//~| WARN: this changes meaning in Rust 2024
assert_type_eq(a, &0u32);
assert_type_eq(b, &0u32);
assert_type_eq(c, 0u32);

// Test featuring both additions and removals.
let (&a, (b, &[ref c])) = &(&0, &mut (0, &[0]));
//~^ ERROR: reference patterns may only be written when the default binding mode is `move` in Rust 2024
//~| WARN: this changes meaning in Rust 2024
assert_type_eq(a, 0u32);
assert_type_eq(b, &0u32);
assert_type_eq(c, &0u32);

// Test that bindings' subpatterns' modes are updated properly.
let [mut a @ b] = &[0];
//~^ ERROR: binding modifiers may only be written when the default binding mode is `move` in Rust 2024
//~| WARN: this changes meaning in Rust 2024
assert_type_eq(a, 0u32);
assert_type_eq(b, &0u32);

// Test that bindings' subpatterns' modes are checked properly.
let [a @ mut b] = &[0];
//~^ ERROR: binding modifiers may only be written when the default binding mode is `move` in Rust 2024
//~| WARN: this changes meaning in Rust 2024
assert_type_eq(a, &0u32);
assert_type_eq(b, 0u32);

// Test that we respect bindings' subpatterns' types when rewriting `&ref x` to `x`.
let [Foo(&ref a @ ref b), Foo(&ref c @ d)] = [&Foo(&0); 2];
//~^ ERROR: reference patterns may only be written when the default binding mode is `move` in Rust 2024
//~| WARN: this changes meaning in Rust 2024
assert_type_eq(a, &0u32);
assert_type_eq(b, &0u32);
assert_type_eq(c, &0u32);
assert_type_eq(d, 0u32);

// Test that we respect bindings' subpatterns' modes when rewriting `&ref x` to `x`.
let [Foo(&ref a @ [ref b]), Foo(&ref c @ [d])] = [&Foo(&[0]); 2];
//~^ ERROR: reference patterns may only be written when the default binding mode is `move` in Rust 2024
//~| WARN: this changes meaning in Rust 2024
assert_type_eq(a, &[0u32]);
assert_type_eq(b, &0u32);
assert_type_eq(c, &[0u32]);
assert_type_eq(d, 0u32);
}
Loading

0 comments on commit f1e4d94

Please sign in to comment.