Skip to content

Commit f516b7b

Browse files
committed
add mixed-edition tests
1 parent 8519577 commit f516b7b

File tree

6 files changed

+476
-0
lines changed

6 files changed

+476
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
//@[classic2021] edition: 2024
2+
//@[structural2021] edition: 2024
3+
//@[classic2024] edition: 2021
4+
//@[structural2024] edition: 2021
5+
//! This contains macros in an edition *different* to the one used in `../mixed-editions.rs`, in
6+
//! order to test typing mixed-edition patterns.
7+
8+
#[macro_export]
9+
macro_rules! match_ctor {
10+
($p:pat) => {
11+
[$p]
12+
};
13+
}
14+
15+
#[macro_export]
16+
macro_rules! match_ref {
17+
($p:pat) => {
18+
&$p
19+
};
20+
}
21+
22+
#[macro_export]
23+
macro_rules! bind {
24+
($i:ident) => {
25+
$i
26+
}
27+
}
28+
29+
#[macro_export]
30+
macro_rules! bind_ref {
31+
($i:ident) => {
32+
ref $i
33+
}
34+
}
35+
36+
#[macro_export]
37+
macro_rules! bind_mut {
38+
($i:ident) => {
39+
mut $i
40+
}
41+
}
42+
43+
#[macro_export]
44+
macro_rules! bind_ref_mut {
45+
($i:ident) => {
46+
ref mut $i
47+
}
48+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
error[E0658]: binding cannot be both mutable and by-reference
2+
--> $DIR/mixed-editions.rs:41:10
3+
|
4+
LL | let [bind_mut!(y)] = &[0];
5+
| ^^^^^^^^^^^^
6+
|
7+
= note: see issue #123076 <https://github.com/rust-lang/rust/issues/123076> for more information
8+
= help: add `#![feature(mut_ref)]` to the crate attributes to enable
9+
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
10+
= note: this error originates in the macro `bind_mut` (in Nightly builds, run with -Z macro-backtrace for more info)
11+
12+
error[E0596]: cannot borrow as mutable inside an `&` pattern
13+
--> $DIR/mixed-editions.rs:76:21
14+
|
15+
LL | let match_ref!([x]) = &mut &mut [0];
16+
| ^
17+
18+
error[E0596]: cannot borrow as mutable inside an `&` pattern
19+
--> $DIR/mixed-editions.rs:80:22
20+
|
21+
LL | let &match_ctor!(y) = &mut &mut [0];
22+
| - ^
23+
| |
24+
| help: replace this `&` with `&mut`: `&mut`
25+
26+
error[E0596]: cannot borrow as mutable inside an `&` pattern
27+
--> $DIR/mixed-editions.rs:84:17
28+
|
29+
LL | let &[bind!(z)] = &mut &mut [0];
30+
| - ^
31+
| |
32+
| help: replace this `&` with `&mut`: `&mut`
33+
34+
error[E0596]: cannot borrow as mutable inside an `&` pattern
35+
--> $DIR/mixed-editions.rs:110:28
36+
|
37+
LL | let match_ref!(ref mut x) = &mut 0;
38+
| ^
39+
40+
error[E0596]: cannot borrow as mutable inside an `&` pattern
41+
--> $DIR/mixed-editions.rs:112:24
42+
|
43+
LL | let &bind_ref_mut!(x) = &mut 0;
44+
| - ^
45+
| |
46+
| help: replace this `&` with `&mut`: `&mut`
47+
48+
error[E0596]: cannot borrow as mutable inside an `&` pattern
49+
--> $DIR/mixed-editions.rs:116:29
50+
|
51+
LL | let [match_ref!(ref mut x)] = &mut [0];
52+
| ^
53+
54+
error[E0596]: cannot borrow as mutable inside an `&` pattern
55+
--> $DIR/mixed-editions.rs:118:25
56+
|
57+
LL | let [&bind_ref_mut!(x)] = &mut [0];
58+
| - ^
59+
| |
60+
| help: replace this `&` with `&mut`: `&mut`
61+
62+
error: this pattern relies on behavior which may change in edition 2024
63+
--> $DIR/mixed-editions.rs:30:10
64+
|
65+
LL | let [bind_ref!(y)] = &[0];
66+
| ^^^^^^^^^^^^ default binding mode is reset within expansion
67+
|
68+
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2024/match-ergonomics.html>
69+
= note: this error originates in the macro `bind_ref` (in Nightly builds, run with -Z macro-backtrace for more info)
70+
help: make the implied reference pattern explicit
71+
|
72+
LL | let &[bind_ref!(y)] = &[0];
73+
| +
74+
75+
error[E0596]: cannot borrow data in a `&` reference as mutable
76+
--> $DIR/mixed-editions.rs:61:21
77+
|
78+
LL | let match_ref!([x]) = &&mut [0];
79+
| ^ cannot borrow as mutable
80+
81+
error[E0596]: cannot borrow data in a `&` reference as mutable
82+
--> $DIR/mixed-editions.rs:65:22
83+
|
84+
LL | let &match_ctor!(y) = &&mut [0];
85+
| ^ cannot borrow as mutable
86+
87+
error[E0596]: cannot borrow data in a `&` reference as mutable
88+
--> $DIR/mixed-editions.rs:69:11
89+
|
90+
LL | let &[bind!(z)] = &&mut [0];
91+
| ^^^^^^^^ cannot borrow as mutable
92+
|
93+
= note: this error originates in the macro `bind` (in Nightly builds, run with -Z macro-backtrace for more info)
94+
95+
error: aborting due to 12 previous errors
96+
97+
Some errors have detailed explanations: E0596, E0658.
98+
For more information about an error, try `rustc --explain E0596`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
error[E0658]: binding cannot be both mutable and by-reference
2+
--> $DIR/mixed-editions.rs:37:21
3+
|
4+
LL | let match_ctor!(mut x) = &[0];
5+
| ^^^^
6+
|
7+
= note: see issue #123076 <https://github.com/rust-lang/rust/issues/123076> for more information
8+
= help: add `#![feature(mut_ref)]` to the crate attributes to enable
9+
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
10+
11+
error[E0596]: cannot borrow as mutable inside an `&` pattern
12+
--> $DIR/mixed-editions.rs:76:21
13+
|
14+
LL | let match_ref!([x]) = &mut &mut [0];
15+
| ^
16+
17+
error[E0596]: cannot borrow as mutable inside an `&` pattern
18+
--> $DIR/mixed-editions.rs:80:22
19+
|
20+
LL | let &match_ctor!(y) = &mut &mut [0];
21+
| - ^
22+
| |
23+
| help: replace this `&` with `&mut`: `&mut`
24+
25+
error[E0596]: cannot borrow as mutable inside an `&` pattern
26+
--> $DIR/mixed-editions.rs:84:17
27+
|
28+
LL | let &[bind!(z)] = &mut &mut [0];
29+
| - ^
30+
| |
31+
| help: replace this `&` with `&mut`: `&mut`
32+
33+
error[E0596]: cannot borrow as mutable inside an `&` pattern
34+
--> $DIR/mixed-editions.rs:110:28
35+
|
36+
LL | let match_ref!(ref mut x) = &mut 0;
37+
| ^
38+
39+
error[E0596]: cannot borrow as mutable inside an `&` pattern
40+
--> $DIR/mixed-editions.rs:112:24
41+
|
42+
LL | let &bind_ref_mut!(x) = &mut 0;
43+
| - ^
44+
| |
45+
| help: replace this `&` with `&mut`: `&mut`
46+
47+
error[E0596]: cannot borrow as mutable inside an `&` pattern
48+
--> $DIR/mixed-editions.rs:116:29
49+
|
50+
LL | let [match_ref!(ref mut x)] = &mut [0];
51+
| ^
52+
53+
error[E0596]: cannot borrow as mutable inside an `&` pattern
54+
--> $DIR/mixed-editions.rs:118:25
55+
|
56+
LL | let [&bind_ref_mut!(x)] = &mut [0];
57+
| - ^
58+
| |
59+
| help: replace this `&` with `&mut`: `&mut`
60+
61+
error: this pattern relies on behavior which may change in edition 2024
62+
--> $DIR/mixed-editions.rs:26:21
63+
|
64+
LL | let match_ctor!(ref x) = &[0];
65+
| ^^^ cannot override to bind by-reference when that is the implicit default
66+
|
67+
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2024/match-ergonomics.html>
68+
help: make the implied reference pattern explicit
69+
--> $DIR/auxiliary/mixed-editions-macros.rs:11:9
70+
|
71+
LL | &[$p]
72+
| +
73+
74+
error[E0596]: cannot borrow data in a `&` reference as mutable
75+
--> $DIR/mixed-editions.rs:61:21
76+
|
77+
LL | let match_ref!([x]) = &&mut [0];
78+
| ^ cannot borrow as mutable
79+
80+
error[E0596]: cannot borrow data in a `&` reference as mutable
81+
--> $DIR/mixed-editions.rs:65:22
82+
|
83+
LL | let &match_ctor!(y) = &&mut [0];
84+
| ^ cannot borrow as mutable
85+
86+
error[E0596]: cannot borrow data in a `&` reference as mutable
87+
--> $DIR/mixed-editions.rs:69:11
88+
|
89+
LL | let &[bind!(z)] = &&mut [0];
90+
| ^^^^^^^^ cannot borrow as mutable
91+
|
92+
= note: this error originates in the macro `bind` (in Nightly builds, run with -Z macro-backtrace for more info)
93+
94+
error: aborting due to 12 previous errors
95+
96+
Some errors have detailed explanations: E0596, E0658.
97+
For more information about an error, try `rustc --explain E0596`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
//@ revisions: classic2021 structural2021 classic2024 structural2024
2+
//@[classic2021] edition: 2021
3+
//@[structural2021] edition: 2021
4+
//@[classic2024] edition: 2024
5+
//@[structural2024] edition: 2024
6+
//@ aux-build:mixed-editions-macros.rs
7+
//! Tests for typing mixed-edition patterns under the `ref_pat_eat_one_layer_2024` and
8+
//! `ref_pat_eat_one_layer_2024_structural` feature gates.
9+
//! This is meant both to check that patterns are typed with edition-appropriate typing rules and
10+
//! that we keep our internal state consistent when mixing editions.
11+
#![allow(incomplete_features, unused)]
12+
#![cfg_attr(any(classic2021, classic2024), feature(ref_pat_eat_one_layer_2024))]
13+
#![cfg_attr(any(structural2021, structural2024), feature(ref_pat_eat_one_layer_2024_structural))]
14+
15+
extern crate mixed_editions_macros;
16+
use mixed_editions_macros::*;
17+
18+
// Tests type equality in a way that avoids coercing `&&T` to `&T`.
19+
trait Eq<T> {}
20+
impl<T> Eq<T> for T {}
21+
fn assert_type_eq<T, U: Eq<T>>(_: T, _: U) {}
22+
23+
/// Make sure binding with `ref` in the presence of an inherited reference is forbidden when and
24+
/// only when the binding is from edition 2024.
25+
fn ref_binding_tests() {
26+
let match_ctor!(ref x) = &[0];
27+
//[classic2024,structural2024]~^ ERROR: this pattern relies on behavior which may change in edition 2024
28+
#[cfg(any(classic2021, structural2021))] assert_type_eq(x, &0u32);
29+
30+
let [bind_ref!(y)] = &[0];
31+
//[classic2021,structural2021]~^ ERROR: this pattern relies on behavior which may change in edition 2024
32+
#[cfg(any(classic2024, structural2024))] assert_type_eq(y, &0u32);
33+
}
34+
35+
/// Likewise, when binding with `mut`.
36+
fn mut_binding_tests() {
37+
let match_ctor!(mut x) = &[0];
38+
//[classic2024,structural2024]~^ ERROR: binding cannot be both mutable and by-reference
39+
#[cfg(any(classic2021, structural2021))] assert_type_eq(x, 0u32);
40+
41+
let [bind_mut!(y)] = &[0];
42+
//[classic2021,structural2021]~^ ERROR: binding cannot be both mutable and by-reference
43+
#[cfg(any(classic2024, structural2024))] assert_type_eq(y, 0u32);
44+
}
45+
46+
/// Make sure reference patterns correspond to one deref on edition 2024 and two on edition 2021.
47+
fn layers_eaten_tests() {
48+
let match_ctor!(&x) = &[&0];
49+
#[cfg(any(classic2021, structural2021))] assert_type_eq(x, 0u32);
50+
#[cfg(any(classic2024, structural2024))] assert_type_eq(x, &0u32);
51+
52+
let [match_ref!(y)] = &[&0];
53+
#[cfg(any(classic2021, structural2021))] assert_type_eq(y, &0u32);
54+
#[cfg(any(classic2024, structural2024))] assert_type_eq(y, 0u32);
55+
}
56+
57+
/// Make sure downgrading mutable binding modes inside shared refs ("Rule 3") doesn't break.
58+
/// This only applies to `ref_pat_eat_one_layer_2024_structural`, which has Rule 3 in all editions;
59+
/// under `ref_pat_eat_one_layer_2024`, these should be errors.
60+
fn rule_3_tests() {
61+
let match_ref!([x]) = &&mut [0];
62+
//[classic2021,classic2024]~^ ERROR: cannot borrow data in a `&` reference as mutable
63+
#[cfg(any(structural2021, structural2024))] assert_type_eq(x, &0u32);
64+
65+
let &match_ctor!(y) = &&mut [0];
66+
//[classic2021,classic2024]~^ ERROR: cannot borrow data in a `&` reference as mutable
67+
#[cfg(any(structural2021, structural2024))] assert_type_eq(y, &0u32);
68+
69+
let &[bind!(z)] = &&mut [0];
70+
//[classic2021,classic2024]~^ ERROR: cannot borrow data in a `&` reference as mutable
71+
#[cfg(any(structural2021, structural2024))] assert_type_eq(z, &0u32);
72+
}
73+
74+
/// Test that the interaction between Rules 3 and 5 doesn't break.
75+
fn rules_3_and_5_tests() {
76+
let match_ref!([x]) = &mut &mut [0];
77+
//[classic2021,classic2024]~^ ERROR: cannot borrow as mutable inside an `&` pattern
78+
#[cfg(any(structural2021, structural2024))] assert_type_eq(x, &0u32);
79+
80+
let &match_ctor!(y) = &mut &mut [0];
81+
//[classic2021,classic2024]~^ ERROR: cannot borrow as mutable inside an `&` pattern
82+
#[cfg(any(structural2021, structural2024))] assert_type_eq(y, &0u32);
83+
84+
let &[bind!(z)] = &mut &mut [0];
85+
//[classic2021,classic2024]~^ ERROR: cannot borrow as mutable inside an `&` pattern
86+
#[cfg(any(structural2021, structural2024))] assert_type_eq(z, &0u32);
87+
}
88+
89+
/// Make sure matching a lone shared reference with a `&` ("Rule 4") doesn't break.
90+
fn rule_4_tests() {
91+
let match_ref!([x]) = &[0];
92+
assert_type_eq(x, 0u32);
93+
94+
let &match_ctor!(y) = &[0];
95+
assert_type_eq(y, 0u32);
96+
}
97+
98+
/// Make sure matching a `&mut` reference with a `&` pattern ("Rule 5") doesn't break.
99+
fn rule_5_tests() {
100+
let match_ref!(x) = &mut 0;
101+
assert_type_eq(x, 0u32);
102+
103+
// also test inherited references (assumes rule 4)
104+
let [match_ref!(y)] = &mut [0];
105+
assert_type_eq(y, 0u32);
106+
}
107+
108+
/// Make sure binding with `ref mut` is an error within a `&` pattern matching a `&mut` reference.
109+
fn rule_5_mutability_error_tests() {
110+
let match_ref!(ref mut x) = &mut 0;
111+
//~^ ERROR: cannot borrow as mutable inside an `&` pattern
112+
let &bind_ref_mut!(x) = &mut 0;
113+
//~^ ERROR: cannot borrow as mutable inside an `&` pattern
114+
115+
// also test inherited references (assumes rule 4)
116+
let [match_ref!(ref mut x)] = &mut [0];
117+
//~^ ERROR: cannot borrow as mutable inside an `&` pattern
118+
let [&bind_ref_mut!(x)] = &mut [0];
119+
//~^ ERROR: cannot borrow as mutable inside an `&` pattern
120+
}
121+
122+
fn main() {}

0 commit comments

Comments
 (0)