From e24833a4a8789c0acd1850284b3fd660d7a0094a Mon Sep 17 00:00:00 2001 From: dianne Date: Wed, 22 Jan 2025 23:59:03 -0800 Subject: [PATCH 01/11] add test revisions for old-edition behavior of feature gates This also adds `#[cfg]` attributes to tests for bindings' types, to make it visually clearer which revisions type successfully. --- .../borrowck-errors.classic2021.stderr | 37 ++ .../borrowck-errors.classic2024.stderr | 22 +- .../experimental/borrowck-errors.rs | 24 +- .../borrowck-errors.stable2021.stderr | 12 +- .../borrowck-errors.structural2021.stderr | 25 ++ .../borrowck-errors.structural2024.stderr | 4 +- .../mut-ref-mut.classic2024.stderr | 6 +- .../experimental/mut-ref-mut.rs | 16 +- .../mut-ref-mut.structural2024.stderr | 6 +- .../pattern-errors.classic2021.stderr | 354 ++++++++++++++++++ .../pattern-errors.classic2024.stderr | 20 +- .../experimental/pattern-errors.rs | 115 +++--- .../pattern-errors.stable2021.stderr | 52 +-- .../pattern-errors.structural2021.stderr | 354 ++++++++++++++++++ .../pattern-errors.structural2024.stderr | 50 +-- ...nding-on-inh-ref-errors.classic2021.stderr | 9 + ...nding-on-inh-ref-errors.classic2024.stderr | 20 +- .../ref-binding-on-inh-ref-errors.rs | 26 +- ...inding-on-inh-ref-errors.stable2021.stderr | 6 +- ...ng-on-inh-ref-errors.structural2021.stderr | 9 + ...ng-on-inh-ref-errors.structural2024.stderr | 46 +-- ...t-inside-shared-ref-pat.classic2021.stderr | 63 ++++ ...ut-inside-shared-ref-pat.classic2024.fixed | 24 +- ...t-inside-shared-ref-pat.classic2024.stderr | 14 +- .../ref-mut-inside-shared-ref-pat.rs | 24 +- ...ut-inside-shared-ref-pat.stable2021.stderr | 12 +- ...nside-shared-ref-pat.structural2021.stderr | 55 +++ ...inside-shared-ref-pat.structural2024.fixed | 24 +- ...nside-shared-ref-pat.structural2024.stderr | 12 +- ...well-typed-edition-2024.classic2021.stderr | 254 +++++++++++++ .../experimental/well-typed-edition-2024.rs | 112 +++--- .../well-typed-edition-2024.stable2021.stderr | 44 +-- ...l-typed-edition-2024.structural2021.stderr | 254 +++++++++++++ 33 files changed, 1783 insertions(+), 322 deletions(-) create mode 100644 tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/borrowck-errors.classic2021.stderr create mode 100644 tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/borrowck-errors.structural2021.stderr create mode 100644 tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/pattern-errors.classic2021.stderr create mode 100644 tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/pattern-errors.structural2021.stderr create mode 100644 tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref-binding-on-inh-ref-errors.classic2021.stderr create mode 100644 tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref-binding-on-inh-ref-errors.structural2021.stderr create mode 100644 tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref-mut-inside-shared-ref-pat.classic2021.stderr create mode 100644 tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref-mut-inside-shared-ref-pat.structural2021.stderr create mode 100644 tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/well-typed-edition-2024.classic2021.stderr create mode 100644 tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/well-typed-edition-2024.structural2021.stderr diff --git a/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/borrowck-errors.classic2021.stderr b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/borrowck-errors.classic2021.stderr new file mode 100644 index 0000000000000..355a8af6760f3 --- /dev/null +++ b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/borrowck-errors.classic2021.stderr @@ -0,0 +1,37 @@ +error[E0507]: cannot move out of a shared reference + --> $DIR/borrowck-errors.rs:31:29 + | +LL | if let Some(&Some(x)) = Some(&Some(&mut 0)) { + | - ^^^^^^^^^^^^^^^^^^^ + | | + | data moved here + | move occurs because `x` has type `&mut u32`, which does not implement the `Copy` trait + | +help: consider removing the borrow + | +LL - if let Some(&Some(x)) = Some(&Some(&mut 0)) { +LL + if let Some(Some(x)) = Some(&Some(&mut 0)) { + | + +error[E0596]: cannot borrow data in a `&` reference as mutable + --> $DIR/borrowck-errors.rs:36:10 + | +LL | let &ref mut x = &0; + | ^^^^^^^^^ cannot borrow as mutable + +error[E0596]: cannot borrow data in a `&` reference as mutable + --> $DIR/borrowck-errors.rs:41:23 + | +LL | if let &Some(Some(x)) = &Some(&mut Some(0)) { + | ^ cannot borrow as mutable + +error[E0596]: cannot borrow data in a `&` reference as mutable + --> $DIR/borrowck-errors.rs:46:11 + | +LL | let &[x] = &&mut [0]; + | ^ cannot borrow as mutable + +error: aborting due to 4 previous errors + +Some errors have detailed explanations: E0507, E0596. +For more information about an error, try `rustc --explain E0507`. diff --git a/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/borrowck-errors.classic2024.stderr b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/borrowck-errors.classic2024.stderr index 1c44617830828..d40bdb9111b30 100644 --- a/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/borrowck-errors.classic2024.stderr +++ b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/borrowck-errors.classic2024.stderr @@ -1,25 +1,25 @@ -error[E0508]: cannot move out of type `[&mut u32; 1]`, a non-copy array - --> $DIR/borrowck-errors.rs:13:16 +error[E0508]: cannot move out of type `[&mut i32; 1]`, a non-copy array + --> $DIR/borrowck-errors.rs:15:16 | LL | let [&x] = &[&mut 0]; | - ^^^^^^^^^ cannot move out of here | | | data moved here - | move occurs because `x` has type `&mut u32`, which does not implement the `Copy` trait + | move occurs because `x` has type `&mut i32`, which does not implement the `Copy` trait | help: consider borrowing the pattern binding | LL | let [&ref x] = &[&mut 0]; | +++ -error[E0508]: cannot move out of type `[&mut u32; 1]`, a non-copy array - --> $DIR/borrowck-errors.rs:19:16 +error[E0508]: cannot move out of type `[&mut i32; 1]`, a non-copy array + --> $DIR/borrowck-errors.rs:22:16 | LL | let [&x] = &mut [&mut 0]; | - ^^^^^^^^^^^^^ cannot move out of here | | | data moved here - | move occurs because `x` has type `&mut u32`, which does not implement the `Copy` trait + | move occurs because `x` has type `&mut i32`, which does not implement the `Copy` trait | help: consider borrowing the pattern binding | @@ -27,7 +27,7 @@ LL | let [&ref x] = &mut [&mut 0]; | +++ error[E0507]: cannot move out of a shared reference - --> $DIR/borrowck-errors.rs:27:29 + --> $DIR/borrowck-errors.rs:31:29 | LL | if let Some(&Some(x)) = Some(&Some(&mut 0)) { | - ^^^^^^^^^^^^^^^^^^^ @@ -42,25 +42,25 @@ LL + if let Some(Some(x)) = Some(&Some(&mut 0)) { | error[E0596]: cannot borrow data in a `&` reference as mutable - --> $DIR/borrowck-errors.rs:32:10 + --> $DIR/borrowck-errors.rs:36:10 | LL | let &ref mut x = &0; | ^^^^^^^^^ cannot borrow as mutable error[E0596]: cannot borrow data in a `&` reference as mutable - --> $DIR/borrowck-errors.rs:37:23 + --> $DIR/borrowck-errors.rs:41:23 | LL | if let &Some(Some(x)) = &Some(&mut Some(0)) { | ^ cannot borrow as mutable error[E0596]: cannot borrow data in a `&` reference as mutable - --> $DIR/borrowck-errors.rs:42:11 + --> $DIR/borrowck-errors.rs:46:11 | LL | let &[x] = &&mut [0]; | ^ cannot borrow as mutable error[E0508]: cannot move out of type `[&mut i32; 1]`, a non-copy array - --> $DIR/borrowck-errors.rs:46:20 + --> $DIR/borrowck-errors.rs:50:20 | LL | let [&mut x] = &mut [&mut 0]; | - ^^^^^^^^^^^^^ cannot move out of here diff --git a/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/borrowck-errors.rs b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/borrowck-errors.rs index 59cafc50d8661..621ca7cc792eb 100644 --- a/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/borrowck-errors.rs +++ b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/borrowck-errors.rs @@ -1,11 +1,13 @@ -//@ revisions: stable2021 classic2024 structural2024 +//@ revisions: stable2021 classic2021 structural2021 classic2024 structural2024 //@[stable2021] edition: 2021 +//@[classic2021] edition: 2021 +//@[structural2021] edition: 2021 //@[classic2024] edition: 2024 //@[structural2024] edition: 2024 //! Tests for pattern errors not handled by the pattern typing rules, but by borrowck. #![allow(incomplete_features)] -#![cfg_attr(classic2024, feature(ref_pat_eat_one_layer_2024))] -#![cfg_attr(structural2024, feature(ref_pat_eat_one_layer_2024_structural))] +#![cfg_attr(any(classic2021, classic2024), feature(ref_pat_eat_one_layer_2024))] +#![cfg_attr(any(structural2021, structural2024), feature(ref_pat_eat_one_layer_2024_structural))] /// These patterns additionally use `&` to match a `&mut` reference type, which causes compilation /// to fail in HIR typeck on stable. As such, they need to be separate from the other tests. @@ -14,13 +16,15 @@ fn errors_caught_in_hir_typeck_on_stable() { //[stable2021]~^ mismatched types //[stable2021]~| types differ in mutability //[classic2024]~^^^ ERROR: cannot move out of type - let _: &u32 = x; + #[cfg(any(classic2021, structural2021))] let _: u32 = x; + #[cfg(structural2024)] let _: &u32 = x; let [&x] = &mut [&mut 0]; //[stable2021]~^ mismatched types //[stable2021]~| types differ in mutability //[classic2024]~^^^ ERROR: cannot move out of type - let _: &u32 = x; + #[cfg(any(classic2021, structural2021))] let _: u32 = x; + #[cfg(structural2024)] let _: &u32 = x; } pub fn main() { @@ -35,16 +39,16 @@ pub fn main() { // For 2021 edition, this is also a regression test for #136223 // since the maximum mutability is downgraded during the pattern check process. if let &Some(Some(x)) = &Some(&mut Some(0)) { - //[stable2021,classic2024]~^ ERROR: cannot borrow data in a `&` reference as mutable - let _: &u32 = x; + //[stable2021,classic2021,classic2024]~^ ERROR: cannot borrow data in a `&` reference as mutable + #[cfg(any(structural2021, structural2024))] let _: &u32 = x; } let &[x] = &&mut [0]; - //[stable2021,classic2024]~^ ERROR: cannot borrow data in a `&` reference as mutable - let _: &u32 = x; + //[stable2021,classic2021,classic2024]~^ ERROR: cannot borrow data in a `&` reference as mutable + #[cfg(any(structural2021, structural2024))] let _: &u32 = x; let [&mut x] = &mut [&mut 0]; //[classic2024]~^ ERROR: cannot move out of type - #[cfg(stable2021)] let _: u32 = x; + #[cfg(any(stable2021, classic2021, structural2021))] let _: u32 = x; #[cfg(structural2024)] let _: &mut u32 = x; } diff --git a/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/borrowck-errors.stable2021.stderr b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/borrowck-errors.stable2021.stderr index deefe21ca7d45..edcf9f3035707 100644 --- a/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/borrowck-errors.stable2021.stderr +++ b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/borrowck-errors.stable2021.stderr @@ -1,5 +1,5 @@ error[E0308]: mismatched types - --> $DIR/borrowck-errors.rs:13:10 + --> $DIR/borrowck-errors.rs:15:10 | LL | let [&x] = &[&mut 0]; | ^^ --------- this expression has type `&[&mut {integer}; 1]` @@ -15,7 +15,7 @@ LL + let [x] = &[&mut 0]; | error[E0308]: mismatched types - --> $DIR/borrowck-errors.rs:19:10 + --> $DIR/borrowck-errors.rs:22:10 | LL | let [&x] = &mut [&mut 0]; | ^^ ------------- this expression has type `&mut [&mut {integer}; 1]` @@ -31,7 +31,7 @@ LL + let [x] = &mut [&mut 0]; | error[E0507]: cannot move out of a shared reference - --> $DIR/borrowck-errors.rs:27:29 + --> $DIR/borrowck-errors.rs:31:29 | LL | if let Some(&Some(x)) = Some(&Some(&mut 0)) { | - ^^^^^^^^^^^^^^^^^^^ @@ -46,19 +46,19 @@ LL + if let Some(Some(x)) = Some(&Some(&mut 0)) { | error[E0596]: cannot borrow data in a `&` reference as mutable - --> $DIR/borrowck-errors.rs:32:10 + --> $DIR/borrowck-errors.rs:36:10 | LL | let &ref mut x = &0; | ^^^^^^^^^ cannot borrow as mutable error[E0596]: cannot borrow data in a `&` reference as mutable - --> $DIR/borrowck-errors.rs:37:23 + --> $DIR/borrowck-errors.rs:41:23 | LL | if let &Some(Some(x)) = &Some(&mut Some(0)) { | ^ cannot borrow as mutable error[E0596]: cannot borrow data in a `&` reference as mutable - --> $DIR/borrowck-errors.rs:42:11 + --> $DIR/borrowck-errors.rs:46:11 | LL | let &[x] = &&mut [0]; | ^ cannot borrow as mutable diff --git a/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/borrowck-errors.structural2021.stderr b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/borrowck-errors.structural2021.stderr new file mode 100644 index 0000000000000..208f6c8bbed03 --- /dev/null +++ b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/borrowck-errors.structural2021.stderr @@ -0,0 +1,25 @@ +error[E0507]: cannot move out of a shared reference + --> $DIR/borrowck-errors.rs:31:29 + | +LL | if let Some(&Some(x)) = Some(&Some(&mut 0)) { + | - ^^^^^^^^^^^^^^^^^^^ + | | + | data moved here + | move occurs because `x` has type `&mut u32`, which does not implement the `Copy` trait + | +help: consider removing the borrow + | +LL - if let Some(&Some(x)) = Some(&Some(&mut 0)) { +LL + if let Some(Some(x)) = Some(&Some(&mut 0)) { + | + +error[E0596]: cannot borrow data in a `&` reference as mutable + --> $DIR/borrowck-errors.rs:36:10 + | +LL | let &ref mut x = &0; + | ^^^^^^^^^ cannot borrow as mutable + +error: aborting due to 2 previous errors + +Some errors have detailed explanations: E0507, E0596. +For more information about an error, try `rustc --explain E0507`. diff --git a/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/borrowck-errors.structural2024.stderr b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/borrowck-errors.structural2024.stderr index 30d2f9f3d702f..208f6c8bbed03 100644 --- a/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/borrowck-errors.structural2024.stderr +++ b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/borrowck-errors.structural2024.stderr @@ -1,5 +1,5 @@ error[E0507]: cannot move out of a shared reference - --> $DIR/borrowck-errors.rs:27:29 + --> $DIR/borrowck-errors.rs:31:29 | LL | if let Some(&Some(x)) = Some(&Some(&mut 0)) { | - ^^^^^^^^^^^^^^^^^^^ @@ -14,7 +14,7 @@ LL + if let Some(Some(x)) = Some(&Some(&mut 0)) { | error[E0596]: cannot borrow data in a `&` reference as mutable - --> $DIR/borrowck-errors.rs:32:10 + --> $DIR/borrowck-errors.rs:36:10 | LL | let &ref mut x = &0; | ^^^^^^^^^ cannot borrow as mutable diff --git a/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/mut-ref-mut.classic2024.stderr b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/mut-ref-mut.classic2024.stderr index fa95b2b5a575d..6ddced3d16812 100644 --- a/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/mut-ref-mut.classic2024.stderr +++ b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/mut-ref-mut.classic2024.stderr @@ -1,5 +1,5 @@ error[E0658]: binding cannot be both mutable and by-reference - --> $DIR/mut-ref-mut.rs:14:13 + --> $DIR/mut-ref-mut.rs:18:13 | LL | let Foo(mut a) = &Foo(0); | ^^^^ @@ -9,7 +9,7 @@ LL | let Foo(mut a) = &Foo(0); = note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date error[E0658]: binding cannot be both mutable and by-reference - --> $DIR/mut-ref-mut.rs:19:13 + --> $DIR/mut-ref-mut.rs:23:13 | LL | let Foo(mut a) = &mut Foo(0); | ^^^^ @@ -19,7 +19,7 @@ LL | let Foo(mut a) = &mut Foo(0); = note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date error[E0308]: mismatched types - --> $DIR/mut-ref-mut.rs:24:10 + --> $DIR/mut-ref-mut.rs:28:10 | LL | let [&mut mut x] = &[&mut 0]; | ^^^^^ diff --git a/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/mut-ref-mut.rs b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/mut-ref-mut.rs index fbd6514df73d6..c8e988ad76d97 100644 --- a/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/mut-ref-mut.rs +++ b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/mut-ref-mut.rs @@ -1,29 +1,33 @@ -//@ revisions: stable2021 classic2024 structural2024 +//@ revisions: stable2021 classic2021 structural2021 classic2024 structural2024 //@[stable2021] edition: 2021 +//@[classic2021] edition: 2021 +//@[structural2021] edition: 2021 //@[classic2024] edition: 2024 //@[structural2024] edition: 2024 //@[stable2021] run-pass +//@[classic2021] run-pass +//@[structural2021] run-pass //! Test diagnostics for binding with `mut` when the default binding mode is by-ref. #![allow(incomplete_features, unused_assignments, unused_variables)] -#![cfg_attr(classic2024, feature(ref_pat_eat_one_layer_2024))] -#![cfg_attr(structural2024, feature(ref_pat_eat_one_layer_2024_structural))] +#![cfg_attr(any(classic2021, classic2024), feature(ref_pat_eat_one_layer_2024))] +#![cfg_attr(any(structural2021, structural2024), feature(ref_pat_eat_one_layer_2024_structural))] pub fn main() { struct Foo(u8); let Foo(mut a) = &Foo(0); //[classic2024,structural2024]~^ ERROR: binding cannot be both mutable and by-reference - #[cfg(stable2021)] { a = 42 } + #[cfg(any(stable2021, classic2021, structural2021))] { a = 42 } #[cfg(any(classic2024, structural2024))] { a = &42 } let Foo(mut a) = &mut Foo(0); //[classic2024,structural2024]~^ ERROR: binding cannot be both mutable and by-reference - #[cfg(stable2021)] { a = 42 } + #[cfg(any(stable2021, classic2021, structural2021))] { 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 } + #[cfg(any(stable2021, classic2021, structural2021))] { x = 0 } } diff --git a/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/mut-ref-mut.structural2024.stderr b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/mut-ref-mut.structural2024.stderr index fd82da70a18de..c0c0f966b6804 100644 --- a/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/mut-ref-mut.structural2024.stderr +++ b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/mut-ref-mut.structural2024.stderr @@ -1,5 +1,5 @@ error[E0658]: binding cannot be both mutable and by-reference - --> $DIR/mut-ref-mut.rs:14:13 + --> $DIR/mut-ref-mut.rs:18:13 | LL | let Foo(mut a) = &Foo(0); | ^^^^ @@ -9,7 +9,7 @@ LL | let Foo(mut a) = &Foo(0); = note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date error[E0658]: binding cannot be both mutable and by-reference - --> $DIR/mut-ref-mut.rs:19:13 + --> $DIR/mut-ref-mut.rs:23:13 | LL | let Foo(mut a) = &mut Foo(0); | ^^^^ @@ -19,7 +19,7 @@ LL | let Foo(mut a) = &mut Foo(0); = note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date error[E0658]: binding cannot be both mutable and by-reference - --> $DIR/mut-ref-mut.rs:24:15 + --> $DIR/mut-ref-mut.rs:28:15 | LL | let [&mut mut x] = &[&mut 0]; | ^^^^ diff --git a/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/pattern-errors.classic2021.stderr b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/pattern-errors.classic2021.stderr new file mode 100644 index 0000000000000..e039884cdb14b --- /dev/null +++ b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/pattern-errors.classic2021.stderr @@ -0,0 +1,354 @@ +error[E0308]: mismatched types + --> $DIR/pattern-errors.rs:20:27 + | +LL | if let Some(&mut Some(&x)) = &Some(&mut Some(0)) { + | ^^ ------------------- this expression has type `&Option<&mut Option<{integer}>>` + | | + | expected integer, found `&_` + | + = note: expected type `{integer}` + found reference `&_` +help: consider removing `&` from the pattern + | +LL - if let Some(&mut Some(&x)) = &Some(&mut Some(0)) { +LL + if let Some(&mut Some(x)) = &Some(&mut Some(0)) { + | + +error[E0308]: mismatched types + --> $DIR/pattern-errors.rs:33:17 + | +LL | if let Some(&mut Some(&_)) = &Some(&Some(0)) { + | ^^^^^^^^^^^^^ --------------- this expression has type `&Option<&Option<{integer}>>` + | | + | types differ in mutability + | + = note: expected reference `&Option<{integer}>` + found mutable reference `&mut _` + +error[E0308]: mismatched types + --> $DIR/pattern-errors.rs:38:23 + | +LL | if let Some(&Some(&mut x)) = &Some(&mut Some(0)) { + | ^^^^^^ ------------------- this expression has type `&Option<&mut Option<{integer}>>` + | | + | expected integer, found `&mut _` + | + = note: expected type `{integer}` + found mutable reference `&mut _` +note: to declare a mutable binding use: `mut x` + --> $DIR/pattern-errors.rs:38:23 + | +LL | if let Some(&Some(&mut x)) = &Some(&mut Some(0)) { + | ^^^^^^ +help: consider removing `&mut` from the pattern + | +LL - if let Some(&Some(&mut x)) = &Some(&mut Some(0)) { +LL + if let Some(&Some(x)) = &Some(&mut Some(0)) { + | + +error[E0308]: mismatched types + --> $DIR/pattern-errors.rs:45:23 + | +LL | if let Some(&Some(&mut _)) = &mut Some(&Some(0)) { + | ^^^^^^ ------------------- this expression has type `&mut Option<&Option<{integer}>>` + | | + | expected integer, found `&mut _` + | + = note: expected type `{integer}` + found mutable reference `&mut _` + +error[E0308]: mismatched types + --> $DIR/pattern-errors.rs:50:17 + | +LL | if let Some(&Some(Some(&mut x))) = &Some(Some(&mut Some(0))) { + | ^^^^^^^^^^^^^^^^^^^ ------------------------- this expression has type `&Option>>` + | | + | expected `Option<&mut Option<{integer}>>`, found `&_` + | + = note: expected enum `Option<&mut Option<{integer}>>` + found reference `&_` + +error[E0308]: mismatched types + --> $DIR/pattern-errors.rs:57:17 + | +LL | if let Some(&mut Some(x)) = &Some(Some(0)) { + | ^^^^^^^^^^^^ -------------- this expression has type `&Option>` + | | + | expected `Option<{integer}>`, found `&mut _` + | + = note: expected enum `Option<{integer}>` + found mutable reference `&mut _` + +error[E0308]: mismatched types + --> $DIR/pattern-errors.rs:66:11 + | +LL | let &[&mut x] = &&mut [0]; + | ^^^^^^ --------- this expression has type `&&mut [{integer}; 1]` + | | + | expected integer, found `&mut _` + | + = note: expected type `{integer}` + found mutable reference `&mut _` +note: to declare a mutable binding use: `mut x` + --> $DIR/pattern-errors.rs:66:11 + | +LL | let &[&mut x] = &&mut [0]; + | ^^^^^^ +help: consider removing `&mut` from the pattern + | +LL - let &[&mut x] = &&mut [0]; +LL + let &[x] = &&mut [0]; + | + +error[E0308]: mismatched types + --> $DIR/pattern-errors.rs:73:11 + | +LL | let &[&mut x] = &mut &mut [0]; + | ^^^^^^ ------------- this expression has type `&mut &mut [{integer}; 1]` + | | + | expected integer, found `&mut _` + | + = note: expected type `{integer}` + found mutable reference `&mut _` +note: to declare a mutable binding use: `mut x` + --> $DIR/pattern-errors.rs:73:11 + | +LL | let &[&mut x] = &mut &mut [0]; + | ^^^^^^ +help: consider removing `&mut` from the pattern + | +LL - let &[&mut x] = &mut &mut [0]; +LL + let &[x] = &mut &mut [0]; + | + +error[E0308]: mismatched types + --> $DIR/pattern-errors.rs:80:11 + | +LL | let &[&mut ref x] = &&mut [0]; + | ^^^^^^^^^^ --------- this expression has type `&&mut [{integer}; 1]` + | | + | expected integer, found `&mut _` + | + = note: expected type `{integer}` + found mutable reference `&mut _` +note: to declare a mutable binding use: `mut x` + --> $DIR/pattern-errors.rs:80:11 + | +LL | let &[&mut ref x] = &&mut [0]; + | ^^^^^^^^^^ +help: consider removing `&mut` from the pattern + | +LL - let &[&mut ref x] = &&mut [0]; +LL + let &[ref x] = &&mut [0]; + | + +error[E0308]: mismatched types + --> $DIR/pattern-errors.rs:87:11 + | +LL | let &[&mut ref x] = &mut &mut [0]; + | ^^^^^^^^^^ ------------- this expression has type `&mut &mut [{integer}; 1]` + | | + | expected integer, found `&mut _` + | + = note: expected type `{integer}` + found mutable reference `&mut _` +note: to declare a mutable binding use: `mut x` + --> $DIR/pattern-errors.rs:87:11 + | +LL | let &[&mut ref x] = &mut &mut [0]; + | ^^^^^^^^^^ +help: consider removing `&mut` from the pattern + | +LL - let &[&mut ref x] = &mut &mut [0]; +LL + let &[ref x] = &mut &mut [0]; + | + +error[E0308]: mismatched types + --> $DIR/pattern-errors.rs:94:11 + | +LL | let &[&mut mut x] = &&mut [0]; + | ^^^^^^^^^^ --------- this expression has type `&&mut [{integer}; 1]` + | | + | expected integer, found `&mut _` + | + = note: expected type `{integer}` + found mutable reference `&mut _` +note: to declare a mutable binding use: `mut x` + --> $DIR/pattern-errors.rs:94:11 + | +LL | let &[&mut mut x] = &&mut [0]; + | ^^^^^^^^^^ +help: consider removing `&mut` from the pattern + | +LL - let &[&mut mut x] = &&mut [0]; +LL + let &[mut x] = &&mut [0]; + | + +error[E0308]: mismatched types + --> $DIR/pattern-errors.rs:101:11 + | +LL | let &[&mut mut x] = &mut &mut [0]; + | ^^^^^^^^^^ ------------- this expression has type `&mut &mut [{integer}; 1]` + | | + | expected integer, found `&mut _` + | + = note: expected type `{integer}` + found mutable reference `&mut _` +note: to declare a mutable binding use: `mut x` + --> $DIR/pattern-errors.rs:101:11 + | +LL | let &[&mut mut x] = &mut &mut [0]; + | ^^^^^^^^^^ +help: consider removing `&mut` from the pattern + | +LL - let &[&mut mut x] = &mut &mut [0]; +LL + let &[mut x] = &mut &mut [0]; + | + +error[E0308]: mismatched types + --> $DIR/pattern-errors.rs:122:11 + | +LL | let [&&mut x] = &[&mut 0]; + | ^^^^^^ --------- this expression has type `&[&mut {integer}; 1]` + | | + | expected integer, found `&mut _` + | + = note: expected type `{integer}` + found mutable reference `&mut _` +help: consider removing `&mut` from the pattern + | +LL - let [&&mut x] = &[&mut 0]; +LL + let [&x] = &[&mut 0]; + | + +error[E0308]: mismatched types + --> $DIR/pattern-errors.rs:129:11 + | +LL | let [&&mut x] = &mut [&mut 0]; + | ^^^^^^ ------------- this expression has type `&mut [&mut {integer}; 1]` + | | + | expected integer, found `&mut _` + | + = note: expected type `{integer}` + found mutable reference `&mut _` +help: consider removing `&mut` from the pattern + | +LL - let [&&mut x] = &mut [&mut 0]; +LL + let [&x] = &mut [&mut 0]; + | + +error[E0308]: mismatched types + --> $DIR/pattern-errors.rs:136:11 + | +LL | let [&&mut ref x] = &[&mut 0]; + | ^^^^^^^^^^ --------- this expression has type `&[&mut {integer}; 1]` + | | + | expected integer, found `&mut _` + | + = note: expected type `{integer}` + found mutable reference `&mut _` +help: consider removing `&mut` from the pattern + | +LL - let [&&mut ref x] = &[&mut 0]; +LL + let [&ref x] = &[&mut 0]; + | + +error[E0308]: mismatched types + --> $DIR/pattern-errors.rs:143:11 + | +LL | let [&&mut ref x] = &mut [&mut 0]; + | ^^^^^^^^^^ ------------- this expression has type `&mut [&mut {integer}; 1]` + | | + | expected integer, found `&mut _` + | + = note: expected type `{integer}` + found mutable reference `&mut _` +help: consider removing `&mut` from the pattern + | +LL - let [&&mut ref x] = &mut [&mut 0]; +LL + let [&ref x] = &mut [&mut 0]; + | + +error[E0308]: mismatched types + --> $DIR/pattern-errors.rs:150:11 + | +LL | let [&&mut mut x] = &[&mut 0]; + | ^^^^^^^^^^ --------- this expression has type `&[&mut {integer}; 1]` + | | + | expected integer, found `&mut _` + | + = note: expected type `{integer}` + found mutable reference `&mut _` +help: consider removing `&mut` from the pattern + | +LL - let [&&mut mut x] = &[&mut 0]; +LL + let [&mut x] = &[&mut 0]; + | + +error[E0308]: mismatched types + --> $DIR/pattern-errors.rs:157:11 + | +LL | let [&&mut mut x] = &mut [&mut 0]; + | ^^^^^^^^^^ ------------- this expression has type `&mut [&mut {integer}; 1]` + | | + | expected integer, found `&mut _` + | + = note: expected type `{integer}` + found mutable reference `&mut _` +help: consider removing `&mut` from the pattern + | +LL - let [&&mut mut x] = &mut [&mut 0]; +LL + let [&mut x] = &mut [&mut 0]; + | + +error[E0308]: mismatched types + --> $DIR/pattern-errors.rs:172:15 + | +LL | let [&mut &x] = &[&mut 0]; + | ^^ --------- this expression has type `&[&mut {integer}; 1]` + | | + | expected integer, found `&_` + | + = note: expected type `{integer}` + found reference `&_` +help: consider removing `&` from the pattern + | +LL - let [&mut &x] = &[&mut 0]; +LL + let [&mut x] = &[&mut 0]; + | + +error[E0308]: mismatched types + --> $DIR/pattern-errors.rs:178:15 + | +LL | let [&mut &ref x] = &[&mut 0]; + | ^^^^^^ --------- this expression has type `&[&mut {integer}; 1]` + | | + | expected integer, found `&_` + | + = note: expected type `{integer}` + found reference `&_` +help: consider removing `&` from the pattern + | +LL - let [&mut &ref x] = &[&mut 0]; +LL + let [&mut ref x] = &[&mut 0]; + | + +error[E0308]: mismatched types + --> $DIR/pattern-errors.rs:184:15 + | +LL | let [&mut &(mut x)] = &[&mut 0]; + | ^^^^^^^^ --------- this expression has type `&[&mut {integer}; 1]` + | | + | expected integer, found `&_` + | + = note: expected type `{integer}` + found reference `&_` +help: consider removing `&` from the pattern + | +LL - let [&mut &(mut x)] = &[&mut 0]; +LL + let [&mut mut x)] = &[&mut 0]; + | + +error: aborting due to 21 previous errors + +For more information about this error, try `rustc --explain E0308`. diff --git a/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/pattern-errors.classic2024.stderr b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/pattern-errors.classic2024.stderr index 6726a72631533..b98cfe337efee 100644 --- a/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/pattern-errors.classic2024.stderr +++ b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/pattern-errors.classic2024.stderr @@ -1,5 +1,5 @@ error[E0308]: mismatched types - --> $DIR/pattern-errors.rs:12:17 + --> $DIR/pattern-errors.rs:14:17 | LL | if let Some(&mut x) = &Some(&mut 0) { | ^^^^^ @@ -12,7 +12,7 @@ LL + if let Some(&x) = &Some(&mut 0) { | error[E0308]: mismatched types - --> $DIR/pattern-errors.rs:18:17 + --> $DIR/pattern-errors.rs:20:17 | LL | if let Some(&mut Some(&x)) = &Some(&mut Some(0)) { | ^^^^^ @@ -25,7 +25,7 @@ LL + if let Some(&Some(&x)) = &Some(&mut Some(0)) { | error[E0308]: mismatched types - --> $DIR/pattern-errors.rs:24:22 + --> $DIR/pattern-errors.rs:26:22 | LL | if let Some(Some(&mut x)) = &Some(Some(&mut 0)) { | ^^^^^ @@ -38,7 +38,7 @@ LL + if let Some(Some(&x)) = &Some(Some(&mut 0)) { | error[E0308]: mismatched types - --> $DIR/pattern-errors.rs:31:17 + --> $DIR/pattern-errors.rs:33:17 | LL | if let Some(&mut Some(&_)) = &Some(&Some(0)) { | ^^^^^ @@ -51,7 +51,7 @@ LL + if let Some(&Some(&_)) = &Some(&Some(0)) { | error[E0308]: mismatched types - --> $DIR/pattern-errors.rs:41:23 + --> $DIR/pattern-errors.rs:45:23 | LL | if let Some(&Some(&mut _)) = &mut Some(&Some(0)) { | ^^^^^ @@ -64,7 +64,7 @@ LL + if let Some(&Some(&_)) = &mut Some(&Some(0)) { | error[E0308]: mismatched types - --> $DIR/pattern-errors.rs:51:17 + --> $DIR/pattern-errors.rs:57:17 | LL | if let Some(&mut Some(x)) = &Some(Some(0)) { | ^^^^^ @@ -77,7 +77,7 @@ LL + if let Some(&Some(x)) = &Some(Some(0)) { | error[E0308]: mismatched types - --> $DIR/pattern-errors.rs:147:10 + --> $DIR/pattern-errors.rs:166:10 | LL | let [&mut x] = &[&mut 0]; | ^^^^^ @@ -90,7 +90,7 @@ LL + let [&x] = &[&mut 0]; | error[E0308]: mismatched types - --> $DIR/pattern-errors.rs:153:10 + --> $DIR/pattern-errors.rs:172:10 | LL | let [&mut &x] = &[&mut 0]; | ^^^^^ @@ -103,7 +103,7 @@ LL + let [&&x] = &[&mut 0]; | error[E0308]: mismatched types - --> $DIR/pattern-errors.rs:159:10 + --> $DIR/pattern-errors.rs:178:10 | LL | let [&mut &ref x] = &[&mut 0]; | ^^^^^ @@ -116,7 +116,7 @@ LL + let [&&ref x] = &[&mut 0]; | error[E0308]: mismatched types - --> $DIR/pattern-errors.rs:165:10 + --> $DIR/pattern-errors.rs:184:10 | LL | let [&mut &(mut x)] = &[&mut 0]; | ^^^^^ diff --git a/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/pattern-errors.rs b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/pattern-errors.rs index c07c2972cd055..4173b1819cba5 100644 --- a/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/pattern-errors.rs +++ b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/pattern-errors.rs @@ -1,170 +1,189 @@ -//@ revisions: stable2021 classic2024 structural2024 +//@ revisions: stable2021 classic2021 structural2021 classic2024 structural2024 //@[stable2021] edition: 2021 +//@[classic2021] edition: 2021 +//@[structural2021] edition: 2021 //@[classic2024] edition: 2024 //@[structural2024] edition: 2024 //! Test cases for poorly-typed patterns in edition 2024 which are caught by HIR typeck. These must //! be separate from cases caught by MIR borrowck or the latter errors may not be emitted. #![allow(incomplete_features)] -#![cfg_attr(classic2024, feature(ref_pat_eat_one_layer_2024))] -#![cfg_attr(structural2024, feature(ref_pat_eat_one_layer_2024_structural))] +#![cfg_attr(any(classic2021, classic2024), feature(ref_pat_eat_one_layer_2024))] +#![cfg_attr(any(structural2021, structural2024), feature(ref_pat_eat_one_layer_2024_structural))] pub fn main() { if let Some(&mut x) = &Some(&mut 0) { //[classic2024]~^ ERROR: mismatched types //[classic2024]~| cannot match inherited `&` with `&mut` pattern - #[cfg(stable2021)] let _: u32 = x; + #[cfg(any(stable2021, classic2021, structural2021))] let _: u32 = x; #[cfg(structural2024)] let _: &u32 = x; } if let Some(&mut Some(&x)) = &Some(&mut Some(0)) { - //[stable2021,classic2024]~^ ERROR: mismatched types - //[stable2021]~| expected integer, found `&_` + //[stable2021,classic2021,structural2021,classic2024]~^ ERROR: mismatched types + //[stable2021,classic2021,structural2021]~| expected integer, found `&_` //[classic2024]~| cannot match inherited `&` with `&mut` pattern - let _: u32 = x; + #[cfg(structural2024)] let _: u32 = x; } if let Some(Some(&mut x)) = &Some(Some(&mut 0)) { //[classic2024]~^ ERROR: mismatched types //[classic2024]~| cannot match inherited `&` with `&mut` pattern - #[cfg(stable2021)] let _: u32 = x; + #[cfg(any(stable2021, classic2021, structural2021))] let _: u32 = x; #[cfg(structural2024)] let _: &u32 = x; } if let Some(&mut Some(&_)) = &Some(&Some(0)) { //~^ ERROR: mismatched types - //[stable2021]~| types differ in mutability + //[stable2021,classic2021,structural2021]~| types differ in mutability //[classic2024,structural2024]~| cannot match inherited `&` with `&mut` pattern } - if let Some(&Some(&mut _)) = &Some(&mut Some(0)) { - //[stable2021,structural2024]~^ ERROR: mismatched types + if let Some(&Some(&mut x)) = &Some(&mut Some(0)) { + //[stable2021,classic2021,structural2021,structural2024]~^ ERROR: mismatched types //[stable2021]~| types differ in mutability + //[classic2021,structural2021]~| expected integer, found `&mut _` //[structural2024]~| cannot match inherited `&` with `&mut` pattern + #[cfg(classic2024)] let _: u32 = x; } if let Some(&Some(&mut _)) = &mut Some(&Some(0)) { //~^ ERROR: mismatched types - //[stable2021]~| expected integer, found `&mut _` + //[stable2021,classic2021,structural2021]~| expected integer, found `&mut _` //[classic2024,structural2024]~| cannot match inherited `&` with `&mut` pattern } - if let Some(&Some(Some(&mut _))) = &Some(Some(&mut Some(0))) { - //[stable2021,structural2024]~^ ERROR: mismatched types + if let Some(&Some(Some(&mut x))) = &Some(Some(&mut Some(0))) { + //[stable2021,classic2021,structural2021,structural2024]~^ ERROR: mismatched types //[stable2021]~| expected `Option<&mut Option<{integer}>>`, found `&_` //[structural2024]~| cannot match inherited `&` with `&mut` pattern + // TODO: the error on `structural2021` should be an inherited ref mutability mismatch too + #[cfg(classic2024)] let _: u32 = x; // TODO: this should hold on `classic2021` too } if let Some(&mut Some(x)) = &Some(Some(0)) { //~^ ERROR: mismatched types - //[stable2021]~| expected `Option<{integer}>`, found `&mut _` + //[stable2021,classic2021,structural2021]~| expected `Option<{integer}>`, found `&mut _` //[classic2024,structural2024]~| cannot match inherited `&` with `&mut` pattern + // TODO: the error on `classic2021` and `structural2021` should be the mutability mismatch } } fn structural_errors_0() { let &[&mut x] = &&mut [0]; - //[stable2021,structural2024]~^ ERROR: mismatched types + //[stable2021,classic2021,structural2021,structural2024]~^ ERROR: mismatched types //[stable2021]~| expected integer, found `&mut _` //[structural2024]~| cannot match inherited `&` with `&mut` pattern - let _: u32 = x; + // TODO: the error on `structural2021` should be an inherited ref mutability mismatch too + #[cfg(classic2024)] let _: u32 = x; // TODO: this should hold for `classic2021` too let &[&mut x] = &mut &mut [0]; - //[stable2021,structural2024]~^ ERROR: mismatched types + //[stable2021,classic2021,structural2021,structural2024]~^ ERROR: mismatched types //[stable2021]~| types differ in mutability //[structural2024]~| cannot match inherited `&` with `&mut` pattern - let _: u32 = x; + // TODO: the error on `structural2021` should be an inherited ref mutability mismatch too + #[cfg(classic2024)] let _: u32 = x; // TODO: this should hold for `classic2021` too let &[&mut ref x] = &&mut [0]; - //[stable2021,structural2024]~^ ERROR: mismatched types + //[stable2021,classic2021,structural2021,structural2024]~^ ERROR: mismatched types //[stable2021]~| expected integer, found `&mut _` //[structural2024]~| cannot match inherited `&` with `&mut` pattern - let _: &u32 = x; + // TODO: the error on `structural2021` should be an inherited ref mutability mismatch too + #[cfg(classic2024)] let _: &u32 = x; // TODO: this should hold for `classic2021` too let &[&mut ref x] = &mut &mut [0]; - //[stable2021,structural2024]~^ ERROR: mismatched types + //[stable2021,classic2021,structural2021,structural2024]~^ ERROR: mismatched types //[stable2021]~| types differ in mutability //[structural2024]~| cannot match inherited `&` with `&mut` pattern - let _: &u32 = x; + // TODO: the error on `structural2021` should be an inherited ref mutability mismatch too + #[cfg(classic2024)] let _: &u32 = x; // TODO: this should hold for `classic2021` too let &[&mut mut x] = &&mut [0]; - //[stable2021,structural2024]~^ ERROR: mismatched types + //[stable2021,classic2021,structural2021,structural2024]~^ ERROR: mismatched types //[stable2021]~| expected integer, found `&mut _` //[structural2024]~| cannot match inherited `&` with `&mut` pattern - let _: u32 = x; + // TODO: the error on `structural2021` should be an inherited ref mutability mismatch too + #[cfg(classic2024)] let _: u32 = x; // TODO: this should hold for `classic2021` too let &[&mut mut x] = &mut &mut [0]; - //[stable2021,structural2024]~^ ERROR: mismatched types + //[stable2021,classic2021,structural2021,structural2024]~^ ERROR: mismatched types //[stable2021]~| types differ in mutability //[structural2024]~| cannot match inherited `&` with `&mut` pattern - let _: u32 = x; + // TODO: the error on `structural2021` should be an inherited ref mutability mismatch too + #[cfg(classic2024)] let _: u32 = x; // TODO: this should hold for `classic2021` too } fn structural_errors_1() { let [&(mut x)] = &[&0]; //[structural2024]~^ ERROR: binding cannot be both mutable and by-reference - #[cfg(stable2021)] let _: u32 = x; + #[cfg(any(stable2021, classic2021, structural2021))] let _: u32 = x; #[cfg(classic2024)] let _: &u32 = x; let [&(mut x)] = &mut [&0]; //[structural2024]~^ ERROR: binding cannot be both mutable and by-reference - #[cfg(stable2021)] let _: u32 = x; + #[cfg(any(stable2021, classic2021, structural2021))] let _: u32 = x; #[cfg(classic2024)] let _: &u32 = x; } fn structural_errors_2() { let [&&mut x] = &[&mut 0]; - //[stable2021,structural2024]~^ ERROR: mismatched types + //[stable2021,classic2021,structural2021,structural2024]~^ ERROR: mismatched types //[stable2021]~| types differ in mutability + //[classic2021,structural2021] expected integer, found `&mut _` //[structural2024]~| cannot match inherited `&` with `&mut` pattern - let _: u32 = x; + #[cfg(classic2024)] let _: u32 = x; let [&&mut x] = &mut [&mut 0]; - //[stable2021,structural2024]~^ ERROR: mismatched types + //[stable2021,classic2021,structural2021,structural2024]~^ ERROR: mismatched types //[stable2021]~| types differ in mutability + //[classic2021,structural2021] expected integer, found `&mut _` //[structural2024]~| cannot match inherited `&` with `&mut` pattern - let _: u32 = x; + #[cfg(classic2024)] let _: u32 = x; let [&&mut ref x] = &[&mut 0]; - //[stable2021,structural2024]~^ ERROR: mismatched types + //[stable2021,classic2021,structural2021,structural2024]~^ ERROR: mismatched types //[stable2021]~| types differ in mutability + //[classic2021,structural2021] expected integer, found `&mut _` //[structural2024]~| cannot match inherited `&` with `&mut` pattern - let _: &u32 = x; + #[cfg(classic2024)] let _: &u32 = x; let [&&mut ref x] = &mut [&mut 0]; - //[stable2021,structural2024]~^ ERROR: mismatched types + //[stable2021,classic2021,structural2021,structural2024]~^ ERROR: mismatched types //[stable2021]~| types differ in mutability + //[classic2021,structural2021] expected integer, found `&mut _` //[structural2024]~| cannot match inherited `&` with `&mut` pattern - let _: &u32 = x; + #[cfg(classic2024)] let _: &u32 = x; let [&&mut mut x] = &[&mut 0]; - //[stable2021,structural2024]~^ ERROR: mismatched types + //[stable2021,classic2021,structural2021,structural2024]~^ ERROR: mismatched types //[stable2021]~| types differ in mutability + //[classic2021,structural2021] expected integer, found `&mut _` //[structural2024]~| cannot match inherited `&` with `&mut` pattern - let _: u32 = x; + #[cfg(classic2024)] let _: u32 = x; let [&&mut mut x] = &mut [&mut 0]; - //[stable2021,structural2024]~^ ERROR: mismatched types + //[stable2021,classic2021,structural2021,structural2024]~^ ERROR: mismatched types //[stable2021]~| types differ in mutability + //[classic2021,structural2021] expected integer, found `&mut _` //[structural2024]~| cannot match inherited `&` with `&mut` pattern - let _: u32 = x; + #[cfg(classic2024)] let _: u32 = x; } fn classic_errors_0() { let [&mut x] = &[&mut 0]; //[classic2024]~^ ERROR: mismatched types //[classic2024]~| cannot match inherited `&` with `&mut` pattern - #[cfg(stable2021)] let _: u32 = x; + #[cfg(any(stable2021, classic2021, structural2021))] let _: u32 = x; #[cfg(structural2024)] let _: &u32 = x; let [&mut &x] = &[&mut 0]; - //[stable2021,classic2024]~^ ERROR: mismatched types + //[stable2021,classic2021,structural2021,classic2024]~^ ERROR: mismatched types //[stable2021]~| expected integer, found `&_` //[classic2024]~| cannot match inherited `&` with `&mut` pattern - let _: u32 = x; + #[cfg(structural2024)] let _: u32 = x; let [&mut &ref x] = &[&mut 0]; - //[stable2021,classic2024]~^ ERROR: mismatched types + //[stable2021,classic2021,structural2021,classic2024]~^ ERROR: mismatched types //[stable2021]~| expected integer, found `&_` //[classic2024]~| cannot match inherited `&` with `&mut` pattern - let _: &u32 = x; + #[cfg(structural2024)] let _: &u32 = x; let [&mut &(mut x)] = &[&mut 0]; - //[stable2021,classic2024]~^ ERROR: mismatched types + //[stable2021,classic2021,structural2021,classic2024]~^ ERROR: mismatched types //[stable2021]~| expected integer, found `&_` //[classic2024]~| cannot match inherited `&` with `&mut` pattern - let _: u32 = x; + #[cfg(structural2024)] let _: u32 = x; } diff --git a/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/pattern-errors.stable2021.stderr b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/pattern-errors.stable2021.stderr index ad19b122c20d7..ad9541b71a028 100644 --- a/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/pattern-errors.stable2021.stderr +++ b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/pattern-errors.stable2021.stderr @@ -1,5 +1,5 @@ error[E0308]: mismatched types - --> $DIR/pattern-errors.rs:18:27 + --> $DIR/pattern-errors.rs:20:27 | LL | if let Some(&mut Some(&x)) = &Some(&mut Some(0)) { | ^^ ------------------- this expression has type `&Option<&mut Option<{integer}>>` @@ -15,7 +15,7 @@ LL + if let Some(&mut Some(x)) = &Some(&mut Some(0)) { | error[E0308]: mismatched types - --> $DIR/pattern-errors.rs:31:17 + --> $DIR/pattern-errors.rs:33:17 | LL | if let Some(&mut Some(&_)) = &Some(&Some(0)) { | ^^^^^^^^^^^^^ --------------- this expression has type `&Option<&Option<{integer}>>` @@ -26,9 +26,9 @@ LL | if let Some(&mut Some(&_)) = &Some(&Some(0)) { found mutable reference `&mut _` error[E0308]: mismatched types - --> $DIR/pattern-errors.rs:36:17 + --> $DIR/pattern-errors.rs:38:17 | -LL | if let Some(&Some(&mut _)) = &Some(&mut Some(0)) { +LL | if let Some(&Some(&mut x)) = &Some(&mut Some(0)) { | ^^^^^^^^^^^^^ ------------------- this expression has type `&Option<&mut Option<{integer}>>` | | | types differ in mutability @@ -37,7 +37,7 @@ LL | if let Some(&Some(&mut _)) = &Some(&mut Some(0)) { found reference `&_` error[E0308]: mismatched types - --> $DIR/pattern-errors.rs:41:23 + --> $DIR/pattern-errors.rs:45:23 | LL | if let Some(&Some(&mut _)) = &mut Some(&Some(0)) { | ^^^^^^ ------------------- this expression has type `&mut Option<&Option<{integer}>>` @@ -48,9 +48,9 @@ LL | if let Some(&Some(&mut _)) = &mut Some(&Some(0)) { found mutable reference `&mut _` error[E0308]: mismatched types - --> $DIR/pattern-errors.rs:46:17 + --> $DIR/pattern-errors.rs:50:17 | -LL | if let Some(&Some(Some(&mut _))) = &Some(Some(&mut Some(0))) { +LL | if let Some(&Some(Some(&mut x))) = &Some(Some(&mut Some(0))) { | ^^^^^^^^^^^^^^^^^^^ ------------------------- this expression has type `&Option>>` | | | expected `Option<&mut Option<{integer}>>`, found `&_` @@ -59,7 +59,7 @@ LL | if let Some(&Some(Some(&mut _))) = &Some(Some(&mut Some(0))) { found reference `&_` error[E0308]: mismatched types - --> $DIR/pattern-errors.rs:51:17 + --> $DIR/pattern-errors.rs:57:17 | LL | if let Some(&mut Some(x)) = &Some(Some(0)) { | ^^^^^^^^^^^^ -------------- this expression has type `&Option>` @@ -70,7 +70,7 @@ LL | if let Some(&mut Some(x)) = &Some(Some(0)) { found mutable reference `&mut _` error[E0308]: mismatched types - --> $DIR/pattern-errors.rs:59:11 + --> $DIR/pattern-errors.rs:66:11 | LL | let &[&mut x] = &&mut [0]; | ^^^^^^ --------- this expression has type `&&mut [{integer}; 1]` @@ -80,7 +80,7 @@ LL | let &[&mut x] = &&mut [0]; = note: expected type `{integer}` found mutable reference `&mut _` note: to declare a mutable binding use: `mut x` - --> $DIR/pattern-errors.rs:59:11 + --> $DIR/pattern-errors.rs:66:11 | LL | let &[&mut x] = &&mut [0]; | ^^^^^^ @@ -91,7 +91,7 @@ LL + let &[x] = &&mut [0]; | error[E0308]: mismatched types - --> $DIR/pattern-errors.rs:65:9 + --> $DIR/pattern-errors.rs:73:9 | LL | let &[&mut x] = &mut &mut [0]; | ^^^^^^^^^ ------------- this expression has type `&mut &mut [{integer}; 1]` @@ -102,7 +102,7 @@ LL | let &[&mut x] = &mut &mut [0]; found reference `&_` error[E0308]: mismatched types - --> $DIR/pattern-errors.rs:71:11 + --> $DIR/pattern-errors.rs:80:11 | LL | let &[&mut ref x] = &&mut [0]; | ^^^^^^^^^^ --------- this expression has type `&&mut [{integer}; 1]` @@ -112,7 +112,7 @@ LL | let &[&mut ref x] = &&mut [0]; = note: expected type `{integer}` found mutable reference `&mut _` note: to declare a mutable binding use: `mut x` - --> $DIR/pattern-errors.rs:71:11 + --> $DIR/pattern-errors.rs:80:11 | LL | let &[&mut ref x] = &&mut [0]; | ^^^^^^^^^^ @@ -123,7 +123,7 @@ LL + let &[ref x] = &&mut [0]; | error[E0308]: mismatched types - --> $DIR/pattern-errors.rs:77:9 + --> $DIR/pattern-errors.rs:87:9 | LL | let &[&mut ref x] = &mut &mut [0]; | ^^^^^^^^^^^^^ ------------- this expression has type `&mut &mut [{integer}; 1]` @@ -134,7 +134,7 @@ LL | let &[&mut ref x] = &mut &mut [0]; found reference `&_` error[E0308]: mismatched types - --> $DIR/pattern-errors.rs:83:11 + --> $DIR/pattern-errors.rs:94:11 | LL | let &[&mut mut x] = &&mut [0]; | ^^^^^^^^^^ --------- this expression has type `&&mut [{integer}; 1]` @@ -144,7 +144,7 @@ LL | let &[&mut mut x] = &&mut [0]; = note: expected type `{integer}` found mutable reference `&mut _` note: to declare a mutable binding use: `mut x` - --> $DIR/pattern-errors.rs:83:11 + --> $DIR/pattern-errors.rs:94:11 | LL | let &[&mut mut x] = &&mut [0]; | ^^^^^^^^^^ @@ -155,7 +155,7 @@ LL + let &[mut x] = &&mut [0]; | error[E0308]: mismatched types - --> $DIR/pattern-errors.rs:89:9 + --> $DIR/pattern-errors.rs:101:9 | LL | let &[&mut mut x] = &mut &mut [0]; | ^^^^^^^^^^^^^ ------------- this expression has type `&mut &mut [{integer}; 1]` @@ -166,7 +166,7 @@ LL | let &[&mut mut x] = &mut &mut [0]; found reference `&_` error[E0308]: mismatched types - --> $DIR/pattern-errors.rs:109:10 + --> $DIR/pattern-errors.rs:122:10 | LL | let [&&mut x] = &[&mut 0]; | ^^^^^^^ --------- this expression has type `&[&mut {integer}; 1]` @@ -177,7 +177,7 @@ LL | let [&&mut x] = &[&mut 0]; found reference `&_` error[E0308]: mismatched types - --> $DIR/pattern-errors.rs:115:10 + --> $DIR/pattern-errors.rs:129:10 | LL | let [&&mut x] = &mut [&mut 0]; | ^^^^^^^ ------------- this expression has type `&mut [&mut {integer}; 1]` @@ -188,7 +188,7 @@ LL | let [&&mut x] = &mut [&mut 0]; found reference `&_` error[E0308]: mismatched types - --> $DIR/pattern-errors.rs:121:10 + --> $DIR/pattern-errors.rs:136:10 | LL | let [&&mut ref x] = &[&mut 0]; | ^^^^^^^^^^^ --------- this expression has type `&[&mut {integer}; 1]` @@ -199,7 +199,7 @@ LL | let [&&mut ref x] = &[&mut 0]; found reference `&_` error[E0308]: mismatched types - --> $DIR/pattern-errors.rs:127:10 + --> $DIR/pattern-errors.rs:143:10 | LL | let [&&mut ref x] = &mut [&mut 0]; | ^^^^^^^^^^^ ------------- this expression has type `&mut [&mut {integer}; 1]` @@ -210,7 +210,7 @@ LL | let [&&mut ref x] = &mut [&mut 0]; found reference `&_` error[E0308]: mismatched types - --> $DIR/pattern-errors.rs:133:10 + --> $DIR/pattern-errors.rs:150:10 | LL | let [&&mut mut x] = &[&mut 0]; | ^^^^^^^^^^^ --------- this expression has type `&[&mut {integer}; 1]` @@ -221,7 +221,7 @@ LL | let [&&mut mut x] = &[&mut 0]; found reference `&_` error[E0308]: mismatched types - --> $DIR/pattern-errors.rs:139:10 + --> $DIR/pattern-errors.rs:157:10 | LL | let [&&mut mut x] = &mut [&mut 0]; | ^^^^^^^^^^^ ------------- this expression has type `&mut [&mut {integer}; 1]` @@ -232,7 +232,7 @@ LL | let [&&mut mut x] = &mut [&mut 0]; found reference `&_` error[E0308]: mismatched types - --> $DIR/pattern-errors.rs:153:15 + --> $DIR/pattern-errors.rs:172:15 | LL | let [&mut &x] = &[&mut 0]; | ^^ --------- this expression has type `&[&mut {integer}; 1]` @@ -248,7 +248,7 @@ LL + let [&mut x] = &[&mut 0]; | error[E0308]: mismatched types - --> $DIR/pattern-errors.rs:159:15 + --> $DIR/pattern-errors.rs:178:15 | LL | let [&mut &ref x] = &[&mut 0]; | ^^^^^^ --------- this expression has type `&[&mut {integer}; 1]` @@ -264,7 +264,7 @@ LL + let [&mut ref x] = &[&mut 0]; | error[E0308]: mismatched types - --> $DIR/pattern-errors.rs:165:15 + --> $DIR/pattern-errors.rs:184:15 | LL | let [&mut &(mut x)] = &[&mut 0]; | ^^^^^^^^ --------- this expression has type `&[&mut {integer}; 1]` diff --git a/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/pattern-errors.structural2021.stderr b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/pattern-errors.structural2021.stderr new file mode 100644 index 0000000000000..e039884cdb14b --- /dev/null +++ b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/pattern-errors.structural2021.stderr @@ -0,0 +1,354 @@ +error[E0308]: mismatched types + --> $DIR/pattern-errors.rs:20:27 + | +LL | if let Some(&mut Some(&x)) = &Some(&mut Some(0)) { + | ^^ ------------------- this expression has type `&Option<&mut Option<{integer}>>` + | | + | expected integer, found `&_` + | + = note: expected type `{integer}` + found reference `&_` +help: consider removing `&` from the pattern + | +LL - if let Some(&mut Some(&x)) = &Some(&mut Some(0)) { +LL + if let Some(&mut Some(x)) = &Some(&mut Some(0)) { + | + +error[E0308]: mismatched types + --> $DIR/pattern-errors.rs:33:17 + | +LL | if let Some(&mut Some(&_)) = &Some(&Some(0)) { + | ^^^^^^^^^^^^^ --------------- this expression has type `&Option<&Option<{integer}>>` + | | + | types differ in mutability + | + = note: expected reference `&Option<{integer}>` + found mutable reference `&mut _` + +error[E0308]: mismatched types + --> $DIR/pattern-errors.rs:38:23 + | +LL | if let Some(&Some(&mut x)) = &Some(&mut Some(0)) { + | ^^^^^^ ------------------- this expression has type `&Option<&mut Option<{integer}>>` + | | + | expected integer, found `&mut _` + | + = note: expected type `{integer}` + found mutable reference `&mut _` +note: to declare a mutable binding use: `mut x` + --> $DIR/pattern-errors.rs:38:23 + | +LL | if let Some(&Some(&mut x)) = &Some(&mut Some(0)) { + | ^^^^^^ +help: consider removing `&mut` from the pattern + | +LL - if let Some(&Some(&mut x)) = &Some(&mut Some(0)) { +LL + if let Some(&Some(x)) = &Some(&mut Some(0)) { + | + +error[E0308]: mismatched types + --> $DIR/pattern-errors.rs:45:23 + | +LL | if let Some(&Some(&mut _)) = &mut Some(&Some(0)) { + | ^^^^^^ ------------------- this expression has type `&mut Option<&Option<{integer}>>` + | | + | expected integer, found `&mut _` + | + = note: expected type `{integer}` + found mutable reference `&mut _` + +error[E0308]: mismatched types + --> $DIR/pattern-errors.rs:50:17 + | +LL | if let Some(&Some(Some(&mut x))) = &Some(Some(&mut Some(0))) { + | ^^^^^^^^^^^^^^^^^^^ ------------------------- this expression has type `&Option>>` + | | + | expected `Option<&mut Option<{integer}>>`, found `&_` + | + = note: expected enum `Option<&mut Option<{integer}>>` + found reference `&_` + +error[E0308]: mismatched types + --> $DIR/pattern-errors.rs:57:17 + | +LL | if let Some(&mut Some(x)) = &Some(Some(0)) { + | ^^^^^^^^^^^^ -------------- this expression has type `&Option>` + | | + | expected `Option<{integer}>`, found `&mut _` + | + = note: expected enum `Option<{integer}>` + found mutable reference `&mut _` + +error[E0308]: mismatched types + --> $DIR/pattern-errors.rs:66:11 + | +LL | let &[&mut x] = &&mut [0]; + | ^^^^^^ --------- this expression has type `&&mut [{integer}; 1]` + | | + | expected integer, found `&mut _` + | + = note: expected type `{integer}` + found mutable reference `&mut _` +note: to declare a mutable binding use: `mut x` + --> $DIR/pattern-errors.rs:66:11 + | +LL | let &[&mut x] = &&mut [0]; + | ^^^^^^ +help: consider removing `&mut` from the pattern + | +LL - let &[&mut x] = &&mut [0]; +LL + let &[x] = &&mut [0]; + | + +error[E0308]: mismatched types + --> $DIR/pattern-errors.rs:73:11 + | +LL | let &[&mut x] = &mut &mut [0]; + | ^^^^^^ ------------- this expression has type `&mut &mut [{integer}; 1]` + | | + | expected integer, found `&mut _` + | + = note: expected type `{integer}` + found mutable reference `&mut _` +note: to declare a mutable binding use: `mut x` + --> $DIR/pattern-errors.rs:73:11 + | +LL | let &[&mut x] = &mut &mut [0]; + | ^^^^^^ +help: consider removing `&mut` from the pattern + | +LL - let &[&mut x] = &mut &mut [0]; +LL + let &[x] = &mut &mut [0]; + | + +error[E0308]: mismatched types + --> $DIR/pattern-errors.rs:80:11 + | +LL | let &[&mut ref x] = &&mut [0]; + | ^^^^^^^^^^ --------- this expression has type `&&mut [{integer}; 1]` + | | + | expected integer, found `&mut _` + | + = note: expected type `{integer}` + found mutable reference `&mut _` +note: to declare a mutable binding use: `mut x` + --> $DIR/pattern-errors.rs:80:11 + | +LL | let &[&mut ref x] = &&mut [0]; + | ^^^^^^^^^^ +help: consider removing `&mut` from the pattern + | +LL - let &[&mut ref x] = &&mut [0]; +LL + let &[ref x] = &&mut [0]; + | + +error[E0308]: mismatched types + --> $DIR/pattern-errors.rs:87:11 + | +LL | let &[&mut ref x] = &mut &mut [0]; + | ^^^^^^^^^^ ------------- this expression has type `&mut &mut [{integer}; 1]` + | | + | expected integer, found `&mut _` + | + = note: expected type `{integer}` + found mutable reference `&mut _` +note: to declare a mutable binding use: `mut x` + --> $DIR/pattern-errors.rs:87:11 + | +LL | let &[&mut ref x] = &mut &mut [0]; + | ^^^^^^^^^^ +help: consider removing `&mut` from the pattern + | +LL - let &[&mut ref x] = &mut &mut [0]; +LL + let &[ref x] = &mut &mut [0]; + | + +error[E0308]: mismatched types + --> $DIR/pattern-errors.rs:94:11 + | +LL | let &[&mut mut x] = &&mut [0]; + | ^^^^^^^^^^ --------- this expression has type `&&mut [{integer}; 1]` + | | + | expected integer, found `&mut _` + | + = note: expected type `{integer}` + found mutable reference `&mut _` +note: to declare a mutable binding use: `mut x` + --> $DIR/pattern-errors.rs:94:11 + | +LL | let &[&mut mut x] = &&mut [0]; + | ^^^^^^^^^^ +help: consider removing `&mut` from the pattern + | +LL - let &[&mut mut x] = &&mut [0]; +LL + let &[mut x] = &&mut [0]; + | + +error[E0308]: mismatched types + --> $DIR/pattern-errors.rs:101:11 + | +LL | let &[&mut mut x] = &mut &mut [0]; + | ^^^^^^^^^^ ------------- this expression has type `&mut &mut [{integer}; 1]` + | | + | expected integer, found `&mut _` + | + = note: expected type `{integer}` + found mutable reference `&mut _` +note: to declare a mutable binding use: `mut x` + --> $DIR/pattern-errors.rs:101:11 + | +LL | let &[&mut mut x] = &mut &mut [0]; + | ^^^^^^^^^^ +help: consider removing `&mut` from the pattern + | +LL - let &[&mut mut x] = &mut &mut [0]; +LL + let &[mut x] = &mut &mut [0]; + | + +error[E0308]: mismatched types + --> $DIR/pattern-errors.rs:122:11 + | +LL | let [&&mut x] = &[&mut 0]; + | ^^^^^^ --------- this expression has type `&[&mut {integer}; 1]` + | | + | expected integer, found `&mut _` + | + = note: expected type `{integer}` + found mutable reference `&mut _` +help: consider removing `&mut` from the pattern + | +LL - let [&&mut x] = &[&mut 0]; +LL + let [&x] = &[&mut 0]; + | + +error[E0308]: mismatched types + --> $DIR/pattern-errors.rs:129:11 + | +LL | let [&&mut x] = &mut [&mut 0]; + | ^^^^^^ ------------- this expression has type `&mut [&mut {integer}; 1]` + | | + | expected integer, found `&mut _` + | + = note: expected type `{integer}` + found mutable reference `&mut _` +help: consider removing `&mut` from the pattern + | +LL - let [&&mut x] = &mut [&mut 0]; +LL + let [&x] = &mut [&mut 0]; + | + +error[E0308]: mismatched types + --> $DIR/pattern-errors.rs:136:11 + | +LL | let [&&mut ref x] = &[&mut 0]; + | ^^^^^^^^^^ --------- this expression has type `&[&mut {integer}; 1]` + | | + | expected integer, found `&mut _` + | + = note: expected type `{integer}` + found mutable reference `&mut _` +help: consider removing `&mut` from the pattern + | +LL - let [&&mut ref x] = &[&mut 0]; +LL + let [&ref x] = &[&mut 0]; + | + +error[E0308]: mismatched types + --> $DIR/pattern-errors.rs:143:11 + | +LL | let [&&mut ref x] = &mut [&mut 0]; + | ^^^^^^^^^^ ------------- this expression has type `&mut [&mut {integer}; 1]` + | | + | expected integer, found `&mut _` + | + = note: expected type `{integer}` + found mutable reference `&mut _` +help: consider removing `&mut` from the pattern + | +LL - let [&&mut ref x] = &mut [&mut 0]; +LL + let [&ref x] = &mut [&mut 0]; + | + +error[E0308]: mismatched types + --> $DIR/pattern-errors.rs:150:11 + | +LL | let [&&mut mut x] = &[&mut 0]; + | ^^^^^^^^^^ --------- this expression has type `&[&mut {integer}; 1]` + | | + | expected integer, found `&mut _` + | + = note: expected type `{integer}` + found mutable reference `&mut _` +help: consider removing `&mut` from the pattern + | +LL - let [&&mut mut x] = &[&mut 0]; +LL + let [&mut x] = &[&mut 0]; + | + +error[E0308]: mismatched types + --> $DIR/pattern-errors.rs:157:11 + | +LL | let [&&mut mut x] = &mut [&mut 0]; + | ^^^^^^^^^^ ------------- this expression has type `&mut [&mut {integer}; 1]` + | | + | expected integer, found `&mut _` + | + = note: expected type `{integer}` + found mutable reference `&mut _` +help: consider removing `&mut` from the pattern + | +LL - let [&&mut mut x] = &mut [&mut 0]; +LL + let [&mut x] = &mut [&mut 0]; + | + +error[E0308]: mismatched types + --> $DIR/pattern-errors.rs:172:15 + | +LL | let [&mut &x] = &[&mut 0]; + | ^^ --------- this expression has type `&[&mut {integer}; 1]` + | | + | expected integer, found `&_` + | + = note: expected type `{integer}` + found reference `&_` +help: consider removing `&` from the pattern + | +LL - let [&mut &x] = &[&mut 0]; +LL + let [&mut x] = &[&mut 0]; + | + +error[E0308]: mismatched types + --> $DIR/pattern-errors.rs:178:15 + | +LL | let [&mut &ref x] = &[&mut 0]; + | ^^^^^^ --------- this expression has type `&[&mut {integer}; 1]` + | | + | expected integer, found `&_` + | + = note: expected type `{integer}` + found reference `&_` +help: consider removing `&` from the pattern + | +LL - let [&mut &ref x] = &[&mut 0]; +LL + let [&mut ref x] = &[&mut 0]; + | + +error[E0308]: mismatched types + --> $DIR/pattern-errors.rs:184:15 + | +LL | let [&mut &(mut x)] = &[&mut 0]; + | ^^^^^^^^ --------- this expression has type `&[&mut {integer}; 1]` + | | + | expected integer, found `&_` + | + = note: expected type `{integer}` + found reference `&_` +help: consider removing `&` from the pattern + | +LL - let [&mut &(mut x)] = &[&mut 0]; +LL + let [&mut mut x)] = &[&mut 0]; + | + +error: aborting due to 21 previous errors + +For more information about this error, try `rustc --explain E0308`. diff --git a/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/pattern-errors.structural2024.stderr b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/pattern-errors.structural2024.stderr index fdf48a5a71b5f..950d9f8b35110 100644 --- a/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/pattern-errors.structural2024.stderr +++ b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/pattern-errors.structural2024.stderr @@ -1,5 +1,5 @@ error[E0308]: mismatched types - --> $DIR/pattern-errors.rs:31:17 + --> $DIR/pattern-errors.rs:33:17 | LL | if let Some(&mut Some(&_)) = &Some(&Some(0)) { | ^^^^^ @@ -12,20 +12,20 @@ LL + if let Some(&Some(&_)) = &Some(&Some(0)) { | error[E0308]: mismatched types - --> $DIR/pattern-errors.rs:36:23 + --> $DIR/pattern-errors.rs:38:23 | -LL | if let Some(&Some(&mut _)) = &Some(&mut Some(0)) { +LL | if let Some(&Some(&mut x)) = &Some(&mut Some(0)) { | ^^^^^ | = note: cannot match inherited `&` with `&mut` pattern help: replace this `&mut` pattern with `&` | -LL - if let Some(&Some(&mut _)) = &Some(&mut Some(0)) { -LL + if let Some(&Some(&_)) = &Some(&mut Some(0)) { +LL - if let Some(&Some(&mut x)) = &Some(&mut Some(0)) { +LL + if let Some(&Some(&x)) = &Some(&mut Some(0)) { | error[E0308]: mismatched types - --> $DIR/pattern-errors.rs:41:23 + --> $DIR/pattern-errors.rs:45:23 | LL | if let Some(&Some(&mut _)) = &mut Some(&Some(0)) { | ^^^^^ @@ -38,20 +38,20 @@ LL + if let Some(&Some(&_)) = &mut Some(&Some(0)) { | error[E0308]: mismatched types - --> $DIR/pattern-errors.rs:46:28 + --> $DIR/pattern-errors.rs:50:28 | -LL | if let Some(&Some(Some(&mut _))) = &Some(Some(&mut Some(0))) { +LL | if let Some(&Some(Some(&mut x))) = &Some(Some(&mut Some(0))) { | ^^^^^ | = note: cannot match inherited `&` with `&mut` pattern help: replace this `&mut` pattern with `&` | -LL - if let Some(&Some(Some(&mut _))) = &Some(Some(&mut Some(0))) { -LL + if let Some(&Some(Some(&_))) = &Some(Some(&mut Some(0))) { +LL - if let Some(&Some(Some(&mut x))) = &Some(Some(&mut Some(0))) { +LL + if let Some(&Some(Some(&x))) = &Some(Some(&mut Some(0))) { | error[E0308]: mismatched types - --> $DIR/pattern-errors.rs:51:17 + --> $DIR/pattern-errors.rs:57:17 | LL | if let Some(&mut Some(x)) = &Some(Some(0)) { | ^^^^^ @@ -64,7 +64,7 @@ LL + if let Some(&Some(x)) = &Some(Some(0)) { | error[E0308]: mismatched types - --> $DIR/pattern-errors.rs:59:11 + --> $DIR/pattern-errors.rs:66:11 | LL | let &[&mut x] = &&mut [0]; | ^^^^^ @@ -77,7 +77,7 @@ LL + let &[&x] = &&mut [0]; | error[E0308]: mismatched types - --> $DIR/pattern-errors.rs:65:11 + --> $DIR/pattern-errors.rs:73:11 | LL | let &[&mut x] = &mut &mut [0]; | ^^^^^ @@ -90,7 +90,7 @@ LL + let &[&x] = &mut &mut [0]; | error[E0308]: mismatched types - --> $DIR/pattern-errors.rs:71:11 + --> $DIR/pattern-errors.rs:80:11 | LL | let &[&mut ref x] = &&mut [0]; | ^^^^^ @@ -103,7 +103,7 @@ LL + let &[&ref x] = &&mut [0]; | error[E0308]: mismatched types - --> $DIR/pattern-errors.rs:77:11 + --> $DIR/pattern-errors.rs:87:11 | LL | let &[&mut ref x] = &mut &mut [0]; | ^^^^^ @@ -116,7 +116,7 @@ LL + let &[&ref x] = &mut &mut [0]; | error[E0308]: mismatched types - --> $DIR/pattern-errors.rs:83:11 + --> $DIR/pattern-errors.rs:94:11 | LL | let &[&mut mut x] = &&mut [0]; | ^^^^^ @@ -129,7 +129,7 @@ LL + let &[&mut x] = &&mut [0]; | error[E0308]: mismatched types - --> $DIR/pattern-errors.rs:89:11 + --> $DIR/pattern-errors.rs:101:11 | LL | let &[&mut mut x] = &mut &mut [0]; | ^^^^^ @@ -142,7 +142,7 @@ LL + let &[&mut x] = &mut &mut [0]; | error[E0658]: binding cannot be both mutable and by-reference - --> $DIR/pattern-errors.rs:97:12 + --> $DIR/pattern-errors.rs:110:12 | LL | let [&(mut x)] = &[&0]; | ^^^^ @@ -152,7 +152,7 @@ LL | let [&(mut x)] = &[&0]; = note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date error[E0658]: binding cannot be both mutable and by-reference - --> $DIR/pattern-errors.rs:102:12 + --> $DIR/pattern-errors.rs:115:12 | LL | let [&(mut x)] = &mut [&0]; | ^^^^ @@ -162,7 +162,7 @@ LL | let [&(mut x)] = &mut [&0]; = note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date error[E0308]: mismatched types - --> $DIR/pattern-errors.rs:109:11 + --> $DIR/pattern-errors.rs:122:11 | LL | let [&&mut x] = &[&mut 0]; | ^^^^^ @@ -175,7 +175,7 @@ LL + let [&&x] = &[&mut 0]; | error[E0308]: mismatched types - --> $DIR/pattern-errors.rs:115:11 + --> $DIR/pattern-errors.rs:129:11 | LL | let [&&mut x] = &mut [&mut 0]; | ^^^^^ @@ -188,7 +188,7 @@ LL + let [&&x] = &mut [&mut 0]; | error[E0308]: mismatched types - --> $DIR/pattern-errors.rs:121:11 + --> $DIR/pattern-errors.rs:136:11 | LL | let [&&mut ref x] = &[&mut 0]; | ^^^^^ @@ -201,7 +201,7 @@ LL + let [&&ref x] = &[&mut 0]; | error[E0308]: mismatched types - --> $DIR/pattern-errors.rs:127:11 + --> $DIR/pattern-errors.rs:143:11 | LL | let [&&mut ref x] = &mut [&mut 0]; | ^^^^^ @@ -214,7 +214,7 @@ LL + let [&&ref x] = &mut [&mut 0]; | error[E0308]: mismatched types - --> $DIR/pattern-errors.rs:133:11 + --> $DIR/pattern-errors.rs:150:11 | LL | let [&&mut mut x] = &[&mut 0]; | ^^^^^ @@ -227,7 +227,7 @@ LL + let [&&mut x] = &[&mut 0]; | error[E0308]: mismatched types - --> $DIR/pattern-errors.rs:139:11 + --> $DIR/pattern-errors.rs:157:11 | LL | let [&&mut mut x] = &mut [&mut 0]; | ^^^^^ diff --git a/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref-binding-on-inh-ref-errors.classic2021.stderr b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref-binding-on-inh-ref-errors.classic2021.stderr new file mode 100644 index 0000000000000..1dda2dca4a48a --- /dev/null +++ b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref-binding-on-inh-ref-errors.classic2021.stderr @@ -0,0 +1,9 @@ +error[E0596]: cannot borrow data in a `&` reference as mutable + --> $DIR/ref-binding-on-inh-ref-errors.rs:71:10 + | +LL | let [ref mut x] = &[0]; + | ^^^^^^^^^ cannot borrow as mutable + +error: aborting due to 1 previous error + +For more information about this error, try `rustc --explain E0596`. diff --git a/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref-binding-on-inh-ref-errors.classic2024.stderr b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref-binding-on-inh-ref-errors.classic2024.stderr index 56125be2d6fc8..44cb005a748da 100644 --- a/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref-binding-on-inh-ref-errors.classic2024.stderr +++ b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref-binding-on-inh-ref-errors.classic2024.stderr @@ -1,5 +1,5 @@ error[E0308]: mismatched types - --> $DIR/ref-binding-on-inh-ref-errors.rs:54:10 + --> $DIR/ref-binding-on-inh-ref-errors.rs:58:10 | LL | let [&mut ref x] = &[&mut 0]; | ^^^^^ @@ -12,14 +12,14 @@ LL + let [&ref x] = &[&mut 0]; | error: binding modifiers may only be written when the default binding mode is `move` - --> $DIR/ref-binding-on-inh-ref-errors.rs:67:10 + --> $DIR/ref-binding-on-inh-ref-errors.rs:71:10 | LL | let [ref mut x] = &[0]; | ^^^^^^^ binding modifier not allowed under `ref` default binding mode | = note: for more information, see note: matching on a reference type with a non-reference pattern changes the default binding mode - --> $DIR/ref-binding-on-inh-ref-errors.rs:67:9 + --> $DIR/ref-binding-on-inh-ref-errors.rs:71:9 | LL | let [ref mut x] = &[0]; | ^^^^^^^^^^^ this matches on type `&_` @@ -29,20 +29,20 @@ LL | let &[ref mut x] = &[0]; | + error[E0596]: cannot borrow data in a `&` reference as mutable - --> $DIR/ref-binding-on-inh-ref-errors.rs:67:10 + --> $DIR/ref-binding-on-inh-ref-errors.rs:71:10 | LL | let [ref mut x] = &[0]; | ^^^^^^^^^ cannot borrow as mutable error: binding modifiers may only be written when the default binding mode is `move` - --> $DIR/ref-binding-on-inh-ref-errors.rs:75:10 + --> $DIR/ref-binding-on-inh-ref-errors.rs:79:10 | LL | let [ref x] = &[0]; | ^^^ binding modifier not allowed under `ref` default binding mode | = note: for more information, see note: matching on a reference type with a non-reference pattern changes the default binding mode - --> $DIR/ref-binding-on-inh-ref-errors.rs:75:9 + --> $DIR/ref-binding-on-inh-ref-errors.rs:79:9 | LL | let [ref x] = &[0]; | ^^^^^^^ this matches on type `&_` @@ -52,14 +52,14 @@ LL | let &[ref x] = &[0]; | + error: binding modifiers may only be written when the default binding mode is `move` - --> $DIR/ref-binding-on-inh-ref-errors.rs:79:10 + --> $DIR/ref-binding-on-inh-ref-errors.rs:83:10 | LL | let [ref x] = &mut [0]; | ^^^ binding modifier not allowed under `ref mut` default binding mode | = note: for more information, see note: matching on a reference type with a non-reference pattern changes the default binding mode - --> $DIR/ref-binding-on-inh-ref-errors.rs:79:9 + --> $DIR/ref-binding-on-inh-ref-errors.rs:83:9 | LL | let [ref x] = &mut [0]; | ^^^^^^^ this matches on type `&mut _` @@ -69,14 +69,14 @@ LL | let &mut [ref x] = &mut [0]; | ++++ error: binding modifiers may only be written when the default binding mode is `move` - --> $DIR/ref-binding-on-inh-ref-errors.rs:83:10 + --> $DIR/ref-binding-on-inh-ref-errors.rs:87:10 | LL | let [ref mut x] = &mut [0]; | ^^^^^^^ binding modifier not allowed under `ref mut` default binding mode | = note: for more information, see note: matching on a reference type with a non-reference pattern changes the default binding mode - --> $DIR/ref-binding-on-inh-ref-errors.rs:83:9 + --> $DIR/ref-binding-on-inh-ref-errors.rs:87:9 | LL | let [ref mut x] = &mut [0]; | ^^^^^^^^^^^ this matches on type `&mut _` diff --git a/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref-binding-on-inh-ref-errors.rs b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref-binding-on-inh-ref-errors.rs index 4e048570c33c2..ea6f028fe4b8b 100644 --- a/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref-binding-on-inh-ref-errors.rs +++ b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref-binding-on-inh-ref-errors.rs @@ -1,35 +1,37 @@ -//@ revisions: stable2021 classic2024 structural2024 +//@ revisions: stable2021 classic2021 structural2021 classic2024 structural2024 //@[stable2021] edition: 2021 +//@[classic2021] edition: 2021 +//@[structural2021] edition: 2021 //@[classic2024] edition: 2024 //@[structural2024] edition: 2024 //! 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. #![allow(incomplete_features)] -#![cfg_attr(classic2024, feature(ref_pat_eat_one_layer_2024))] -#![cfg_attr(structural2024, feature(ref_pat_eat_one_layer_2024_structural))] +#![cfg_attr(any(classic2021, classic2024), feature(ref_pat_eat_one_layer_2024))] +#![cfg_attr(any(structural2021, structural2024), feature(ref_pat_eat_one_layer_2024_structural))] /// These only fail on the eat-inner variant of the new edition 2024 pattern typing rules. /// The eat-outer variant eats the inherited reference, so binding with `ref` isn't a problem. fn errors_from_eating_the_real_reference() { let [&ref x] = &[&0]; //[structural2024]~^ ERROR: binding modifiers may only be written when the default binding mode is `move` - #[cfg(stable2021)] let _: &u32 = x; + #[cfg(any(stable2021, classic2021, structural2021))] let _: &u32 = x; #[cfg(classic2024)] let _: &&u32 = x; let [&ref x] = &mut [&0]; //[structural2024]~^ ERROR: binding modifiers may only be written when the default binding mode is `move` - #[cfg(stable2021)] let _: &u32 = x; + #[cfg(any(stable2021, classic2021, structural2021))] let _: &u32 = x; #[cfg(classic2024)] let _: &&u32 = x; let [&mut ref x] = &mut [&mut 0]; //[structural2024]~^ ERROR: binding modifiers may only be written when the default binding mode is `move` - #[cfg(stable2021)] let _: &u32 = x; + #[cfg(any(stable2021, classic2021, structural2021))] let _: &u32 = x; #[cfg(classic2024)] let _: &&mut u32 = x; let [&mut ref mut x] = &mut [&mut 0]; //[structural2024]~^ ERROR: binding modifiers may only be written when the default binding mode is `move` - #[cfg(stable2021)] let _: &mut u32 = x; + #[cfg(any(stable2021, classic2021, structural2021))] let _: &mut u32 = x; #[cfg(classic2024)] let _: &mut &mut u32 = x; } @@ -40,12 +42,14 @@ fn errors_from_eating_the_real_reference_caught_in_hir_typeck_on_stable() { //[stable2021]~^ ERROR: mismatched types //[stable2021]~| types differ in mutability //[structural2024]~^^^ ERROR: binding modifiers may only be written when the default binding mode is `move` + #[cfg(any(classic2021, structural2021))] let _: &u32 = x; #[cfg(classic2024)] let _: &&mut u32 = x; let [&ref x] = &mut [&mut 0]; //[stable2021]~^ ERROR: mismatched types //[stable2021]~| types differ in mutability //[structural2024]~^^^ ERROR: binding modifiers may only be written when the default binding mode is `move` + #[cfg(any(classic2021, structural2021))] let _: &u32 = x; #[cfg(classic2024)] let _: &&mut u32 = x; } @@ -55,7 +59,7 @@ fn errors_dependent_on_eating_order_caught_in_hir_typeck_when_eating_outer() { //[classic2024]~^ ERROR: mismatched types //[classic2024]~| cannot match inherited `&` with `&mut` pattern //[structural2024]~^^^ ERROR: binding modifiers may only be written when the default binding mode is `move` - #[cfg(stable2021)] let _: &u32 = x; + #[cfg(any(stable2021, classic2021, structural2021))] let _: &u32 = x; } /// These should be errors in all editions. In edition 2024, they should be caught by the pattern @@ -74,13 +78,13 @@ fn borrowck_errors_in_old_editions() { pub fn main() { let [ref x] = &[0]; //[classic2024,structural2024]~^ ERROR: binding modifiers may only be written when the default binding mode is `move` - #[cfg(stable2021)] let _: &u32 = x; + #[cfg(any(stable2021, classic2021, structural2021))] let _: &u32 = x; let [ref x] = &mut [0]; //[classic2024,structural2024]~^ ERROR: binding modifiers may only be written when the default binding mode is `move` - #[cfg(stable2021)] let _: &u32 = x; + #[cfg(any(stable2021, classic2021, structural2021))] let _: &u32 = x; let [ref mut x] = &mut [0]; //[classic2024,structural2024]~^ ERROR: binding modifiers may only be written when the default binding mode is `move` - #[cfg(stable2021)] let _: &mut u32 = x; + #[cfg(any(stable2021, classic2021, structural2021))] let _: &mut u32 = x; } diff --git a/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref-binding-on-inh-ref-errors.stable2021.stderr b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref-binding-on-inh-ref-errors.stable2021.stderr index 26095d8460572..2ec6650dd7d0b 100644 --- a/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref-binding-on-inh-ref-errors.stable2021.stderr +++ b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref-binding-on-inh-ref-errors.stable2021.stderr @@ -1,5 +1,5 @@ error[E0308]: mismatched types - --> $DIR/ref-binding-on-inh-ref-errors.rs:39:10 + --> $DIR/ref-binding-on-inh-ref-errors.rs:41:10 | LL | let [&ref x] = &[&mut 0]; | ^^^^^^ --------- this expression has type `&[&mut {integer}; 1]` @@ -15,7 +15,7 @@ LL + let [ref x] = &[&mut 0]; | error[E0308]: mismatched types - --> $DIR/ref-binding-on-inh-ref-errors.rs:45:10 + --> $DIR/ref-binding-on-inh-ref-errors.rs:48:10 | LL | let [&ref x] = &mut [&mut 0]; | ^^^^^^ ------------- this expression has type `&mut [&mut {integer}; 1]` @@ -31,7 +31,7 @@ LL + let [ref x] = &mut [&mut 0]; | error[E0596]: cannot borrow data in a `&` reference as mutable - --> $DIR/ref-binding-on-inh-ref-errors.rs:67:10 + --> $DIR/ref-binding-on-inh-ref-errors.rs:71:10 | LL | let [ref mut x] = &[0]; | ^^^^^^^^^ cannot borrow as mutable diff --git a/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref-binding-on-inh-ref-errors.structural2021.stderr b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref-binding-on-inh-ref-errors.structural2021.stderr new file mode 100644 index 0000000000000..1dda2dca4a48a --- /dev/null +++ b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref-binding-on-inh-ref-errors.structural2021.stderr @@ -0,0 +1,9 @@ +error[E0596]: cannot borrow data in a `&` reference as mutable + --> $DIR/ref-binding-on-inh-ref-errors.rs:71:10 + | +LL | let [ref mut x] = &[0]; + | ^^^^^^^^^ cannot borrow as mutable + +error: aborting due to 1 previous error + +For more information about this error, try `rustc --explain E0596`. diff --git a/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref-binding-on-inh-ref-errors.structural2024.stderr b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref-binding-on-inh-ref-errors.structural2024.stderr index 31930e8c03371..6f62ad06cc493 100644 --- a/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref-binding-on-inh-ref-errors.structural2024.stderr +++ b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref-binding-on-inh-ref-errors.structural2024.stderr @@ -1,12 +1,12 @@ error: binding modifiers may only be written when the default binding mode is `move` - --> $DIR/ref-binding-on-inh-ref-errors.rs:15:11 + --> $DIR/ref-binding-on-inh-ref-errors.rs:17:11 | LL | let [&ref x] = &[&0]; | ^^^ binding modifier not allowed under `ref` default binding mode | = note: for more information, see note: matching on a reference type with a non-reference pattern changes the default binding mode - --> $DIR/ref-binding-on-inh-ref-errors.rs:15:9 + --> $DIR/ref-binding-on-inh-ref-errors.rs:17:9 | LL | let [&ref x] = &[&0]; | ^^^^^^^^ this matches on type `&_` @@ -16,14 +16,14 @@ LL | let &[&ref x] = &[&0]; | + error: binding modifiers may only be written when the default binding mode is `move` - --> $DIR/ref-binding-on-inh-ref-errors.rs:20:11 + --> $DIR/ref-binding-on-inh-ref-errors.rs:22:11 | LL | let [&ref x] = &mut [&0]; | ^^^ binding modifier not allowed under `ref` default binding mode | = note: for more information, see note: matching on a reference type with a non-reference pattern changes the default binding mode - --> $DIR/ref-binding-on-inh-ref-errors.rs:20:9 + --> $DIR/ref-binding-on-inh-ref-errors.rs:22:9 | LL | let [&ref x] = &mut [&0]; | ^^^^^^^^ this matches on type `&mut _` @@ -33,14 +33,14 @@ LL | let &mut [&ref x] = &mut [&0]; | ++++ error: binding modifiers may only be written when the default binding mode is `move` - --> $DIR/ref-binding-on-inh-ref-errors.rs:25:15 + --> $DIR/ref-binding-on-inh-ref-errors.rs:27:15 | LL | let [&mut ref x] = &mut [&mut 0]; | ^^^ binding modifier not allowed under `ref mut` default binding mode | = note: for more information, see note: matching on a reference type with a non-reference pattern changes the default binding mode - --> $DIR/ref-binding-on-inh-ref-errors.rs:25:9 + --> $DIR/ref-binding-on-inh-ref-errors.rs:27:9 | LL | let [&mut ref x] = &mut [&mut 0]; | ^^^^^^^^^^^^ this matches on type `&mut _` @@ -50,14 +50,14 @@ LL | let &mut [&mut ref x] = &mut [&mut 0]; | ++++ error: binding modifiers may only be written when the default binding mode is `move` - --> $DIR/ref-binding-on-inh-ref-errors.rs:30:15 + --> $DIR/ref-binding-on-inh-ref-errors.rs:32:15 | LL | let [&mut ref mut x] = &mut [&mut 0]; | ^^^^^^^ binding modifier not allowed under `ref mut` default binding mode | = note: for more information, see note: matching on a reference type with a non-reference pattern changes the default binding mode - --> $DIR/ref-binding-on-inh-ref-errors.rs:30:9 + --> $DIR/ref-binding-on-inh-ref-errors.rs:32:9 | LL | let [&mut ref mut x] = &mut [&mut 0]; | ^^^^^^^^^^^^^^^^ this matches on type `&mut _` @@ -67,14 +67,14 @@ LL | let &mut [&mut ref mut x] = &mut [&mut 0]; | ++++ error: binding modifiers may only be written when the default binding mode is `move` - --> $DIR/ref-binding-on-inh-ref-errors.rs:39:11 + --> $DIR/ref-binding-on-inh-ref-errors.rs:41:11 | LL | let [&ref x] = &[&mut 0]; | ^^^ binding modifier not allowed under `ref` default binding mode | = note: for more information, see note: matching on a reference type with a non-reference pattern changes the default binding mode - --> $DIR/ref-binding-on-inh-ref-errors.rs:39:9 + --> $DIR/ref-binding-on-inh-ref-errors.rs:41:9 | LL | let [&ref x] = &[&mut 0]; | ^^^^^^^^ this matches on type `&_` @@ -84,14 +84,14 @@ LL | let &[&ref x] = &[&mut 0]; | + error: binding modifiers may only be written when the default binding mode is `move` - --> $DIR/ref-binding-on-inh-ref-errors.rs:45:11 + --> $DIR/ref-binding-on-inh-ref-errors.rs:48:11 | LL | let [&ref x] = &mut [&mut 0]; | ^^^ binding modifier not allowed under `ref` default binding mode | = note: for more information, see note: matching on a reference type with a non-reference pattern changes the default binding mode - --> $DIR/ref-binding-on-inh-ref-errors.rs:45:9 + --> $DIR/ref-binding-on-inh-ref-errors.rs:48:9 | LL | let [&ref x] = &mut [&mut 0]; | ^^^^^^^^ this matches on type `&mut _` @@ -101,14 +101,14 @@ LL | let &mut [&ref x] = &mut [&mut 0]; | ++++ error: binding modifiers may only be written when the default binding mode is `move` - --> $DIR/ref-binding-on-inh-ref-errors.rs:54:15 + --> $DIR/ref-binding-on-inh-ref-errors.rs:58:15 | LL | let [&mut ref x] = &[&mut 0]; | ^^^ binding modifier not allowed under `ref` default binding mode | = note: for more information, see note: matching on a reference type with a non-reference pattern changes the default binding mode - --> $DIR/ref-binding-on-inh-ref-errors.rs:54:9 + --> $DIR/ref-binding-on-inh-ref-errors.rs:58:9 | LL | let [&mut ref x] = &[&mut 0]; | ^^^^^^^^^^^^ this matches on type `&_` @@ -118,14 +118,14 @@ LL | let &[&mut ref x] = &[&mut 0]; | + error: binding modifiers may only be written when the default binding mode is `move` - --> $DIR/ref-binding-on-inh-ref-errors.rs:67:10 + --> $DIR/ref-binding-on-inh-ref-errors.rs:71:10 | LL | let [ref mut x] = &[0]; | ^^^^^^^ binding modifier not allowed under `ref` default binding mode | = note: for more information, see note: matching on a reference type with a non-reference pattern changes the default binding mode - --> $DIR/ref-binding-on-inh-ref-errors.rs:67:9 + --> $DIR/ref-binding-on-inh-ref-errors.rs:71:9 | LL | let [ref mut x] = &[0]; | ^^^^^^^^^^^ this matches on type `&_` @@ -135,20 +135,20 @@ LL | let &[ref mut x] = &[0]; | + error[E0596]: cannot borrow data in a `&` reference as mutable - --> $DIR/ref-binding-on-inh-ref-errors.rs:67:10 + --> $DIR/ref-binding-on-inh-ref-errors.rs:71:10 | LL | let [ref mut x] = &[0]; | ^^^^^^^^^ cannot borrow as mutable error: binding modifiers may only be written when the default binding mode is `move` - --> $DIR/ref-binding-on-inh-ref-errors.rs:75:10 + --> $DIR/ref-binding-on-inh-ref-errors.rs:79:10 | LL | let [ref x] = &[0]; | ^^^ binding modifier not allowed under `ref` default binding mode | = note: for more information, see note: matching on a reference type with a non-reference pattern changes the default binding mode - --> $DIR/ref-binding-on-inh-ref-errors.rs:75:9 + --> $DIR/ref-binding-on-inh-ref-errors.rs:79:9 | LL | let [ref x] = &[0]; | ^^^^^^^ this matches on type `&_` @@ -158,14 +158,14 @@ LL | let &[ref x] = &[0]; | + error: binding modifiers may only be written when the default binding mode is `move` - --> $DIR/ref-binding-on-inh-ref-errors.rs:79:10 + --> $DIR/ref-binding-on-inh-ref-errors.rs:83:10 | LL | let [ref x] = &mut [0]; | ^^^ binding modifier not allowed under `ref mut` default binding mode | = note: for more information, see note: matching on a reference type with a non-reference pattern changes the default binding mode - --> $DIR/ref-binding-on-inh-ref-errors.rs:79:9 + --> $DIR/ref-binding-on-inh-ref-errors.rs:83:9 | LL | let [ref x] = &mut [0]; | ^^^^^^^ this matches on type `&mut _` @@ -175,14 +175,14 @@ LL | let &mut [ref x] = &mut [0]; | ++++ error: binding modifiers may only be written when the default binding mode is `move` - --> $DIR/ref-binding-on-inh-ref-errors.rs:83:10 + --> $DIR/ref-binding-on-inh-ref-errors.rs:87:10 | LL | let [ref mut x] = &mut [0]; | ^^^^^^^ binding modifier not allowed under `ref mut` default binding mode | = note: for more information, see note: matching on a reference type with a non-reference pattern changes the default binding mode - --> $DIR/ref-binding-on-inh-ref-errors.rs:83:9 + --> $DIR/ref-binding-on-inh-ref-errors.rs:87:9 | LL | let [ref mut x] = &mut [0]; | ^^^^^^^^^^^ this matches on type `&mut _` diff --git a/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref-mut-inside-shared-ref-pat.classic2021.stderr b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref-mut-inside-shared-ref-pat.classic2021.stderr new file mode 100644 index 0000000000000..379ba4a11c554 --- /dev/null +++ b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref-mut-inside-shared-ref-pat.classic2021.stderr @@ -0,0 +1,63 @@ +error[E0308]: mismatched types + --> $DIR/ref-mut-inside-shared-ref-pat.rs:16:17 + | +LL | if let Some(&Some(ref mut x)) = &mut Some(Some(0)) { // TODO: `classic2021` and `structural2021` shouldn't have mismatched types + | ^^^^^^^^^^^^^^^^ ------------------ this expression has type `&mut Option>` + | | + | expected `Option<{integer}>`, found `&_` + | + = note: expected enum `Option<{integer}>` + found reference `&_` + +error[E0596]: cannot borrow as mutable inside an `&` pattern + --> $DIR/ref-mut-inside-shared-ref-pat.rs:16:31 + | +LL | if let Some(&Some(ref mut x)) = &mut Some(Some(0)) { // TODO: `classic2021` and `structural2021` shouldn't have mismatched types + | - ^ + | | + | help: replace this `&` with `&mut`: `&mut` + +error[E0596]: cannot borrow as mutable inside an `&` pattern + --> $DIR/ref-mut-inside-shared-ref-pat.rs:22:31 + | +LL | if let &Some(Some(ref mut x)) = &mut Some(Some(0)) { + | - ^ + | | + | help: replace this `&` with `&mut`: `&mut` + +error[E0596]: cannot borrow as mutable inside an `&` pattern + --> $DIR/ref-mut-inside-shared-ref-pat.rs:31:15 + | +LL | let &pat!(x) = &mut 0; + | - ^ + | | + | help: replace this `&` with `&mut`: `&mut` + +error[E0596]: cannot borrow as mutable inside an `&` pattern + --> $DIR/ref-mut-inside-shared-ref-pat.rs:36:19 + | +LL | let &(ref mut a, ref mut b) = &mut (true, false); + | - ^ + | | + | help: replace this `&` with `&mut`: `&mut` + +error[E0596]: cannot borrow as mutable inside an `&` pattern + --> $DIR/ref-mut-inside-shared-ref-pat.rs:36:30 + | +LL | let &(ref mut a, ref mut b) = &mut (true, false); + | - ^ + | | + | help: replace this `&` with `&mut`: `&mut` + +error[E0596]: cannot borrow as mutable inside an `&` pattern + --> $DIR/ref-mut-inside-shared-ref-pat.rs:43:11 + | +LL | let &[x] = &mut &mut [0]; + | - ^ + | | + | help: replace this `&` with `&mut`: `&mut` + +error: aborting due to 7 previous errors + +Some errors have detailed explanations: E0308, E0596. +For more information about an error, try `rustc --explain E0308`. diff --git a/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref-mut-inside-shared-ref-pat.classic2024.fixed b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref-mut-inside-shared-ref-pat.classic2024.fixed index c01784d5076b7..105313a14896d 100644 --- a/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref-mut-inside-shared-ref-pat.classic2024.fixed +++ b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref-mut-inside-shared-ref-pat.classic2024.fixed @@ -1,5 +1,7 @@ -//@ revisions: stable2021 classic2024 structural2024 +//@ revisions: stable2021 classic2021 structural2021 classic2024 structural2024 //@[stable2021] edition: 2021 +//@[classic2021] edition: 2021 +//@[structural2021] edition: 2021 //@[classic2024] edition: 2024 //@[structural2024] edition: 2024 //@[classic2024] run-rustfix @@ -7,19 +9,19 @@ //! Tests for `&` patterns matched against `&mut` reference types where the inner pattern attempts //! to bind by mutable reference. #![allow(incomplete_features)] -#![cfg_attr(classic2024, feature(ref_pat_eat_one_layer_2024))] -#![cfg_attr(structural2024, feature(ref_pat_eat_one_layer_2024_structural))] +#![cfg_attr(any(classic2021, classic2024), feature(ref_pat_eat_one_layer_2024))] +#![cfg_attr(any(structural2021, structural2024), feature(ref_pat_eat_one_layer_2024_structural))] pub fn main() { - if let Some(&mut Some(ref mut x)) = &mut Some(Some(0)) { - //[stable2021]~^ ERROR: mismatched types - //[classic2024,structural2024]~^^ ERROR: cannot borrow as mutable inside an `&` pattern + if let Some(&mut Some(ref mut x)) = &mut Some(Some(0)) { // TODO: `classic2021` and `structural2021` shouldn't have mismatched types + //[stable2021,classic2021,structural2021]~^ ERROR: mismatched types + //[classic2021,structural2021,classic2024,structural2024]~^^ ERROR: cannot borrow as mutable inside an `&` pattern let _: &mut u8 = x; } if let &mut Some(Some(ref mut x)) = &mut Some(Some(0)) { //[stable2021]~^ ERROR: mismatched types - //[classic2024,structural2024]~^^ ERROR: cannot borrow as mutable inside an `&` pattern + //[classic2021,structural2021,classic2024,structural2024]~^^ ERROR: cannot borrow as mutable inside an `&` pattern let _: &mut u8 = x; } @@ -28,18 +30,18 @@ pub fn main() { } let &mut pat!(x) = &mut 0; //[stable2021]~^ ERROR: mismatched types - //[classic2024,structural2024]~^^ ERROR: cannot borrow as mutable inside an `&` pattern + //[classic2021,structural2021,classic2024,structural2024]~^^ ERROR: cannot borrow as mutable inside an `&` pattern let _: &mut u8 = x; let &mut (ref mut a, ref mut b) = &mut (true, false); //[stable2021]~^ ERROR: mismatched types - //[classic2024,structural2024]~^^ ERROR: cannot borrow as mutable inside an `&` pattern - //[classic2024,structural2024]~| ERROR: cannot borrow as mutable inside an `&` pattern + //[classic2021,structural2021,classic2024,structural2024]~^^ ERROR: cannot borrow as mutable inside an `&` pattern + //[classic2021,structural2021,classic2024,structural2024]~| ERROR: cannot borrow as mutable inside an `&` pattern let _: &mut bool = a; let _: &mut bool = b; let &mut [x] = &mut &mut [0]; //[stable2021]~^ ERROR: mismatched types - //[classic2024]~^^ ERROR: cannot borrow as mutable inside an `&` pattern + //[classic2021,classic2024]~^^ ERROR: cannot borrow as mutable inside an `&` pattern let _: &u32 = x; } diff --git a/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref-mut-inside-shared-ref-pat.classic2024.stderr b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref-mut-inside-shared-ref-pat.classic2024.stderr index 5e98b77be40cd..93ddf4d60fd5c 100644 --- a/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref-mut-inside-shared-ref-pat.classic2024.stderr +++ b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref-mut-inside-shared-ref-pat.classic2024.stderr @@ -1,13 +1,13 @@ error[E0596]: cannot borrow as mutable inside an `&` pattern - --> $DIR/ref-mut-inside-shared-ref-pat.rs:14:31 + --> $DIR/ref-mut-inside-shared-ref-pat.rs:16:31 | -LL | if let Some(&Some(ref mut x)) = &mut Some(Some(0)) { +LL | if let Some(&Some(ref mut x)) = &mut Some(Some(0)) { // TODO: `classic2021` and `structural2021` shouldn't have mismatched types | - ^ | | | help: replace this `&` with `&mut`: `&mut` error[E0596]: cannot borrow as mutable inside an `&` pattern - --> $DIR/ref-mut-inside-shared-ref-pat.rs:20:31 + --> $DIR/ref-mut-inside-shared-ref-pat.rs:22:31 | LL | if let &Some(Some(ref mut x)) = &mut Some(Some(0)) { | - ^ @@ -15,7 +15,7 @@ LL | if let &Some(Some(ref mut x)) = &mut Some(Some(0)) { | help: replace this `&` with `&mut`: `&mut` error[E0596]: cannot borrow as mutable inside an `&` pattern - --> $DIR/ref-mut-inside-shared-ref-pat.rs:29:15 + --> $DIR/ref-mut-inside-shared-ref-pat.rs:31:15 | LL | let &pat!(x) = &mut 0; | - ^ @@ -23,7 +23,7 @@ LL | let &pat!(x) = &mut 0; | help: replace this `&` with `&mut`: `&mut` error[E0596]: cannot borrow as mutable inside an `&` pattern - --> $DIR/ref-mut-inside-shared-ref-pat.rs:34:19 + --> $DIR/ref-mut-inside-shared-ref-pat.rs:36:19 | LL | let &(ref mut a, ref mut b) = &mut (true, false); | - ^ @@ -31,7 +31,7 @@ LL | let &(ref mut a, ref mut b) = &mut (true, false); | help: replace this `&` with `&mut`: `&mut` error[E0596]: cannot borrow as mutable inside an `&` pattern - --> $DIR/ref-mut-inside-shared-ref-pat.rs:34:30 + --> $DIR/ref-mut-inside-shared-ref-pat.rs:36:30 | LL | let &(ref mut a, ref mut b) = &mut (true, false); | - ^ @@ -39,7 +39,7 @@ LL | let &(ref mut a, ref mut b) = &mut (true, false); | help: replace this `&` with `&mut`: `&mut` error[E0596]: cannot borrow as mutable inside an `&` pattern - --> $DIR/ref-mut-inside-shared-ref-pat.rs:41:11 + --> $DIR/ref-mut-inside-shared-ref-pat.rs:43:11 | LL | let &[x] = &mut &mut [0]; | - ^ diff --git a/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref-mut-inside-shared-ref-pat.rs b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref-mut-inside-shared-ref-pat.rs index fe40dabb55394..800aca06396cb 100644 --- a/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref-mut-inside-shared-ref-pat.rs +++ b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref-mut-inside-shared-ref-pat.rs @@ -1,5 +1,7 @@ -//@ revisions: stable2021 classic2024 structural2024 +//@ revisions: stable2021 classic2021 structural2021 classic2024 structural2024 //@[stable2021] edition: 2021 +//@[classic2021] edition: 2021 +//@[structural2021] edition: 2021 //@[classic2024] edition: 2024 //@[structural2024] edition: 2024 //@[classic2024] run-rustfix @@ -7,19 +9,19 @@ //! Tests for `&` patterns matched against `&mut` reference types where the inner pattern attempts //! to bind by mutable reference. #![allow(incomplete_features)] -#![cfg_attr(classic2024, feature(ref_pat_eat_one_layer_2024))] -#![cfg_attr(structural2024, feature(ref_pat_eat_one_layer_2024_structural))] +#![cfg_attr(any(classic2021, classic2024), feature(ref_pat_eat_one_layer_2024))] +#![cfg_attr(any(structural2021, structural2024), feature(ref_pat_eat_one_layer_2024_structural))] pub fn main() { - if let Some(&Some(ref mut x)) = &mut Some(Some(0)) { - //[stable2021]~^ ERROR: mismatched types - //[classic2024,structural2024]~^^ ERROR: cannot borrow as mutable inside an `&` pattern + if let Some(&Some(ref mut x)) = &mut Some(Some(0)) { // TODO: `classic2021` and `structural2021` shouldn't have mismatched types + //[stable2021,classic2021,structural2021]~^ ERROR: mismatched types + //[classic2021,structural2021,classic2024,structural2024]~^^ ERROR: cannot borrow as mutable inside an `&` pattern let _: &mut u8 = x; } if let &Some(Some(ref mut x)) = &mut Some(Some(0)) { //[stable2021]~^ ERROR: mismatched types - //[classic2024,structural2024]~^^ ERROR: cannot borrow as mutable inside an `&` pattern + //[classic2021,structural2021,classic2024,structural2024]~^^ ERROR: cannot borrow as mutable inside an `&` pattern let _: &mut u8 = x; } @@ -28,18 +30,18 @@ pub fn main() { } let &pat!(x) = &mut 0; //[stable2021]~^ ERROR: mismatched types - //[classic2024,structural2024]~^^ ERROR: cannot borrow as mutable inside an `&` pattern + //[classic2021,structural2021,classic2024,structural2024]~^^ ERROR: cannot borrow as mutable inside an `&` pattern let _: &mut u8 = x; let &(ref mut a, ref mut b) = &mut (true, false); //[stable2021]~^ ERROR: mismatched types - //[classic2024,structural2024]~^^ ERROR: cannot borrow as mutable inside an `&` pattern - //[classic2024,structural2024]~| ERROR: cannot borrow as mutable inside an `&` pattern + //[classic2021,structural2021,classic2024,structural2024]~^^ ERROR: cannot borrow as mutable inside an `&` pattern + //[classic2021,structural2021,classic2024,structural2024]~| ERROR: cannot borrow as mutable inside an `&` pattern let _: &mut bool = a; let _: &mut bool = b; let &[x] = &mut &mut [0]; //[stable2021]~^ ERROR: mismatched types - //[classic2024]~^^ ERROR: cannot borrow as mutable inside an `&` pattern + //[classic2021,classic2024]~^^ ERROR: cannot borrow as mutable inside an `&` pattern let _: &u32 = x; } diff --git a/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref-mut-inside-shared-ref-pat.stable2021.stderr b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref-mut-inside-shared-ref-pat.stable2021.stderr index 72c6c05e18443..9a3b896d4f842 100644 --- a/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref-mut-inside-shared-ref-pat.stable2021.stderr +++ b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref-mut-inside-shared-ref-pat.stable2021.stderr @@ -1,7 +1,7 @@ error[E0308]: mismatched types - --> $DIR/ref-mut-inside-shared-ref-pat.rs:14:17 + --> $DIR/ref-mut-inside-shared-ref-pat.rs:16:17 | -LL | if let Some(&Some(ref mut x)) = &mut Some(Some(0)) { +LL | if let Some(&Some(ref mut x)) = &mut Some(Some(0)) { // TODO: `classic2021` and `structural2021` shouldn't have mismatched types | ^^^^^^^^^^^^^^^^ ------------------ this expression has type `&mut Option>` | | | expected `Option<{integer}>`, found `&_` @@ -10,7 +10,7 @@ LL | if let Some(&Some(ref mut x)) = &mut Some(Some(0)) { found reference `&_` error[E0308]: mismatched types - --> $DIR/ref-mut-inside-shared-ref-pat.rs:20:12 + --> $DIR/ref-mut-inside-shared-ref-pat.rs:22:12 | LL | if let &Some(Some(ref mut x)) = &mut Some(Some(0)) { | ^^^^^^^^^^^^^^^^^^^^^^ ------------------ this expression has type `&mut Option>` @@ -21,7 +21,7 @@ LL | if let &Some(Some(ref mut x)) = &mut Some(Some(0)) { found reference `&_` error[E0308]: mismatched types - --> $DIR/ref-mut-inside-shared-ref-pat.rs:29:9 + --> $DIR/ref-mut-inside-shared-ref-pat.rs:31:9 | LL | let &pat!(x) = &mut 0; | ^^^^^^^^ ------ this expression has type `&mut {integer}` @@ -32,7 +32,7 @@ LL | let &pat!(x) = &mut 0; found reference `&_` error[E0308]: mismatched types - --> $DIR/ref-mut-inside-shared-ref-pat.rs:34:9 + --> $DIR/ref-mut-inside-shared-ref-pat.rs:36:9 | LL | let &(ref mut a, ref mut b) = &mut (true, false); | ^^^^^^^^^^^^^^^^^^^^^^^ ------------------ this expression has type `&mut (bool, bool)` @@ -43,7 +43,7 @@ LL | let &(ref mut a, ref mut b) = &mut (true, false); found reference `&_` error[E0308]: mismatched types - --> $DIR/ref-mut-inside-shared-ref-pat.rs:41:9 + --> $DIR/ref-mut-inside-shared-ref-pat.rs:43:9 | LL | let &[x] = &mut &mut [0]; | ^^^^ ------------- this expression has type `&mut &mut [{integer}; 1]` diff --git a/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref-mut-inside-shared-ref-pat.structural2021.stderr b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref-mut-inside-shared-ref-pat.structural2021.stderr new file mode 100644 index 0000000000000..1a138494b85a0 --- /dev/null +++ b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref-mut-inside-shared-ref-pat.structural2021.stderr @@ -0,0 +1,55 @@ +error[E0308]: mismatched types + --> $DIR/ref-mut-inside-shared-ref-pat.rs:16:17 + | +LL | if let Some(&Some(ref mut x)) = &mut Some(Some(0)) { // TODO: `classic2021` and `structural2021` shouldn't have mismatched types + | ^^^^^^^^^^^^^^^^ ------------------ this expression has type `&mut Option>` + | | + | expected `Option<{integer}>`, found `&_` + | + = note: expected enum `Option<{integer}>` + found reference `&_` + +error[E0596]: cannot borrow as mutable inside an `&` pattern + --> $DIR/ref-mut-inside-shared-ref-pat.rs:16:31 + | +LL | if let Some(&Some(ref mut x)) = &mut Some(Some(0)) { // TODO: `classic2021` and `structural2021` shouldn't have mismatched types + | - ^ + | | + | help: replace this `&` with `&mut`: `&mut` + +error[E0596]: cannot borrow as mutable inside an `&` pattern + --> $DIR/ref-mut-inside-shared-ref-pat.rs:22:31 + | +LL | if let &Some(Some(ref mut x)) = &mut Some(Some(0)) { + | - ^ + | | + | help: replace this `&` with `&mut`: `&mut` + +error[E0596]: cannot borrow as mutable inside an `&` pattern + --> $DIR/ref-mut-inside-shared-ref-pat.rs:31:15 + | +LL | let &pat!(x) = &mut 0; + | - ^ + | | + | help: replace this `&` with `&mut`: `&mut` + +error[E0596]: cannot borrow as mutable inside an `&` pattern + --> $DIR/ref-mut-inside-shared-ref-pat.rs:36:19 + | +LL | let &(ref mut a, ref mut b) = &mut (true, false); + | - ^ + | | + | help: replace this `&` with `&mut`: `&mut` + +error[E0596]: cannot borrow as mutable inside an `&` pattern + --> $DIR/ref-mut-inside-shared-ref-pat.rs:36:30 + | +LL | let &(ref mut a, ref mut b) = &mut (true, false); + | - ^ + | | + | help: replace this `&` with `&mut`: `&mut` + +error: aborting due to 6 previous errors + +Some errors have detailed explanations: E0308, E0596. +For more information about an error, try `rustc --explain E0308`. diff --git a/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref-mut-inside-shared-ref-pat.structural2024.fixed b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref-mut-inside-shared-ref-pat.structural2024.fixed index 4ee849b38c53b..78fbffdb27033 100644 --- a/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref-mut-inside-shared-ref-pat.structural2024.fixed +++ b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref-mut-inside-shared-ref-pat.structural2024.fixed @@ -1,5 +1,7 @@ -//@ revisions: stable2021 classic2024 structural2024 +//@ revisions: stable2021 classic2021 structural2021 classic2024 structural2024 //@[stable2021] edition: 2021 +//@[classic2021] edition: 2021 +//@[structural2021] edition: 2021 //@[classic2024] edition: 2024 //@[structural2024] edition: 2024 //@[classic2024] run-rustfix @@ -7,19 +9,19 @@ //! Tests for `&` patterns matched against `&mut` reference types where the inner pattern attempts //! to bind by mutable reference. #![allow(incomplete_features)] -#![cfg_attr(classic2024, feature(ref_pat_eat_one_layer_2024))] -#![cfg_attr(structural2024, feature(ref_pat_eat_one_layer_2024_structural))] +#![cfg_attr(any(classic2021, classic2024), feature(ref_pat_eat_one_layer_2024))] +#![cfg_attr(any(structural2021, structural2024), feature(ref_pat_eat_one_layer_2024_structural))] pub fn main() { - if let Some(&mut Some(ref mut x)) = &mut Some(Some(0)) { - //[stable2021]~^ ERROR: mismatched types - //[classic2024,structural2024]~^^ ERROR: cannot borrow as mutable inside an `&` pattern + if let Some(&mut Some(ref mut x)) = &mut Some(Some(0)) { // TODO: `classic2021` and `structural2021` shouldn't have mismatched types + //[stable2021,classic2021,structural2021]~^ ERROR: mismatched types + //[classic2021,structural2021,classic2024,structural2024]~^^ ERROR: cannot borrow as mutable inside an `&` pattern let _: &mut u8 = x; } if let &mut Some(Some(ref mut x)) = &mut Some(Some(0)) { //[stable2021]~^ ERROR: mismatched types - //[classic2024,structural2024]~^^ ERROR: cannot borrow as mutable inside an `&` pattern + //[classic2021,structural2021,classic2024,structural2024]~^^ ERROR: cannot borrow as mutable inside an `&` pattern let _: &mut u8 = x; } @@ -28,18 +30,18 @@ pub fn main() { } let &mut pat!(x) = &mut 0; //[stable2021]~^ ERROR: mismatched types - //[classic2024,structural2024]~^^ ERROR: cannot borrow as mutable inside an `&` pattern + //[classic2021,structural2021,classic2024,structural2024]~^^ ERROR: cannot borrow as mutable inside an `&` pattern let _: &mut u8 = x; let &mut (ref mut a, ref mut b) = &mut (true, false); //[stable2021]~^ ERROR: mismatched types - //[classic2024,structural2024]~^^ ERROR: cannot borrow as mutable inside an `&` pattern - //[classic2024,structural2024]~| ERROR: cannot borrow as mutable inside an `&` pattern + //[classic2021,structural2021,classic2024,structural2024]~^^ ERROR: cannot borrow as mutable inside an `&` pattern + //[classic2021,structural2021,classic2024,structural2024]~| ERROR: cannot borrow as mutable inside an `&` pattern let _: &mut bool = a; let _: &mut bool = b; let &[x] = &mut &mut [0]; //[stable2021]~^ ERROR: mismatched types - //[classic2024]~^^ ERROR: cannot borrow as mutable inside an `&` pattern + //[classic2021,classic2024]~^^ ERROR: cannot borrow as mutable inside an `&` pattern let _: &u32 = x; } diff --git a/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref-mut-inside-shared-ref-pat.structural2024.stderr b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref-mut-inside-shared-ref-pat.structural2024.stderr index 69cb6c438b6ed..11e39abb860b7 100644 --- a/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref-mut-inside-shared-ref-pat.structural2024.stderr +++ b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref-mut-inside-shared-ref-pat.structural2024.stderr @@ -1,13 +1,13 @@ error[E0596]: cannot borrow as mutable inside an `&` pattern - --> $DIR/ref-mut-inside-shared-ref-pat.rs:14:31 + --> $DIR/ref-mut-inside-shared-ref-pat.rs:16:31 | -LL | if let Some(&Some(ref mut x)) = &mut Some(Some(0)) { +LL | if let Some(&Some(ref mut x)) = &mut Some(Some(0)) { // TODO: `classic2021` and `structural2021` shouldn't have mismatched types | - ^ | | | help: replace this `&` with `&mut`: `&mut` error[E0596]: cannot borrow as mutable inside an `&` pattern - --> $DIR/ref-mut-inside-shared-ref-pat.rs:20:31 + --> $DIR/ref-mut-inside-shared-ref-pat.rs:22:31 | LL | if let &Some(Some(ref mut x)) = &mut Some(Some(0)) { | - ^ @@ -15,7 +15,7 @@ LL | if let &Some(Some(ref mut x)) = &mut Some(Some(0)) { | help: replace this `&` with `&mut`: `&mut` error[E0596]: cannot borrow as mutable inside an `&` pattern - --> $DIR/ref-mut-inside-shared-ref-pat.rs:29:15 + --> $DIR/ref-mut-inside-shared-ref-pat.rs:31:15 | LL | let &pat!(x) = &mut 0; | - ^ @@ -23,7 +23,7 @@ LL | let &pat!(x) = &mut 0; | help: replace this `&` with `&mut`: `&mut` error[E0596]: cannot borrow as mutable inside an `&` pattern - --> $DIR/ref-mut-inside-shared-ref-pat.rs:34:19 + --> $DIR/ref-mut-inside-shared-ref-pat.rs:36:19 | LL | let &(ref mut a, ref mut b) = &mut (true, false); | - ^ @@ -31,7 +31,7 @@ LL | let &(ref mut a, ref mut b) = &mut (true, false); | help: replace this `&` with `&mut`: `&mut` error[E0596]: cannot borrow as mutable inside an `&` pattern - --> $DIR/ref-mut-inside-shared-ref-pat.rs:34:30 + --> $DIR/ref-mut-inside-shared-ref-pat.rs:36:30 | LL | let &(ref mut a, ref mut b) = &mut (true, false); | - ^ diff --git a/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/well-typed-edition-2024.classic2021.stderr b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/well-typed-edition-2024.classic2021.stderr new file mode 100644 index 0000000000000..c9195dbfbf5f8 --- /dev/null +++ b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/well-typed-edition-2024.classic2021.stderr @@ -0,0 +1,254 @@ +error[E0308]: mismatched types + --> $DIR/well-typed-edition-2024.rs:32:23 + | +LL | if let Some(Some(&&x)) = &Some(Some(&0)) { + | ^^ --------------- this expression has type `&Option>` + | | + | expected integer, found `&_` + | + = note: expected type `{integer}` + found reference `&_` +help: consider removing `&` from the pattern + | +LL - if let Some(Some(&&x)) = &Some(Some(&0)) { +LL + if let Some(Some(&x)) = &Some(Some(&0)) { + | + +error[E0308]: mismatched types + --> $DIR/well-typed-edition-2024.rs:39:22 + | +LL | if let Some(Some(&x)) = &Some(&Some(0)) { + | ^^ --------------- this expression has type `&Option<&Option<{integer}>>` + | | + | expected integer, found `&_` + | + = note: expected type `{integer}` + found reference `&_` +help: consider removing `&` from the pattern + | +LL - if let Some(Some(&x)) = &Some(&Some(0)) { +LL + if let Some(Some(x)) = &Some(&Some(0)) { + | + +error[E0308]: mismatched types + --> $DIR/well-typed-edition-2024.rs:44:17 + | +LL | if let Some(&Some(x)) = &Some(Some(0)) { + | ^^^^^^^^ -------------- this expression has type `&Option>` + | | + | expected `Option<{integer}>`, found `&_` + | + = note: expected enum `Option<{integer}>` + found reference `&_` + +error[E0308]: mismatched types + --> $DIR/well-typed-edition-2024.rs:49:22 + | +LL | if let Some(Some(&mut x)) = &mut Some(&mut Some(0)) { + | ^^^^^^ ----------------------- this expression has type `&mut Option<&mut Option<{integer}>>` + | | + | expected integer, found `&mut _` + | + = note: expected type `{integer}` + found mutable reference `&mut _` +note: to declare a mutable binding use: `mut x` + --> $DIR/well-typed-edition-2024.rs:49:22 + | +LL | if let Some(Some(&mut x)) = &mut Some(&mut Some(0)) { + | ^^^^^^ +help: consider removing `&mut` from the pattern + | +LL - if let Some(Some(&mut x)) = &mut Some(&mut Some(0)) { +LL + if let Some(Some(x)) = &mut Some(&mut Some(0)) { + | + +error[E0308]: mismatched types + --> $DIR/well-typed-edition-2024.rs:63:23 + | +LL | if let Some(&Some(&x)) = &Some(&Some(0)) { + | ^^ --------------- this expression has type `&Option<&Option<{integer}>>` + | | + | expected integer, found `&_` + | + = note: expected type `{integer}` + found reference `&_` +help: consider removing `&` from the pattern + | +LL - if let Some(&Some(&x)) = &Some(&Some(0)) { +LL + if let Some(&Some(x)) = &Some(&Some(0)) { + | + +error[E0308]: mismatched types + --> $DIR/well-typed-edition-2024.rs:70:17 + | +LL | if let Some(&Some(Some(&x))) = &Some(Some(&mut Some(0))) { + | ^^^^^^^^^^^^^^^ ------------------------- this expression has type `&Option>>` + | | + | expected `Option<&mut Option<{integer}>>`, found `&_` + | + = note: expected enum `Option<&mut Option<{integer}>>` + found reference `&_` + +error[E0308]: mismatched types + --> $DIR/well-typed-edition-2024.rs:75:17 + | +LL | if let Some(&Some(x)) = &mut Some(Some(0)) { + | ^^^^^^^^ ------------------ this expression has type `&mut Option>` + | | + | expected `Option<{integer}>`, found `&_` + | + = note: expected enum `Option<{integer}>` + found reference `&_` + +error[E0308]: mismatched types + --> $DIR/well-typed-edition-2024.rs:82:23 + | +LL | if let Some(&Some(&x)) = &Some(&mut Some(0)) { + | ^^ ------------------- this expression has type `&Option<&mut Option<{integer}>>` + | | + | expected integer, found `&_` + | + = note: expected type `{integer}` + found reference `&_` +help: consider removing `&` from the pattern + | +LL - if let Some(&Some(&x)) = &Some(&mut Some(0)) { +LL + if let Some(&Some(x)) = &Some(&mut Some(0)) { + | + +error[E0308]: mismatched types + --> $DIR/well-typed-edition-2024.rs:88:23 + | +LL | if let Some(&Some(&x)) = &mut Some(&Some(0)) { + | ^^ ------------------- this expression has type `&mut Option<&Option<{integer}>>` + | | + | expected integer, found `&_` + | + = note: expected type `{integer}` + found reference `&_` +help: consider removing `&` from the pattern + | +LL - if let Some(&Some(&x)) = &mut Some(&Some(0)) { +LL + if let Some(&Some(x)) = &mut Some(&Some(0)) { + | + +error[E0308]: mismatched types + --> $DIR/well-typed-edition-2024.rs:96:10 + | +LL | let [&mut x] = &mut [&0]; + | ^^^^^^ --------- this expression has type `&mut [&{integer}; 1]` + | | + | types differ in mutability + | + = note: expected reference `&{integer}` + found mutable reference `&mut _` +note: to declare a mutable binding use: `mut x` + --> $DIR/well-typed-edition-2024.rs:96:10 + | +LL | let [&mut x] = &mut [&0]; + | ^^^^^^ +help: consider removing `&mut` from the pattern + | +LL - let [&mut x] = &mut [&0]; +LL + let [x] = &mut [&0]; + | + +error[E0308]: mismatched types + --> $DIR/well-typed-edition-2024.rs:102:10 + | +LL | let [&mut ref x] = &mut [&0]; + | ^^^^^^^^^^ --------- this expression has type `&mut [&{integer}; 1]` + | | + | types differ in mutability + | + = note: expected reference `&{integer}` + found mutable reference `&mut _` +note: to declare a mutable binding use: `mut x` + --> $DIR/well-typed-edition-2024.rs:102:10 + | +LL | let [&mut ref x] = &mut [&0]; + | ^^^^^^^^^^ +help: consider removing `&mut` from the pattern + | +LL - let [&mut ref x] = &mut [&0]; +LL + let [ref x] = &mut [&0]; + | + +error[E0308]: mismatched types + --> $DIR/well-typed-edition-2024.rs:108:10 + | +LL | let [&mut ref mut x] = &mut [&0]; + | ^^^^^^^^^^^^^^ --------- this expression has type `&mut [&{integer}; 1]` + | | + | types differ in mutability + | + = note: expected reference `&{integer}` + found mutable reference `&mut _` +note: to declare a mutable binding use: `mut x` + --> $DIR/well-typed-edition-2024.rs:108:10 + | +LL | let [&mut ref mut x] = &mut [&0]; + | ^^^^^^^^^^^^^^ +help: consider removing `&mut` from the pattern + | +LL - let [&mut ref mut x] = &mut [&0]; +LL + let [ref mut x] = &mut [&0]; + | + +error[E0308]: mismatched types + --> $DIR/well-typed-edition-2024.rs:114:10 + | +LL | let [&mut mut x] = &mut [&0]; + | ^^^^^^^^^^ --------- this expression has type `&mut [&{integer}; 1]` + | | + | types differ in mutability + | + = note: expected reference `&{integer}` + found mutable reference `&mut _` +note: to declare a mutable binding use: `mut x` + --> $DIR/well-typed-edition-2024.rs:114:10 + | +LL | let [&mut mut x] = &mut [&0]; + | ^^^^^^^^^^ +help: consider removing `&mut` from the pattern + | +LL - let [&mut mut x] = &mut [&0]; +LL + let [mut x] = &mut [&0]; + | + +error[E0308]: mismatched types + --> $DIR/well-typed-edition-2024.rs:120:10 + | +LL | let [&mut &x] = &mut [&0]; + | ^^^^^^^ --------- this expression has type `&mut [&{integer}; 1]` + | | + | types differ in mutability + | + = note: expected reference `&{integer}` + found mutable reference `&mut _` + +error[E0308]: mismatched types + --> $DIR/well-typed-edition-2024.rs:126:10 + | +LL | let [&mut &ref x] = &mut [&0]; + | ^^^^^^^^^^^ --------- this expression has type `&mut [&{integer}; 1]` + | | + | types differ in mutability + | + = note: expected reference `&{integer}` + found mutable reference `&mut _` + +error[E0308]: mismatched types + --> $DIR/well-typed-edition-2024.rs:132:10 + | +LL | let [&mut &(mut x)] = &mut [&0]; + | ^^^^^^^^^^^^^ --------- this expression has type `&mut [&{integer}; 1]` + | | + | types differ in mutability + | + = note: expected reference `&{integer}` + found mutable reference `&mut _` + +error: aborting due to 16 previous errors + +For more information about this error, try `rustc --explain E0308`. diff --git a/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/well-typed-edition-2024.rs b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/well-typed-edition-2024.rs index 3114b9d3bf8cb..e61aa5f35c9a2 100644 --- a/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/well-typed-edition-2024.rs +++ b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/well-typed-edition-2024.rs @@ -1,5 +1,7 @@ -//@ revisions: stable2021 classic2024 structural2024 +//@ revisions: stable2021 classic2021 structural2021 classic2024 structural2024 //@[stable2021] edition: 2021 +//@[classic2021] edition: 2021 +//@[structural2021] edition: 2021 //@[classic2024] edition: 2024 //@[structural2024] edition: 2024 //@[classic2024] run-pass @@ -7,8 +9,8 @@ //! Test cases for well-typed patterns in edition 2024. These are in their own file to ensure we //! pass both HIR typeck and MIR borrowck, as we may skip the latter if grouped with failing tests. #![allow(incomplete_features, unused_mut)] -#![cfg_attr(classic2024, feature(ref_pat_eat_one_layer_2024))] -#![cfg_attr(structural2024, feature(ref_pat_eat_one_layer_2024_structural))] +#![cfg_attr(any(classic2021, classic2024), feature(ref_pat_eat_one_layer_2024))] +#![cfg_attr(any(structural2021, structural2024), feature(ref_pat_eat_one_layer_2024_structural))] pub fn main() { // Tests not using match ergonomics. These should always succeed with the same bindings. @@ -18,110 +20,118 @@ pub fn main() { // Tests for differences in how many layers of reference are eaten by reference patterns if let Some(Some(&x)) = &Some(Some(&0)) { - #[cfg(stable2021)] let _: u32 = x; + #[cfg(any(stable2021, classic2021, structural2021))] let _: u32 = x; #[cfg(any(classic2024, structural2024))] let _: &u32 = x; } if let Some(&Some(x)) = &mut Some(&Some(0)) { // This additionally tests that `&` patterns can eat inherited `&mut` refs. // This is possible on stable when the real reference being eaten is of a `&` type. - #[cfg(stable2021)] let _: u32 = x; + #[cfg(any(stable2021, classic2021, structural2021))] let _: u32 = x; #[cfg(any(classic2024, structural2024))] let _: &u32 = x; } if let Some(Some(&&x)) = &Some(Some(&0)) { - //[stable2021]~^ mismatched types - //[stable2021]~| expected integer, found `&_` - let _: u32 = x; + //[stable2021,classic2021,structural2021]~^ mismatched types + //[stable2021,classic2021,structural2021]~| expected integer, found `&_` + #[cfg(any(classic2024, structural2024))] let _: u32 = x; } // Tests for eating a lone inherited reference if let Some(Some(&x)) = &Some(&Some(0)) { - //[stable2021]~^ mismatched types - //[stable2021]~| expected integer, found `&_` - let _: u32 = x; + //[stable2021,classic2021,structural2021]~^ mismatched types + //[stable2021,classic2021,structural2021]~| expected integer, found `&_` + #[cfg(any(classic2024, structural2024))] let _: u32 = x; // TODO: this should hold on `classic2021` and `structural2021` } if let Some(&Some(x)) = &Some(Some(0)) { - //[stable2021]~^ mismatched types - //[stable2021]~| expected `Option<{integer}>`, found `&_` - let _: u32 = x; + //[stable2021,classic2021,structural2021]~^ mismatched types + //[stable2021,classic2021,structural2021]~| expected `Option<{integer}>`, found `&_` + #[cfg(any(classic2024, structural2024))] let _: u32 = x; // TODO: this should hold on `classic2021` and `structural2021` } if let Some(Some(&mut x)) = &mut Some(&mut Some(0)) { - //[stable2021]~^ mismatched types - //[stable2021]~| expected integer, found `&mut _` - let _: u32 = x; + //[stable2021,classic2021,structural2021]~^ mismatched types + //[stable2021,classic2021,structural2021]~| expected integer, found `&mut _` + #[cfg(any(classic2024, structural2024))] let _: u32 = x; // TODO: this should hold on `classic2021` and `structural2021` } // Tests for `&` patterns matching real `&mut` reference types if let Some(&Some(&x)) = Some(&Some(&mut 0)) { //[stable2021]~^ mismatched types //[stable2021]~| types differ in mutability - let _: u32 = x; + #[cfg(any(classic2021, structural2021, classic2024, structural2024))] let _: u32 = x; } // Tests for eating only one layer and also eating a lone inherited reference if let Some(&Some(&x)) = &Some(&Some(0)) { - //[stable2021]~^ mismatched types - //[stable2021]~| expected integer, found `&_` - let _: u32 = x; + //[stable2021,classic2021,structural2021]~^ mismatched types + //[stable2021,classic2021,structural2021]~| expected integer, found `&_` + #[cfg(any(classic2024, structural2024))] let _: u32 = x; } // Tests for `&` matching a lone inherited possibly-`&mut` reference if let Some(&Some(Some(&x))) = &Some(Some(&mut Some(0))) { - //[stable2021]~^ mismatched types + //[stable2021,classic2021,structural2021]~^ mismatched types //[stable2021]~| expected `Option<&mut Option<{integer}>>`, found `&_` - let _: u32 = x; + #[cfg(any(classic2024, structural2024))] let _: u32 = x; // TODO: this should hold on `classic2021` and `structural2021` } if let Some(&Some(x)) = &mut Some(Some(0)) { - //[stable2021]~^ mismatched types + //[stable2021,classic2021,structural2021]~^ mismatched types //[stable2021]~| expected `Option<{integer}>`, found `&_` - let _: u32 = x; + #[cfg(any(classic2024, structural2024))] let _: u32 = x; // TODO: this should hold on `classic2021` and `structural2021` } // Tests eating one layer, eating a lone inherited ref, and `&` eating `&mut` (realness varies) if let Some(&Some(&x)) = &Some(&mut Some(0)) { - //[stable2021]~^ mismatched types + //[stable2021,classic2021,structural2021]~^ mismatched types //[stable2021]~| types differ in mutability - let _: u32 = x; + //[classic2021,structural2021]~| expected integer, found `&_` + #[cfg(any(classic2024, structural2024))] let _: u32 = x; } if let Some(&Some(&x)) = &mut Some(&Some(0)) { - //[stable2021]~^ mismatched types - //[stable2021]~| expected integer, found `&_` - let _: u32 = x; + //[stable2021,classic2021,structural2021]~^ mismatched types + //[stable2021,classic2021,structural2021]~| expected integer, found `&_` + #[cfg(any(classic2024, structural2024))] let _: u32 = x; } // Tests for eat-inner rulesets matching on the outer reference if matching on the inner // reference causes a mutability mismatch, i.e. `Deref(EatInner, FallbackToOuter)`: let [&mut x] = &mut [&0]; - //[stable2021]~^ mismatched types - //[stable2021]~| types differ in mutability - let _: &u32 = x; + //[stable2021,classic2021,structural2021]~^ mismatched types + //[stable2021,classic2021,structural2021]~| types differ in mutability + // TODO: on `structural2021` `x` should have type `u32` + #[cfg(any(classic2024, structural2024))] let _: &u32 = x; let [&mut ref x] = &mut [&0]; - //[stable2021]~^ mismatched types - //[stable2021]~| types differ in mutability - let _: &&u32 = x; + //[stable2021,classic2021,structural2021]~^ mismatched types + //[stable2021,classic2021,structural2021]~| types differ in mutability + // TODO: on `structural2021` `x` should have type `&u32` + #[cfg(any(classic2024, structural2024))] let _: &&u32 = x; let [&mut ref mut x] = &mut [&0]; - //[stable2021]~^ mismatched types - //[stable2021]~| types differ in mutability - let _: &mut &u32 = x; + //[stable2021,classic2021,structural2021]~^ mismatched types + //[stable2021,classic2021,structural2021]~| types differ in mutability + // TODO: this should be a mut borrow behind shared borrow error on `structural2021` + #[cfg(any(classic2024, structural2024))] let _: &mut &u32 = x; let [&mut mut x] = &mut [&0]; - //[stable2021]~^ mismatched types - //[stable2021]~| types differ in mutability - let _: &u32 = x; + //[stable2021,classic2021,structural2021]~^ mismatched types + //[stable2021,classic2021,structural2021]~| types differ in mutability + // TODO: on `structural2021` `x` should have type `u32` + #[cfg(any(classic2024, structural2024))] let _: &u32 = x; let [&mut &x] = &mut [&0]; - //[stable2021]~^ mismatched types - //[stable2021]~| types differ in mutability - let _: u32 = x; + //[stable2021,classic2021,structural2021]~^ mismatched types + //[stable2021,classic2021,structural2021]~| types differ in mutability + // TODO: [structural2021]~| expected integer, found `&_` + #[cfg(any(classic2024, structural2024))] let _: u32 = x; let [&mut &ref x] = &mut [&0]; - //[stable2021]~^ mismatched types - //[stable2021]~| types differ in mutability - let _: &u32 = x; + //[stable2021,classic2021,structural2021]~^ mismatched types + //[stable2021,classic2021,structural2021]~| types differ in mutability + // TODO: [structural2021]~| expected integer, found `&_` + #[cfg(any(classic2024, structural2024))] let _: &u32 = x; let [&mut &(mut x)] = &mut [&0]; - //[stable2021]~^ mismatched types - //[stable2021]~| types differ in mutability - let _: u32 = x; + //[stable2021,classic2021,structural2021]~^ mismatched types + //[stable2021,classic2021,structural2021]~| types differ in mutability + // TODO: [structural2021]~| expected integer, found `&_` + #[cfg(any(classic2024, structural2024))] let _: u32 = x; } diff --git a/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/well-typed-edition-2024.stable2021.stderr b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/well-typed-edition-2024.stable2021.stderr index b1a8024397bdc..fb99e3983e8cc 100644 --- a/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/well-typed-edition-2024.stable2021.stderr +++ b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/well-typed-edition-2024.stable2021.stderr @@ -1,5 +1,5 @@ error[E0308]: mismatched types - --> $DIR/well-typed-edition-2024.rs:30:23 + --> $DIR/well-typed-edition-2024.rs:32:23 | LL | if let Some(Some(&&x)) = &Some(Some(&0)) { | ^^ --------------- this expression has type `&Option>` @@ -15,7 +15,7 @@ LL + if let Some(Some(&x)) = &Some(Some(&0)) { | error[E0308]: mismatched types - --> $DIR/well-typed-edition-2024.rs:37:22 + --> $DIR/well-typed-edition-2024.rs:39:22 | LL | if let Some(Some(&x)) = &Some(&Some(0)) { | ^^ --------------- this expression has type `&Option<&Option<{integer}>>` @@ -31,7 +31,7 @@ LL + if let Some(Some(x)) = &Some(&Some(0)) { | error[E0308]: mismatched types - --> $DIR/well-typed-edition-2024.rs:42:17 + --> $DIR/well-typed-edition-2024.rs:44:17 | LL | if let Some(&Some(x)) = &Some(Some(0)) { | ^^^^^^^^ -------------- this expression has type `&Option>` @@ -42,7 +42,7 @@ LL | if let Some(&Some(x)) = &Some(Some(0)) { found reference `&_` error[E0308]: mismatched types - --> $DIR/well-typed-edition-2024.rs:47:22 + --> $DIR/well-typed-edition-2024.rs:49:22 | LL | if let Some(Some(&mut x)) = &mut Some(&mut Some(0)) { | ^^^^^^ ----------------------- this expression has type `&mut Option<&mut Option<{integer}>>` @@ -52,7 +52,7 @@ LL | if let Some(Some(&mut x)) = &mut Some(&mut Some(0)) { = note: expected type `{integer}` found mutable reference `&mut _` note: to declare a mutable binding use: `mut x` - --> $DIR/well-typed-edition-2024.rs:47:22 + --> $DIR/well-typed-edition-2024.rs:49:22 | LL | if let Some(Some(&mut x)) = &mut Some(&mut Some(0)) { | ^^^^^^ @@ -63,7 +63,7 @@ LL + if let Some(Some(x)) = &mut Some(&mut Some(0)) { | error[E0308]: mismatched types - --> $DIR/well-typed-edition-2024.rs:54:23 + --> $DIR/well-typed-edition-2024.rs:56:23 | LL | if let Some(&Some(&x)) = Some(&Some(&mut 0)) { | ^^ ------------------- this expression has type `Option<&Option<&mut {integer}>>` @@ -79,7 +79,7 @@ LL + if let Some(&Some(x)) = Some(&Some(&mut 0)) { | error[E0308]: mismatched types - --> $DIR/well-typed-edition-2024.rs:61:23 + --> $DIR/well-typed-edition-2024.rs:63:23 | LL | if let Some(&Some(&x)) = &Some(&Some(0)) { | ^^ --------------- this expression has type `&Option<&Option<{integer}>>` @@ -95,7 +95,7 @@ LL + if let Some(&Some(x)) = &Some(&Some(0)) { | error[E0308]: mismatched types - --> $DIR/well-typed-edition-2024.rs:68:17 + --> $DIR/well-typed-edition-2024.rs:70:17 | LL | if let Some(&Some(Some(&x))) = &Some(Some(&mut Some(0))) { | ^^^^^^^^^^^^^^^ ------------------------- this expression has type `&Option>>` @@ -106,7 +106,7 @@ LL | if let Some(&Some(Some(&x))) = &Some(Some(&mut Some(0))) { found reference `&_` error[E0308]: mismatched types - --> $DIR/well-typed-edition-2024.rs:73:17 + --> $DIR/well-typed-edition-2024.rs:75:17 | LL | if let Some(&Some(x)) = &mut Some(Some(0)) { | ^^^^^^^^ ------------------ this expression has type `&mut Option>` @@ -117,7 +117,7 @@ LL | if let Some(&Some(x)) = &mut Some(Some(0)) { found reference `&_` error[E0308]: mismatched types - --> $DIR/well-typed-edition-2024.rs:80:17 + --> $DIR/well-typed-edition-2024.rs:82:17 | LL | if let Some(&Some(&x)) = &Some(&mut Some(0)) { | ^^^^^^^^^ ------------------- this expression has type `&Option<&mut Option<{integer}>>` @@ -128,7 +128,7 @@ LL | if let Some(&Some(&x)) = &Some(&mut Some(0)) { found reference `&_` error[E0308]: mismatched types - --> $DIR/well-typed-edition-2024.rs:85:23 + --> $DIR/well-typed-edition-2024.rs:88:23 | LL | if let Some(&Some(&x)) = &mut Some(&Some(0)) { | ^^ ------------------- this expression has type `&mut Option<&Option<{integer}>>` @@ -144,7 +144,7 @@ LL + if let Some(&Some(x)) = &mut Some(&Some(0)) { | error[E0308]: mismatched types - --> $DIR/well-typed-edition-2024.rs:93:10 + --> $DIR/well-typed-edition-2024.rs:96:10 | LL | let [&mut x] = &mut [&0]; | ^^^^^^ --------- this expression has type `&mut [&{integer}; 1]` @@ -154,7 +154,7 @@ LL | let [&mut x] = &mut [&0]; = note: expected reference `&{integer}` found mutable reference `&mut _` note: to declare a mutable binding use: `mut x` - --> $DIR/well-typed-edition-2024.rs:93:10 + --> $DIR/well-typed-edition-2024.rs:96:10 | LL | let [&mut x] = &mut [&0]; | ^^^^^^ @@ -165,7 +165,7 @@ LL + let [x] = &mut [&0]; | error[E0308]: mismatched types - --> $DIR/well-typed-edition-2024.rs:98:10 + --> $DIR/well-typed-edition-2024.rs:102:10 | LL | let [&mut ref x] = &mut [&0]; | ^^^^^^^^^^ --------- this expression has type `&mut [&{integer}; 1]` @@ -175,7 +175,7 @@ LL | let [&mut ref x] = &mut [&0]; = note: expected reference `&{integer}` found mutable reference `&mut _` note: to declare a mutable binding use: `mut x` - --> $DIR/well-typed-edition-2024.rs:98:10 + --> $DIR/well-typed-edition-2024.rs:102:10 | LL | let [&mut ref x] = &mut [&0]; | ^^^^^^^^^^ @@ -186,7 +186,7 @@ LL + let [ref x] = &mut [&0]; | error[E0308]: mismatched types - --> $DIR/well-typed-edition-2024.rs:103:10 + --> $DIR/well-typed-edition-2024.rs:108:10 | LL | let [&mut ref mut x] = &mut [&0]; | ^^^^^^^^^^^^^^ --------- this expression has type `&mut [&{integer}; 1]` @@ -196,7 +196,7 @@ LL | let [&mut ref mut x] = &mut [&0]; = note: expected reference `&{integer}` found mutable reference `&mut _` note: to declare a mutable binding use: `mut x` - --> $DIR/well-typed-edition-2024.rs:103:10 + --> $DIR/well-typed-edition-2024.rs:108:10 | LL | let [&mut ref mut x] = &mut [&0]; | ^^^^^^^^^^^^^^ @@ -207,7 +207,7 @@ LL + let [ref mut x] = &mut [&0]; | error[E0308]: mismatched types - --> $DIR/well-typed-edition-2024.rs:108:10 + --> $DIR/well-typed-edition-2024.rs:114:10 | LL | let [&mut mut x] = &mut [&0]; | ^^^^^^^^^^ --------- this expression has type `&mut [&{integer}; 1]` @@ -217,7 +217,7 @@ LL | let [&mut mut x] = &mut [&0]; = note: expected reference `&{integer}` found mutable reference `&mut _` note: to declare a mutable binding use: `mut x` - --> $DIR/well-typed-edition-2024.rs:108:10 + --> $DIR/well-typed-edition-2024.rs:114:10 | LL | let [&mut mut x] = &mut [&0]; | ^^^^^^^^^^ @@ -228,7 +228,7 @@ LL + let [mut x] = &mut [&0]; | error[E0308]: mismatched types - --> $DIR/well-typed-edition-2024.rs:113:10 + --> $DIR/well-typed-edition-2024.rs:120:10 | LL | let [&mut &x] = &mut [&0]; | ^^^^^^^ --------- this expression has type `&mut [&{integer}; 1]` @@ -239,7 +239,7 @@ LL | let [&mut &x] = &mut [&0]; found mutable reference `&mut _` error[E0308]: mismatched types - --> $DIR/well-typed-edition-2024.rs:118:10 + --> $DIR/well-typed-edition-2024.rs:126:10 | LL | let [&mut &ref x] = &mut [&0]; | ^^^^^^^^^^^ --------- this expression has type `&mut [&{integer}; 1]` @@ -250,7 +250,7 @@ LL | let [&mut &ref x] = &mut [&0]; found mutable reference `&mut _` error[E0308]: mismatched types - --> $DIR/well-typed-edition-2024.rs:123:10 + --> $DIR/well-typed-edition-2024.rs:132:10 | LL | let [&mut &(mut x)] = &mut [&0]; | ^^^^^^^^^^^^^ --------- this expression has type `&mut [&{integer}; 1]` diff --git a/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/well-typed-edition-2024.structural2021.stderr b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/well-typed-edition-2024.structural2021.stderr new file mode 100644 index 0000000000000..c9195dbfbf5f8 --- /dev/null +++ b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/well-typed-edition-2024.structural2021.stderr @@ -0,0 +1,254 @@ +error[E0308]: mismatched types + --> $DIR/well-typed-edition-2024.rs:32:23 + | +LL | if let Some(Some(&&x)) = &Some(Some(&0)) { + | ^^ --------------- this expression has type `&Option>` + | | + | expected integer, found `&_` + | + = note: expected type `{integer}` + found reference `&_` +help: consider removing `&` from the pattern + | +LL - if let Some(Some(&&x)) = &Some(Some(&0)) { +LL + if let Some(Some(&x)) = &Some(Some(&0)) { + | + +error[E0308]: mismatched types + --> $DIR/well-typed-edition-2024.rs:39:22 + | +LL | if let Some(Some(&x)) = &Some(&Some(0)) { + | ^^ --------------- this expression has type `&Option<&Option<{integer}>>` + | | + | expected integer, found `&_` + | + = note: expected type `{integer}` + found reference `&_` +help: consider removing `&` from the pattern + | +LL - if let Some(Some(&x)) = &Some(&Some(0)) { +LL + if let Some(Some(x)) = &Some(&Some(0)) { + | + +error[E0308]: mismatched types + --> $DIR/well-typed-edition-2024.rs:44:17 + | +LL | if let Some(&Some(x)) = &Some(Some(0)) { + | ^^^^^^^^ -------------- this expression has type `&Option>` + | | + | expected `Option<{integer}>`, found `&_` + | + = note: expected enum `Option<{integer}>` + found reference `&_` + +error[E0308]: mismatched types + --> $DIR/well-typed-edition-2024.rs:49:22 + | +LL | if let Some(Some(&mut x)) = &mut Some(&mut Some(0)) { + | ^^^^^^ ----------------------- this expression has type `&mut Option<&mut Option<{integer}>>` + | | + | expected integer, found `&mut _` + | + = note: expected type `{integer}` + found mutable reference `&mut _` +note: to declare a mutable binding use: `mut x` + --> $DIR/well-typed-edition-2024.rs:49:22 + | +LL | if let Some(Some(&mut x)) = &mut Some(&mut Some(0)) { + | ^^^^^^ +help: consider removing `&mut` from the pattern + | +LL - if let Some(Some(&mut x)) = &mut Some(&mut Some(0)) { +LL + if let Some(Some(x)) = &mut Some(&mut Some(0)) { + | + +error[E0308]: mismatched types + --> $DIR/well-typed-edition-2024.rs:63:23 + | +LL | if let Some(&Some(&x)) = &Some(&Some(0)) { + | ^^ --------------- this expression has type `&Option<&Option<{integer}>>` + | | + | expected integer, found `&_` + | + = note: expected type `{integer}` + found reference `&_` +help: consider removing `&` from the pattern + | +LL - if let Some(&Some(&x)) = &Some(&Some(0)) { +LL + if let Some(&Some(x)) = &Some(&Some(0)) { + | + +error[E0308]: mismatched types + --> $DIR/well-typed-edition-2024.rs:70:17 + | +LL | if let Some(&Some(Some(&x))) = &Some(Some(&mut Some(0))) { + | ^^^^^^^^^^^^^^^ ------------------------- this expression has type `&Option>>` + | | + | expected `Option<&mut Option<{integer}>>`, found `&_` + | + = note: expected enum `Option<&mut Option<{integer}>>` + found reference `&_` + +error[E0308]: mismatched types + --> $DIR/well-typed-edition-2024.rs:75:17 + | +LL | if let Some(&Some(x)) = &mut Some(Some(0)) { + | ^^^^^^^^ ------------------ this expression has type `&mut Option>` + | | + | expected `Option<{integer}>`, found `&_` + | + = note: expected enum `Option<{integer}>` + found reference `&_` + +error[E0308]: mismatched types + --> $DIR/well-typed-edition-2024.rs:82:23 + | +LL | if let Some(&Some(&x)) = &Some(&mut Some(0)) { + | ^^ ------------------- this expression has type `&Option<&mut Option<{integer}>>` + | | + | expected integer, found `&_` + | + = note: expected type `{integer}` + found reference `&_` +help: consider removing `&` from the pattern + | +LL - if let Some(&Some(&x)) = &Some(&mut Some(0)) { +LL + if let Some(&Some(x)) = &Some(&mut Some(0)) { + | + +error[E0308]: mismatched types + --> $DIR/well-typed-edition-2024.rs:88:23 + | +LL | if let Some(&Some(&x)) = &mut Some(&Some(0)) { + | ^^ ------------------- this expression has type `&mut Option<&Option<{integer}>>` + | | + | expected integer, found `&_` + | + = note: expected type `{integer}` + found reference `&_` +help: consider removing `&` from the pattern + | +LL - if let Some(&Some(&x)) = &mut Some(&Some(0)) { +LL + if let Some(&Some(x)) = &mut Some(&Some(0)) { + | + +error[E0308]: mismatched types + --> $DIR/well-typed-edition-2024.rs:96:10 + | +LL | let [&mut x] = &mut [&0]; + | ^^^^^^ --------- this expression has type `&mut [&{integer}; 1]` + | | + | types differ in mutability + | + = note: expected reference `&{integer}` + found mutable reference `&mut _` +note: to declare a mutable binding use: `mut x` + --> $DIR/well-typed-edition-2024.rs:96:10 + | +LL | let [&mut x] = &mut [&0]; + | ^^^^^^ +help: consider removing `&mut` from the pattern + | +LL - let [&mut x] = &mut [&0]; +LL + let [x] = &mut [&0]; + | + +error[E0308]: mismatched types + --> $DIR/well-typed-edition-2024.rs:102:10 + | +LL | let [&mut ref x] = &mut [&0]; + | ^^^^^^^^^^ --------- this expression has type `&mut [&{integer}; 1]` + | | + | types differ in mutability + | + = note: expected reference `&{integer}` + found mutable reference `&mut _` +note: to declare a mutable binding use: `mut x` + --> $DIR/well-typed-edition-2024.rs:102:10 + | +LL | let [&mut ref x] = &mut [&0]; + | ^^^^^^^^^^ +help: consider removing `&mut` from the pattern + | +LL - let [&mut ref x] = &mut [&0]; +LL + let [ref x] = &mut [&0]; + | + +error[E0308]: mismatched types + --> $DIR/well-typed-edition-2024.rs:108:10 + | +LL | let [&mut ref mut x] = &mut [&0]; + | ^^^^^^^^^^^^^^ --------- this expression has type `&mut [&{integer}; 1]` + | | + | types differ in mutability + | + = note: expected reference `&{integer}` + found mutable reference `&mut _` +note: to declare a mutable binding use: `mut x` + --> $DIR/well-typed-edition-2024.rs:108:10 + | +LL | let [&mut ref mut x] = &mut [&0]; + | ^^^^^^^^^^^^^^ +help: consider removing `&mut` from the pattern + | +LL - let [&mut ref mut x] = &mut [&0]; +LL + let [ref mut x] = &mut [&0]; + | + +error[E0308]: mismatched types + --> $DIR/well-typed-edition-2024.rs:114:10 + | +LL | let [&mut mut x] = &mut [&0]; + | ^^^^^^^^^^ --------- this expression has type `&mut [&{integer}; 1]` + | | + | types differ in mutability + | + = note: expected reference `&{integer}` + found mutable reference `&mut _` +note: to declare a mutable binding use: `mut x` + --> $DIR/well-typed-edition-2024.rs:114:10 + | +LL | let [&mut mut x] = &mut [&0]; + | ^^^^^^^^^^ +help: consider removing `&mut` from the pattern + | +LL - let [&mut mut x] = &mut [&0]; +LL + let [mut x] = &mut [&0]; + | + +error[E0308]: mismatched types + --> $DIR/well-typed-edition-2024.rs:120:10 + | +LL | let [&mut &x] = &mut [&0]; + | ^^^^^^^ --------- this expression has type `&mut [&{integer}; 1]` + | | + | types differ in mutability + | + = note: expected reference `&{integer}` + found mutable reference `&mut _` + +error[E0308]: mismatched types + --> $DIR/well-typed-edition-2024.rs:126:10 + | +LL | let [&mut &ref x] = &mut [&0]; + | ^^^^^^^^^^^ --------- this expression has type `&mut [&{integer}; 1]` + | | + | types differ in mutability + | + = note: expected reference `&{integer}` + found mutable reference `&mut _` + +error[E0308]: mismatched types + --> $DIR/well-typed-edition-2024.rs:132:10 + | +LL | let [&mut &(mut x)] = &mut [&0]; + | ^^^^^^^^^^^^^ --------- this expression has type `&mut [&{integer}; 1]` + | | + | types differ in mutability + | + = note: expected reference `&{integer}` + found mutable reference `&mut _` + +error: aborting due to 16 previous errors + +For more information about this error, try `rustc --explain E0308`. From 3e77657312dbd7da6a398825860b01e838893b3c Mon Sep 17 00:00:00 2001 From: dianne Date: Thu, 23 Jan 2025 00:21:17 -0800 Subject: [PATCH 02/11] remove old edition-2021-specific tests These are superseded by the old-edition revisions on the shared tests. --- .../ref_pat_eat_one_layer_2021.rs | 17 --- .../ref_pat_eat_one_layer_2021_fail.rs | 37 ----- .../ref_pat_eat_one_layer_2021_fail.stderr | 133 ------------------ 3 files changed, 187 deletions(-) delete mode 100644 tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref_pat_eat_one_layer_2021.rs delete mode 100644 tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref_pat_eat_one_layer_2021_fail.rs delete mode 100644 tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref_pat_eat_one_layer_2021_fail.stderr diff --git a/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref_pat_eat_one_layer_2021.rs b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref_pat_eat_one_layer_2021.rs deleted file mode 100644 index ab3264704acc0..0000000000000 --- a/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref_pat_eat_one_layer_2021.rs +++ /dev/null @@ -1,17 +0,0 @@ -//@ run-pass -//@ edition: 2021 -//@ revisions: classic structural -#![allow(incomplete_features)] -#![cfg_attr(classic, feature(ref_pat_eat_one_layer_2024))] -#![cfg_attr(structural, feature(ref_pat_eat_one_layer_2024_structural))] - -pub fn main() { - #[cfg(structural)] - if let &Some(Some(x)) = &Some(&mut Some(0)) { - let _: &u32 = x; - } - - if let Some(&x) = Some(&mut 0) { - let _: u32 = x; - } -} diff --git a/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref_pat_eat_one_layer_2021_fail.rs b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref_pat_eat_one_layer_2021_fail.rs deleted file mode 100644 index d28567f2859a3..0000000000000 --- a/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref_pat_eat_one_layer_2021_fail.rs +++ /dev/null @@ -1,37 +0,0 @@ -//@ edition: 2021 -#![allow(incomplete_features)] -#![feature(ref_pat_eat_one_layer_2024)] -pub fn main() { - if let Some(Some(&x)) = &Some(&Some(0)) { - //~^ ERROR: mismatched types - let _: u32 = x; - } - if let Some(Some(&x)) = &Some(Some(&0)) { - let _: &u32 = x; - //~^ ERROR: mismatched types - } - if let Some(Some(&&x)) = &Some(Some(&0)) { - //~^ ERROR: mismatched types - let _: u32 = x; - } - if let Some(&Some(x)) = &Some(Some(0)) { - //~^ ERROR: mismatched types - let _: u32 = x; - } - if let Some(Some(&mut x)) = &mut Some(&mut Some(0)) { - //~^ ERROR: mismatched types - let _: u32 = x; - } - if let Some(Some(&x)) = &Some(&Some(0)) { - //~^ ERROR: mismatched types - let _: u32 = x; - } - if let Some(&mut Some(&x)) = &Some(&mut Some(0)) { - //~^ ERROR: mismatched types - let _: u32 = x; - } - if let Some(&Some(&mut x)) = &mut Some(&Some(0)) { - //~^ ERROR: mismatched types - let _: u32 = x; - } -} diff --git a/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref_pat_eat_one_layer_2021_fail.stderr b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref_pat_eat_one_layer_2021_fail.stderr deleted file mode 100644 index 0158ed0f42357..0000000000000 --- a/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref_pat_eat_one_layer_2021_fail.stderr +++ /dev/null @@ -1,133 +0,0 @@ -error[E0308]: mismatched types - --> $DIR/ref_pat_eat_one_layer_2021_fail.rs:5:22 - | -LL | if let Some(Some(&x)) = &Some(&Some(0)) { - | ^^ --------------- this expression has type `&Option<&Option<{integer}>>` - | | - | expected integer, found `&_` - | - = note: expected type `{integer}` - found reference `&_` -help: consider removing `&` from the pattern - | -LL - if let Some(Some(&x)) = &Some(&Some(0)) { -LL + if let Some(Some(x)) = &Some(&Some(0)) { - | - -error[E0308]: mismatched types - --> $DIR/ref_pat_eat_one_layer_2021_fail.rs:10:23 - | -LL | let _: &u32 = x; - | ---- ^ expected `&u32`, found integer - | | - | expected due to this - | -help: consider borrowing here - | -LL | let _: &u32 = &x; - | + - -error[E0308]: mismatched types - --> $DIR/ref_pat_eat_one_layer_2021_fail.rs:13:23 - | -LL | if let Some(Some(&&x)) = &Some(Some(&0)) { - | ^^ --------------- this expression has type `&Option>` - | | - | expected integer, found `&_` - | - = note: expected type `{integer}` - found reference `&_` -help: consider removing `&` from the pattern - | -LL - if let Some(Some(&&x)) = &Some(Some(&0)) { -LL + if let Some(Some(&x)) = &Some(Some(&0)) { - | - -error[E0308]: mismatched types - --> $DIR/ref_pat_eat_one_layer_2021_fail.rs:17:17 - | -LL | if let Some(&Some(x)) = &Some(Some(0)) { - | ^^^^^^^^ -------------- this expression has type `&Option>` - | | - | expected `Option<{integer}>`, found `&_` - | - = note: expected enum `Option<{integer}>` - found reference `&_` - -error[E0308]: mismatched types - --> $DIR/ref_pat_eat_one_layer_2021_fail.rs:21:22 - | -LL | if let Some(Some(&mut x)) = &mut Some(&mut Some(0)) { - | ^^^^^^ ----------------------- this expression has type `&mut Option<&mut Option<{integer}>>` - | | - | expected integer, found `&mut _` - | - = note: expected type `{integer}` - found mutable reference `&mut _` -note: to declare a mutable binding use: `mut x` - --> $DIR/ref_pat_eat_one_layer_2021_fail.rs:21:22 - | -LL | if let Some(Some(&mut x)) = &mut Some(&mut Some(0)) { - | ^^^^^^ -help: consider removing `&mut` from the pattern - | -LL - if let Some(Some(&mut x)) = &mut Some(&mut Some(0)) { -LL + if let Some(Some(x)) = &mut Some(&mut Some(0)) { - | - -error[E0308]: mismatched types - --> $DIR/ref_pat_eat_one_layer_2021_fail.rs:25:22 - | -LL | if let Some(Some(&x)) = &Some(&Some(0)) { - | ^^ --------------- this expression has type `&Option<&Option<{integer}>>` - | | - | expected integer, found `&_` - | - = note: expected type `{integer}` - found reference `&_` -help: consider removing `&` from the pattern - | -LL - if let Some(Some(&x)) = &Some(&Some(0)) { -LL + if let Some(Some(x)) = &Some(&Some(0)) { - | - -error[E0308]: mismatched types - --> $DIR/ref_pat_eat_one_layer_2021_fail.rs:29:27 - | -LL | if let Some(&mut Some(&x)) = &Some(&mut Some(0)) { - | ^^ ------------------- this expression has type `&Option<&mut Option<{integer}>>` - | | - | expected integer, found `&_` - | - = note: expected type `{integer}` - found reference `&_` -help: consider removing `&` from the pattern - | -LL - if let Some(&mut Some(&x)) = &Some(&mut Some(0)) { -LL + if let Some(&mut Some(x)) = &Some(&mut Some(0)) { - | - -error[E0308]: mismatched types - --> $DIR/ref_pat_eat_one_layer_2021_fail.rs:33:23 - | -LL | if let Some(&Some(&mut x)) = &mut Some(&Some(0)) { - | ^^^^^^ ------------------- this expression has type `&mut Option<&Option<{integer}>>` - | | - | expected integer, found `&mut _` - | - = note: expected type `{integer}` - found mutable reference `&mut _` -note: to declare a mutable binding use: `mut x` - --> $DIR/ref_pat_eat_one_layer_2021_fail.rs:33:23 - | -LL | if let Some(&Some(&mut x)) = &mut Some(&Some(0)) { - | ^^^^^^ -help: consider removing `&mut` from the pattern - | -LL - if let Some(&Some(&mut x)) = &mut Some(&Some(0)) { -LL + if let Some(&Some(x)) = &mut Some(&Some(0)) { - | - -error: aborting due to 8 previous errors - -For more information about this error, try `rustc --explain E0308`. From 8dc64a405d53f30fef4a080afb68d10e59318a3d Mon Sep 17 00:00:00 2001 From: dianne Date: Sat, 25 Jan 2025 23:32:12 -0800 Subject: [PATCH 03/11] "classic2021" and "structural2021" rulesets: add eat-inherited-ref-alone deref rules --- compiler/rustc_hir_typeck/src/pat.rs | 39 +++- .../pattern-errors.classic2021.stderr | 167 ++---------------- .../pattern-errors.classic2024.stderr | 10 +- .../experimental/pattern-errors.rs | 54 +++--- .../pattern-errors.stable2021.stderr | 38 ++-- .../pattern-errors.structural2021.stderr | 154 ++++++---------- .../pattern-errors.structural2024.stderr | 30 ++-- ...t-inside-shared-ref-pat.classic2021.stderr | 18 +- ...ut-inside-shared-ref-pat.classic2024.fixed | 4 +- ...t-inside-shared-ref-pat.classic2024.stderr | 2 +- .../ref-mut-inside-shared-ref-pat.rs | 4 +- ...ut-inside-shared-ref-pat.stable2021.stderr | 2 +- ...nside-shared-ref-pat.structural2021.stderr | 18 +- ...inside-shared-ref-pat.structural2024.fixed | 4 +- ...nside-shared-ref-pat.structural2024.stderr | 2 +- ...well-typed-edition-2024.classic2021.stderr | 72 +------- .../experimental/well-typed-edition-2024.rs | 26 +-- ...l-typed-edition-2024.structural2021.stderr | 72 +------- 18 files changed, 195 insertions(+), 521 deletions(-) diff --git a/compiler/rustc_hir_typeck/src/pat.rs b/compiler/rustc_hir_typeck/src/pat.rs index ae00bb4e218ab..47175cda63b88 100644 --- a/compiler/rustc_hir_typeck/src/pat.rs +++ b/compiler/rustc_hir_typeck/src/pat.rs @@ -230,10 +230,12 @@ enum InheritedRefMatchRule { /// underlying type is not a reference type, the inherited reference will be consumed. EatInner, /// When the underlying type is a reference type, reference patterns consume both layers of - /// reference, i.e. they both reset the binding mode and consume the reference type. Reference - /// patterns are not permitted when there is no underlying reference type, i.e. they can't eat - /// only an inherited reference. This is the current stable Rust behavior. - EatBoth, + /// reference, i.e. they both reset the binding mode and consume the reference type. + EatBoth { + /// Whether to allow reference patterns to consume only an inherited reference when matching + /// against a non-reference type. This is `false` for stable Rust. + eat_inherited_ref_alone: bool, + }, } impl<'a, 'tcx> FnCtxt<'a, 'tcx> { @@ -259,10 +261,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { } else { // Currently, matching against an inherited ref on edition 2024 is an error. // Use `EatBoth` as a fallback to be similar to stable Rust. - InheritedRefMatchRule::EatBoth + InheritedRefMatchRule::EatBoth { eat_inherited_ref_alone: false } } } else { - InheritedRefMatchRule::EatBoth + InheritedRefMatchRule::EatBoth { + eat_inherited_ref_alone: self.tcx.features().ref_pat_eat_one_layer_2024() + || self.tcx.features().ref_pat_eat_one_layer_2024_structural(), + } } } @@ -2381,9 +2386,29 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { return expected; } } - InheritedRefMatchRule::EatBoth => { + InheritedRefMatchRule::EatBoth { eat_inherited_ref_alone: true } => { // Reset binding mode on old editions pat_info.binding_mode = ByRef::No; + + if let ty::Ref(_, _, _) = *expected.kind() { + // Consume both the inherited and inner references. + } else { + // The expected type isn't a reference type, so only match against the + // inherited reference. + if pat_mutbl > inh_mut { + // We can't match a lone inherited shared reference with `&mut`. + self.error_inherited_ref_mutability_mismatch(pat, pat_prefix_span); + } + + self.typeck_results.borrow_mut().skipped_ref_pats_mut().insert(pat.hir_id); + self.check_pat(inner, expected, pat_info); + return expected; + } + } + InheritedRefMatchRule::EatBoth { eat_inherited_ref_alone: false } => { + // Reset binding mode on stable Rust. This will be a type error below if + // `expected` is not a reference type. + pat_info.binding_mode = ByRef::No; self.add_rust_2024_migration_desugared_pat( pat_info.top_info.hir_id, pat, diff --git a/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/pattern-errors.classic2021.stderr b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/pattern-errors.classic2021.stderr index e039884cdb14b..a856a0eaf2a7c 100644 --- a/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/pattern-errors.classic2021.stderr +++ b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/pattern-errors.classic2021.stderr @@ -58,155 +58,20 @@ LL | if let Some(&Some(&mut _)) = &mut Some(&Some(0)) { found mutable reference `&mut _` error[E0308]: mismatched types - --> $DIR/pattern-errors.rs:50:17 - | -LL | if let Some(&Some(Some(&mut x))) = &Some(Some(&mut Some(0))) { - | ^^^^^^^^^^^^^^^^^^^ ------------------------- this expression has type `&Option>>` - | | - | expected `Option<&mut Option<{integer}>>`, found `&_` - | - = note: expected enum `Option<&mut Option<{integer}>>` - found reference `&_` - -error[E0308]: mismatched types - --> $DIR/pattern-errors.rs:57:17 + --> $DIR/pattern-errors.rs:56:17 | LL | if let Some(&mut Some(x)) = &Some(Some(0)) { - | ^^^^^^^^^^^^ -------------- this expression has type `&Option>` - | | - | expected `Option<{integer}>`, found `&mut _` - | - = note: expected enum `Option<{integer}>` - found mutable reference `&mut _` - -error[E0308]: mismatched types - --> $DIR/pattern-errors.rs:66:11 - | -LL | let &[&mut x] = &&mut [0]; - | ^^^^^^ --------- this expression has type `&&mut [{integer}; 1]` - | | - | expected integer, found `&mut _` - | - = note: expected type `{integer}` - found mutable reference `&mut _` -note: to declare a mutable binding use: `mut x` - --> $DIR/pattern-errors.rs:66:11 - | -LL | let &[&mut x] = &&mut [0]; - | ^^^^^^ -help: consider removing `&mut` from the pattern - | -LL - let &[&mut x] = &&mut [0]; -LL + let &[x] = &&mut [0]; - | - -error[E0308]: mismatched types - --> $DIR/pattern-errors.rs:73:11 - | -LL | let &[&mut x] = &mut &mut [0]; - | ^^^^^^ ------------- this expression has type `&mut &mut [{integer}; 1]` - | | - | expected integer, found `&mut _` - | - = note: expected type `{integer}` - found mutable reference `&mut _` -note: to declare a mutable binding use: `mut x` - --> $DIR/pattern-errors.rs:73:11 - | -LL | let &[&mut x] = &mut &mut [0]; - | ^^^^^^ -help: consider removing `&mut` from the pattern - | -LL - let &[&mut x] = &mut &mut [0]; -LL + let &[x] = &mut &mut [0]; - | - -error[E0308]: mismatched types - --> $DIR/pattern-errors.rs:80:11 - | -LL | let &[&mut ref x] = &&mut [0]; - | ^^^^^^^^^^ --------- this expression has type `&&mut [{integer}; 1]` - | | - | expected integer, found `&mut _` - | - = note: expected type `{integer}` - found mutable reference `&mut _` -note: to declare a mutable binding use: `mut x` - --> $DIR/pattern-errors.rs:80:11 - | -LL | let &[&mut ref x] = &&mut [0]; - | ^^^^^^^^^^ -help: consider removing `&mut` from the pattern - | -LL - let &[&mut ref x] = &&mut [0]; -LL + let &[ref x] = &&mut [0]; - | - -error[E0308]: mismatched types - --> $DIR/pattern-errors.rs:87:11 - | -LL | let &[&mut ref x] = &mut &mut [0]; - | ^^^^^^^^^^ ------------- this expression has type `&mut &mut [{integer}; 1]` - | | - | expected integer, found `&mut _` - | - = note: expected type `{integer}` - found mutable reference `&mut _` -note: to declare a mutable binding use: `mut x` - --> $DIR/pattern-errors.rs:87:11 - | -LL | let &[&mut ref x] = &mut &mut [0]; - | ^^^^^^^^^^ -help: consider removing `&mut` from the pattern - | -LL - let &[&mut ref x] = &mut &mut [0]; -LL + let &[ref x] = &mut &mut [0]; - | - -error[E0308]: mismatched types - --> $DIR/pattern-errors.rs:94:11 + | ^^^^^ | -LL | let &[&mut mut x] = &&mut [0]; - | ^^^^^^^^^^ --------- this expression has type `&&mut [{integer}; 1]` - | | - | expected integer, found `&mut _` - | - = note: expected type `{integer}` - found mutable reference `&mut _` -note: to declare a mutable binding use: `mut x` - --> $DIR/pattern-errors.rs:94:11 - | -LL | let &[&mut mut x] = &&mut [0]; - | ^^^^^^^^^^ -help: consider removing `&mut` from the pattern - | -LL - let &[&mut mut x] = &&mut [0]; -LL + let &[mut x] = &&mut [0]; - | - -error[E0308]: mismatched types - --> $DIR/pattern-errors.rs:101:11 - | -LL | let &[&mut mut x] = &mut &mut [0]; - | ^^^^^^^^^^ ------------- this expression has type `&mut &mut [{integer}; 1]` - | | - | expected integer, found `&mut _` - | - = note: expected type `{integer}` - found mutable reference `&mut _` -note: to declare a mutable binding use: `mut x` - --> $DIR/pattern-errors.rs:101:11 - | -LL | let &[&mut mut x] = &mut &mut [0]; - | ^^^^^^^^^^ -help: consider removing `&mut` from the pattern + = note: cannot match inherited `&` with `&mut` pattern +help: replace this `&mut` pattern with `&` | -LL - let &[&mut mut x] = &mut &mut [0]; -LL + let &[mut x] = &mut &mut [0]; +LL - if let Some(&mut Some(x)) = &Some(Some(0)) { +LL + if let Some(&Some(x)) = &Some(Some(0)) { | error[E0308]: mismatched types - --> $DIR/pattern-errors.rs:122:11 + --> $DIR/pattern-errors.rs:114:11 | LL | let [&&mut x] = &[&mut 0]; | ^^^^^^ --------- this expression has type `&[&mut {integer}; 1]` @@ -222,7 +87,7 @@ LL + let [&x] = &[&mut 0]; | error[E0308]: mismatched types - --> $DIR/pattern-errors.rs:129:11 + --> $DIR/pattern-errors.rs:121:11 | LL | let [&&mut x] = &mut [&mut 0]; | ^^^^^^ ------------- this expression has type `&mut [&mut {integer}; 1]` @@ -238,7 +103,7 @@ LL + let [&x] = &mut [&mut 0]; | error[E0308]: mismatched types - --> $DIR/pattern-errors.rs:136:11 + --> $DIR/pattern-errors.rs:128:11 | LL | let [&&mut ref x] = &[&mut 0]; | ^^^^^^^^^^ --------- this expression has type `&[&mut {integer}; 1]` @@ -254,7 +119,7 @@ LL + let [&ref x] = &[&mut 0]; | error[E0308]: mismatched types - --> $DIR/pattern-errors.rs:143:11 + --> $DIR/pattern-errors.rs:135:11 | LL | let [&&mut ref x] = &mut [&mut 0]; | ^^^^^^^^^^ ------------- this expression has type `&mut [&mut {integer}; 1]` @@ -270,7 +135,7 @@ LL + let [&ref x] = &mut [&mut 0]; | error[E0308]: mismatched types - --> $DIR/pattern-errors.rs:150:11 + --> $DIR/pattern-errors.rs:142:11 | LL | let [&&mut mut x] = &[&mut 0]; | ^^^^^^^^^^ --------- this expression has type `&[&mut {integer}; 1]` @@ -286,7 +151,7 @@ LL + let [&mut x] = &[&mut 0]; | error[E0308]: mismatched types - --> $DIR/pattern-errors.rs:157:11 + --> $DIR/pattern-errors.rs:149:11 | LL | let [&&mut mut x] = &mut [&mut 0]; | ^^^^^^^^^^ ------------- this expression has type `&mut [&mut {integer}; 1]` @@ -302,7 +167,7 @@ LL + let [&mut x] = &mut [&mut 0]; | error[E0308]: mismatched types - --> $DIR/pattern-errors.rs:172:15 + --> $DIR/pattern-errors.rs:164:15 | LL | let [&mut &x] = &[&mut 0]; | ^^ --------- this expression has type `&[&mut {integer}; 1]` @@ -318,7 +183,7 @@ LL + let [&mut x] = &[&mut 0]; | error[E0308]: mismatched types - --> $DIR/pattern-errors.rs:178:15 + --> $DIR/pattern-errors.rs:170:15 | LL | let [&mut &ref x] = &[&mut 0]; | ^^^^^^ --------- this expression has type `&[&mut {integer}; 1]` @@ -334,7 +199,7 @@ LL + let [&mut ref x] = &[&mut 0]; | error[E0308]: mismatched types - --> $DIR/pattern-errors.rs:184:15 + --> $DIR/pattern-errors.rs:176:15 | LL | let [&mut &(mut x)] = &[&mut 0]; | ^^^^^^^^ --------- this expression has type `&[&mut {integer}; 1]` @@ -349,6 +214,6 @@ LL - let [&mut &(mut x)] = &[&mut 0]; LL + let [&mut mut x)] = &[&mut 0]; | -error: aborting due to 21 previous errors +error: aborting due to 14 previous errors For more information about this error, try `rustc --explain E0308`. diff --git a/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/pattern-errors.classic2024.stderr b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/pattern-errors.classic2024.stderr index b98cfe337efee..90510d23e6614 100644 --- a/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/pattern-errors.classic2024.stderr +++ b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/pattern-errors.classic2024.stderr @@ -64,7 +64,7 @@ LL + if let Some(&Some(&_)) = &mut Some(&Some(0)) { | error[E0308]: mismatched types - --> $DIR/pattern-errors.rs:57:17 + --> $DIR/pattern-errors.rs:56:17 | LL | if let Some(&mut Some(x)) = &Some(Some(0)) { | ^^^^^ @@ -77,7 +77,7 @@ LL + if let Some(&Some(x)) = &Some(Some(0)) { | error[E0308]: mismatched types - --> $DIR/pattern-errors.rs:166:10 + --> $DIR/pattern-errors.rs:158:10 | LL | let [&mut x] = &[&mut 0]; | ^^^^^ @@ -90,7 +90,7 @@ LL + let [&x] = &[&mut 0]; | error[E0308]: mismatched types - --> $DIR/pattern-errors.rs:172:10 + --> $DIR/pattern-errors.rs:164:10 | LL | let [&mut &x] = &[&mut 0]; | ^^^^^ @@ -103,7 +103,7 @@ LL + let [&&x] = &[&mut 0]; | error[E0308]: mismatched types - --> $DIR/pattern-errors.rs:178:10 + --> $DIR/pattern-errors.rs:170:10 | LL | let [&mut &ref x] = &[&mut 0]; | ^^^^^ @@ -116,7 +116,7 @@ LL + let [&&ref x] = &[&mut 0]; | error[E0308]: mismatched types - --> $DIR/pattern-errors.rs:184:10 + --> $DIR/pattern-errors.rs:176:10 | LL | let [&mut &(mut x)] = &[&mut 0]; | ^^^^^ diff --git a/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/pattern-errors.rs b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/pattern-errors.rs index 4173b1819cba5..5e677445644a6 100644 --- a/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/pattern-errors.rs +++ b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/pattern-errors.rs @@ -48,62 +48,54 @@ pub fn main() { //[classic2024,structural2024]~| cannot match inherited `&` with `&mut` pattern } if let Some(&Some(Some(&mut x))) = &Some(Some(&mut Some(0))) { - //[stable2021,classic2021,structural2021,structural2024]~^ ERROR: mismatched types + //[stable2021,structural2021,structural2024]~^ ERROR: mismatched types //[stable2021]~| expected `Option<&mut Option<{integer}>>`, found `&_` - //[structural2024]~| cannot match inherited `&` with `&mut` pattern - // TODO: the error on `structural2021` should be an inherited ref mutability mismatch too - #[cfg(classic2024)] let _: u32 = x; // TODO: this should hold on `classic2021` too + //[structural2021,structural2024]~| cannot match inherited `&` with `&mut` pattern + #[cfg(any(classic2021, classic2024))] let _: u32 = x; } if let Some(&mut Some(x)) = &Some(Some(0)) { //~^ ERROR: mismatched types - //[stable2021,classic2021,structural2021]~| expected `Option<{integer}>`, found `&mut _` - //[classic2024,structural2024]~| cannot match inherited `&` with `&mut` pattern - // TODO: the error on `classic2021` and `structural2021` should be the mutability mismatch + //[stable2021]~| expected `Option<{integer}>`, found `&mut _` + //[classic2021,structural2021,classic2024,structural2024]~| cannot match inherited `&` with `&mut` pattern } } fn structural_errors_0() { let &[&mut x] = &&mut [0]; - //[stable2021,classic2021,structural2021,structural2024]~^ ERROR: mismatched types + //[stable2021,structural2021,structural2024]~^ ERROR: mismatched types //[stable2021]~| expected integer, found `&mut _` - //[structural2024]~| cannot match inherited `&` with `&mut` pattern - // TODO: the error on `structural2021` should be an inherited ref mutability mismatch too - #[cfg(classic2024)] let _: u32 = x; // TODO: this should hold for `classic2021` too + //[structural2021,structural2024]~| cannot match inherited `&` with `&mut` pattern + #[cfg(any(classic2021, classic2024))] let _: u32 = x; let &[&mut x] = &mut &mut [0]; - //[stable2021,classic2021,structural2021,structural2024]~^ ERROR: mismatched types + //[stable2021,structural2021,structural2024]~^ ERROR: mismatched types //[stable2021]~| types differ in mutability - //[structural2024]~| cannot match inherited `&` with `&mut` pattern - // TODO: the error on `structural2021` should be an inherited ref mutability mismatch too - #[cfg(classic2024)] let _: u32 = x; // TODO: this should hold for `classic2021` too + //[structural2021,structural2024]~| cannot match inherited `&` with `&mut` pattern + #[cfg(any(classic2021, classic2024))] let _: u32 = x; let &[&mut ref x] = &&mut [0]; - //[stable2021,classic2021,structural2021,structural2024]~^ ERROR: mismatched types + //[stable2021,structural2021,structural2024]~^ ERROR: mismatched types //[stable2021]~| expected integer, found `&mut _` - //[structural2024]~| cannot match inherited `&` with `&mut` pattern - // TODO: the error on `structural2021` should be an inherited ref mutability mismatch too - #[cfg(classic2024)] let _: &u32 = x; // TODO: this should hold for `classic2021` too + //[structural2021,structural2024]~| cannot match inherited `&` with `&mut` pattern + #[cfg(any(classic2021, classic2024))] let _: &u32 = x; let &[&mut ref x] = &mut &mut [0]; - //[stable2021,classic2021,structural2021,structural2024]~^ ERROR: mismatched types + //[stable2021,structural2021,structural2024]~^ ERROR: mismatched types //[stable2021]~| types differ in mutability - //[structural2024]~| cannot match inherited `&` with `&mut` pattern - // TODO: the error on `structural2021` should be an inherited ref mutability mismatch too - #[cfg(classic2024)] let _: &u32 = x; // TODO: this should hold for `classic2021` too + //[structural2021,structural2024]~| cannot match inherited `&` with `&mut` pattern + #[cfg(any(classic2021, classic2024))] let _: &u32 = x; let &[&mut mut x] = &&mut [0]; - //[stable2021,classic2021,structural2021,structural2024]~^ ERROR: mismatched types + //[stable2021,structural2021,structural2024]~^ ERROR: mismatched types //[stable2021]~| expected integer, found `&mut _` - //[structural2024]~| cannot match inherited `&` with `&mut` pattern - // TODO: the error on `structural2021` should be an inherited ref mutability mismatch too - #[cfg(classic2024)] let _: u32 = x; // TODO: this should hold for `classic2021` too + //[structural2021,structural2024]~| cannot match inherited `&` with `&mut` pattern + #[cfg(any(classic2021, classic2024))] let _: u32 = x; let &[&mut mut x] = &mut &mut [0]; - //[stable2021,classic2021,structural2021,structural2024]~^ ERROR: mismatched types + //[stable2021,structural2021,structural2024]~^ ERROR: mismatched types //[stable2021]~| types differ in mutability - //[structural2024]~| cannot match inherited `&` with `&mut` pattern - // TODO: the error on `structural2021` should be an inherited ref mutability mismatch too - #[cfg(classic2024)] let _: u32 = x; // TODO: this should hold for `classic2021` too + //[structural2021,structural2024]~| cannot match inherited `&` with `&mut` pattern + #[cfg(any(classic2021, classic2024))] let _: u32 = x; } fn structural_errors_1() { diff --git a/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/pattern-errors.stable2021.stderr b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/pattern-errors.stable2021.stderr index ad9541b71a028..76e6d2f562a27 100644 --- a/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/pattern-errors.stable2021.stderr +++ b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/pattern-errors.stable2021.stderr @@ -59,7 +59,7 @@ LL | if let Some(&Some(Some(&mut x))) = &Some(Some(&mut Some(0))) { found reference `&_` error[E0308]: mismatched types - --> $DIR/pattern-errors.rs:57:17 + --> $DIR/pattern-errors.rs:56:17 | LL | if let Some(&mut Some(x)) = &Some(Some(0)) { | ^^^^^^^^^^^^ -------------- this expression has type `&Option>` @@ -70,7 +70,7 @@ LL | if let Some(&mut Some(x)) = &Some(Some(0)) { found mutable reference `&mut _` error[E0308]: mismatched types - --> $DIR/pattern-errors.rs:66:11 + --> $DIR/pattern-errors.rs:64:11 | LL | let &[&mut x] = &&mut [0]; | ^^^^^^ --------- this expression has type `&&mut [{integer}; 1]` @@ -80,7 +80,7 @@ LL | let &[&mut x] = &&mut [0]; = note: expected type `{integer}` found mutable reference `&mut _` note: to declare a mutable binding use: `mut x` - --> $DIR/pattern-errors.rs:66:11 + --> $DIR/pattern-errors.rs:64:11 | LL | let &[&mut x] = &&mut [0]; | ^^^^^^ @@ -91,7 +91,7 @@ LL + let &[x] = &&mut [0]; | error[E0308]: mismatched types - --> $DIR/pattern-errors.rs:73:9 + --> $DIR/pattern-errors.rs:70:9 | LL | let &[&mut x] = &mut &mut [0]; | ^^^^^^^^^ ------------- this expression has type `&mut &mut [{integer}; 1]` @@ -102,7 +102,7 @@ LL | let &[&mut x] = &mut &mut [0]; found reference `&_` error[E0308]: mismatched types - --> $DIR/pattern-errors.rs:80:11 + --> $DIR/pattern-errors.rs:76:11 | LL | let &[&mut ref x] = &&mut [0]; | ^^^^^^^^^^ --------- this expression has type `&&mut [{integer}; 1]` @@ -112,7 +112,7 @@ LL | let &[&mut ref x] = &&mut [0]; = note: expected type `{integer}` found mutable reference `&mut _` note: to declare a mutable binding use: `mut x` - --> $DIR/pattern-errors.rs:80:11 + --> $DIR/pattern-errors.rs:76:11 | LL | let &[&mut ref x] = &&mut [0]; | ^^^^^^^^^^ @@ -123,7 +123,7 @@ LL + let &[ref x] = &&mut [0]; | error[E0308]: mismatched types - --> $DIR/pattern-errors.rs:87:9 + --> $DIR/pattern-errors.rs:82:9 | LL | let &[&mut ref x] = &mut &mut [0]; | ^^^^^^^^^^^^^ ------------- this expression has type `&mut &mut [{integer}; 1]` @@ -134,7 +134,7 @@ LL | let &[&mut ref x] = &mut &mut [0]; found reference `&_` error[E0308]: mismatched types - --> $DIR/pattern-errors.rs:94:11 + --> $DIR/pattern-errors.rs:88:11 | LL | let &[&mut mut x] = &&mut [0]; | ^^^^^^^^^^ --------- this expression has type `&&mut [{integer}; 1]` @@ -144,7 +144,7 @@ LL | let &[&mut mut x] = &&mut [0]; = note: expected type `{integer}` found mutable reference `&mut _` note: to declare a mutable binding use: `mut x` - --> $DIR/pattern-errors.rs:94:11 + --> $DIR/pattern-errors.rs:88:11 | LL | let &[&mut mut x] = &&mut [0]; | ^^^^^^^^^^ @@ -155,7 +155,7 @@ LL + let &[mut x] = &&mut [0]; | error[E0308]: mismatched types - --> $DIR/pattern-errors.rs:101:9 + --> $DIR/pattern-errors.rs:94:9 | LL | let &[&mut mut x] = &mut &mut [0]; | ^^^^^^^^^^^^^ ------------- this expression has type `&mut &mut [{integer}; 1]` @@ -166,7 +166,7 @@ LL | let &[&mut mut x] = &mut &mut [0]; found reference `&_` error[E0308]: mismatched types - --> $DIR/pattern-errors.rs:122:10 + --> $DIR/pattern-errors.rs:114:10 | LL | let [&&mut x] = &[&mut 0]; | ^^^^^^^ --------- this expression has type `&[&mut {integer}; 1]` @@ -177,7 +177,7 @@ LL | let [&&mut x] = &[&mut 0]; found reference `&_` error[E0308]: mismatched types - --> $DIR/pattern-errors.rs:129:10 + --> $DIR/pattern-errors.rs:121:10 | LL | let [&&mut x] = &mut [&mut 0]; | ^^^^^^^ ------------- this expression has type `&mut [&mut {integer}; 1]` @@ -188,7 +188,7 @@ LL | let [&&mut x] = &mut [&mut 0]; found reference `&_` error[E0308]: mismatched types - --> $DIR/pattern-errors.rs:136:10 + --> $DIR/pattern-errors.rs:128:10 | LL | let [&&mut ref x] = &[&mut 0]; | ^^^^^^^^^^^ --------- this expression has type `&[&mut {integer}; 1]` @@ -199,7 +199,7 @@ LL | let [&&mut ref x] = &[&mut 0]; found reference `&_` error[E0308]: mismatched types - --> $DIR/pattern-errors.rs:143:10 + --> $DIR/pattern-errors.rs:135:10 | LL | let [&&mut ref x] = &mut [&mut 0]; | ^^^^^^^^^^^ ------------- this expression has type `&mut [&mut {integer}; 1]` @@ -210,7 +210,7 @@ LL | let [&&mut ref x] = &mut [&mut 0]; found reference `&_` error[E0308]: mismatched types - --> $DIR/pattern-errors.rs:150:10 + --> $DIR/pattern-errors.rs:142:10 | LL | let [&&mut mut x] = &[&mut 0]; | ^^^^^^^^^^^ --------- this expression has type `&[&mut {integer}; 1]` @@ -221,7 +221,7 @@ LL | let [&&mut mut x] = &[&mut 0]; found reference `&_` error[E0308]: mismatched types - --> $DIR/pattern-errors.rs:157:10 + --> $DIR/pattern-errors.rs:149:10 | LL | let [&&mut mut x] = &mut [&mut 0]; | ^^^^^^^^^^^ ------------- this expression has type `&mut [&mut {integer}; 1]` @@ -232,7 +232,7 @@ LL | let [&&mut mut x] = &mut [&mut 0]; found reference `&_` error[E0308]: mismatched types - --> $DIR/pattern-errors.rs:172:15 + --> $DIR/pattern-errors.rs:164:15 | LL | let [&mut &x] = &[&mut 0]; | ^^ --------- this expression has type `&[&mut {integer}; 1]` @@ -248,7 +248,7 @@ LL + let [&mut x] = &[&mut 0]; | error[E0308]: mismatched types - --> $DIR/pattern-errors.rs:178:15 + --> $DIR/pattern-errors.rs:170:15 | LL | let [&mut &ref x] = &[&mut 0]; | ^^^^^^ --------- this expression has type `&[&mut {integer}; 1]` @@ -264,7 +264,7 @@ LL + let [&mut ref x] = &[&mut 0]; | error[E0308]: mismatched types - --> $DIR/pattern-errors.rs:184:15 + --> $DIR/pattern-errors.rs:176:15 | LL | let [&mut &(mut x)] = &[&mut 0]; | ^^^^^^^^ --------- this expression has type `&[&mut {integer}; 1]` diff --git a/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/pattern-errors.structural2021.stderr b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/pattern-errors.structural2021.stderr index e039884cdb14b..1ca6bff3f38c3 100644 --- a/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/pattern-errors.structural2021.stderr +++ b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/pattern-errors.structural2021.stderr @@ -58,155 +58,111 @@ LL | if let Some(&Some(&mut _)) = &mut Some(&Some(0)) { found mutable reference `&mut _` error[E0308]: mismatched types - --> $DIR/pattern-errors.rs:50:17 + --> $DIR/pattern-errors.rs:50:28 | LL | if let Some(&Some(Some(&mut x))) = &Some(Some(&mut Some(0))) { - | ^^^^^^^^^^^^^^^^^^^ ------------------------- this expression has type `&Option>>` - | | - | expected `Option<&mut Option<{integer}>>`, found `&_` + | ^^^^^ + | + = note: cannot match inherited `&` with `&mut` pattern +help: replace this `&mut` pattern with `&` + | +LL - if let Some(&Some(Some(&mut x))) = &Some(Some(&mut Some(0))) { +LL + if let Some(&Some(Some(&x))) = &Some(Some(&mut Some(0))) { | - = note: expected enum `Option<&mut Option<{integer}>>` - found reference `&_` error[E0308]: mismatched types - --> $DIR/pattern-errors.rs:57:17 + --> $DIR/pattern-errors.rs:56:17 | LL | if let Some(&mut Some(x)) = &Some(Some(0)) { - | ^^^^^^^^^^^^ -------------- this expression has type `&Option>` - | | - | expected `Option<{integer}>`, found `&mut _` + | ^^^^^ + | + = note: cannot match inherited `&` with `&mut` pattern +help: replace this `&mut` pattern with `&` + | +LL - if let Some(&mut Some(x)) = &Some(Some(0)) { +LL + if let Some(&Some(x)) = &Some(Some(0)) { | - = note: expected enum `Option<{integer}>` - found mutable reference `&mut _` error[E0308]: mismatched types - --> $DIR/pattern-errors.rs:66:11 + --> $DIR/pattern-errors.rs:64:11 | LL | let &[&mut x] = &&mut [0]; - | ^^^^^^ --------- this expression has type `&&mut [{integer}; 1]` - | | - | expected integer, found `&mut _` + | ^^^^^ | - = note: expected type `{integer}` - found mutable reference `&mut _` -note: to declare a mutable binding use: `mut x` - --> $DIR/pattern-errors.rs:66:11 - | -LL | let &[&mut x] = &&mut [0]; - | ^^^^^^ -help: consider removing `&mut` from the pattern + = note: cannot match inherited `&` with `&mut` pattern +help: replace this `&mut` pattern with `&` | LL - let &[&mut x] = &&mut [0]; -LL + let &[x] = &&mut [0]; +LL + let &[&x] = &&mut [0]; | error[E0308]: mismatched types - --> $DIR/pattern-errors.rs:73:11 + --> $DIR/pattern-errors.rs:70:11 | LL | let &[&mut x] = &mut &mut [0]; - | ^^^^^^ ------------- this expression has type `&mut &mut [{integer}; 1]` - | | - | expected integer, found `&mut _` + | ^^^^^ | - = note: expected type `{integer}` - found mutable reference `&mut _` -note: to declare a mutable binding use: `mut x` - --> $DIR/pattern-errors.rs:73:11 - | -LL | let &[&mut x] = &mut &mut [0]; - | ^^^^^^ -help: consider removing `&mut` from the pattern + = note: cannot match inherited `&` with `&mut` pattern +help: replace this `&mut` pattern with `&` | LL - let &[&mut x] = &mut &mut [0]; -LL + let &[x] = &mut &mut [0]; +LL + let &[&x] = &mut &mut [0]; | error[E0308]: mismatched types - --> $DIR/pattern-errors.rs:80:11 + --> $DIR/pattern-errors.rs:76:11 | LL | let &[&mut ref x] = &&mut [0]; - | ^^^^^^^^^^ --------- this expression has type `&&mut [{integer}; 1]` - | | - | expected integer, found `&mut _` + | ^^^^^ | - = note: expected type `{integer}` - found mutable reference `&mut _` -note: to declare a mutable binding use: `mut x` - --> $DIR/pattern-errors.rs:80:11 - | -LL | let &[&mut ref x] = &&mut [0]; - | ^^^^^^^^^^ -help: consider removing `&mut` from the pattern + = note: cannot match inherited `&` with `&mut` pattern +help: replace this `&mut` pattern with `&` | LL - let &[&mut ref x] = &&mut [0]; -LL + let &[ref x] = &&mut [0]; +LL + let &[&ref x] = &&mut [0]; | error[E0308]: mismatched types - --> $DIR/pattern-errors.rs:87:11 + --> $DIR/pattern-errors.rs:82:11 | LL | let &[&mut ref x] = &mut &mut [0]; - | ^^^^^^^^^^ ------------- this expression has type `&mut &mut [{integer}; 1]` - | | - | expected integer, found `&mut _` + | ^^^^^ | - = note: expected type `{integer}` - found mutable reference `&mut _` -note: to declare a mutable binding use: `mut x` - --> $DIR/pattern-errors.rs:87:11 - | -LL | let &[&mut ref x] = &mut &mut [0]; - | ^^^^^^^^^^ -help: consider removing `&mut` from the pattern + = note: cannot match inherited `&` with `&mut` pattern +help: replace this `&mut` pattern with `&` | LL - let &[&mut ref x] = &mut &mut [0]; -LL + let &[ref x] = &mut &mut [0]; +LL + let &[&ref x] = &mut &mut [0]; | error[E0308]: mismatched types - --> $DIR/pattern-errors.rs:94:11 + --> $DIR/pattern-errors.rs:88:11 | LL | let &[&mut mut x] = &&mut [0]; - | ^^^^^^^^^^ --------- this expression has type `&&mut [{integer}; 1]` - | | - | expected integer, found `&mut _` + | ^^^^^ | - = note: expected type `{integer}` - found mutable reference `&mut _` -note: to declare a mutable binding use: `mut x` - --> $DIR/pattern-errors.rs:94:11 - | -LL | let &[&mut mut x] = &&mut [0]; - | ^^^^^^^^^^ -help: consider removing `&mut` from the pattern + = note: cannot match inherited `&` with `&mut` pattern +help: replace this `&mut` pattern with `&` | LL - let &[&mut mut x] = &&mut [0]; -LL + let &[mut x] = &&mut [0]; +LL + let &[&mut x] = &&mut [0]; | error[E0308]: mismatched types - --> $DIR/pattern-errors.rs:101:11 + --> $DIR/pattern-errors.rs:94:11 | LL | let &[&mut mut x] = &mut &mut [0]; - | ^^^^^^^^^^ ------------- this expression has type `&mut &mut [{integer}; 1]` - | | - | expected integer, found `&mut _` + | ^^^^^ | - = note: expected type `{integer}` - found mutable reference `&mut _` -note: to declare a mutable binding use: `mut x` - --> $DIR/pattern-errors.rs:101:11 - | -LL | let &[&mut mut x] = &mut &mut [0]; - | ^^^^^^^^^^ -help: consider removing `&mut` from the pattern + = note: cannot match inherited `&` with `&mut` pattern +help: replace this `&mut` pattern with `&` | LL - let &[&mut mut x] = &mut &mut [0]; -LL + let &[mut x] = &mut &mut [0]; +LL + let &[&mut x] = &mut &mut [0]; | error[E0308]: mismatched types - --> $DIR/pattern-errors.rs:122:11 + --> $DIR/pattern-errors.rs:114:11 | LL | let [&&mut x] = &[&mut 0]; | ^^^^^^ --------- this expression has type `&[&mut {integer}; 1]` @@ -222,7 +178,7 @@ LL + let [&x] = &[&mut 0]; | error[E0308]: mismatched types - --> $DIR/pattern-errors.rs:129:11 + --> $DIR/pattern-errors.rs:121:11 | LL | let [&&mut x] = &mut [&mut 0]; | ^^^^^^ ------------- this expression has type `&mut [&mut {integer}; 1]` @@ -238,7 +194,7 @@ LL + let [&x] = &mut [&mut 0]; | error[E0308]: mismatched types - --> $DIR/pattern-errors.rs:136:11 + --> $DIR/pattern-errors.rs:128:11 | LL | let [&&mut ref x] = &[&mut 0]; | ^^^^^^^^^^ --------- this expression has type `&[&mut {integer}; 1]` @@ -254,7 +210,7 @@ LL + let [&ref x] = &[&mut 0]; | error[E0308]: mismatched types - --> $DIR/pattern-errors.rs:143:11 + --> $DIR/pattern-errors.rs:135:11 | LL | let [&&mut ref x] = &mut [&mut 0]; | ^^^^^^^^^^ ------------- this expression has type `&mut [&mut {integer}; 1]` @@ -270,7 +226,7 @@ LL + let [&ref x] = &mut [&mut 0]; | error[E0308]: mismatched types - --> $DIR/pattern-errors.rs:150:11 + --> $DIR/pattern-errors.rs:142:11 | LL | let [&&mut mut x] = &[&mut 0]; | ^^^^^^^^^^ --------- this expression has type `&[&mut {integer}; 1]` @@ -286,7 +242,7 @@ LL + let [&mut x] = &[&mut 0]; | error[E0308]: mismatched types - --> $DIR/pattern-errors.rs:157:11 + --> $DIR/pattern-errors.rs:149:11 | LL | let [&&mut mut x] = &mut [&mut 0]; | ^^^^^^^^^^ ------------- this expression has type `&mut [&mut {integer}; 1]` @@ -302,7 +258,7 @@ LL + let [&mut x] = &mut [&mut 0]; | error[E0308]: mismatched types - --> $DIR/pattern-errors.rs:172:15 + --> $DIR/pattern-errors.rs:164:15 | LL | let [&mut &x] = &[&mut 0]; | ^^ --------- this expression has type `&[&mut {integer}; 1]` @@ -318,7 +274,7 @@ LL + let [&mut x] = &[&mut 0]; | error[E0308]: mismatched types - --> $DIR/pattern-errors.rs:178:15 + --> $DIR/pattern-errors.rs:170:15 | LL | let [&mut &ref x] = &[&mut 0]; | ^^^^^^ --------- this expression has type `&[&mut {integer}; 1]` @@ -334,7 +290,7 @@ LL + let [&mut ref x] = &[&mut 0]; | error[E0308]: mismatched types - --> $DIR/pattern-errors.rs:184:15 + --> $DIR/pattern-errors.rs:176:15 | LL | let [&mut &(mut x)] = &[&mut 0]; | ^^^^^^^^ --------- this expression has type `&[&mut {integer}; 1]` diff --git a/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/pattern-errors.structural2024.stderr b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/pattern-errors.structural2024.stderr index 950d9f8b35110..3658893df9cc1 100644 --- a/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/pattern-errors.structural2024.stderr +++ b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/pattern-errors.structural2024.stderr @@ -51,7 +51,7 @@ LL + if let Some(&Some(Some(&x))) = &Some(Some(&mut Some(0))) { | error[E0308]: mismatched types - --> $DIR/pattern-errors.rs:57:17 + --> $DIR/pattern-errors.rs:56:17 | LL | if let Some(&mut Some(x)) = &Some(Some(0)) { | ^^^^^ @@ -64,7 +64,7 @@ LL + if let Some(&Some(x)) = &Some(Some(0)) { | error[E0308]: mismatched types - --> $DIR/pattern-errors.rs:66:11 + --> $DIR/pattern-errors.rs:64:11 | LL | let &[&mut x] = &&mut [0]; | ^^^^^ @@ -77,7 +77,7 @@ LL + let &[&x] = &&mut [0]; | error[E0308]: mismatched types - --> $DIR/pattern-errors.rs:73:11 + --> $DIR/pattern-errors.rs:70:11 | LL | let &[&mut x] = &mut &mut [0]; | ^^^^^ @@ -90,7 +90,7 @@ LL + let &[&x] = &mut &mut [0]; | error[E0308]: mismatched types - --> $DIR/pattern-errors.rs:80:11 + --> $DIR/pattern-errors.rs:76:11 | LL | let &[&mut ref x] = &&mut [0]; | ^^^^^ @@ -103,7 +103,7 @@ LL + let &[&ref x] = &&mut [0]; | error[E0308]: mismatched types - --> $DIR/pattern-errors.rs:87:11 + --> $DIR/pattern-errors.rs:82:11 | LL | let &[&mut ref x] = &mut &mut [0]; | ^^^^^ @@ -116,7 +116,7 @@ LL + let &[&ref x] = &mut &mut [0]; | error[E0308]: mismatched types - --> $DIR/pattern-errors.rs:94:11 + --> $DIR/pattern-errors.rs:88:11 | LL | let &[&mut mut x] = &&mut [0]; | ^^^^^ @@ -129,7 +129,7 @@ LL + let &[&mut x] = &&mut [0]; | error[E0308]: mismatched types - --> $DIR/pattern-errors.rs:101:11 + --> $DIR/pattern-errors.rs:94:11 | LL | let &[&mut mut x] = &mut &mut [0]; | ^^^^^ @@ -142,7 +142,7 @@ LL + let &[&mut x] = &mut &mut [0]; | error[E0658]: binding cannot be both mutable and by-reference - --> $DIR/pattern-errors.rs:110:12 + --> $DIR/pattern-errors.rs:102:12 | LL | let [&(mut x)] = &[&0]; | ^^^^ @@ -152,7 +152,7 @@ LL | let [&(mut x)] = &[&0]; = note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date error[E0658]: binding cannot be both mutable and by-reference - --> $DIR/pattern-errors.rs:115:12 + --> $DIR/pattern-errors.rs:107:12 | LL | let [&(mut x)] = &mut [&0]; | ^^^^ @@ -162,7 +162,7 @@ LL | let [&(mut x)] = &mut [&0]; = note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date error[E0308]: mismatched types - --> $DIR/pattern-errors.rs:122:11 + --> $DIR/pattern-errors.rs:114:11 | LL | let [&&mut x] = &[&mut 0]; | ^^^^^ @@ -175,7 +175,7 @@ LL + let [&&x] = &[&mut 0]; | error[E0308]: mismatched types - --> $DIR/pattern-errors.rs:129:11 + --> $DIR/pattern-errors.rs:121:11 | LL | let [&&mut x] = &mut [&mut 0]; | ^^^^^ @@ -188,7 +188,7 @@ LL + let [&&x] = &mut [&mut 0]; | error[E0308]: mismatched types - --> $DIR/pattern-errors.rs:136:11 + --> $DIR/pattern-errors.rs:128:11 | LL | let [&&mut ref x] = &[&mut 0]; | ^^^^^ @@ -201,7 +201,7 @@ LL + let [&&ref x] = &[&mut 0]; | error[E0308]: mismatched types - --> $DIR/pattern-errors.rs:143:11 + --> $DIR/pattern-errors.rs:135:11 | LL | let [&&mut ref x] = &mut [&mut 0]; | ^^^^^ @@ -214,7 +214,7 @@ LL + let [&&ref x] = &mut [&mut 0]; | error[E0308]: mismatched types - --> $DIR/pattern-errors.rs:150:11 + --> $DIR/pattern-errors.rs:142:11 | LL | let [&&mut mut x] = &[&mut 0]; | ^^^^^ @@ -227,7 +227,7 @@ LL + let [&&mut x] = &[&mut 0]; | error[E0308]: mismatched types - --> $DIR/pattern-errors.rs:157:11 + --> $DIR/pattern-errors.rs:149:11 | LL | let [&&mut mut x] = &mut [&mut 0]; | ^^^^^ diff --git a/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref-mut-inside-shared-ref-pat.classic2021.stderr b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref-mut-inside-shared-ref-pat.classic2021.stderr index 379ba4a11c554..8127ca92e025e 100644 --- a/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref-mut-inside-shared-ref-pat.classic2021.stderr +++ b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref-mut-inside-shared-ref-pat.classic2021.stderr @@ -1,18 +1,7 @@ -error[E0308]: mismatched types - --> $DIR/ref-mut-inside-shared-ref-pat.rs:16:17 - | -LL | if let Some(&Some(ref mut x)) = &mut Some(Some(0)) { // TODO: `classic2021` and `structural2021` shouldn't have mismatched types - | ^^^^^^^^^^^^^^^^ ------------------ this expression has type `&mut Option>` - | | - | expected `Option<{integer}>`, found `&_` - | - = note: expected enum `Option<{integer}>` - found reference `&_` - error[E0596]: cannot borrow as mutable inside an `&` pattern --> $DIR/ref-mut-inside-shared-ref-pat.rs:16:31 | -LL | if let Some(&Some(ref mut x)) = &mut Some(Some(0)) { // TODO: `classic2021` and `structural2021` shouldn't have mismatched types +LL | if let Some(&Some(ref mut x)) = &mut Some(Some(0)) { | - ^ | | | help: replace this `&` with `&mut`: `&mut` @@ -57,7 +46,6 @@ LL | let &[x] = &mut &mut [0]; | | | help: replace this `&` with `&mut`: `&mut` -error: aborting due to 7 previous errors +error: aborting due to 6 previous errors -Some errors have detailed explanations: E0308, E0596. -For more information about an error, try `rustc --explain E0308`. +For more information about this error, try `rustc --explain E0596`. diff --git a/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref-mut-inside-shared-ref-pat.classic2024.fixed b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref-mut-inside-shared-ref-pat.classic2024.fixed index 105313a14896d..57de9cb4c10d0 100644 --- a/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref-mut-inside-shared-ref-pat.classic2024.fixed +++ b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref-mut-inside-shared-ref-pat.classic2024.fixed @@ -13,8 +13,8 @@ #![cfg_attr(any(structural2021, structural2024), feature(ref_pat_eat_one_layer_2024_structural))] pub fn main() { - if let Some(&mut Some(ref mut x)) = &mut Some(Some(0)) { // TODO: `classic2021` and `structural2021` shouldn't have mismatched types - //[stable2021,classic2021,structural2021]~^ ERROR: mismatched types + if let Some(&mut Some(ref mut x)) = &mut Some(Some(0)) { + //[stable2021]~^ ERROR: mismatched types //[classic2021,structural2021,classic2024,structural2024]~^^ ERROR: cannot borrow as mutable inside an `&` pattern let _: &mut u8 = x; } diff --git a/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref-mut-inside-shared-ref-pat.classic2024.stderr b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref-mut-inside-shared-ref-pat.classic2024.stderr index 93ddf4d60fd5c..8127ca92e025e 100644 --- a/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref-mut-inside-shared-ref-pat.classic2024.stderr +++ b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref-mut-inside-shared-ref-pat.classic2024.stderr @@ -1,7 +1,7 @@ error[E0596]: cannot borrow as mutable inside an `&` pattern --> $DIR/ref-mut-inside-shared-ref-pat.rs:16:31 | -LL | if let Some(&Some(ref mut x)) = &mut Some(Some(0)) { // TODO: `classic2021` and `structural2021` shouldn't have mismatched types +LL | if let Some(&Some(ref mut x)) = &mut Some(Some(0)) { | - ^ | | | help: replace this `&` with `&mut`: `&mut` diff --git a/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref-mut-inside-shared-ref-pat.rs b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref-mut-inside-shared-ref-pat.rs index 800aca06396cb..277ff90b673ce 100644 --- a/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref-mut-inside-shared-ref-pat.rs +++ b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref-mut-inside-shared-ref-pat.rs @@ -13,8 +13,8 @@ #![cfg_attr(any(structural2021, structural2024), feature(ref_pat_eat_one_layer_2024_structural))] pub fn main() { - if let Some(&Some(ref mut x)) = &mut Some(Some(0)) { // TODO: `classic2021` and `structural2021` shouldn't have mismatched types - //[stable2021,classic2021,structural2021]~^ ERROR: mismatched types + if let Some(&Some(ref mut x)) = &mut Some(Some(0)) { + //[stable2021]~^ ERROR: mismatched types //[classic2021,structural2021,classic2024,structural2024]~^^ ERROR: cannot borrow as mutable inside an `&` pattern let _: &mut u8 = x; } diff --git a/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref-mut-inside-shared-ref-pat.stable2021.stderr b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref-mut-inside-shared-ref-pat.stable2021.stderr index 9a3b896d4f842..5fbaacd7281d2 100644 --- a/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref-mut-inside-shared-ref-pat.stable2021.stderr +++ b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref-mut-inside-shared-ref-pat.stable2021.stderr @@ -1,7 +1,7 @@ error[E0308]: mismatched types --> $DIR/ref-mut-inside-shared-ref-pat.rs:16:17 | -LL | if let Some(&Some(ref mut x)) = &mut Some(Some(0)) { // TODO: `classic2021` and `structural2021` shouldn't have mismatched types +LL | if let Some(&Some(ref mut x)) = &mut Some(Some(0)) { | ^^^^^^^^^^^^^^^^ ------------------ this expression has type `&mut Option>` | | | expected `Option<{integer}>`, found `&_` diff --git a/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref-mut-inside-shared-ref-pat.structural2021.stderr b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref-mut-inside-shared-ref-pat.structural2021.stderr index 1a138494b85a0..e735cfed2492f 100644 --- a/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref-mut-inside-shared-ref-pat.structural2021.stderr +++ b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref-mut-inside-shared-ref-pat.structural2021.stderr @@ -1,18 +1,7 @@ -error[E0308]: mismatched types - --> $DIR/ref-mut-inside-shared-ref-pat.rs:16:17 - | -LL | if let Some(&Some(ref mut x)) = &mut Some(Some(0)) { // TODO: `classic2021` and `structural2021` shouldn't have mismatched types - | ^^^^^^^^^^^^^^^^ ------------------ this expression has type `&mut Option>` - | | - | expected `Option<{integer}>`, found `&_` - | - = note: expected enum `Option<{integer}>` - found reference `&_` - error[E0596]: cannot borrow as mutable inside an `&` pattern --> $DIR/ref-mut-inside-shared-ref-pat.rs:16:31 | -LL | if let Some(&Some(ref mut x)) = &mut Some(Some(0)) { // TODO: `classic2021` and `structural2021` shouldn't have mismatched types +LL | if let Some(&Some(ref mut x)) = &mut Some(Some(0)) { | - ^ | | | help: replace this `&` with `&mut`: `&mut` @@ -49,7 +38,6 @@ LL | let &(ref mut a, ref mut b) = &mut (true, false); | | | help: replace this `&` with `&mut`: `&mut` -error: aborting due to 6 previous errors +error: aborting due to 5 previous errors -Some errors have detailed explanations: E0308, E0596. -For more information about an error, try `rustc --explain E0308`. +For more information about this error, try `rustc --explain E0596`. diff --git a/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref-mut-inside-shared-ref-pat.structural2024.fixed b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref-mut-inside-shared-ref-pat.structural2024.fixed index 78fbffdb27033..bcde4b90f592c 100644 --- a/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref-mut-inside-shared-ref-pat.structural2024.fixed +++ b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref-mut-inside-shared-ref-pat.structural2024.fixed @@ -13,8 +13,8 @@ #![cfg_attr(any(structural2021, structural2024), feature(ref_pat_eat_one_layer_2024_structural))] pub fn main() { - if let Some(&mut Some(ref mut x)) = &mut Some(Some(0)) { // TODO: `classic2021` and `structural2021` shouldn't have mismatched types - //[stable2021,classic2021,structural2021]~^ ERROR: mismatched types + if let Some(&mut Some(ref mut x)) = &mut Some(Some(0)) { + //[stable2021]~^ ERROR: mismatched types //[classic2021,structural2021,classic2024,structural2024]~^^ ERROR: cannot borrow as mutable inside an `&` pattern let _: &mut u8 = x; } diff --git a/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref-mut-inside-shared-ref-pat.structural2024.stderr b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref-mut-inside-shared-ref-pat.structural2024.stderr index 11e39abb860b7..e735cfed2492f 100644 --- a/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref-mut-inside-shared-ref-pat.structural2024.stderr +++ b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref-mut-inside-shared-ref-pat.structural2024.stderr @@ -1,7 +1,7 @@ error[E0596]: cannot borrow as mutable inside an `&` pattern --> $DIR/ref-mut-inside-shared-ref-pat.rs:16:31 | -LL | if let Some(&Some(ref mut x)) = &mut Some(Some(0)) { // TODO: `classic2021` and `structural2021` shouldn't have mismatched types +LL | if let Some(&Some(ref mut x)) = &mut Some(Some(0)) { | - ^ | | | help: replace this `&` with `&mut`: `&mut` diff --git a/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/well-typed-edition-2024.classic2021.stderr b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/well-typed-edition-2024.classic2021.stderr index c9195dbfbf5f8..f784d7e9988db 100644 --- a/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/well-typed-edition-2024.classic2021.stderr +++ b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/well-typed-edition-2024.classic2021.stderr @@ -14,54 +14,6 @@ LL - if let Some(Some(&&x)) = &Some(Some(&0)) { LL + if let Some(Some(&x)) = &Some(Some(&0)) { | -error[E0308]: mismatched types - --> $DIR/well-typed-edition-2024.rs:39:22 - | -LL | if let Some(Some(&x)) = &Some(&Some(0)) { - | ^^ --------------- this expression has type `&Option<&Option<{integer}>>` - | | - | expected integer, found `&_` - | - = note: expected type `{integer}` - found reference `&_` -help: consider removing `&` from the pattern - | -LL - if let Some(Some(&x)) = &Some(&Some(0)) { -LL + if let Some(Some(x)) = &Some(&Some(0)) { - | - -error[E0308]: mismatched types - --> $DIR/well-typed-edition-2024.rs:44:17 - | -LL | if let Some(&Some(x)) = &Some(Some(0)) { - | ^^^^^^^^ -------------- this expression has type `&Option>` - | | - | expected `Option<{integer}>`, found `&_` - | - = note: expected enum `Option<{integer}>` - found reference `&_` - -error[E0308]: mismatched types - --> $DIR/well-typed-edition-2024.rs:49:22 - | -LL | if let Some(Some(&mut x)) = &mut Some(&mut Some(0)) { - | ^^^^^^ ----------------------- this expression has type `&mut Option<&mut Option<{integer}>>` - | | - | expected integer, found `&mut _` - | - = note: expected type `{integer}` - found mutable reference `&mut _` -note: to declare a mutable binding use: `mut x` - --> $DIR/well-typed-edition-2024.rs:49:22 - | -LL | if let Some(Some(&mut x)) = &mut Some(&mut Some(0)) { - | ^^^^^^ -help: consider removing `&mut` from the pattern - | -LL - if let Some(Some(&mut x)) = &mut Some(&mut Some(0)) { -LL + if let Some(Some(x)) = &mut Some(&mut Some(0)) { - | - error[E0308]: mismatched types --> $DIR/well-typed-edition-2024.rs:63:23 | @@ -78,28 +30,6 @@ LL - if let Some(&Some(&x)) = &Some(&Some(0)) { LL + if let Some(&Some(x)) = &Some(&Some(0)) { | -error[E0308]: mismatched types - --> $DIR/well-typed-edition-2024.rs:70:17 - | -LL | if let Some(&Some(Some(&x))) = &Some(Some(&mut Some(0))) { - | ^^^^^^^^^^^^^^^ ------------------------- this expression has type `&Option>>` - | | - | expected `Option<&mut Option<{integer}>>`, found `&_` - | - = note: expected enum `Option<&mut Option<{integer}>>` - found reference `&_` - -error[E0308]: mismatched types - --> $DIR/well-typed-edition-2024.rs:75:17 - | -LL | if let Some(&Some(x)) = &mut Some(Some(0)) { - | ^^^^^^^^ ------------------ this expression has type `&mut Option>` - | | - | expected `Option<{integer}>`, found `&_` - | - = note: expected enum `Option<{integer}>` - found reference `&_` - error[E0308]: mismatched types --> $DIR/well-typed-edition-2024.rs:82:23 | @@ -249,6 +179,6 @@ LL | let [&mut &(mut x)] = &mut [&0]; = note: expected reference `&{integer}` found mutable reference `&mut _` -error: aborting due to 16 previous errors +error: aborting due to 11 previous errors For more information about this error, try `rustc --explain E0308`. diff --git a/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/well-typed-edition-2024.rs b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/well-typed-edition-2024.rs index e61aa5f35c9a2..851011918fe95 100644 --- a/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/well-typed-edition-2024.rs +++ b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/well-typed-edition-2024.rs @@ -37,19 +37,19 @@ pub fn main() { // Tests for eating a lone inherited reference if let Some(Some(&x)) = &Some(&Some(0)) { - //[stable2021,classic2021,structural2021]~^ mismatched types - //[stable2021,classic2021,structural2021]~| expected integer, found `&_` - #[cfg(any(classic2024, structural2024))] let _: u32 = x; // TODO: this should hold on `classic2021` and `structural2021` + //[stable2021]~^ mismatched types + //[stable2021]~| expected integer, found `&_` + #[cfg(any(classic2021, structural2021, classic2024, structural2024))] let _: u32 = x; } if let Some(&Some(x)) = &Some(Some(0)) { - //[stable2021,classic2021,structural2021]~^ mismatched types - //[stable2021,classic2021,structural2021]~| expected `Option<{integer}>`, found `&_` - #[cfg(any(classic2024, structural2024))] let _: u32 = x; // TODO: this should hold on `classic2021` and `structural2021` + //[stable2021]~^ mismatched types + //[stable2021]~| expected `Option<{integer}>`, found `&_` + #[cfg(any(classic2021, structural2021, classic2024, structural2024))] let _: u32 = x; } if let Some(Some(&mut x)) = &mut Some(&mut Some(0)) { - //[stable2021,classic2021,structural2021]~^ mismatched types - //[stable2021,classic2021,structural2021]~| expected integer, found `&mut _` - #[cfg(any(classic2024, structural2024))] let _: u32 = x; // TODO: this should hold on `classic2021` and `structural2021` + //[stable2021]~^ mismatched types + //[stable2021]~| expected integer, found `&mut _` + #[cfg(any(classic2021, structural2021, classic2024, structural2024))] let _: u32 = x; } // Tests for `&` patterns matching real `&mut` reference types @@ -68,14 +68,14 @@ pub fn main() { // Tests for `&` matching a lone inherited possibly-`&mut` reference if let Some(&Some(Some(&x))) = &Some(Some(&mut Some(0))) { - //[stable2021,classic2021,structural2021]~^ mismatched types + //[stable2021]~^ mismatched types //[stable2021]~| expected `Option<&mut Option<{integer}>>`, found `&_` - #[cfg(any(classic2024, structural2024))] let _: u32 = x; // TODO: this should hold on `classic2021` and `structural2021` + #[cfg(any(classic2021, structural2021, classic2024, structural2024))] let _: u32 = x; } if let Some(&Some(x)) = &mut Some(Some(0)) { - //[stable2021,classic2021,structural2021]~^ mismatched types + //[stable2021]~^ mismatched types //[stable2021]~| expected `Option<{integer}>`, found `&_` - #[cfg(any(classic2024, structural2024))] let _: u32 = x; // TODO: this should hold on `classic2021` and `structural2021` + #[cfg(any(classic2021, structural2021, classic2024, structural2024))] let _: u32 = x; } // Tests eating one layer, eating a lone inherited ref, and `&` eating `&mut` (realness varies) diff --git a/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/well-typed-edition-2024.structural2021.stderr b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/well-typed-edition-2024.structural2021.stderr index c9195dbfbf5f8..f784d7e9988db 100644 --- a/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/well-typed-edition-2024.structural2021.stderr +++ b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/well-typed-edition-2024.structural2021.stderr @@ -14,54 +14,6 @@ LL - if let Some(Some(&&x)) = &Some(Some(&0)) { LL + if let Some(Some(&x)) = &Some(Some(&0)) { | -error[E0308]: mismatched types - --> $DIR/well-typed-edition-2024.rs:39:22 - | -LL | if let Some(Some(&x)) = &Some(&Some(0)) { - | ^^ --------------- this expression has type `&Option<&Option<{integer}>>` - | | - | expected integer, found `&_` - | - = note: expected type `{integer}` - found reference `&_` -help: consider removing `&` from the pattern - | -LL - if let Some(Some(&x)) = &Some(&Some(0)) { -LL + if let Some(Some(x)) = &Some(&Some(0)) { - | - -error[E0308]: mismatched types - --> $DIR/well-typed-edition-2024.rs:44:17 - | -LL | if let Some(&Some(x)) = &Some(Some(0)) { - | ^^^^^^^^ -------------- this expression has type `&Option>` - | | - | expected `Option<{integer}>`, found `&_` - | - = note: expected enum `Option<{integer}>` - found reference `&_` - -error[E0308]: mismatched types - --> $DIR/well-typed-edition-2024.rs:49:22 - | -LL | if let Some(Some(&mut x)) = &mut Some(&mut Some(0)) { - | ^^^^^^ ----------------------- this expression has type `&mut Option<&mut Option<{integer}>>` - | | - | expected integer, found `&mut _` - | - = note: expected type `{integer}` - found mutable reference `&mut _` -note: to declare a mutable binding use: `mut x` - --> $DIR/well-typed-edition-2024.rs:49:22 - | -LL | if let Some(Some(&mut x)) = &mut Some(&mut Some(0)) { - | ^^^^^^ -help: consider removing `&mut` from the pattern - | -LL - if let Some(Some(&mut x)) = &mut Some(&mut Some(0)) { -LL + if let Some(Some(x)) = &mut Some(&mut Some(0)) { - | - error[E0308]: mismatched types --> $DIR/well-typed-edition-2024.rs:63:23 | @@ -78,28 +30,6 @@ LL - if let Some(&Some(&x)) = &Some(&Some(0)) { LL + if let Some(&Some(x)) = &Some(&Some(0)) { | -error[E0308]: mismatched types - --> $DIR/well-typed-edition-2024.rs:70:17 - | -LL | if let Some(&Some(Some(&x))) = &Some(Some(&mut Some(0))) { - | ^^^^^^^^^^^^^^^ ------------------------- this expression has type `&Option>>` - | | - | expected `Option<&mut Option<{integer}>>`, found `&_` - | - = note: expected enum `Option<&mut Option<{integer}>>` - found reference `&_` - -error[E0308]: mismatched types - --> $DIR/well-typed-edition-2024.rs:75:17 - | -LL | if let Some(&Some(x)) = &mut Some(Some(0)) { - | ^^^^^^^^ ------------------ this expression has type `&mut Option>` - | | - | expected `Option<{integer}>`, found `&_` - | - = note: expected enum `Option<{integer}>` - found reference `&_` - error[E0308]: mismatched types --> $DIR/well-typed-edition-2024.rs:82:23 | @@ -249,6 +179,6 @@ LL | let [&mut &(mut x)] = &mut [&0]; = note: expected reference `&{integer}` found mutable reference `&mut _` -error: aborting due to 16 previous errors +error: aborting due to 11 previous errors For more information about this error, try `rustc --explain E0308`. From 443c51d5d6eb0903e3eef9a44e1dfb017e69958d Mon Sep 17 00:00:00 2001 From: dianne Date: Sun, 26 Jan 2025 00:27:37 -0800 Subject: [PATCH 04/11] "structural2021" ruleset: add fallback-to-outer (eat both) deref rule --- compiler/rustc_hir_typeck/src/pat.rs | 43 +++++- ...well-typed-edition-2024.classic2021.stderr | 52 +++---- .../experimental/well-typed-edition-2024.rs | 47 +++--- .../well-typed-edition-2024.stable2021.stderr | 52 +++---- ...l-typed-edition-2024.structural2021.stderr | 136 +++++------------- 5 files changed, 151 insertions(+), 179 deletions(-) diff --git a/compiler/rustc_hir_typeck/src/pat.rs b/compiler/rustc_hir_typeck/src/pat.rs index 47175cda63b88..be72af63fe50f 100644 --- a/compiler/rustc_hir_typeck/src/pat.rs +++ b/compiler/rustc_hir_typeck/src/pat.rs @@ -235,6 +235,9 @@ enum InheritedRefMatchRule { /// Whether to allow reference patterns to consume only an inherited reference when matching /// against a non-reference type. This is `false` for stable Rust. eat_inherited_ref_alone: bool, + /// Whether to allow a `&mut` reference pattern to eat a `&` reference type if it's also + /// able to consume a mutable inherited reference. This is `false` for stable Rust. + fallback_to_outer: bool, }, } @@ -261,12 +264,17 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { } else { // Currently, matching against an inherited ref on edition 2024 is an error. // Use `EatBoth` as a fallback to be similar to stable Rust. - InheritedRefMatchRule::EatBoth { eat_inherited_ref_alone: false } + InheritedRefMatchRule::EatBoth { + eat_inherited_ref_alone: false, + fallback_to_outer: false, + } } } else { + let has_structural_gate = self.tcx.features().ref_pat_eat_one_layer_2024_structural(); InheritedRefMatchRule::EatBoth { - eat_inherited_ref_alone: self.tcx.features().ref_pat_eat_one_layer_2024() - || self.tcx.features().ref_pat_eat_one_layer_2024_structural(), + eat_inherited_ref_alone: has_structural_gate + || self.tcx.features().ref_pat_eat_one_layer_2024(), + fallback_to_outer: has_structural_gate, } } } @@ -2386,12 +2394,31 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { return expected; } } - InheritedRefMatchRule::EatBoth { eat_inherited_ref_alone: true } => { + InheritedRefMatchRule::EatBoth { + eat_inherited_ref_alone: true, + fallback_to_outer, + } => { // Reset binding mode on old editions pat_info.binding_mode = ByRef::No; - if let ty::Ref(_, _, _) = *expected.kind() { + if let ty::Ref(_, inner_ty, _) = *expected.kind() { // Consume both the inherited and inner references. + if fallback_to_outer && inh_mut.is_mut() { + // If we can fall back to matching the inherited reference, the expected + // type is a reference type (of any mutability), and the inherited + // reference is mutable, we'll always be able to match. We handle that + // here to avoid adding fallback-to-outer to the common logic below. + // NB: This way of phrasing the logic will catch more cases than those + // that need to fall back to matching the inherited reference. However, + // as long as `&` patterns can match mutable (inherited) references + // (RFC 3627, Rule 5) this should be sound. + debug_assert!(ref_pat_matches_mut_ref); + self.check_pat(inner, inner_ty, pat_info); + return expected; + } else { + // Otherwise, use the common logic below for matching the inner + // reference type. + } } else { // The expected type isn't a reference type, so only match against the // inherited reference. @@ -2405,9 +2432,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { return expected; } } - InheritedRefMatchRule::EatBoth { eat_inherited_ref_alone: false } => { + rule @ InheritedRefMatchRule::EatBoth { + eat_inherited_ref_alone: false, + fallback_to_outer, + } => { // Reset binding mode on stable Rust. This will be a type error below if // `expected` is not a reference type. + debug_assert!(!fallback_to_outer, "typing rule `{rule:?}` is unimplemented."); pat_info.binding_mode = ByRef::No; self.add_rust_2024_migration_desugared_pat( pat_info.top_info.hir_id, diff --git a/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/well-typed-edition-2024.classic2021.stderr b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/well-typed-edition-2024.classic2021.stderr index f784d7e9988db..086d4bb133ba2 100644 --- a/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/well-typed-edition-2024.classic2021.stderr +++ b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/well-typed-edition-2024.classic2021.stderr @@ -105,28 +105,7 @@ LL + let [ref x] = &mut [&0]; | error[E0308]: mismatched types - --> $DIR/well-typed-edition-2024.rs:108:10 - | -LL | let [&mut ref mut x] = &mut [&0]; - | ^^^^^^^^^^^^^^ --------- this expression has type `&mut [&{integer}; 1]` - | | - | types differ in mutability - | - = note: expected reference `&{integer}` - found mutable reference `&mut _` -note: to declare a mutable binding use: `mut x` - --> $DIR/well-typed-edition-2024.rs:108:10 - | -LL | let [&mut ref mut x] = &mut [&0]; - | ^^^^^^^^^^^^^^ -help: consider removing `&mut` from the pattern - | -LL - let [&mut ref mut x] = &mut [&0]; -LL + let [ref mut x] = &mut [&0]; - | - -error[E0308]: mismatched types - --> $DIR/well-typed-edition-2024.rs:114:10 + --> $DIR/well-typed-edition-2024.rs:117:10 | LL | let [&mut mut x] = &mut [&0]; | ^^^^^^^^^^ --------- this expression has type `&mut [&{integer}; 1]` @@ -136,7 +115,7 @@ LL | let [&mut mut x] = &mut [&0]; = note: expected reference `&{integer}` found mutable reference `&mut _` note: to declare a mutable binding use: `mut x` - --> $DIR/well-typed-edition-2024.rs:114:10 + --> $DIR/well-typed-edition-2024.rs:117:10 | LL | let [&mut mut x] = &mut [&0]; | ^^^^^^^^^^ @@ -147,7 +126,7 @@ LL + let [mut x] = &mut [&0]; | error[E0308]: mismatched types - --> $DIR/well-typed-edition-2024.rs:120:10 + --> $DIR/well-typed-edition-2024.rs:123:10 | LL | let [&mut &x] = &mut [&0]; | ^^^^^^^ --------- this expression has type `&mut [&{integer}; 1]` @@ -158,7 +137,7 @@ LL | let [&mut &x] = &mut [&0]; found mutable reference `&mut _` error[E0308]: mismatched types - --> $DIR/well-typed-edition-2024.rs:126:10 + --> $DIR/well-typed-edition-2024.rs:129:10 | LL | let [&mut &ref x] = &mut [&0]; | ^^^^^^^^^^^ --------- this expression has type `&mut [&{integer}; 1]` @@ -169,7 +148,7 @@ LL | let [&mut &ref x] = &mut [&0]; found mutable reference `&mut _` error[E0308]: mismatched types - --> $DIR/well-typed-edition-2024.rs:132:10 + --> $DIR/well-typed-edition-2024.rs:135:10 | LL | let [&mut &(mut x)] = &mut [&0]; | ^^^^^^^^^^^^^ --------- this expression has type `&mut [&{integer}; 1]` @@ -179,6 +158,27 @@ LL | let [&mut &(mut x)] = &mut [&0]; = note: expected reference `&{integer}` found mutable reference `&mut _` +error[E0308]: mismatched types + --> $DIR/well-typed-edition-2024.rs:109:14 + | +LL | let [&mut ref mut x] = &mut [&0]; + | ^^^^^^^^^^^^^^ --------- this expression has type `&mut [&{integer}; 1]` + | | + | types differ in mutability + | + = note: expected reference `&{integer}` + found mutable reference `&mut _` +note: to declare a mutable binding use: `mut x` + --> $DIR/well-typed-edition-2024.rs:109:14 + | +LL | let [&mut ref mut x] = &mut [&0]; + | ^^^^^^^^^^^^^^ +help: consider removing `&mut` from the pattern + | +LL - let [&mut ref mut x] = &mut [&0]; +LL + let [ref mut x] = &mut [&0]; + | + error: aborting due to 11 previous errors For more information about this error, try `rustc --explain E0308`. diff --git a/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/well-typed-edition-2024.rs b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/well-typed-edition-2024.rs index 851011918fe95..63a09fe90c9f2 100644 --- a/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/well-typed-edition-2024.rs +++ b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/well-typed-edition-2024.rs @@ -91,47 +91,50 @@ pub fn main() { #[cfg(any(classic2024, structural2024))] let _: u32 = x; } - // Tests for eat-inner rulesets matching on the outer reference if matching on the inner - // reference causes a mutability mismatch, i.e. `Deref(EatInner, FallbackToOuter)`: + // Tests for eat-inner and eat-both rulesets matching on the outer reference if matching on the + // inner reference causes a mutability mismatch. i.e. tests for "fallback-to-outer" deref rules. let [&mut x] = &mut [&0]; - //[stable2021,classic2021,structural2021]~^ mismatched types - //[stable2021,classic2021,structural2021]~| types differ in mutability - // TODO: on `structural2021` `x` should have type `u32` + //[stable2021,classic2021]~^ mismatched types + //[stable2021,classic2021]~| types differ in mutability + #[cfg(structural2021)] let _: u32 = x; #[cfg(any(classic2024, structural2024))] let _: &u32 = x; let [&mut ref x] = &mut [&0]; - //[stable2021,classic2021,structural2021]~^ mismatched types - //[stable2021,classic2021,structural2021]~| types differ in mutability - // TODO: on `structural2021` `x` should have type `&u32` + //[stable2021,classic2021]~^ mismatched types + //[stable2021,classic2021]~| types differ in mutability + #[cfg(structural2021)] let _: &u32 = x; #[cfg(any(classic2024, structural2024))] let _: &&u32 = x; - let [&mut ref mut x] = &mut [&0]; - //[stable2021,classic2021,structural2021]~^ mismatched types - //[stable2021,classic2021,structural2021]~| types differ in mutability - // TODO: this should be a mut borrow behind shared borrow error on `structural2021` - #[cfg(any(classic2024, structural2024))] let _: &mut &u32 = x; + fn borrowck_error_on_structural2021() { + let [&mut ref mut x] = &mut [&0]; + //[stable2021,classic2021]~^ mismatched types + //[stable2021,classic2021]~| types differ in mutability + //[structural2021]~^^^ cannot borrow data in a `&` reference as mutable + #[cfg(any(classic2024, structural2024))] let _: &mut &u32 = x; + } + borrowck_error_on_structural2021(); let [&mut mut x] = &mut [&0]; - //[stable2021,classic2021,structural2021]~^ mismatched types - //[stable2021,classic2021,structural2021]~| types differ in mutability - // TODO: on `structural2021` `x` should have type `u32` + //[stable2021,classic2021]~^ mismatched types + //[stable2021,classic2021]~| types differ in mutability + #[cfg(structural2021)] let _: u32 = x; #[cfg(any(classic2024, structural2024))] let _: &u32 = x; let [&mut &x] = &mut [&0]; //[stable2021,classic2021,structural2021]~^ mismatched types - //[stable2021,classic2021,structural2021]~| types differ in mutability - // TODO: [structural2021]~| expected integer, found `&_` + //[stable2021,classic2021]~| types differ in mutability + //[structural2021]~| expected integer, found `&_` #[cfg(any(classic2024, structural2024))] let _: u32 = x; let [&mut &ref x] = &mut [&0]; //[stable2021,classic2021,structural2021]~^ mismatched types - //[stable2021,classic2021,structural2021]~| types differ in mutability - // TODO: [structural2021]~| expected integer, found `&_` + //[stable2021,classic2021]~| types differ in mutability + //[structural2021]~| expected integer, found `&_` #[cfg(any(classic2024, structural2024))] let _: &u32 = x; let [&mut &(mut x)] = &mut [&0]; //[stable2021,classic2021,structural2021]~^ mismatched types - //[stable2021,classic2021,structural2021]~| types differ in mutability - // TODO: [structural2021]~| expected integer, found `&_` + //[stable2021,classic2021]~| types differ in mutability + //[structural2021]~| expected integer, found `&_` #[cfg(any(classic2024, structural2024))] let _: u32 = x; } diff --git a/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/well-typed-edition-2024.stable2021.stderr b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/well-typed-edition-2024.stable2021.stderr index fb99e3983e8cc..adb47172f34cc 100644 --- a/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/well-typed-edition-2024.stable2021.stderr +++ b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/well-typed-edition-2024.stable2021.stderr @@ -186,28 +186,7 @@ LL + let [ref x] = &mut [&0]; | error[E0308]: mismatched types - --> $DIR/well-typed-edition-2024.rs:108:10 - | -LL | let [&mut ref mut x] = &mut [&0]; - | ^^^^^^^^^^^^^^ --------- this expression has type `&mut [&{integer}; 1]` - | | - | types differ in mutability - | - = note: expected reference `&{integer}` - found mutable reference `&mut _` -note: to declare a mutable binding use: `mut x` - --> $DIR/well-typed-edition-2024.rs:108:10 - | -LL | let [&mut ref mut x] = &mut [&0]; - | ^^^^^^^^^^^^^^ -help: consider removing `&mut` from the pattern - | -LL - let [&mut ref mut x] = &mut [&0]; -LL + let [ref mut x] = &mut [&0]; - | - -error[E0308]: mismatched types - --> $DIR/well-typed-edition-2024.rs:114:10 + --> $DIR/well-typed-edition-2024.rs:117:10 | LL | let [&mut mut x] = &mut [&0]; | ^^^^^^^^^^ --------- this expression has type `&mut [&{integer}; 1]` @@ -217,7 +196,7 @@ LL | let [&mut mut x] = &mut [&0]; = note: expected reference `&{integer}` found mutable reference `&mut _` note: to declare a mutable binding use: `mut x` - --> $DIR/well-typed-edition-2024.rs:114:10 + --> $DIR/well-typed-edition-2024.rs:117:10 | LL | let [&mut mut x] = &mut [&0]; | ^^^^^^^^^^ @@ -228,7 +207,7 @@ LL + let [mut x] = &mut [&0]; | error[E0308]: mismatched types - --> $DIR/well-typed-edition-2024.rs:120:10 + --> $DIR/well-typed-edition-2024.rs:123:10 | LL | let [&mut &x] = &mut [&0]; | ^^^^^^^ --------- this expression has type `&mut [&{integer}; 1]` @@ -239,7 +218,7 @@ LL | let [&mut &x] = &mut [&0]; found mutable reference `&mut _` error[E0308]: mismatched types - --> $DIR/well-typed-edition-2024.rs:126:10 + --> $DIR/well-typed-edition-2024.rs:129:10 | LL | let [&mut &ref x] = &mut [&0]; | ^^^^^^^^^^^ --------- this expression has type `&mut [&{integer}; 1]` @@ -250,7 +229,7 @@ LL | let [&mut &ref x] = &mut [&0]; found mutable reference `&mut _` error[E0308]: mismatched types - --> $DIR/well-typed-edition-2024.rs:132:10 + --> $DIR/well-typed-edition-2024.rs:135:10 | LL | let [&mut &(mut x)] = &mut [&0]; | ^^^^^^^^^^^^^ --------- this expression has type `&mut [&{integer}; 1]` @@ -260,6 +239,27 @@ LL | let [&mut &(mut x)] = &mut [&0]; = note: expected reference `&{integer}` found mutable reference `&mut _` +error[E0308]: mismatched types + --> $DIR/well-typed-edition-2024.rs:109:14 + | +LL | let [&mut ref mut x] = &mut [&0]; + | ^^^^^^^^^^^^^^ --------- this expression has type `&mut [&{integer}; 1]` + | | + | types differ in mutability + | + = note: expected reference `&{integer}` + found mutable reference `&mut _` +note: to declare a mutable binding use: `mut x` + --> $DIR/well-typed-edition-2024.rs:109:14 + | +LL | let [&mut ref mut x] = &mut [&0]; + | ^^^^^^^^^^^^^^ +help: consider removing `&mut` from the pattern + | +LL - let [&mut ref mut x] = &mut [&0]; +LL + let [ref mut x] = &mut [&0]; + | + error: aborting due to 17 previous errors For more information about this error, try `rustc --explain E0308`. diff --git a/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/well-typed-edition-2024.structural2021.stderr b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/well-typed-edition-2024.structural2021.stderr index f784d7e9988db..f8c2bd9a92128 100644 --- a/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/well-typed-edition-2024.structural2021.stderr +++ b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/well-typed-edition-2024.structural2021.stderr @@ -63,122 +63,60 @@ LL + if let Some(&Some(x)) = &mut Some(&Some(0)) { | error[E0308]: mismatched types - --> $DIR/well-typed-edition-2024.rs:96:10 + --> $DIR/well-typed-edition-2024.rs:123:15 | -LL | let [&mut x] = &mut [&0]; - | ^^^^^^ --------- this expression has type `&mut [&{integer}; 1]` - | | - | types differ in mutability - | - = note: expected reference `&{integer}` - found mutable reference `&mut _` -note: to declare a mutable binding use: `mut x` - --> $DIR/well-typed-edition-2024.rs:96:10 - | -LL | let [&mut x] = &mut [&0]; - | ^^^^^^ -help: consider removing `&mut` from the pattern - | -LL - let [&mut x] = &mut [&0]; -LL + let [x] = &mut [&0]; - | - -error[E0308]: mismatched types - --> $DIR/well-typed-edition-2024.rs:102:10 - | -LL | let [&mut ref x] = &mut [&0]; - | ^^^^^^^^^^ --------- this expression has type `&mut [&{integer}; 1]` - | | - | types differ in mutability - | - = note: expected reference `&{integer}` - found mutable reference `&mut _` -note: to declare a mutable binding use: `mut x` - --> $DIR/well-typed-edition-2024.rs:102:10 - | -LL | let [&mut ref x] = &mut [&0]; - | ^^^^^^^^^^ -help: consider removing `&mut` from the pattern - | -LL - let [&mut ref x] = &mut [&0]; -LL + let [ref x] = &mut [&0]; - | - -error[E0308]: mismatched types - --> $DIR/well-typed-edition-2024.rs:108:10 - | -LL | let [&mut ref mut x] = &mut [&0]; - | ^^^^^^^^^^^^^^ --------- this expression has type `&mut [&{integer}; 1]` - | | - | types differ in mutability - | - = note: expected reference `&{integer}` - found mutable reference `&mut _` -note: to declare a mutable binding use: `mut x` - --> $DIR/well-typed-edition-2024.rs:108:10 +LL | let [&mut &x] = &mut [&0]; + | ^^ --------- this expression has type `&mut [&{integer}; 1]` + | | + | expected integer, found `&_` | -LL | let [&mut ref mut x] = &mut [&0]; - | ^^^^^^^^^^^^^^ -help: consider removing `&mut` from the pattern + = note: expected type `{integer}` + found reference `&_` +help: consider removing `&` from the pattern | -LL - let [&mut ref mut x] = &mut [&0]; -LL + let [ref mut x] = &mut [&0]; +LL - let [&mut &x] = &mut [&0]; +LL + let [&mut x] = &mut [&0]; | error[E0308]: mismatched types - --> $DIR/well-typed-edition-2024.rs:114:10 - | -LL | let [&mut mut x] = &mut [&0]; - | ^^^^^^^^^^ --------- this expression has type `&mut [&{integer}; 1]` - | | - | types differ in mutability + --> $DIR/well-typed-edition-2024.rs:129:15 | - = note: expected reference `&{integer}` - found mutable reference `&mut _` -note: to declare a mutable binding use: `mut x` - --> $DIR/well-typed-edition-2024.rs:114:10 +LL | let [&mut &ref x] = &mut [&0]; + | ^^^^^^ --------- this expression has type `&mut [&{integer}; 1]` + | | + | expected integer, found `&_` | -LL | let [&mut mut x] = &mut [&0]; - | ^^^^^^^^^^ -help: consider removing `&mut` from the pattern + = note: expected type `{integer}` + found reference `&_` +help: consider removing `&` from the pattern | -LL - let [&mut mut x] = &mut [&0]; -LL + let [mut x] = &mut [&0]; +LL - let [&mut &ref x] = &mut [&0]; +LL + let [&mut ref x] = &mut [&0]; | error[E0308]: mismatched types - --> $DIR/well-typed-edition-2024.rs:120:10 + --> $DIR/well-typed-edition-2024.rs:135:15 | -LL | let [&mut &x] = &mut [&0]; - | ^^^^^^^ --------- this expression has type `&mut [&{integer}; 1]` - | | - | types differ in mutability +LL | let [&mut &(mut x)] = &mut [&0]; + | ^^^^^^^^ --------- this expression has type `&mut [&{integer}; 1]` + | | + | expected integer, found `&_` | - = note: expected reference `&{integer}` - found mutable reference `&mut _` - -error[E0308]: mismatched types - --> $DIR/well-typed-edition-2024.rs:126:10 + = note: expected type `{integer}` + found reference `&_` +help: consider removing `&` from the pattern | -LL | let [&mut &ref x] = &mut [&0]; - | ^^^^^^^^^^^ --------- this expression has type `&mut [&{integer}; 1]` - | | - | types differ in mutability +LL - let [&mut &(mut x)] = &mut [&0]; +LL + let [&mut mut x)] = &mut [&0]; | - = note: expected reference `&{integer}` - found mutable reference `&mut _` -error[E0308]: mismatched types - --> $DIR/well-typed-edition-2024.rs:132:10 - | -LL | let [&mut &(mut x)] = &mut [&0]; - | ^^^^^^^^^^^^^ --------- this expression has type `&mut [&{integer}; 1]` - | | - | types differ in mutability +error[E0596]: cannot borrow data in a `&` reference as mutable + --> $DIR/well-typed-edition-2024.rs:109:19 | - = note: expected reference `&{integer}` - found mutable reference `&mut _` +LL | let [&mut ref mut x] = &mut [&0]; + | ^^^^^^^^^ cannot borrow as mutable -error: aborting due to 11 previous errors +error: aborting due to 8 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`. From 1ed74aaf0c6d673c8dbd345b178c3faf769c7e85 Mon Sep 17 00:00:00 2001 From: dianne Date: Sun, 26 Jan 2025 03:16:17 -0800 Subject: [PATCH 05/11] add mixed-edition tests --- .../auxiliary/mixed-editions-macros.rs | 48 +++++++ .../mixed-editions.classic2021.stderr | 103 +++++++++++++++ .../mixed-editions.classic2024.stderr | 97 ++++++++++++++ .../experimental/mixed-editions.rs | 122 ++++++++++++++++++ .../mixed-editions.structural2021.stderr | 61 +++++++++ .../mixed-editions.structural2024.stderr | 55 ++++++++ 6 files changed, 486 insertions(+) create mode 100644 tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/auxiliary/mixed-editions-macros.rs create mode 100644 tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/mixed-editions.classic2021.stderr create mode 100644 tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/mixed-editions.classic2024.stderr create mode 100644 tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/mixed-editions.rs create mode 100644 tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/mixed-editions.structural2021.stderr create mode 100644 tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/mixed-editions.structural2024.stderr diff --git a/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/auxiliary/mixed-editions-macros.rs b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/auxiliary/mixed-editions-macros.rs new file mode 100644 index 0000000000000..14d26be91a082 --- /dev/null +++ b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/auxiliary/mixed-editions-macros.rs @@ -0,0 +1,48 @@ +//@[classic2021] edition: 2024 +//@[structural2021] edition: 2024 +//@[classic2024] edition: 2021 +//@[structural2024] edition: 2021 +//! This contains macros in an edition *different* to the one used in `../mixed-editions.rs`, in +//! order to test typing mixed-edition patterns. + +#[macro_export] +macro_rules! match_ctor { + ($p:pat) => { + [$p] + }; +} + +#[macro_export] +macro_rules! match_ref { + ($p:pat) => { + &$p + }; +} + +#[macro_export] +macro_rules! bind { + ($i:ident) => { + $i + } +} + +#[macro_export] +macro_rules! bind_ref { + ($i:ident) => { + ref $i + } +} + +#[macro_export] +macro_rules! bind_mut { + ($i:ident) => { + mut $i + } +} + +#[macro_export] +macro_rules! bind_ref_mut { + ($i:ident) => { + ref mut $i + } +} diff --git a/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/mixed-editions.classic2021.stderr b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/mixed-editions.classic2021.stderr new file mode 100644 index 0000000000000..7e3caaf979748 --- /dev/null +++ b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/mixed-editions.classic2021.stderr @@ -0,0 +1,103 @@ +error[E0658]: binding cannot be both mutable and by-reference + --> $DIR/mixed-editions.rs:41:10 + | +LL | let [bind_mut!(y)] = &[0]; + | ^^^^^^^^^^^^ + | + = note: see issue #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 + = note: this error originates in the macro `bind_mut` (in Nightly builds, run with -Z macro-backtrace for more info) + +error[E0596]: cannot borrow as mutable inside an `&` pattern + --> $DIR/mixed-editions.rs:76:21 + | +LL | let match_ref!([x]) = &mut &mut [0]; + | ^ + +error[E0596]: cannot borrow as mutable inside an `&` pattern + --> $DIR/mixed-editions.rs:80:22 + | +LL | let &match_ctor!(y) = &mut &mut [0]; + | - ^ + | | + | help: replace this `&` with `&mut`: `&mut` + +error[E0596]: cannot borrow as mutable inside an `&` pattern + --> $DIR/mixed-editions.rs:84:17 + | +LL | let &[bind!(z)] = &mut &mut [0]; + | - ^ + | | + | help: replace this `&` with `&mut`: `&mut` + +error[E0596]: cannot borrow as mutable inside an `&` pattern + --> $DIR/mixed-editions.rs:110:28 + | +LL | let match_ref!(ref mut x) = &mut 0; + | ^ + +error[E0596]: cannot borrow as mutable inside an `&` pattern + --> $DIR/mixed-editions.rs:112:24 + | +LL | let &bind_ref_mut!(x) = &mut 0; + | - ^ + | | + | help: replace this `&` with `&mut`: `&mut` + +error[E0596]: cannot borrow as mutable inside an `&` pattern + --> $DIR/mixed-editions.rs:116:29 + | +LL | let [match_ref!(ref mut x)] = &mut [0]; + | ^ + +error[E0596]: cannot borrow as mutable inside an `&` pattern + --> $DIR/mixed-editions.rs:118:25 + | +LL | let [&bind_ref_mut!(x)] = &mut [0]; + | - ^ + | | + | help: replace this `&` with `&mut`: `&mut` + +error: binding modifiers may only be written when the default binding mode is `move` + --> $DIR/mixed-editions.rs:30:10 + | +LL | let [bind_ref!(y)] = &[0]; + | ^^^^^^^^^^^^ occurs within macro expansion + | + = note: for more information, see +note: matching on a reference type with a non-reference pattern changes the default binding mode + --> $DIR/mixed-editions.rs:30:9 + | +LL | let [bind_ref!(y)] = &[0]; + | ^^^^^^^^^^^^^^ this matches on type `&_` + = note: this error originates in the macro `bind_ref` (in Nightly builds, run with -Z macro-backtrace for more info) +help: make the implied reference pattern explicit + | +LL | let &[bind_ref!(y)] = &[0]; + | + + +error[E0596]: cannot borrow data in a `&` reference as mutable + --> $DIR/mixed-editions.rs:61:21 + | +LL | let match_ref!([x]) = &&mut [0]; + | ^ cannot borrow as mutable + +error[E0596]: cannot borrow data in a `&` reference as mutable + --> $DIR/mixed-editions.rs:65:22 + | +LL | let &match_ctor!(y) = &&mut [0]; + | ^ cannot borrow as mutable + +error[E0596]: cannot borrow data in a `&` reference as mutable + --> $DIR/mixed-editions.rs:69:11 + | +LL | let &[bind!(z)] = &&mut [0]; + | ^^^^^^^^ cannot borrow as mutable + | + = note: this error originates in the macro `bind` (in Nightly builds, run with -Z macro-backtrace for more info) + +error: aborting due to 12 previous errors + +Some errors have detailed explanations: E0596, E0658. +For more information about an error, try `rustc --explain E0596`. diff --git a/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/mixed-editions.classic2024.stderr b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/mixed-editions.classic2024.stderr new file mode 100644 index 0000000000000..466993a1671f4 --- /dev/null +++ b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/mixed-editions.classic2024.stderr @@ -0,0 +1,97 @@ +error[E0658]: binding cannot be both mutable and by-reference + --> $DIR/mixed-editions.rs:37:21 + | +LL | let match_ctor!(mut x) = &[0]; + | ^^^^ + | + = note: see issue #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[E0596]: cannot borrow as mutable inside an `&` pattern + --> $DIR/mixed-editions.rs:76:21 + | +LL | let match_ref!([x]) = &mut &mut [0]; + | ^ + +error[E0596]: cannot borrow as mutable inside an `&` pattern + --> $DIR/mixed-editions.rs:80:22 + | +LL | let &match_ctor!(y) = &mut &mut [0]; + | - ^ + | | + | help: replace this `&` with `&mut`: `&mut` + +error[E0596]: cannot borrow as mutable inside an `&` pattern + --> $DIR/mixed-editions.rs:84:17 + | +LL | let &[bind!(z)] = &mut &mut [0]; + | - ^ + | | + | help: replace this `&` with `&mut`: `&mut` + +error[E0596]: cannot borrow as mutable inside an `&` pattern + --> $DIR/mixed-editions.rs:110:28 + | +LL | let match_ref!(ref mut x) = &mut 0; + | ^ + +error[E0596]: cannot borrow as mutable inside an `&` pattern + --> $DIR/mixed-editions.rs:112:24 + | +LL | let &bind_ref_mut!(x) = &mut 0; + | - ^ + | | + | help: replace this `&` with `&mut`: `&mut` + +error[E0596]: cannot borrow as mutable inside an `&` pattern + --> $DIR/mixed-editions.rs:116:29 + | +LL | let [match_ref!(ref mut x)] = &mut [0]; + | ^ + +error[E0596]: cannot borrow as mutable inside an `&` pattern + --> $DIR/mixed-editions.rs:118:25 + | +LL | let [&bind_ref_mut!(x)] = &mut [0]; + | - ^ + | | + | help: replace this `&` with `&mut`: `&mut` + +error: binding modifiers may only be written when the default binding mode is `move` + --> $DIR/mixed-editions.rs:26:21 + | +LL | let match_ctor!(ref x) = &[0]; + | ^^^ binding modifier not allowed under `ref` default binding mode + | + = note: for more information, see +help: make the implied reference pattern explicit + --> $DIR/auxiliary/mixed-editions-macros.rs:11:9 + | +LL | &[$p] + | + + +error[E0596]: cannot borrow data in a `&` reference as mutable + --> $DIR/mixed-editions.rs:61:21 + | +LL | let match_ref!([x]) = &&mut [0]; + | ^ cannot borrow as mutable + +error[E0596]: cannot borrow data in a `&` reference as mutable + --> $DIR/mixed-editions.rs:65:22 + | +LL | let &match_ctor!(y) = &&mut [0]; + | ^ cannot borrow as mutable + +error[E0596]: cannot borrow data in a `&` reference as mutable + --> $DIR/mixed-editions.rs:69:11 + | +LL | let &[bind!(z)] = &&mut [0]; + | ^^^^^^^^ cannot borrow as mutable + | + = note: this error originates in the macro `bind` (in Nightly builds, run with -Z macro-backtrace for more info) + +error: aborting due to 12 previous errors + +Some errors have detailed explanations: E0596, E0658. +For more information about an error, try `rustc --explain E0596`. diff --git a/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/mixed-editions.rs b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/mixed-editions.rs new file mode 100644 index 0000000000000..0a22b55ab6374 --- /dev/null +++ b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/mixed-editions.rs @@ -0,0 +1,122 @@ +//@ revisions: classic2021 structural2021 classic2024 structural2024 +//@[classic2021] edition: 2021 +//@[structural2021] edition: 2021 +//@[classic2024] edition: 2024 +//@[structural2024] edition: 2024 +//@ aux-build:mixed-editions-macros.rs +//! Tests for typing mixed-edition patterns under the `ref_pat_eat_one_layer_2024` and +//! `ref_pat_eat_one_layer_2024_structural` feature gates. +//! This is meant both to check that patterns are typed with edition-appropriate typing rules and +//! that we keep our internal state consistent when mixing editions. +#![allow(incomplete_features, unused)] +#![cfg_attr(any(classic2021, classic2024), feature(ref_pat_eat_one_layer_2024))] +#![cfg_attr(any(structural2021, structural2024), feature(ref_pat_eat_one_layer_2024_structural))] + +extern crate mixed_editions_macros; +use mixed_editions_macros::*; + +// Tests type equality in a way that avoids coercing `&&T` to `&T`. +trait Eq {} +impl Eq for T {} +fn assert_type_eq>(_: T, _: U) {} + +/// Make sure binding with `ref` in the presence of an inherited reference is forbidden when and +/// only when the binding is from edition 2024. +fn ref_binding_tests() { + let match_ctor!(ref x) = &[0]; + //[classic2024,structural2024]~^ ERROR: binding modifiers may only be written when the default binding mode is `move` + #[cfg(any(classic2021, structural2021))] assert_type_eq(x, &0u32); + + let [bind_ref!(y)] = &[0]; + //[classic2021,structural2021]~^ ERROR: binding modifiers may only be written when the default binding mode is `move` + #[cfg(any(classic2024, structural2024))] assert_type_eq(y, &0u32); +} + +/// Likewise, when binding with `mut`. +fn mut_binding_tests() { + let match_ctor!(mut x) = &[0]; + //[classic2024,structural2024]~^ ERROR: binding cannot be both mutable and by-reference + #[cfg(any(classic2021, structural2021))] assert_type_eq(x, 0u32); + + let [bind_mut!(y)] = &[0]; + //[classic2021,structural2021]~^ ERROR: binding cannot be both mutable and by-reference + #[cfg(any(classic2024, structural2024))] assert_type_eq(y, 0u32); +} + +/// Make sure reference patterns correspond to one deref on edition 2024 and two on edition 2021. +fn layers_eaten_tests() { + let match_ctor!(&x) = &[&0]; + #[cfg(any(classic2021, structural2021))] assert_type_eq(x, 0u32); + #[cfg(any(classic2024, structural2024))] assert_type_eq(x, &0u32); + + let [match_ref!(y)] = &[&0]; + #[cfg(any(classic2021, structural2021))] assert_type_eq(y, &0u32); + #[cfg(any(classic2024, structural2024))] assert_type_eq(y, 0u32); +} + +/// Make sure downgrading mutable binding modes inside shared refs ("Rule 3") doesn't break. +/// This only applies to `ref_pat_eat_one_layer_2024_structural`, which has Rule 3 in all editions; +/// under `ref_pat_eat_one_layer_2024`, these should be errors. +fn rule_3_tests() { + let match_ref!([x]) = &&mut [0]; + //[classic2021,classic2024]~^ ERROR: cannot borrow data in a `&` reference as mutable + #[cfg(any(structural2021, structural2024))] assert_type_eq(x, &0u32); + + let &match_ctor!(y) = &&mut [0]; + //[classic2021,classic2024]~^ ERROR: cannot borrow data in a `&` reference as mutable + #[cfg(any(structural2021, structural2024))] assert_type_eq(y, &0u32); + + let &[bind!(z)] = &&mut [0]; + //[classic2021,classic2024]~^ ERROR: cannot borrow data in a `&` reference as mutable + #[cfg(any(structural2021, structural2024))] assert_type_eq(z, &0u32); +} + +/// Test that the interaction between Rules 3 and 5 doesn't break. +fn rules_3_and_5_tests() { + let match_ref!([x]) = &mut &mut [0]; + //[classic2021,classic2024]~^ ERROR: cannot borrow as mutable inside an `&` pattern + #[cfg(any(structural2021, structural2024))] assert_type_eq(x, &0u32); + + let &match_ctor!(y) = &mut &mut [0]; + //[classic2021,classic2024]~^ ERROR: cannot borrow as mutable inside an `&` pattern + #[cfg(any(structural2021, structural2024))] assert_type_eq(y, &0u32); + + let &[bind!(z)] = &mut &mut [0]; + //[classic2021,classic2024]~^ ERROR: cannot borrow as mutable inside an `&` pattern + #[cfg(any(structural2021, structural2024))] assert_type_eq(z, &0u32); +} + +/// Make sure matching a lone shared reference with a `&` ("Rule 4") doesn't break. +fn rule_4_tests() { + let match_ref!([x]) = &[0]; + assert_type_eq(x, 0u32); + + let &match_ctor!(y) = &[0]; + assert_type_eq(y, 0u32); +} + +/// Make sure matching a `&mut` reference with a `&` pattern ("Rule 5") doesn't break. +fn rule_5_tests() { + let match_ref!(x) = &mut 0; + assert_type_eq(x, 0u32); + + // also test inherited references (assumes rule 4) + let [match_ref!(y)] = &mut [0]; + assert_type_eq(y, 0u32); +} + +/// Make sure binding with `ref mut` is an error within a `&` pattern matching a `&mut` reference. +fn rule_5_mutability_error_tests() { + let match_ref!(ref mut x) = &mut 0; + //~^ ERROR: cannot borrow as mutable inside an `&` pattern + let &bind_ref_mut!(x) = &mut 0; + //~^ ERROR: cannot borrow as mutable inside an `&` pattern + + // also test inherited references (assumes rule 4) + let [match_ref!(ref mut x)] = &mut [0]; + //~^ ERROR: cannot borrow as mutable inside an `&` pattern + let [&bind_ref_mut!(x)] = &mut [0]; + //~^ ERROR: cannot borrow as mutable inside an `&` pattern +} + +fn main() {} diff --git a/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/mixed-editions.structural2021.stderr b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/mixed-editions.structural2021.stderr new file mode 100644 index 0000000000000..4075dc9529da1 --- /dev/null +++ b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/mixed-editions.structural2021.stderr @@ -0,0 +1,61 @@ +error[E0658]: binding cannot be both mutable and by-reference + --> $DIR/mixed-editions.rs:41:10 + | +LL | let [bind_mut!(y)] = &[0]; + | ^^^^^^^^^^^^ + | + = note: see issue #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 + = note: this error originates in the macro `bind_mut` (in Nightly builds, run with -Z macro-backtrace for more info) + +error[E0596]: cannot borrow as mutable inside an `&` pattern + --> $DIR/mixed-editions.rs:110:28 + | +LL | let match_ref!(ref mut x) = &mut 0; + | ^ + +error[E0596]: cannot borrow as mutable inside an `&` pattern + --> $DIR/mixed-editions.rs:112:24 + | +LL | let &bind_ref_mut!(x) = &mut 0; + | - ^ + | | + | help: replace this `&` with `&mut`: `&mut` + +error[E0596]: cannot borrow as mutable inside an `&` pattern + --> $DIR/mixed-editions.rs:116:29 + | +LL | let [match_ref!(ref mut x)] = &mut [0]; + | ^ + +error[E0596]: cannot borrow as mutable inside an `&` pattern + --> $DIR/mixed-editions.rs:118:25 + | +LL | let [&bind_ref_mut!(x)] = &mut [0]; + | - ^ + | | + | help: replace this `&` with `&mut`: `&mut` + +error: binding modifiers may only be written when the default binding mode is `move` + --> $DIR/mixed-editions.rs:30:10 + | +LL | let [bind_ref!(y)] = &[0]; + | ^^^^^^^^^^^^ occurs within macro expansion + | + = note: for more information, see +note: matching on a reference type with a non-reference pattern changes the default binding mode + --> $DIR/mixed-editions.rs:30:9 + | +LL | let [bind_ref!(y)] = &[0]; + | ^^^^^^^^^^^^^^ this matches on type `&_` + = note: this error originates in the macro `bind_ref` (in Nightly builds, run with -Z macro-backtrace for more info) +help: make the implied reference pattern explicit + | +LL | let &[bind_ref!(y)] = &[0]; + | + + +error: aborting due to 6 previous errors + +Some errors have detailed explanations: E0596, E0658. +For more information about an error, try `rustc --explain E0596`. diff --git a/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/mixed-editions.structural2024.stderr b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/mixed-editions.structural2024.stderr new file mode 100644 index 0000000000000..819a54299ea1c --- /dev/null +++ b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/mixed-editions.structural2024.stderr @@ -0,0 +1,55 @@ +error[E0658]: binding cannot be both mutable and by-reference + --> $DIR/mixed-editions.rs:37:21 + | +LL | let match_ctor!(mut x) = &[0]; + | ^^^^ + | + = note: see issue #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[E0596]: cannot borrow as mutable inside an `&` pattern + --> $DIR/mixed-editions.rs:110:28 + | +LL | let match_ref!(ref mut x) = &mut 0; + | ^ + +error[E0596]: cannot borrow as mutable inside an `&` pattern + --> $DIR/mixed-editions.rs:112:24 + | +LL | let &bind_ref_mut!(x) = &mut 0; + | - ^ + | | + | help: replace this `&` with `&mut`: `&mut` + +error[E0596]: cannot borrow as mutable inside an `&` pattern + --> $DIR/mixed-editions.rs:116:29 + | +LL | let [match_ref!(ref mut x)] = &mut [0]; + | ^ + +error[E0596]: cannot borrow as mutable inside an `&` pattern + --> $DIR/mixed-editions.rs:118:25 + | +LL | let [&bind_ref_mut!(x)] = &mut [0]; + | - ^ + | | + | help: replace this `&` with `&mut`: `&mut` + +error: binding modifiers may only be written when the default binding mode is `move` + --> $DIR/mixed-editions.rs:26:21 + | +LL | let match_ctor!(ref x) = &[0]; + | ^^^ binding modifier not allowed under `ref` default binding mode + | + = note: for more information, see +help: make the implied reference pattern explicit + --> $DIR/auxiliary/mixed-editions-macros.rs:11:9 + | +LL | &[$p] + | + + +error: aborting due to 6 previous errors + +Some errors have detailed explanations: E0596, E0658. +For more information about an error, try `rustc --explain E0596`. From 2c595d649aa3ca7cb07ddbbd07ec38a753d6a70d Mon Sep 17 00:00:00 2001 From: dianne Date: Sun, 26 Jan 2025 03:33:48 -0800 Subject: [PATCH 06/11] update unstable book --- .../ref-pat-eat-one-layer-2024-structural.md | 5 +++-- .../src/language-features/ref-pat-eat-one-layer-2024.md | 5 +++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/doc/unstable-book/src/language-features/ref-pat-eat-one-layer-2024-structural.md b/src/doc/unstable-book/src/language-features/ref-pat-eat-one-layer-2024-structural.md index bc58768611175..bfdb579cd3523 100644 --- a/src/doc/unstable-book/src/language-features/ref-pat-eat-one-layer-2024-structural.md +++ b/src/doc/unstable-book/src/language-features/ref-pat-eat-one-layer-2024-structural.md @@ -10,10 +10,11 @@ This feature is incomplete and not yet intended for general use. This implements experimental, Edition-dependent match ergonomics under consideration for inclusion in Rust. -For more information, see the corresponding typing rules for [Editions 2024 and later]. -On earlier Editions, the current behavior is unspecified. +For more information, see the corresponding typing rules for [Editions 2021 and earlier] and for +[Editions 2024 and later]. For alternative experimental match ergonomics, see the feature [`ref_pat_eat_one_layer_2024`](./ref-pat-eat-one-layer-2024.md). +[Editions 2021 and earlier]: https://nadrieril.github.io/typing-rust-patterns/?compare=false&opts1=AQEBAQIBAQEBAAAAAAAAAAAAAAAAAAA%3D&mode=rules&do_cmp=false [Editions 2024 and later]: https://nadrieril.github.io/typing-rust-patterns/?compare=false&opts1=AQEBAgEBAQEBAgIAAAAAAAAAAAAAAAA%3D&mode=rules&do_cmp=false diff --git a/src/doc/unstable-book/src/language-features/ref-pat-eat-one-layer-2024.md b/src/doc/unstable-book/src/language-features/ref-pat-eat-one-layer-2024.md index 43de1849a5ebb..58d8d2905030c 100644 --- a/src/doc/unstable-book/src/language-features/ref-pat-eat-one-layer-2024.md +++ b/src/doc/unstable-book/src/language-features/ref-pat-eat-one-layer-2024.md @@ -10,10 +10,11 @@ This feature is incomplete and not yet intended for general use. This implements experimental, Edition-dependent match ergonomics under consideration for inclusion in Rust. -For more information, see the corresponding typing rules for [Editions 2024 and later]. -On earlier Editions, the current behavior is unspecified. +For more information, see the corresponding typing rules for [Editions 2021 and earlier] and for +[Editions 2024 and later]. For alternative experimental match ergonomics, see the feature [`ref_pat_eat_one_layer_2024_structural`](./ref-pat-eat-one-layer-2024-structural.md). +[Editions 2021 and earlier]: https://nadrieril.github.io/typing-rust-patterns/?compare=false&opts1=AQEBAQABAQABAAAAAQEBAAEBAAABAAA%3D&mode=rules&do_cmp=false [Editions 2024 and later]: https://nadrieril.github.io/typing-rust-patterns/?compare=false&opts1=AQEBAAABAQABAgIAAQEBAAEBAAABAAA%3D&mode=rules&do_cmp=false From 799e0f76903f694632db29bf7fc38c8a7f1453b6 Mon Sep 17 00:00:00 2001 From: dianne Date: Thu, 30 Jan 2025 16:03:02 -0800 Subject: [PATCH 07/11] add FIXMEs for diagnostic improvements --- compiler/rustc_hir_typeck/src/pat.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/compiler/rustc_hir_typeck/src/pat.rs b/compiler/rustc_hir_typeck/src/pat.rs index be72af63fe50f..1e28571807e72 100644 --- a/compiler/rustc_hir_typeck/src/pat.rs +++ b/compiler/rustc_hir_typeck/src/pat.rs @@ -2384,6 +2384,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { // NB: This assumes that `&` patterns can match against mutable // references (RFC 3627, Rule 5). If we implement a pattern typing // ruleset with Rule 4 but not Rule 5, we'll need to check that here. + // FIXME(ref_pat_eat_one_layer_2024_structural): If we already tried + // matching the real reference, the error message should explain that + // falling back to the inherited reference didn't work. This should be + // the same error as the old-Edition version below. debug_assert!(ref_pat_matches_mut_ref); self.error_inherited_ref_mutability_mismatch(pat, pat_prefix_span); } @@ -2418,6 +2422,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { } else { // Otherwise, use the common logic below for matching the inner // reference type. + // FIXME(ref_pat_eat_one_layer_2024_structural): If this results in a + // mutability mismatch, the error message should explain that falling + // back to the inherited reference didn't work. This should be the same + // error as the Edition 2024 version above. } } else { // The expected type isn't a reference type, so only match against the From 20149629ba8b143f0529295be22450488e173f30 Mon Sep 17 00:00:00 2001 From: dianne Date: Sun, 26 Jan 2025 04:20:30 -0800 Subject: [PATCH 08/11] "classic2021" ruleset: experimentally add fallback-to-outer (eat both) My reasoning: the ruleset implemented by the same feature gate in Edition 2024 always tries to eat the inherited reference first. For consistency, it makes sense to me to say across all editions that users should consider the inherited reference's mutability when wondering if a `&mut` pattern will type. --- compiler/rustc_hir_typeck/src/pat.rs | 46 +++--- .../ref-pat-eat-one-layer-2024.md | 2 +- ...well-typed-edition-2024.classic2021.stderr | 134 +++++------------- .../experimental/well-typed-edition-2024.rs | 36 ++--- 4 files changed, 73 insertions(+), 145 deletions(-) diff --git a/compiler/rustc_hir_typeck/src/pat.rs b/compiler/rustc_hir_typeck/src/pat.rs index 1e28571807e72..4201ef6674120 100644 --- a/compiler/rustc_hir_typeck/src/pat.rs +++ b/compiler/rustc_hir_typeck/src/pat.rs @@ -232,12 +232,13 @@ enum InheritedRefMatchRule { /// When the underlying type is a reference type, reference patterns consume both layers of /// reference, i.e. they both reset the binding mode and consume the reference type. EatBoth { - /// Whether to allow reference patterns to consume only an inherited reference when matching - /// against a non-reference type. This is `false` for stable Rust. - eat_inherited_ref_alone: bool, - /// Whether to allow a `&mut` reference pattern to eat a `&` reference type if it's also - /// able to consume a mutable inherited reference. This is `false` for stable Rust. - fallback_to_outer: bool, + /// This represents two behaviors implemented by both the `ref_pat_eat_one_layer_2024` and + /// `ref_pat_eat_one_layer_2024_structural` feature gates, and is false for stable Rust. + /// - Whether to allow reference patterns to consume only an inherited reference when + /// matching against a non-reference type. + /// - Whether to allow a `&mut` reference pattern to eat a `&` reference type if it's also + /// able to consume a mutable inherited reference. + consider_inherited_ref_first: bool, }, } @@ -264,17 +265,12 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { } else { // Currently, matching against an inherited ref on edition 2024 is an error. // Use `EatBoth` as a fallback to be similar to stable Rust. - InheritedRefMatchRule::EatBoth { - eat_inherited_ref_alone: false, - fallback_to_outer: false, - } + InheritedRefMatchRule::EatBoth { consider_inherited_ref_first: false } } } else { - let has_structural_gate = self.tcx.features().ref_pat_eat_one_layer_2024_structural(); InheritedRefMatchRule::EatBoth { - eat_inherited_ref_alone: has_structural_gate - || self.tcx.features().ref_pat_eat_one_layer_2024(), - fallback_to_outer: has_structural_gate, + consider_inherited_ref_first: self.tcx.features().ref_pat_eat_one_layer_2024() + || self.tcx.features().ref_pat_eat_one_layer_2024_structural(), } } } @@ -2398,20 +2394,18 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { return expected; } } - InheritedRefMatchRule::EatBoth { - eat_inherited_ref_alone: true, - fallback_to_outer, - } => { + InheritedRefMatchRule::EatBoth { consider_inherited_ref_first: true } => { // Reset binding mode on old editions pat_info.binding_mode = ByRef::No; if let ty::Ref(_, inner_ty, _) = *expected.kind() { // Consume both the inherited and inner references. - if fallback_to_outer && inh_mut.is_mut() { - // If we can fall back to matching the inherited reference, the expected - // type is a reference type (of any mutability), and the inherited - // reference is mutable, we'll always be able to match. We handle that - // here to avoid adding fallback-to-outer to the common logic below. + if inh_mut.is_mut() { + // If the expected type is a reference type (of any mutability) and the + // inherited ref is mutable, we'll be able to match, since we can fall + // back to matching the inherited ref if the real reference isn't + // mutable enough for our pattern. We handle that here to avoid adding + // fallback-to-outer to the common logic below. // NB: This way of phrasing the logic will catch more cases than those // that need to fall back to matching the inherited reference. However, // as long as `&` patterns can match mutable (inherited) references @@ -2440,13 +2434,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { return expected; } } - rule @ InheritedRefMatchRule::EatBoth { - eat_inherited_ref_alone: false, - fallback_to_outer, - } => { + InheritedRefMatchRule::EatBoth { consider_inherited_ref_first: false } => { // Reset binding mode on stable Rust. This will be a type error below if // `expected` is not a reference type. - debug_assert!(!fallback_to_outer, "typing rule `{rule:?}` is unimplemented."); pat_info.binding_mode = ByRef::No; self.add_rust_2024_migration_desugared_pat( pat_info.top_info.hir_id, diff --git a/src/doc/unstable-book/src/language-features/ref-pat-eat-one-layer-2024.md b/src/doc/unstable-book/src/language-features/ref-pat-eat-one-layer-2024.md index 58d8d2905030c..0c90cec0dbddb 100644 --- a/src/doc/unstable-book/src/language-features/ref-pat-eat-one-layer-2024.md +++ b/src/doc/unstable-book/src/language-features/ref-pat-eat-one-layer-2024.md @@ -16,5 +16,5 @@ For more information, see the corresponding typing rules for [Editions 2021 and For alternative experimental match ergonomics, see the feature [`ref_pat_eat_one_layer_2024_structural`](./ref-pat-eat-one-layer-2024-structural.md). -[Editions 2021 and earlier]: https://nadrieril.github.io/typing-rust-patterns/?compare=false&opts1=AQEBAQABAQABAAAAAQEBAAEBAAABAAA%3D&mode=rules&do_cmp=false +[Editions 2021 and earlier]: https://nadrieril.github.io/typing-rust-patterns/?compare=false&opts1=AQEBAQIBAQABAAAAAQEBAAEBAAABAAA%3D&mode=rules&do_cmp=false [Editions 2024 and later]: https://nadrieril.github.io/typing-rust-patterns/?compare=false&opts1=AQEBAAABAQABAgIAAQEBAAEBAAABAAA%3D&mode=rules&do_cmp=false diff --git a/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/well-typed-edition-2024.classic2021.stderr b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/well-typed-edition-2024.classic2021.stderr index 086d4bb133ba2..f8c2bd9a92128 100644 --- a/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/well-typed-edition-2024.classic2021.stderr +++ b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/well-typed-edition-2024.classic2021.stderr @@ -63,122 +63,60 @@ LL + if let Some(&Some(x)) = &mut Some(&Some(0)) { | error[E0308]: mismatched types - --> $DIR/well-typed-edition-2024.rs:96:10 + --> $DIR/well-typed-edition-2024.rs:123:15 | -LL | let [&mut x] = &mut [&0]; - | ^^^^^^ --------- this expression has type `&mut [&{integer}; 1]` - | | - | types differ in mutability - | - = note: expected reference `&{integer}` - found mutable reference `&mut _` -note: to declare a mutable binding use: `mut x` - --> $DIR/well-typed-edition-2024.rs:96:10 - | -LL | let [&mut x] = &mut [&0]; - | ^^^^^^ -help: consider removing `&mut` from the pattern - | -LL - let [&mut x] = &mut [&0]; -LL + let [x] = &mut [&0]; - | - -error[E0308]: mismatched types - --> $DIR/well-typed-edition-2024.rs:102:10 - | -LL | let [&mut ref x] = &mut [&0]; - | ^^^^^^^^^^ --------- this expression has type `&mut [&{integer}; 1]` - | | - | types differ in mutability - | - = note: expected reference `&{integer}` - found mutable reference `&mut _` -note: to declare a mutable binding use: `mut x` - --> $DIR/well-typed-edition-2024.rs:102:10 - | -LL | let [&mut ref x] = &mut [&0]; - | ^^^^^^^^^^ -help: consider removing `&mut` from the pattern - | -LL - let [&mut ref x] = &mut [&0]; -LL + let [ref x] = &mut [&0]; - | - -error[E0308]: mismatched types - --> $DIR/well-typed-edition-2024.rs:117:10 - | -LL | let [&mut mut x] = &mut [&0]; - | ^^^^^^^^^^ --------- this expression has type `&mut [&{integer}; 1]` - | | - | types differ in mutability - | - = note: expected reference `&{integer}` - found mutable reference `&mut _` -note: to declare a mutable binding use: `mut x` - --> $DIR/well-typed-edition-2024.rs:117:10 +LL | let [&mut &x] = &mut [&0]; + | ^^ --------- this expression has type `&mut [&{integer}; 1]` + | | + | expected integer, found `&_` | -LL | let [&mut mut x] = &mut [&0]; - | ^^^^^^^^^^ -help: consider removing `&mut` from the pattern + = note: expected type `{integer}` + found reference `&_` +help: consider removing `&` from the pattern | -LL - let [&mut mut x] = &mut [&0]; -LL + let [mut x] = &mut [&0]; +LL - let [&mut &x] = &mut [&0]; +LL + let [&mut x] = &mut [&0]; | error[E0308]: mismatched types - --> $DIR/well-typed-edition-2024.rs:123:10 + --> $DIR/well-typed-edition-2024.rs:129:15 | -LL | let [&mut &x] = &mut [&0]; - | ^^^^^^^ --------- this expression has type `&mut [&{integer}; 1]` - | | - | types differ in mutability +LL | let [&mut &ref x] = &mut [&0]; + | ^^^^^^ --------- this expression has type `&mut [&{integer}; 1]` + | | + | expected integer, found `&_` | - = note: expected reference `&{integer}` - found mutable reference `&mut _` - -error[E0308]: mismatched types - --> $DIR/well-typed-edition-2024.rs:129:10 + = note: expected type `{integer}` + found reference `&_` +help: consider removing `&` from the pattern | -LL | let [&mut &ref x] = &mut [&0]; - | ^^^^^^^^^^^ --------- this expression has type `&mut [&{integer}; 1]` - | | - | types differ in mutability +LL - let [&mut &ref x] = &mut [&0]; +LL + let [&mut ref x] = &mut [&0]; | - = note: expected reference `&{integer}` - found mutable reference `&mut _` error[E0308]: mismatched types - --> $DIR/well-typed-edition-2024.rs:135:10 + --> $DIR/well-typed-edition-2024.rs:135:15 | LL | let [&mut &(mut x)] = &mut [&0]; - | ^^^^^^^^^^^^^ --------- this expression has type `&mut [&{integer}; 1]` - | | - | types differ in mutability + | ^^^^^^^^ --------- this expression has type `&mut [&{integer}; 1]` + | | + | expected integer, found `&_` | - = note: expected reference `&{integer}` - found mutable reference `&mut _` - -error[E0308]: mismatched types - --> $DIR/well-typed-edition-2024.rs:109:14 + = note: expected type `{integer}` + found reference `&_` +help: consider removing `&` from the pattern | -LL | let [&mut ref mut x] = &mut [&0]; - | ^^^^^^^^^^^^^^ --------- this expression has type `&mut [&{integer}; 1]` - | | - | types differ in mutability +LL - let [&mut &(mut x)] = &mut [&0]; +LL + let [&mut mut x)] = &mut [&0]; | - = note: expected reference `&{integer}` - found mutable reference `&mut _` -note: to declare a mutable binding use: `mut x` - --> $DIR/well-typed-edition-2024.rs:109:14 + +error[E0596]: cannot borrow data in a `&` reference as mutable + --> $DIR/well-typed-edition-2024.rs:109:19 | LL | let [&mut ref mut x] = &mut [&0]; - | ^^^^^^^^^^^^^^ -help: consider removing `&mut` from the pattern - | -LL - let [&mut ref mut x] = &mut [&0]; -LL + let [ref mut x] = &mut [&0]; - | + | ^^^^^^^^^ cannot borrow as mutable -error: aborting due to 11 previous errors +error: aborting due to 8 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`. diff --git a/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/well-typed-edition-2024.rs b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/well-typed-edition-2024.rs index 63a09fe90c9f2..62c1c28022b7d 100644 --- a/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/well-typed-edition-2024.rs +++ b/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/well-typed-edition-2024.rs @@ -94,47 +94,47 @@ pub fn main() { // Tests for eat-inner and eat-both rulesets matching on the outer reference if matching on the // inner reference causes a mutability mismatch. i.e. tests for "fallback-to-outer" deref rules. let [&mut x] = &mut [&0]; - //[stable2021,classic2021]~^ mismatched types - //[stable2021,classic2021]~| types differ in mutability - #[cfg(structural2021)] let _: u32 = x; + //[stable2021]~^ mismatched types + //[stable2021]~| types differ in mutability + #[cfg(any(classic2021, structural2021))] let _: u32 = x; #[cfg(any(classic2024, structural2024))] let _: &u32 = x; let [&mut ref x] = &mut [&0]; - //[stable2021,classic2021]~^ mismatched types - //[stable2021,classic2021]~| types differ in mutability - #[cfg(structural2021)] let _: &u32 = x; + //[stable2021]~^ mismatched types + //[stable2021]~| types differ in mutability + #[cfg(any(classic2021, structural2021))] let _: &u32 = x; #[cfg(any(classic2024, structural2024))] let _: &&u32 = x; fn borrowck_error_on_structural2021() { let [&mut ref mut x] = &mut [&0]; - //[stable2021,classic2021]~^ mismatched types - //[stable2021,classic2021]~| types differ in mutability - //[structural2021]~^^^ cannot borrow data in a `&` reference as mutable + //[stable2021]~^ mismatched types + //[stable2021]~| types differ in mutability + //[classic2021,structural2021]~^^^ cannot borrow data in a `&` reference as mutable #[cfg(any(classic2024, structural2024))] let _: &mut &u32 = x; } borrowck_error_on_structural2021(); let [&mut mut x] = &mut [&0]; - //[stable2021,classic2021]~^ mismatched types - //[stable2021,classic2021]~| types differ in mutability - #[cfg(structural2021)] let _: u32 = x; + //[stable2021]~^ mismatched types + //[stable2021]~| types differ in mutability + #[cfg(any(classic2021, structural2021))] let _: u32 = x; #[cfg(any(classic2024, structural2024))] let _: &u32 = x; let [&mut &x] = &mut [&0]; //[stable2021,classic2021,structural2021]~^ mismatched types - //[stable2021,classic2021]~| types differ in mutability - //[structural2021]~| expected integer, found `&_` + //[stable2021]~| types differ in mutability + //[classic2021,structural2021]~| expected integer, found `&_` #[cfg(any(classic2024, structural2024))] let _: u32 = x; let [&mut &ref x] = &mut [&0]; //[stable2021,classic2021,structural2021]~^ mismatched types - //[stable2021,classic2021]~| types differ in mutability - //[structural2021]~| expected integer, found `&_` + //[stable2021]~| types differ in mutability + //[classic2021,structural2021]~| expected integer, found `&_` #[cfg(any(classic2024, structural2024))] let _: &u32 = x; let [&mut &(mut x)] = &mut [&0]; //[stable2021,classic2021,structural2021]~^ mismatched types - //[stable2021,classic2021]~| types differ in mutability - //[structural2021]~| expected integer, found `&_` + //[stable2021]~| types differ in mutability + //[classic2021,structural2021]~| expected integer, found `&_` #[cfg(any(classic2024, structural2024))] let _: u32 = x; } From 37bcc1cc5ccd6b87257388e11a7573df61ebc990 Mon Sep 17 00:00:00 2001 From: dianne Date: Sun, 16 Feb 2025 16:20:52 -0800 Subject: [PATCH 09/11] clarify wording on doc comment Co-authored-by: Nadrieril --- compiler/rustc_hir_typeck/src/pat.rs | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/compiler/rustc_hir_typeck/src/pat.rs b/compiler/rustc_hir_typeck/src/pat.rs index 4201ef6674120..40df87188cd32 100644 --- a/compiler/rustc_hir_typeck/src/pat.rs +++ b/compiler/rustc_hir_typeck/src/pat.rs @@ -232,12 +232,15 @@ enum InheritedRefMatchRule { /// When the underlying type is a reference type, reference patterns consume both layers of /// reference, i.e. they both reset the binding mode and consume the reference type. EatBoth { - /// This represents two behaviors implemented by both the `ref_pat_eat_one_layer_2024` and - /// `ref_pat_eat_one_layer_2024_structural` feature gates, and is false for stable Rust. - /// - Whether to allow reference patterns to consume only an inherited reference when - /// matching against a non-reference type. - /// - Whether to allow a `&mut` reference pattern to eat a `&` reference type if it's also - /// able to consume a mutable inherited reference. + /// If `true`, an inherited reference will be considered when determining whether a reference + /// pattern matches a given type: + /// - If the underlying type is not a reference, a reference pattern may eat the inherited reference; + /// - If the underlying type is a reference, a reference pattern matches if it can eat either one + /// of the underlying and inherited references. E.g. a `&mut` pattern is allowed if either the + /// underlying type is `&mut` or the inherited reference is `&mut`. + /// If `false`, a reference pattern is only matched against the underlying type. + /// This is `false` for stable Rust and `true` for both the `ref_pat_eat_one_layer_2024` and + /// `ref_pat_eat_one_layer_2024_structural` feature gates. consider_inherited_ref_first: bool, }, } From 0e758c4ba6d9165106f6db768e23f01fabfd2d8c Mon Sep 17 00:00:00 2001 From: dianne Date: Sun, 16 Feb 2025 17:04:26 -0800 Subject: [PATCH 10/11] rename `consider_inherited_ref_first` -> `consider_inherited_ref` --- compiler/rustc_hir_typeck/src/pat.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/compiler/rustc_hir_typeck/src/pat.rs b/compiler/rustc_hir_typeck/src/pat.rs index 40df87188cd32..9456247de887f 100644 --- a/compiler/rustc_hir_typeck/src/pat.rs +++ b/compiler/rustc_hir_typeck/src/pat.rs @@ -241,7 +241,7 @@ enum InheritedRefMatchRule { /// If `false`, a reference pattern is only matched against the underlying type. /// This is `false` for stable Rust and `true` for both the `ref_pat_eat_one_layer_2024` and /// `ref_pat_eat_one_layer_2024_structural` feature gates. - consider_inherited_ref_first: bool, + consider_inherited_ref: bool, }, } @@ -268,11 +268,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { } else { // Currently, matching against an inherited ref on edition 2024 is an error. // Use `EatBoth` as a fallback to be similar to stable Rust. - InheritedRefMatchRule::EatBoth { consider_inherited_ref_first: false } + InheritedRefMatchRule::EatBoth { consider_inherited_ref: false } } } else { InheritedRefMatchRule::EatBoth { - consider_inherited_ref_first: self.tcx.features().ref_pat_eat_one_layer_2024() + consider_inherited_ref: self.tcx.features().ref_pat_eat_one_layer_2024() || self.tcx.features().ref_pat_eat_one_layer_2024_structural(), } } @@ -2397,7 +2397,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { return expected; } } - InheritedRefMatchRule::EatBoth { consider_inherited_ref_first: true } => { + InheritedRefMatchRule::EatBoth { consider_inherited_ref: true } => { // Reset binding mode on old editions pat_info.binding_mode = ByRef::No; @@ -2437,7 +2437,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { return expected; } } - InheritedRefMatchRule::EatBoth { consider_inherited_ref_first: false } => { + InheritedRefMatchRule::EatBoth { consider_inherited_ref: false } => { // Reset binding mode on stable Rust. This will be a type error below if // `expected` is not a reference type. pat_info.binding_mode = ByRef::No; From 0a15bfb340430075703689630ab99d3bc22a96d5 Mon Sep 17 00:00:00 2001 From: dianne Date: Sun, 16 Feb 2025 18:48:07 -0800 Subject: [PATCH 11/11] simplify fallback-to-outer condition on old editions --- compiler/rustc_hir_typeck/src/pat.rs | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/compiler/rustc_hir_typeck/src/pat.rs b/compiler/rustc_hir_typeck/src/pat.rs index 9456247de887f..73a1432b06ce6 100644 --- a/compiler/rustc_hir_typeck/src/pat.rs +++ b/compiler/rustc_hir_typeck/src/pat.rs @@ -2403,17 +2403,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { if let ty::Ref(_, inner_ty, _) = *expected.kind() { // Consume both the inherited and inner references. - if inh_mut.is_mut() { - // If the expected type is a reference type (of any mutability) and the - // inherited ref is mutable, we'll be able to match, since we can fall - // back to matching the inherited ref if the real reference isn't - // mutable enough for our pattern. We handle that here to avoid adding - // fallback-to-outer to the common logic below. - // NB: This way of phrasing the logic will catch more cases than those - // that need to fall back to matching the inherited reference. However, - // as long as `&` patterns can match mutable (inherited) references - // (RFC 3627, Rule 5) this should be sound. - debug_assert!(ref_pat_matches_mut_ref); + if pat_mutbl.is_mut() && inh_mut.is_mut() { + // As a special case, a `&mut` reference pattern will be able to match + // against a reference type of any mutability if the inherited ref is + // mutable. Since this allows us to match against a shared reference + // type, we refer to this as "falling back" to matching the inherited + // reference, though we consume the real reference as well. We handle + // this here to avoid adding this case to the common logic below. self.check_pat(inner, inner_ty, pat_info); return expected; } else {