Skip to content

Commit aa24aa5

Browse files
committed
Add tests to check the situation with heterogenous spans
The edition gate is a bit stricter than the drop behaviour, which is fine. The cases we want to avoid are the opposite: not gated but 2021 drop behaviour.
1 parent ee7488a commit aa24aa5

6 files changed

+269
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//@ edition: 2021
2+
3+
#[macro_export]
4+
macro_rules! make_if {
5+
(($($tt:tt)*) { $body:expr } { $else:expr }) => {{
6+
if $($tt)* {
7+
$body
8+
} else {
9+
$else
10+
}
11+
}};
12+
(let ($expr:expr) { $body:expr } { $else:expr }) => {{
13+
if let None = $expr {
14+
$body
15+
} else {
16+
$else
17+
}
18+
}};
19+
(let ($expr:expr) let ($expr2:expr) { $body:expr } { $else:expr }) => {{
20+
if let None = $expr && let None = $expr2 {
21+
$body
22+
} else {
23+
$else
24+
}
25+
}};
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//@ edition: 2024
2+
//@ compile-flags: -Z unstable-options
3+
4+
#[macro_export]
5+
macro_rules! make_if {
6+
(($($tt:tt)*) { $body:expr } { $else:expr }) => {{
7+
if $($tt)* {
8+
$body
9+
} else {
10+
$else
11+
}
12+
}};
13+
(let ($expr:expr) { $body:expr } { $else:expr }) => {{
14+
if let None = $expr {
15+
$body
16+
} else {
17+
$else
18+
}
19+
}};
20+
(let ($expr:expr) let ($expr2:expr) { $body:expr } { $else:expr }) => {{
21+
if let None = $expr && let None = $expr2 {
22+
$body
23+
} else {
24+
$else
25+
}
26+
}};
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
error[E0658]: `let` expressions in this position are unstable
2+
--> $DIR/edition-gate-macro-error.rs:19:30
3+
|
4+
LL | macro_in_2021::make_if!((let Some(0) = None && let Some(0) = None) { never!() } { never!() });
5+
| ^^^^^^^^^^^^^^^^^^
6+
|
7+
= note: see issue #53667 <https://github.com/rust-lang/rust/issues/53667> for more information
8+
= help: add `#![feature(let_chains)]` 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[E0658]: `let` expressions in this position are unstable
12+
--> $DIR/edition-gate-macro-error.rs:19:52
13+
|
14+
LL | macro_in_2021::make_if!((let Some(0) = None && let Some(0) = None) { never!() } { never!() });
15+
| ^^^^^^^^^^^^^^^^^^
16+
|
17+
= note: see issue #53667 <https://github.com/rust-lang/rust/issues/53667> for more information
18+
= help: add `#![feature(let_chains)]` to the crate attributes to enable
19+
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
20+
21+
error[E0658]: `let` expressions in this position are unstable
22+
--> $DIR/edition-gate-macro-error.rs:22:5
23+
|
24+
LL | macro_in_2021::make_if!(let (Some(0)) let (Some(0)) { never!() } { never!() });
25+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
26+
|
27+
= note: see issue #53667 <https://github.com/rust-lang/rust/issues/53667> for more information
28+
= help: add `#![feature(let_chains)]` to the crate attributes to enable
29+
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
30+
= note: this error originates in the macro `macro_in_2021::make_if` (in Nightly builds, run with -Z macro-backtrace for more info)
31+
32+
error[E0658]: `let` expressions in this position are unstable
33+
--> $DIR/edition-gate-macro-error.rs:22:5
34+
|
35+
LL | macro_in_2021::make_if!(let (Some(0)) let (Some(0)) { never!() } { never!() });
36+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
37+
|
38+
= note: see issue #53667 <https://github.com/rust-lang/rust/issues/53667> for more information
39+
= help: add `#![feature(let_chains)]` to the crate attributes to enable
40+
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
41+
= note: this error originates in the macro `macro_in_2021::make_if` (in Nightly builds, run with -Z macro-backtrace for more info)
42+
43+
error[E0658]: `let` expressions in this position are unstable
44+
--> $DIR/edition-gate-macro-error.rs:26:30
45+
|
46+
LL | macro_in_2024::make_if!((let Some(0) = None && let Some(0) = None) { never!() } { never!() });
47+
| ^^^^^^^^^^^^^^^^^^
48+
|
49+
= note: see issue #53667 <https://github.com/rust-lang/rust/issues/53667> for more information
50+
= help: add `#![feature(let_chains)]` to the crate attributes to enable
51+
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
52+
53+
error[E0658]: `let` expressions in this position are unstable
54+
--> $DIR/edition-gate-macro-error.rs:26:52
55+
|
56+
LL | macro_in_2024::make_if!((let Some(0) = None && let Some(0) = None) { never!() } { never!() });
57+
| ^^^^^^^^^^^^^^^^^^
58+
|
59+
= note: see issue #53667 <https://github.com/rust-lang/rust/issues/53667> for more information
60+
= help: add `#![feature(let_chains)]` to the crate attributes to enable
61+
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
62+
63+
error: aborting due to 6 previous errors
64+
65+
For more information about this error, try `rustc --explain E0658`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
error[E0658]: `let` expressions in this position are unstable
2+
--> $DIR/edition-gate-macro-error.rs:19:30
3+
|
4+
LL | macro_in_2021::make_if!((let Some(0) = None && let Some(0) = None) { never!() } { never!() });
5+
| ^^^^^^^^^^^^^^^^^^
6+
|
7+
= note: see issue #53667 <https://github.com/rust-lang/rust/issues/53667> for more information
8+
= help: add `#![feature(let_chains)]` 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[E0658]: `let` expressions in this position are unstable
12+
--> $DIR/edition-gate-macro-error.rs:19:52
13+
|
14+
LL | macro_in_2021::make_if!((let Some(0) = None && let Some(0) = None) { never!() } { never!() });
15+
| ^^^^^^^^^^^^^^^^^^
16+
|
17+
= note: see issue #53667 <https://github.com/rust-lang/rust/issues/53667> for more information
18+
= help: add `#![feature(let_chains)]` to the crate attributes to enable
19+
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
20+
21+
error[E0658]: `let` expressions in this position are unstable
22+
--> $DIR/edition-gate-macro-error.rs:22:5
23+
|
24+
LL | macro_in_2021::make_if!(let (Some(0)) let (Some(0)) { never!() } { never!() });
25+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
26+
|
27+
= note: see issue #53667 <https://github.com/rust-lang/rust/issues/53667> for more information
28+
= help: add `#![feature(let_chains)]` to the crate attributes to enable
29+
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
30+
= note: this error originates in the macro `macro_in_2021::make_if` (in Nightly builds, run with -Z macro-backtrace for more info)
31+
32+
error[E0658]: `let` expressions in this position are unstable
33+
--> $DIR/edition-gate-macro-error.rs:22:5
34+
|
35+
LL | macro_in_2021::make_if!(let (Some(0)) let (Some(0)) { never!() } { never!() });
36+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
37+
|
38+
= note: see issue #53667 <https://github.com/rust-lang/rust/issues/53667> for more information
39+
= help: add `#![feature(let_chains)]` to the crate attributes to enable
40+
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
41+
= note: this error originates in the macro `macro_in_2021::make_if` (in Nightly builds, run with -Z macro-backtrace for more info)
42+
43+
error: aborting due to 4 previous errors
44+
45+
For more information about this error, try `rustc --explain E0658`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//@ revisions: edition2021 edition2024
2+
//@ compile-flags: -Z unstable-options -Z lint-mir -Z validate-mir
3+
//@ [edition2021] edition: 2021
4+
//@ [edition2024] edition: 2024
5+
//@ aux-build:macro-in-2021.rs
6+
//@ aux-build:macro-in-2024.rs
7+
8+
use std::unreachable as never;
9+
10+
// Compiletest doesn't specify the needed --extern flags to make `extern crate` unneccessary
11+
extern crate macro_in_2021;
12+
extern crate macro_in_2024;
13+
14+
fn main() {
15+
// Gated on both 2021 and 2024 if the `if` comes from a 2021 macro
16+
// Gated only on 2021 if the `if` comes from a 2024 macro
17+
// No gating if both the `if` and the chain are from a 2024 macro
18+
19+
macro_in_2021::make_if!((let Some(0) = None && let Some(0) = None) { never!() } { never!() });
20+
//~^ ERROR `let` expressions in this position are unstable
21+
//~| ERROR `let` expressions in this position are unstable
22+
macro_in_2021::make_if!(let (Some(0)) let (Some(0)) { never!() } { never!() });
23+
//~^ ERROR `let` expressions in this position are unstable
24+
//~| ERROR `let` expressions in this position are unstable
25+
26+
macro_in_2024::make_if!((let Some(0) = None && let Some(0) = None) { never!() } { never!() });
27+
//[edition2021]~^ ERROR `let` expressions in this position are unstable
28+
//[edition2021]~| ERROR `let` expressions in this position are unstable
29+
macro_in_2024::make_if!(let (Some(0)) let (Some(0)) { never!() } { never!() });
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
//@ revisions: edition2021 edition2024
2+
//@ compile-flags: -Z unstable-options -Z lint-mir -Z validate-mir
3+
//@ [edition2021] edition: 2021
4+
//@ [edition2024] edition: 2024
5+
//@ aux-build:macro-in-2021.rs
6+
//@ aux-build:macro-in-2024.rs
7+
//@ run-pass
8+
9+
#![allow(dead_code)]
10+
11+
use std::unreachable as never;
12+
use std::cell::RefCell;
13+
use std::convert::TryInto;
14+
15+
// Compiletest doesn't specify the needed --extern flags to make `extern crate` unneccessary
16+
extern crate macro_in_2021;
17+
extern crate macro_in_2024;
18+
19+
#[derive(Default)]
20+
struct DropOrderCollector(RefCell<Vec<u32>>);
21+
22+
struct LoudDrop<'a>(&'a DropOrderCollector, u32);
23+
24+
impl Drop for LoudDrop<'_> {
25+
fn drop(&mut self) {
26+
println!("{}", self.1);
27+
self.0.0.borrow_mut().push(self.1);
28+
}
29+
}
30+
31+
impl DropOrderCollector {
32+
fn print(&self, n: u32) {
33+
println!("{n}");
34+
self.0.borrow_mut().push(n)
35+
}
36+
fn some_loud(&self, n: u32) -> Option<LoudDrop> {
37+
Some(LoudDrop(self, n))
38+
}
39+
40+
#[track_caller]
41+
fn validate(self) {
42+
assert!(
43+
self.0
44+
.into_inner()
45+
.into_iter()
46+
.enumerate()
47+
.all(|(idx, item)| idx + 1 == item.try_into().unwrap())
48+
);
49+
}
50+
fn with_macro_2021(self) {
51+
// Edition 2021 drop behaviour
52+
macro_in_2021::make_if!((let None = self.some_loud(2)) { never!() } {self.print(1) });
53+
macro_in_2021::make_if!(let (self.some_loud(4)) { never!() } { self.print(3) });
54+
self.validate();
55+
}
56+
fn with_macro_2024(self) {
57+
// Edition 2024 drop behaviour
58+
macro_in_2024::make_if!((let None = self.some_loud(1)) { never!() } { self.print(2) });
59+
macro_in_2024::make_if!(let (self.some_loud(3)) { never!() } { self.print(4) });
60+
self.validate();
61+
}
62+
}
63+
64+
fn main() {
65+
// 2021 drop order if it's a 2021 macro creating the `if`
66+
// 2024 drop order if it's a 2024 macro creating the `if`
67+
68+
// Compare this with edition-gate-macro-error.rs: We want to avoid exposing 2021 drop order,
69+
// because it can create bad MIR (issue #104843)
70+
// This test doesn't contain any let chains at all: it should be understood
71+
// in combination with `edition-gate-macro-error.rs`
72+
73+
DropOrderCollector::default().with_macro_2021();
74+
DropOrderCollector::default().with_macro_2024();
75+
76+
}

0 commit comments

Comments
 (0)