about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/auxiliary/mixed-editions-macros.rs48
-rw-r--r--tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/mixed-editions.classic2021.stderr103
-rw-r--r--tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/mixed-editions.classic2024.stderr97
-rw-r--r--tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/mixed-editions.rs122
-rw-r--r--tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/mixed-editions.structural2021.stderr61
-rw-r--r--tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/mixed-editions.structural2024.stderr55
6 files changed, 486 insertions, 0 deletions
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 00000000000..14d26be91a0
--- /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 00000000000..7e3caaf9797
--- /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 <https://github.com/rust-lang/rust/issues/123076> for more information
+   = help: add `#![feature(mut_ref)]` to the crate attributes to enable
+   = note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
+   = 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 <https://doc.rust-lang.org/nightly/edition-guide/rust-2024/match-ergonomics.html>
+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 00000000000..466993a1671
--- /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 <https://github.com/rust-lang/rust/issues/123076> for more information
+   = help: add `#![feature(mut_ref)]` to the crate attributes to enable
+   = note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
+
+error[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 <https://doc.rust-lang.org/nightly/edition-guide/rust-2024/match-ergonomics.html>
+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 00000000000..0a22b55ab63
--- /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<T> {}
+impl<T> Eq<T> for T {}
+fn assert_type_eq<T, U: Eq<T>>(_: 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 00000000000..4075dc9529d
--- /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 <https://github.com/rust-lang/rust/issues/123076> for more information
+   = help: add `#![feature(mut_ref)]` to the crate attributes to enable
+   = note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
+   = 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 <https://doc.rust-lang.org/nightly/edition-guide/rust-2024/match-ergonomics.html>
+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 00000000000..819a54299ea
--- /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 <https://github.com/rust-lang/rust/issues/123076> for more information
+   = help: add `#![feature(mut_ref)]` to the crate attributes to enable
+   = note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
+
+error[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 <https://doc.rust-lang.org/nightly/edition-guide/rust-2024/match-ergonomics.html>
+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`.