Skip to content

Commit 0dd4bfa

Browse files
authored
Rollup merge of rust-lang#57302 - sinkuu:unused_assignments_fp, r=estebank
Fix unused_assignments false positive Fixes rust-lang#22630. In liveness analysis, make `continue` jump to the loop condition's `LiveNode` (`cond` as in comment) instead of the loop's one (`expr`). https://github.com/rust-lang/rust/blob/069b0c410808c1d1d33b495e048b1186e9f8d57f/src/librustc/middle/liveness.rs#L1358-L1370
2 parents bb683b9 + 069b0c4 commit 0dd4bfa

File tree

2 files changed

+15
-23
lines changed

2 files changed

+15
-23
lines changed

src/librustc/middle/liveness.rs

+6-23
Original file line numberDiff line numberDiff line change
@@ -911,17 +911,8 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
911911
}
912912

913913
fn compute(&mut self, body: &hir::Expr) -> LiveNode {
914-
// if there is a `break` or `again` at the top level, then it's
915-
// effectively a return---this only occurs in `for` loops,
916-
// where the body is really a closure.
917-
918914
debug!("compute: using id for body, {}", self.ir.tcx.hir().node_to_pretty_string(body.id));
919915

920-
let exit_ln = self.s.exit_ln;
921-
922-
self.break_ln.insert(body.id, exit_ln);
923-
self.cont_ln.insert(body.id, exit_ln);
924-
925916
// the fallthrough exit is only for those cases where we do not
926917
// explicitly return:
927918
let s = self.s;
@@ -1024,19 +1015,10 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
10241015
self.propagate_through_expr(&e, succ)
10251016
}
10261017

1027-
hir::ExprKind::Closure(.., blk_id, _, _) => {
1018+
hir::ExprKind::Closure(..) => {
10281019
debug!("{} is an ExprKind::Closure",
10291020
self.ir.tcx.hir().node_to_pretty_string(expr.id));
10301021

1031-
// The next-node for a break is the successor of the entire
1032-
// loop. The next-node for a continue is the top of this loop.
1033-
let node = self.live_node(expr.hir_id, expr.span);
1034-
1035-
let break_ln = succ;
1036-
let cont_ln = node;
1037-
self.break_ln.insert(blk_id.node_id, break_ln);
1038-
self.cont_ln.insert(blk_id.node_id, cont_ln);
1039-
10401022
// the construction of a closure itself is not important,
10411023
// but we have to consider the closed over variables.
10421024
let caps = self.ir.capture_info_map.get(&expr.id).cloned().unwrap_or_else(||
@@ -1407,15 +1389,16 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
14071389
debug!("propagate_through_loop: using id for loop body {} {}",
14081390
expr.id, self.ir.tcx.hir().node_to_pretty_string(body.id));
14091391

1410-
let break_ln = succ;
1411-
let cont_ln = ln;
1412-
self.break_ln.insert(expr.id, break_ln);
1413-
self.cont_ln.insert(expr.id, cont_ln);
1392+
1393+
self.break_ln.insert(expr.id, succ);
14141394

14151395
let cond_ln = match kind {
14161396
LoopLoop => ln,
14171397
WhileLoop(ref cond) => self.propagate_through_expr(&cond, ln),
14181398
};
1399+
1400+
self.cont_ln.insert(expr.id, cond_ln);
1401+
14191402
let body_ln = self.propagate_through_block(body, cond_ln);
14201403

14211404
// repeat until fixed point is reached:

src/test/ui/liveness/liveness-dead.rs

+9
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,13 @@ fn f5(mut x: i32) {
2727
x = 4; //~ ERROR: value assigned to `x` is never read
2828
}
2929

30+
// #22630
31+
fn f6() {
32+
let mut done = false;
33+
while !done {
34+
done = true; // no error
35+
continue;
36+
}
37+
}
38+
3039
fn main() {}

0 commit comments

Comments
 (0)