Skip to content

Commit

Permalink
add tests differing between stable and new rules (with errors on new …
Browse files Browse the repository at this point in the history
…rules)

Since there are so many ways to write these, I've opted to only include
two sorts of test: simple tests that directly target the rules differing
between rulesets and nuanced tests that produce different errors under
different rulesets. I've also tried not to add any duplicate tests.

`well-typed-edition-2024.rs` already has tests disagreeing with stable,
so I've opted not to include any in this commit that are well-typed
under the new rules.
  • Loading branch information
dianne committed Jan 21, 2025
1 parent f5567e1 commit e288cff
Show file tree
Hide file tree
Showing 6 changed files with 155 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,19 @@ LL | let Foo(mut a) = &mut Foo(0);
= help: add `#![feature(mut_ref)]` to the crate attributes to enable
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date

error: aborting due to 2 previous errors
error[E0308]: mismatched types
--> $DIR/mut-ref-mut.rs:24:10
|
LL | let [&mut mut x] = &[&mut 0];
| ^^^^^
|
= note: cannot match inherited `&` with `&mut` pattern
help: replace this `&mut` pattern with `&`
|
LL | let [&mut x] = &[&mut 0];
| ~

error: aborting due to 3 previous errors

For more information about this error, try `rustc --explain E0658`.
Some errors have detailed explanations: E0308, E0658.
For more information about an error, try `rustc --explain E0308`.
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,10 @@ pub fn main() {
//[classic2024,structural2024]~^ ERROR: binding cannot be both mutable and by-reference
#[cfg(stable2021)] { a = 42 }
#[cfg(any(classic2024, structural2024))] { a = &mut 42 }

let [&mut mut x] = &[&mut 0];
//[classic2024]~^ ERROR: mismatched types
//[classic2024]~| cannot match inherited `&` with `&mut` pattern
//[structural2024]~^^^ binding cannot be both mutable and by-reference
#[cfg(stable2021)] { x = 0 }
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@ LL | let Foo(mut a) = &mut Foo(0);
= help: add `#![feature(mut_ref)]` to the crate attributes to enable
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date

error: aborting due to 2 previous errors
error[E0658]: binding cannot be both mutable and by-reference
--> $DIR/mut-ref-mut.rs:24:15
|
LL | let [&mut mut x] = &[&mut 0];
| ^^^^
|
= note: see issue #123076 <https://github.com/rust-lang/rust/issues/123076> for more information
= help: add `#![feature(mut_ref)]` to the crate attributes to enable
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date

error: aborting due to 3 previous errors

For more information about this error, try `rustc --explain E0658`.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
//@[stable2021] edition: 2021
//@[classic2024] edition: 2024
//@[structural2024] edition: 2024
//@[classic2024] run-pass
//! Tests for errors from binding with `ref x` under a by-ref default binding mode in edition 2024.
//! These can't be in the same body as tests for other errors, since they're emitted during THIR
//! construction. The errors on stable edition 2021 Rust are unrelated.
Expand Down Expand Up @@ -36,8 +35,6 @@ fn errors_from_eating_the_real_reference() {
//[structural2024]~| cannot override to bind by-reference when that is the implicit default
#[cfg(stable2021)] let _: &mut u32 = x;
#[cfg(classic2024)] let _: &mut &mut u32 = x;

errors_from_eating_the_real_reference_caught_in_hir_typeck_on_stable();
}

/// To make absolutely sure binding with `ref` ignores inherited references on stable, let's
Expand All @@ -58,6 +55,43 @@ fn errors_from_eating_the_real_reference_caught_in_hir_typeck_on_stable() {
#[cfg(classic2024)] let _: &&mut u32 = x;
}

/// This one also needs to be quarantined for a typeck error on `classic2024` (eat-outer).
fn errors_dependent_on_eating_order_caught_in_hir_typeck_when_eating_outer() {
let [&mut ref x] = &[&mut 0];
//[classic2024]~^ ERROR: mismatched types
//[classic2024]~| cannot match inherited `&` with `&mut` pattern
//[structural2024]~^^^ ERROR: this pattern relies on behavior which may change in edition 2024
//[structural2024]~| cannot override to bind by-reference when that is the implicit default
#[cfg(stable2021)] let _: &u32 = x;
}

/// These should be errors in all editions. In edition 2024, they should be caught by the pattern
/// typing rules disallowing `ref` when there's an inherited reference. In old editions where that
/// resets the binding mode, they're borrowck errors due to binding with `ref mut`.
/// As a quirk of how the edition 2024 error is emitted during THIR construction, it ends up going
/// through borrowck as well, using the old `ref` behavior as a fallback, so we get that error too.
fn borrowck_errors_in_old_editions() {
let [ref mut x] = &[0];
//~^ ERROR: cannot borrow data in a `&` reference as mutable
//[classic2024,structural2024]~| ERROR: this pattern relies on behavior which may change in edition 2024
//[classic2024,structural2024]~| cannot override to bind by-reference when that is the implicit default
}

/// The remaining tests are purely for testing `ref` bindings in the presence of an inherited
/// reference. These should always fail on edition 2024 and succeed on edition 2021.
pub fn main() {
errors_from_eating_the_real_reference();
let [ref x] = &[0];
//[classic2024,structural2024]~^ ERROR: this pattern relies on behavior which may change in edition 2024
//[classic2024,structural2024]~| cannot override to bind by-reference when that is the implicit default
#[cfg(stable2021)] let _: &u32 = x;

let [ref x] = &mut [0];
//[classic2024,structural2024]~^ ERROR: this pattern relies on behavior which may change in edition 2024
//[classic2024,structural2024]~| cannot override to bind by-reference when that is the implicit default
#[cfg(stable2021)] let _: &u32 = x;

let [ref mut x] = &mut [0];
//[classic2024,structural2024]~^ ERROR: this pattern relies on behavior which may change in edition 2024
//[classic2024,structural2024]~| cannot override to bind by-reference when that is the implicit default
#[cfg(stable2021)] let _: &mut u32 = x;
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error[E0308]: mismatched types
--> $DIR/ref-binding-on-inh-ref-errors.rs:46:10
--> $DIR/ref-binding-on-inh-ref-errors.rs:43:10
|
LL | let [&ref x] = &[&mut 0];
| ^^^^^^ --------- this expression has type `&[&mut {integer}; 1]`
Expand All @@ -15,7 +15,7 @@ LL + let [ref x] = &[&mut 0];
|

error[E0308]: mismatched types
--> $DIR/ref-binding-on-inh-ref-errors.rs:53:10
--> $DIR/ref-binding-on-inh-ref-errors.rs:50:10
|
LL | let [&ref x] = &mut [&mut 0];
| ^^^^^^ ------------- this expression has type `&mut [&mut {integer}; 1]`
Expand All @@ -30,6 +30,13 @@ LL - let [&ref x] = &mut [&mut 0];
LL + let [ref x] = &mut [&mut 0];
|

error: aborting due to 2 previous errors
error[E0596]: cannot borrow data in a `&` reference as mutable
--> $DIR/ref-binding-on-inh-ref-errors.rs:74:10
|
LL | let [ref mut x] = &[0];
| ^^^^^^^^^ cannot borrow as mutable

error: aborting due to 3 previous errors

For more information about this error, try `rustc --explain E0308`.
Some errors have detailed explanations: E0308, E0596.
For more information about an error, try `rustc --explain E0308`.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error: this pattern relies on behavior which may change in edition 2024
--> $DIR/ref-binding-on-inh-ref-errors.rs:16:11
--> $DIR/ref-binding-on-inh-ref-errors.rs:15:11
|
LL | let [&ref x] = &[&0];
| ^^^ cannot override to bind by-reference when that is the implicit default
Expand All @@ -11,7 +11,7 @@ LL | let &[&ref x] = &[&0];
| +

error: this pattern relies on behavior which may change in edition 2024
--> $DIR/ref-binding-on-inh-ref-errors.rs:22:11
--> $DIR/ref-binding-on-inh-ref-errors.rs:21:11
|
LL | let [&ref x] = &mut [&0];
| ^^^ cannot override to bind by-reference when that is the implicit default
Expand All @@ -23,7 +23,7 @@ LL | let &mut [&ref x] = &mut [&0];
| ++++

error: this pattern relies on behavior which may change in edition 2024
--> $DIR/ref-binding-on-inh-ref-errors.rs:28:15
--> $DIR/ref-binding-on-inh-ref-errors.rs:27:15
|
LL | let [&mut ref x] = &mut [&mut 0];
| ^^^ cannot override to bind by-reference when that is the implicit default
Expand All @@ -35,7 +35,7 @@ LL | let &mut [&mut ref x] = &mut [&mut 0];
| ++++

error: this pattern relies on behavior which may change in edition 2024
--> $DIR/ref-binding-on-inh-ref-errors.rs:34:15
--> $DIR/ref-binding-on-inh-ref-errors.rs:33:15
|
LL | let [&mut ref mut x] = &mut [&mut 0];
| ^^^^^^^ cannot override to bind by-reference when that is the implicit default
Expand All @@ -47,7 +47,7 @@ LL | let &mut [&mut ref mut x] = &mut [&mut 0];
| ++++

error: this pattern relies on behavior which may change in edition 2024
--> $DIR/ref-binding-on-inh-ref-errors.rs:46:11
--> $DIR/ref-binding-on-inh-ref-errors.rs:43:11
|
LL | let [&ref x] = &[&mut 0];
| ^^^ cannot override to bind by-reference when that is the implicit default
Expand All @@ -59,7 +59,7 @@ LL | let &[&ref x] = &[&mut 0];
| +

error: this pattern relies on behavior which may change in edition 2024
--> $DIR/ref-binding-on-inh-ref-errors.rs:53:11
--> $DIR/ref-binding-on-inh-ref-errors.rs:50:11
|
LL | let [&ref x] = &mut [&mut 0];
| ^^^ cannot override to bind by-reference when that is the implicit default
Expand All @@ -70,5 +70,72 @@ help: make the implied reference pattern explicit
LL | let &mut [&ref x] = &mut [&mut 0];
| ++++

error: aborting due to 6 previous errors
error: this pattern relies on behavior which may change in edition 2024
--> $DIR/ref-binding-on-inh-ref-errors.rs:60:15
|
LL | let [&mut ref x] = &[&mut 0];
| ^^^ cannot override to bind by-reference when that is the implicit default
|
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2024/match-ergonomics.html>
help: make the implied reference pattern explicit
|
LL | let &[&mut ref x] = &[&mut 0];
| +

error: this pattern relies on behavior which may change in edition 2024
--> $DIR/ref-binding-on-inh-ref-errors.rs:74:10
|
LL | let [ref mut x] = &[0];
| ^^^^^^^ cannot override to bind by-reference when that is the implicit default
|
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2024/match-ergonomics.html>
help: make the implied reference pattern explicit
|
LL | let &[ref mut x] = &[0];
| +

error[E0596]: cannot borrow data in a `&` reference as mutable
--> $DIR/ref-binding-on-inh-ref-errors.rs:74:10
|
LL | let [ref mut x] = &[0];
| ^^^^^^^^^ cannot borrow as mutable

error: this pattern relies on behavior which may change in edition 2024
--> $DIR/ref-binding-on-inh-ref-errors.rs:83:10
|
LL | let [ref x] = &[0];
| ^^^ cannot override to bind by-reference when that is the implicit default
|
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2024/match-ergonomics.html>
help: make the implied reference pattern explicit
|
LL | let &[ref x] = &[0];
| +

error: this pattern relies on behavior which may change in edition 2024
--> $DIR/ref-binding-on-inh-ref-errors.rs:88:10
|
LL | let [ref x] = &mut [0];
| ^^^ cannot override to bind by-reference when that is the implicit default
|
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2024/match-ergonomics.html>
help: make the implied reference pattern explicit
|
LL | let &mut [ref x] = &mut [0];
| ++++

error: this pattern relies on behavior which may change in edition 2024
--> $DIR/ref-binding-on-inh-ref-errors.rs:93:10
|
LL | let [ref mut x] = &mut [0];
| ^^^^^^^ cannot override to bind by-reference when that is the implicit default
|
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2024/match-ergonomics.html>
help: make the implied reference pattern explicit
|
LL | let &mut [ref mut x] = &mut [0];
| ++++

error: aborting due to 12 previous errors

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

0 comments on commit e288cff

Please sign in to comment.