Skip to content

Commit 34cd4ef

Browse files
nikomatsakiscuviper
authored andcommitted
handle case of a variable not captured
(cherry picked from commit fc8113d)
1 parent 042c837 commit 34cd4ef

File tree

4 files changed

+117
-8
lines changed

4 files changed

+117
-8
lines changed

compiler/rustc_typeck/src/check/upvar.rs

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
9090
enum CapturesInfo {
9191
/// We previously captured all of `x`, but now we capture some sub-path.
9292
CapturingLess { source_expr: Option<hir::HirId>, var_name: String },
93-
//CapturingNothing {
94-
// // where the variable appears in the closure (but is not captured)
95-
// use_span: Span,
96-
//},
93+
CapturingNothing {
94+
// where the variable appears in the closure (but is not captured)
95+
use_span: Span,
96+
},
9797
}
9898

9999
/// Reasons that we might issue a migration warning.
@@ -758,6 +758,12 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
758758
captured_name,
759759
));
760760
}
761+
CapturesInfo::CapturingNothing { use_span } => {
762+
diagnostics_builder.span_label(*use_span, format!("in Rust 2018, this causes the closure to capture `{}`, but in Rust 2021, it has no effect",
763+
self.tcx.hir().name(*var_hir_id),
764+
));
765+
}
766+
761767
_ => { }
762768
}
763769

@@ -773,6 +779,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
773779
captured_name,
774780
));
775781
}
782+
CapturesInfo::CapturingNothing { use_span: _ } => {
783+
diagnostics_builder.span_label(drop_location_span, format!("in Rust 2018, `{v}` is dropped here along with the closure, but in Rust 2021 `{v}` is not part of the closure",
784+
v = self.tcx.hir().name(*var_hir_id),
785+
));
786+
}
776787
}
777788
}
778789

@@ -787,6 +798,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
787798
p = captured_name,
788799
));
789800
}
801+
802+
// Cannot happen: if we don't capture a variable, we impl strictly more traits
803+
CapturesInfo::CapturingNothing { use_span } => span_bug!(*use_span, "missing trait from not capturing something"),
790804
}
791805
}
792806
}
@@ -1055,10 +1069,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
10551069
match closure_clause {
10561070
// Only migrate if closure is a move closure
10571071
hir::CaptureBy::Value => {
1058-
let diagnostics_info = FxHashSet::default();
1059-
//diagnostics_info.insert(CapturesInfo::CapturingNothing);
1060-
//let upvars = self.tcx.upvars_mentioned(closure_def_id).expect("must be an upvar");
1061-
//let _span = upvars[&var_hir_id];
1072+
let mut diagnostics_info = FxHashSet::default();
1073+
let upvars = self.tcx.upvars_mentioned(closure_def_id).expect("must be an upvar");
1074+
let upvar = upvars[&var_hir_id];
1075+
diagnostics_info.insert(CapturesInfo::CapturingNothing { use_span: upvar.span });
10621076
return Some(diagnostics_info);
10631077
}
10641078
hir::CaptureBy::Ref => {}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// run-rustfix
2+
3+
#![deny(rust_2021_incompatible_closure_captures)]
4+
//~^ NOTE lint level is defined here
5+
6+
fn main() {
7+
struct Foo(u32);
8+
impl Drop for Foo {
9+
fn drop(&mut self) {
10+
println!("dropped {}", self.0);
11+
}
12+
}
13+
14+
let f0 = Foo(0);
15+
let f1 = Foo(1);
16+
17+
let c0 = move || {
18+
let _ = &f0;
19+
//~^ ERROR changes to closure capture in Rust 2021 will affect drop order
20+
//~| NOTE for more information
21+
let _ = f0;
22+
//~^ NOTE in Rust 2018, this causes the closure to capture `f0`, but in Rust 2021, it has no effect
23+
};
24+
25+
let c1 = move || {
26+
let _ = &f1;
27+
};
28+
29+
println!("dropping 0");
30+
drop(c0);
31+
println!("dropping 1");
32+
drop(c1);
33+
println!("dropped all");
34+
}
35+
//~^ NOTE in Rust 2018, `f0` is dropped here along with the closure, but in Rust 2021 `f0` is not part of the closure
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// run-rustfix
2+
3+
#![deny(rust_2021_incompatible_closure_captures)]
4+
//~^ NOTE lint level is defined here
5+
6+
fn main() {
7+
struct Foo(u32);
8+
impl Drop for Foo {
9+
fn drop(&mut self) {
10+
println!("dropped {}", self.0);
11+
}
12+
}
13+
14+
let f0 = Foo(0);
15+
let f1 = Foo(1);
16+
17+
let c0 = move || {
18+
//~^ ERROR changes to closure capture in Rust 2021 will affect drop order
19+
//~| NOTE for more information
20+
let _ = f0;
21+
//~^ NOTE in Rust 2018, this causes the closure to capture `f0`, but in Rust 2021, it has no effect
22+
};
23+
24+
let c1 = move || {
25+
let _ = &f1;
26+
};
27+
28+
println!("dropping 0");
29+
drop(c0);
30+
println!("dropping 1");
31+
drop(c1);
32+
println!("dropped all");
33+
}
34+
//~^ NOTE in Rust 2018, `f0` is dropped here along with the closure, but in Rust 2021 `f0` is not part of the closure
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
error: changes to closure capture in Rust 2021 will affect drop order
2+
--> $DIR/issue-90465.rs:17:14
3+
|
4+
LL | let c0 = move || {
5+
| ^^^^^^^
6+
...
7+
LL | let _ = f0;
8+
| -- in Rust 2018, this causes the closure to capture `f0`, but in Rust 2021, it has no effect
9+
...
10+
LL | }
11+
| - in Rust 2018, `f0` is dropped here along with the closure, but in Rust 2021 `f0` is not part of the closure
12+
|
13+
note: the lint level is defined here
14+
--> $DIR/issue-90465.rs:3:9
15+
|
16+
LL | #![deny(rust_2021_incompatible_closure_captures)]
17+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
18+
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/disjoint-capture-in-closures.html>
19+
help: add a dummy let to cause `f0` to be fully captured
20+
|
21+
LL ~ let c0 = move || {
22+
LL + let _ = &f0;
23+
|
24+
25+
error: aborting due to previous error
26+

0 commit comments

Comments
 (0)