Skip to content

Commit faa0a10

Browse files
committed
Auto merge of rust-lang#88271 - sexxi-goose:liveness, r=nikomatsakis
2229: Consider varaiables mentioned in closure as used Fixes: rust-lang/project-rfc-2229#57 r? `@nikomatsakis`
2 parents b03ccac + d7b4ee8 commit faa0a10

File tree

5 files changed

+10
-63
lines changed

5 files changed

+10
-63
lines changed

compiler/rustc_passes/src/liveness.rs

+6-30
Original file line numberDiff line numberDiff line change
@@ -337,8 +337,8 @@ impl<'tcx> Visitor<'tcx> for IrMaps<'tcx> {
337337
return;
338338
}
339339

340-
if let Some(captures) = maps.tcx.typeck(local_def_id).closure_min_captures.get(&def_id) {
341-
for &var_hir_id in captures.keys() {
340+
if let Some(upvars) = maps.tcx.upvars_mentioned(def_id) {
341+
for &var_hir_id in upvars.keys() {
342342
let var_name = maps.tcx.hir().name(var_hir_id);
343343
maps.add_variable(Upvar(var_hir_id, var_name));
344344
}
@@ -405,21 +405,14 @@ impl<'tcx> Visitor<'tcx> for IrMaps<'tcx> {
405405
// breaks or continues)
406406
self.add_live_node_for_node(expr.hir_id, ExprNode(expr.span, expr.hir_id));
407407

408-
// Make a live_node for each captured variable, with the span
408+
// Make a live_node for each mentioned variable, with the span
409409
// being the location that the variable is used. This results
410410
// in better error messages than just pointing at the closure
411411
// construction site.
412412
let mut call_caps = Vec::new();
413413
let closure_def_id = self.tcx.hir().local_def_id(expr.hir_id);
414-
if let Some(captures) = self
415-
.tcx
416-
.typeck(closure_def_id)
417-
.closure_min_captures
418-
.get(&closure_def_id.to_def_id())
419-
{
420-
// If closure_min_captures is Some, upvars_mentioned must also be Some
421-
let upvars = self.tcx.upvars_mentioned(closure_def_id).unwrap();
422-
call_caps.extend(captures.keys().map(|var_id| {
414+
if let Some(upvars) = self.tcx.upvars_mentioned(closure_def_id) {
415+
call_caps.extend(upvars.keys().map(|var_id| {
423416
let upvar = upvars[var_id];
424417
let upvar_ln = self.add_live_node(UpvarNode(upvar.span));
425418
CaptureInfo { ln: upvar_ln, var_hid: *var_id }
@@ -494,7 +487,6 @@ struct Liveness<'a, 'tcx> {
494487
ir: &'a mut IrMaps<'tcx>,
495488
typeck_results: &'a ty::TypeckResults<'tcx>,
496489
param_env: ty::ParamEnv<'tcx>,
497-
upvars: Option<&'tcx FxIndexMap<hir::HirId, hir::Upvar>>,
498490
closure_min_captures: Option<&'tcx RootVariableMinCaptureList<'tcx>>,
499491
successors: IndexVec<LiveNode, Option<LiveNode>>,
500492
rwu_table: rwu_table::RWUTable,
@@ -518,7 +510,6 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
518510
fn new(ir: &'a mut IrMaps<'tcx>, body_owner: LocalDefId) -> Liveness<'a, 'tcx> {
519511
let typeck_results = ir.tcx.typeck(body_owner);
520512
let param_env = ir.tcx.param_env(body_owner);
521-
let upvars = ir.tcx.upvars_mentioned(body_owner);
522513
let closure_min_captures = typeck_results.closure_min_captures.get(&body_owner.to_def_id());
523514
let closure_ln = ir.add_live_node(ClosureNode);
524515
let exit_ln = ir.add_live_node(ExitNode);
@@ -530,7 +521,6 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
530521
ir,
531522
typeck_results,
532523
param_env,
533-
upvars,
534524
closure_min_captures,
535525
successors: IndexVec::from_elem_n(None, num_live_nodes),
536526
rwu_table: rwu_table::RWUTable::new(num_live_nodes, num_vars),
@@ -1215,21 +1205,7 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
12151205
acc: u32,
12161206
) -> LiveNode {
12171207
match path.res {
1218-
Res::Local(hid) => {
1219-
let in_upvars = self.upvars.map_or(false, |u| u.contains_key(&hid));
1220-
let in_captures = self.closure_min_captures.map_or(false, |c| c.contains_key(&hid));
1221-
1222-
match (in_upvars, in_captures) {
1223-
(false, _) | (true, true) => self.access_var(hir_id, hid, succ, acc, path.span),
1224-
(true, false) => {
1225-
// This case is possible when with RFC-2229, a wild pattern
1226-
// is used within a closure.
1227-
// eg: `let _ = x`. The closure doesn't capture x here,
1228-
// even though it's mentioned in the closure.
1229-
succ
1230-
}
1231-
}
1232-
}
1208+
Res::Local(hid) => self.access_var(hir_id, hid, succ, acc, path.span),
12331209
_ => succ,
12341210
}
12351211
}

src/test/ui/closures/2229_closure_analysis/issue-87987.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ struct Props {
88

99
fn main() {
1010
// Test 1
11-
let props_2 = Props { //~ WARNING: unused variable: `props_2`
11+
let props_2 = Props {
1212
field_1: 1,
1313
field_2: 1,
1414
};
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,3 @@
1-
warning: unused variable: `props_2`
2-
--> $DIR/issue-87987.rs:11:9
3-
|
4-
LL | let props_2 = Props {
5-
| ^^^^^^^ help: if this is intentional, prefix it with an underscore: `_props_2`
6-
|
7-
= note: `#[warn(unused_variables)]` on by default
8-
91
warning: field is never read: `field_1`
102
--> $DIR/issue-87987.rs:5:5
113
|
@@ -20,5 +12,5 @@ warning: field is never read: `field_2`
2012
LL | field_2: u32,
2113
| ^^^^^^^^^^^^
2214

23-
warning: 3 warnings emitted
15+
warning: 2 warnings emitted
2416

src/test/ui/closures/2229_closure_analysis/run_pass/destructure_patterns.rs

-3
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ fn test3() {
4343

4444
fn test4() {
4545
let t = (String::from("Hello"), String::from("World"));
46-
//~^ WARN unused variable: `t`
4746

4847
let c = || {
4948
let (_, _) = t;
@@ -81,9 +80,7 @@ fn test7() {
8180

8281
fn test8() {
8382
let x = 0;
84-
//~^ WARN unused variable: `x`
8583
let tup = (1, 2);
86-
//~^ WARN unused variable: `tup`
8784
let p = Point { x: 10, y: 20 };
8885

8986
let c = || {

src/test/ui/closures/2229_closure_analysis/run_pass/destructure_patterns.stderr

+2-20
Original file line numberDiff line numberDiff line change
@@ -29,29 +29,11 @@ warning: unused variable: `t2`
2929
LL | let (_, t2) = t;
3030
| ^^ help: if this is intentional, prefix it with an underscore: `_t2`
3131

32-
warning: unused variable: `t`
33-
--> $DIR/destructure_patterns.rs:45:9
34-
|
35-
LL | let t = (String::from("Hello"), String::from("World"));
36-
| ^ help: if this is intentional, prefix it with an underscore: `_t`
37-
3832
warning: unused variable: `x`
39-
--> $DIR/destructure_patterns.rs:91:21
33+
--> $DIR/destructure_patterns.rs:88:21
4034
|
4135
LL | let Point { x, y } = p;
4236
| ^ help: try ignoring the field: `x: _`
4337

44-
warning: unused variable: `x`
45-
--> $DIR/destructure_patterns.rs:83:9
46-
|
47-
LL | let x = 0;
48-
| ^ help: if this is intentional, prefix it with an underscore: `_x`
49-
50-
warning: unused variable: `tup`
51-
--> $DIR/destructure_patterns.rs:85:9
52-
|
53-
LL | let tup = (1, 2);
54-
| ^^^ help: if this is intentional, prefix it with an underscore: `_tup`
55-
56-
warning: 8 warnings emitted
38+
warning: 5 warnings emitted
5739

0 commit comments

Comments
 (0)