Skip to content

Commit 7d18052

Browse files
committed
Add test for closure migration where body is a block fragment.
1 parent 7189c85 commit 7d18052

File tree

3 files changed

+86
-0
lines changed

3 files changed

+86
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// run-rustfix
2+
// edition:2018
3+
// check-pass
4+
#![warn(rust_2021_compatibility)]
5+
6+
macro_rules! m {
7+
(@ $body:expr) => {{
8+
let f = || $body;
9+
//~^ WARNING: drop order
10+
f();
11+
}};
12+
($body:block) => {{
13+
m!(@ $body);
14+
}};
15+
}
16+
17+
fn main() {
18+
let a = (1.to_string(), 2.to_string());
19+
m!({
20+
let _ = &a;
21+
//~^ HELP: add a dummy
22+
let x = a.0;
23+
println!("{}", x);
24+
});
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// run-rustfix
2+
// edition:2018
3+
// check-pass
4+
#![warn(rust_2021_compatibility)]
5+
6+
macro_rules! m {
7+
(@ $body:expr) => {{
8+
let f = || $body;
9+
//~^ WARNING: drop order
10+
f();
11+
}};
12+
($body:block) => {{
13+
m!(@ $body);
14+
}};
15+
}
16+
17+
fn main() {
18+
let a = (1.to_string(), 2.to_string());
19+
m!({
20+
//~^ HELP: add a dummy
21+
let x = a.0;
22+
println!("{}", x);
23+
});
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
warning: changes to closure capture in Rust 2021 will affect drop order
2+
--> $DIR/closure-body-macro-fragment.rs:8:17
3+
|
4+
LL | let f = || $body;
5+
| _________________^
6+
LL | |
7+
LL | | f();
8+
LL | | }};
9+
| | - in Rust 2018, `a` is dropped here, but in Rust 2021, only `a.0` will be dropped here as part of the closure
10+
LL | | ($body:block) => {{
11+
LL | | m!(@ $body);
12+
| |__________________^
13+
...
14+
LL | / m!({
15+
LL | |
16+
LL | | let x = a.0;
17+
| | --- in Rust 2018, this closure captures all of `a`, but in Rust 2021, it will only capture `a.0`
18+
LL | | println!("{}", x);
19+
LL | | });
20+
| |_______- in this macro invocation
21+
|
22+
note: the lint level is defined here
23+
--> $DIR/closure-body-macro-fragment.rs:4:9
24+
|
25+
LL | #![warn(rust_2021_compatibility)]
26+
| ^^^^^^^^^^^^^^^^^^^^^^^
27+
= note: `#[warn(rust_2021_incompatible_closure_captures)]` implied by `#[warn(rust_2021_compatibility)]`
28+
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/disjoint-capture-in-closures.html>
29+
= note: this warning originates in the macro `m` (in Nightly builds, run with -Z macro-backtrace for more info)
30+
help: add a dummy let to cause `a` to be fully captured
31+
|
32+
LL ~ m!({
33+
LL + let _ = &a;
34+
|
35+
36+
warning: 1 warning emitted
37+

0 commit comments

Comments
 (0)