From 62bec714462129adcc622575d69db558b3750a6e Mon Sep 17 00:00:00 2001 From: Matthew Jasper Date: Sat, 15 Jun 2019 10:08:20 +0100 Subject: Fix incorrect double assignment in MIR for while loops --- src/test/ui/nll/assign-while-to-immutable.rs | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 src/test/ui/nll/assign-while-to-immutable.rs (limited to 'src/test') diff --git a/src/test/ui/nll/assign-while-to-immutable.rs b/src/test/ui/nll/assign-while-to-immutable.rs new file mode 100644 index 00000000000..c803321b508 --- /dev/null +++ b/src/test/ui/nll/assign-while-to-immutable.rs @@ -0,0 +1,11 @@ +// We used to incorrectly assign to `x` twice when generating MIR for this +// function, preventing this from compiling. + +// check-pass + +fn main() { + let x = while false { + break; + }; + let y = 'l: while break 'l {}; +} -- cgit 1.4.1-3-g733a5 From 101a2f59b490650c12c5f9e4561a7390bfce78d3 Mon Sep 17 00:00:00 2001 From: Matthew Jasper Date: Sat, 15 Jun 2019 10:08:41 +0100 Subject: Use `as_temp` to evaluate statement expressions --- src/librustc_mir/build/block.rs | 4 +- src/librustc_mir/build/expr/stmt.rs | 80 ++++++++++------------ src/librustc_mir/build/scope.rs | 21 ------ src/librustc_mir/hair/cx/block.rs | 3 - src/librustc_mir/hair/mod.rs | 4 -- src/test/mir-opt/box_expr.rs | 4 +- src/test/mir-opt/copy_propagation_arg.rs | 2 + src/test/mir-opt/generator-drop-cleanup.rs | 2 + src/test/mir-opt/generator-storage-dead-unwind.rs | 6 ++ src/test/mir-opt/issue-38669.rs | 3 + src/test/mir-opt/issue-41110.rs | 2 +- src/test/mir-opt/issue-49232.rs | 4 +- src/test/mir-opt/loop_test.rs | 1 + src/test/mir-opt/match_test.rs | 1 + src/test/mir-opt/nll/region-subtyping-basic.rs | 6 +- src/test/mir-opt/storage_ranges.rs | 2 + .../generator/issue-61442-stmt-expr-with-drop.rs | 32 +++++++++ 17 files changed, 96 insertions(+), 81 deletions(-) create mode 100644 src/test/ui/generator/issue-61442-stmt-expr-with-drop.rs (limited to 'src/test') diff --git a/src/librustc_mir/build/block.rs b/src/librustc_mir/build/block.rs index 749cd6fc8bf..7ea08b15b44 100644 --- a/src/librustc_mir/build/block.rs +++ b/src/librustc_mir/build/block.rs @@ -78,7 +78,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { let source_info = this.source_info(span); for stmt in stmts { - let Stmt { kind, opt_destruction_scope, span: stmt_span } = this.hir.mirror(stmt); + let Stmt { kind, opt_destruction_scope } = this.hir.mirror(stmt); match kind { StmtKind::Expr { scope, expr } => { this.block_context.push(BlockFrame::Statement { ignores_expr_result: true }); @@ -87,7 +87,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { let si = (scope, source_info); this.in_scope(si, LintLevel::Inherited, |this| { let expr = this.hir.mirror(expr); - this.stmt_expr(block, expr, Some(stmt_span)) + this.stmt_expr(block, expr, Some(scope)) }) })); } diff --git a/src/librustc_mir/build/expr/stmt.rs b/src/librustc_mir/build/expr/stmt.rs index 4463e7fd4d4..3c5eafb41a2 100644 --- a/src/librustc_mir/build/expr/stmt.rs +++ b/src/librustc_mir/build/expr/stmt.rs @@ -1,6 +1,7 @@ use crate::build::scope::BreakableScope; use crate::build::{BlockAnd, BlockAndExtension, BlockFrame, Builder}; use crate::hair::*; +use rustc::middle::region; use rustc::mir::*; impl<'a, 'tcx> Builder<'a, 'tcx> { @@ -8,14 +9,13 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { /// If the original expression was an AST statement, /// (e.g., `some().code(&here());`) then `opt_stmt_span` is the /// span of that statement (including its semicolon, if any). - /// Diagnostics use this span (which may be larger than that of - /// `expr`) to identify when statement temporaries are dropped. - pub fn stmt_expr(&mut self, - mut block: BasicBlock, - expr: Expr<'tcx>, - opt_stmt_span: Option) - -> BlockAnd<()> - { + /// The scope is used if a statement temporary must be dropped. + pub fn stmt_expr( + &mut self, + mut block: BasicBlock, + expr: Expr<'tcx>, + statement_scope: Option, + ) -> BlockAnd<()> { let this = self; let expr_span = expr.span; let source_info = this.source_info(expr.span); @@ -30,7 +30,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { } => { let value = this.hir.mirror(value); this.in_scope((region_scope, source_info), lint_level, |this| { - this.stmt_expr(block, value, opt_stmt_span) + this.stmt_expr(block, value, statement_scope) }) } ExprKind::Assign { lhs, rhs } => { @@ -199,7 +199,11 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { block.unit() } _ => { - let expr_ty = expr.ty; + assert!( + statement_scope.is_some(), + "Should not be calling `stmt_expr` on a general expression \ + without a statement scope", + ); // Issue #54382: When creating temp for the value of // expression like: @@ -208,48 +212,34 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { // // it is usually better to focus on `the_value` rather // than the entirety of block(s) surrounding it. - let mut temp_span = expr_span; - let mut temp_in_tail_of_block = false; - if let ExprKind::Block { body } = expr.kind { - if let Some(tail_expr) = &body.expr { - let mut expr = tail_expr; - while let rustc::hir::ExprKind::Block(subblock, _label) = &expr.node { - if let Some(subtail_expr) = &subblock.expr { - expr = subtail_expr - } else { - break; + let adjusted_span = (|| { + if let ExprKind::Block { body } = expr.kind { + if let Some(tail_expr) = &body.expr { + let mut expr = tail_expr; + while let rustc::hir::ExprKind::Block(subblock, _label) = &expr.node { + if let Some(subtail_expr) = &subblock.expr { + expr = subtail_expr + } else { + break; + } } - } - temp_span = expr.span; - temp_in_tail_of_block = true; - } - } - - let temp = { - let mut local_decl = LocalDecl::new_temp(expr.ty.clone(), temp_span); - if temp_in_tail_of_block { - if this.block_context.currently_ignores_tail_results() { - local_decl = local_decl.block_tail(BlockTailInfo { + this.block_context.push(BlockFrame::TailExpr { tail_result_is_ignored: true }); + return Some(expr.span); } } - let temp = this.local_decls.push(local_decl); - let place = Place::from(temp); - debug!("created temp {:?} for expr {:?} in block_context: {:?}", - temp, expr, this.block_context); - place - }; - unpack!(block = this.into(&temp, block, expr)); + None + })(); + + let temp = unpack!(block = + this.as_temp(block, statement_scope, expr, Mutability::Not)); - // Attribute drops of the statement's temps to the - // semicolon at the statement's end. - let drop_point = this.hir.tcx().sess.source_map().end_point(match opt_stmt_span { - None => expr_span, - Some(StatementSpan(span)) => span, - }); + if let Some(span) = adjusted_span { + this.local_decls[temp].source_info.span = span; + this.block_context.pop(); + } - unpack!(block = this.build_drop(block, drop_point, temp, expr_ty)); block.unit() } } diff --git a/src/librustc_mir/build/scope.rs b/src/librustc_mir/build/scope.rs index db58a709e9f..c5b5f251243 100644 --- a/src/librustc_mir/build/scope.rs +++ b/src/librustc_mir/build/scope.rs @@ -805,27 +805,6 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { target } - /// Utility function for *non*-scope code to build their own drops - pub fn build_drop(&mut self, - block: BasicBlock, - span: Span, - location: Place<'tcx>, - ty: Ty<'tcx>) -> BlockAnd<()> { - if !self.hir.needs_drop(ty) { - return block.unit(); - } - let source_info = self.source_info(span); - let next_target = self.cfg.start_new_block(); - let diverge_target = self.diverge_cleanup(); - self.cfg.terminate(block, source_info, - TerminatorKind::Drop { - location, - target: next_target, - unwind: Some(diverge_target), - }); - next_target.unit() - } - /// Utility function for *non*-scope code to build their own drops pub fn build_drop_and_replace(&mut self, block: BasicBlock, diff --git a/src/librustc_mir/hair/cx/block.rs b/src/librustc_mir/hair/cx/block.rs index d5932052d1a..9a73842d2f0 100644 --- a/src/librustc_mir/hair/cx/block.rs +++ b/src/librustc_mir/hair/cx/block.rs @@ -49,7 +49,6 @@ fn mirror_stmts<'a, 'tcx>( for (index, stmt) in stmts.iter().enumerate() { let hir_id = stmt.hir_id; let opt_dxn_ext = cx.region_scope_tree.opt_destruction_scope(hir_id.local_id); - let stmt_span = StatementSpan(cx.tcx.hir().span(hir_id)); match stmt.node { hir::StmtKind::Expr(ref expr) | hir::StmtKind::Semi(ref expr) => { @@ -62,7 +61,6 @@ fn mirror_stmts<'a, 'tcx>( expr: expr.to_ref(), }, opt_destruction_scope: opt_dxn_ext, - span: stmt_span, }))) } hir::StmtKind::Item(..) => { @@ -107,7 +105,6 @@ fn mirror_stmts<'a, 'tcx>( lint_level: LintLevel::Explicit(local.hir_id), }, opt_destruction_scope: opt_dxn_ext, - span: stmt_span, }))); } } diff --git a/src/librustc_mir/hair/mod.rs b/src/librustc_mir/hair/mod.rs index 4694241528b..5431a31c4bb 100644 --- a/src/librustc_mir/hair/mod.rs +++ b/src/librustc_mir/hair/mod.rs @@ -55,14 +55,10 @@ pub enum StmtRef<'tcx> { Mirror(Box>), } -#[derive(Clone, Debug)] -pub struct StatementSpan(pub Span); - #[derive(Clone, Debug)] pub struct Stmt<'tcx> { pub kind: StmtKind<'tcx>, pub opt_destruction_scope: Option, - pub span: StatementSpan, } #[derive(Clone, Debug)] diff --git a/src/test/mir-opt/box_expr.rs b/src/test/mir-opt/box_expr.rs index 9f55d849644..d9fa3d3d473 100644 --- a/src/test/mir-opt/box_expr.rs +++ b/src/test/mir-opt/box_expr.rs @@ -24,7 +24,7 @@ impl Drop for S { // let mut _0: (); // let _1: std::boxed::Box; // let mut _2: std::boxed::Box; -// let mut _3: (); +// let _3: (); // let mut _4: std::boxed::Box; // scope 1 { // } @@ -50,6 +50,7 @@ impl Drop for S { // // bb4: { // StorageDead(_2); +// StorageLive(_3); // StorageLive(_4); // _4 = move _1; // _3 = const std::mem::drop::>(move _4) -> [return: bb5, unwind: bb7]; @@ -69,6 +70,7 @@ impl Drop for S { // // bb8: { // StorageDead(_4); +// StorageDead(_3); // _0 = (); // drop(_1) -> bb9; // } diff --git a/src/test/mir-opt/copy_propagation_arg.rs b/src/test/mir-opt/copy_propagation_arg.rs index 4e05484a80c..5e5fed12fb5 100644 --- a/src/test/mir-opt/copy_propagation_arg.rs +++ b/src/test/mir-opt/copy_propagation_arg.rs @@ -61,12 +61,14 @@ fn main() { // END rustc.foo.CopyPropagation.after.mir // START rustc.bar.CopyPropagation.before.mir // bb0: { +// StorageLive(_2); // StorageLive(_3); // _3 = _1; // _2 = const dummy(move _3) -> bb1; // } // bb1: { // StorageDead(_3); +// StorageDead(_2); // _1 = const 5u8; // ... // return; diff --git a/src/test/mir-opt/generator-drop-cleanup.rs b/src/test/mir-opt/generator-drop-cleanup.rs index 30f6d0deb91..f97e1ba6c89 100644 --- a/src/test/mir-opt/generator-drop-cleanup.rs +++ b/src/test/mir-opt/generator-drop-cleanup.rs @@ -18,6 +18,7 @@ fn main() { // } // bb1: { // StorageDead(_3); +// StorageDead(_2); // goto -> bb5; // } // bb2: { @@ -36,6 +37,7 @@ fn main() { // goto -> bb3; // } // bb7: { +// StorageLive(_2); // StorageLive(_3); // goto -> bb1; // } diff --git a/src/test/mir-opt/generator-storage-dead-unwind.rs b/src/test/mir-opt/generator-storage-dead-unwind.rs index 7be17c4292a..bcdb9375427 100644 --- a/src/test/mir-opt/generator-storage-dead-unwind.rs +++ b/src/test/mir-opt/generator-storage-dead-unwind.rs @@ -54,6 +54,7 @@ fn main() { // } // bb2: { // ... +// StorageLive(_6); // StorageLive(_7); // _7 = move _2; // _6 = const take::(move _7) -> [return: bb9, unwind: bb8]; @@ -81,16 +82,20 @@ fn main() { // } // bb8 (cleanup): { // StorageDead(_7); +// StorageDead(_6); // goto -> bb7; // } // bb9: { // StorageDead(_7); +// StorageDead(_6); +// StorageLive(_8); // StorageLive(_9); // _9 = move _3; // _8 = const take::(move _9) -> [return: bb10, unwind: bb11]; // } // bb10: { // StorageDead(_9); +// StorageDead(_8); // ... // StorageDead(_3); // StorageDead(_2); @@ -98,6 +103,7 @@ fn main() { // } // bb11 (cleanup): { // StorageDead(_9); +// StorageDead(_8); // goto -> bb7; // } // bb12: { diff --git a/src/test/mir-opt/issue-38669.rs b/src/test/mir-opt/issue-38669.rs index 909f9b7b6b7..d980cc891dc 100644 --- a/src/test/mir-opt/issue-38669.rs +++ b/src/test/mir-opt/issue-38669.rs @@ -25,6 +25,7 @@ fn main() { // falseUnwind -> [real: bb3, cleanup: bb1]; // } // bb3: { +// StorageLive(_3); // StorageLive(_4); // _4 = _1; // FakeRead(ForMatchedPlace, _4); @@ -34,6 +35,7 @@ fn main() { // bb5: { // _3 = (); // StorageDead(_4); +// StorageDead(_3); // _1 = const true; // _2 = (); // goto -> bb2; @@ -41,6 +43,7 @@ fn main() { // bb6: { // _0 = (); // StorageDead(_4); +// StorageDead(_3); // StorageDead(_1); // return; // } diff --git a/src/test/mir-opt/issue-41110.rs b/src/test/mir-opt/issue-41110.rs index 0b678be2ab3..e73390f52b5 100644 --- a/src/test/mir-opt/issue-41110.rs +++ b/src/test/mir-opt/issue-41110.rs @@ -42,7 +42,7 @@ impl S { // START rustc.test.ElaborateDrops.after.mir // let mut _0: (); // let _1: S; -// let mut _3: (); +// let _3: (); // let mut _4: S; // let mut _5: S; // let mut _6: bool; diff --git a/src/test/mir-opt/issue-49232.rs b/src/test/mir-opt/issue-49232.rs index 9dde6d821f2..d0dbcbd7515 100644 --- a/src/test/mir-opt/issue-49232.rs +++ b/src/test/mir-opt/issue-49232.rs @@ -21,7 +21,7 @@ fn main() { // let _2: i32; // let mut _3: bool; // let mut _4: !; -// let mut _5: (); +// let _5: (); // let mut _6: &i32; // scope 1 { // } @@ -73,12 +73,14 @@ fn main() { // bb12: { // FakeRead(ForLet, _2); // StorageDead(_3); +// StorageLive(_5); // StorageLive(_6); // _6 = &_2; // _5 = const std::mem::drop::<&i32>(move _6) -> [return: bb13, unwind: bb4]; // } // bb13: { // StorageDead(_6); +// StorageDead(_5); // _1 = (); // StorageDead(_2); // goto -> bb1; diff --git a/src/test/mir-opt/loop_test.rs b/src/test/mir-opt/loop_test.rs index 68ea60d9278..177080c04f9 100644 --- a/src/test/mir-opt/loop_test.rs +++ b/src/test/mir-opt/loop_test.rs @@ -25,6 +25,7 @@ fn main() { // bb3: { // Entry into the loop // _1 = (); // StorageDead(_2); +// StorageDead(_1); // goto -> bb5; // } // ... diff --git a/src/test/mir-opt/match_test.rs b/src/test/mir-opt/match_test.rs index ef60a04d1bd..aeb162772fa 100644 --- a/src/test/mir-opt/match_test.rs +++ b/src/test/mir-opt/match_test.rs @@ -75,6 +75,7 @@ fn main() { // goto -> bb14; // } // bb14: { +// StorageDead(_3); // _0 = (); // StorageDead(_2); // StorageDead(_1); diff --git a/src/test/mir-opt/nll/region-subtyping-basic.rs b/src/test/mir-opt/nll/region-subtyping-basic.rs index fa0dbe51c5d..8228d9740f0 100644 --- a/src/test/mir-opt/nll/region-subtyping-basic.rs +++ b/src/test/mir-opt/nll/region-subtyping-basic.rs @@ -22,9 +22,9 @@ fn main() { // END RUST SOURCE // START rustc.main.nll.0.mir -// | '_#2r | U0 | {bb2[0..=8], bb3[0], bb5[0..=1]} -// | '_#3r | U0 | {bb2[1..=8], bb3[0], bb5[0..=1]} -// | '_#4r | U0 | {bb2[4..=8], bb3[0], bb5[0..=1]} +// | '_#2r | U0 | {bb2[0..=8], bb3[0], bb5[0..=2]} +// | '_#3r | U0 | {bb2[1..=8], bb3[0], bb5[0..=2]} +// | '_#4r | U0 | {bb2[4..=8], bb3[0], bb5[0..=2]} // END rustc.main.nll.0.mir // START rustc.main.nll.0.mir // let _2: &'_#3r usize; diff --git a/src/test/mir-opt/storage_ranges.rs b/src/test/mir-opt/storage_ranges.rs index 6d22e9cd9fa..95570ff76a6 100644 --- a/src/test/mir-opt/storage_ranges.rs +++ b/src/test/mir-opt/storage_ranges.rs @@ -12,6 +12,7 @@ fn main() { // StorageLive(_1); // _1 = const 0i32; // FakeRead(ForLet, _1); +// StorageLive(_2); // StorageLive(_3); // StorageLive(_4); // StorageLive(_5); @@ -23,6 +24,7 @@ fn main() { // _2 = (); // StorageDead(_4); // StorageDead(_3); +// StorageDead(_2); // StorageLive(_6); // _6 = const 1i32; // FakeRead(ForLet, _6); diff --git a/src/test/ui/generator/issue-61442-stmt-expr-with-drop.rs b/src/test/ui/generator/issue-61442-stmt-expr-with-drop.rs new file mode 100644 index 00000000000..ce4642020f0 --- /dev/null +++ b/src/test/ui/generator/issue-61442-stmt-expr-with-drop.rs @@ -0,0 +1,32 @@ +// Test that we don't consider temporaries for statement expressions as live +// across yields + +// check-pass +// edition:2018 + +#![feature(async_await, generators, generator_trait)] + +use std::ops::Generator; + +async fn drop_and_await() { + async {}; + async {}.await; +} + +fn drop_and_yield() { + let x = || { + String::new(); + yield; + }; + Box::pin(x).as_mut().resume(); + let y = static || { + String::new(); + yield; + }; + Box::pin(y).as_mut().resume(); +} + +fn main() { + drop_and_await(); + drop_and_yield(); +} -- cgit 1.4.1-3-g733a5 From b86e6755b982bafc36133d9016e7d5a4cd0de0e8 Mon Sep 17 00:00:00 2001 From: Matthew Jasper Date: Sat, 15 Jun 2019 19:55:21 +0100 Subject: Add StorageDead statements for `while` conditions --- src/librustc_mir/build/expr/into.rs | 17 +---- src/librustc_mir/build/matches/mod.rs | 49 +++---------- src/librustc_mir/build/scope.rs | 125 +++++++++++++++++++------------- src/test/mir-opt/match-arm-scopes.rs | 16 ++-- src/test/mir-opt/match_false_edges.rs | 16 ++-- src/test/mir-opt/match_test.rs | 2 +- src/test/mir-opt/remove_fake_borrows.rs | 16 ++-- src/test/mir-opt/while-storage.rs | 59 +++++++++++++++ 8 files changed, 172 insertions(+), 128 deletions(-) create mode 100644 src/test/mir-opt/while-storage.rs (limited to 'src/test') diff --git a/src/librustc_mir/build/expr/into.rs b/src/librustc_mir/build/expr/into.rs index dc74466e633..0a2ea78bfd7 100644 --- a/src/librustc_mir/build/expr/into.rs +++ b/src/librustc_mir/build/expr/into.rs @@ -179,19 +179,10 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { // conduct the test, if necessary let body_block; if let Some(cond_expr) = opt_cond_expr { - let loop_block_end; - let cond = unpack!( - loop_block_end = this.as_local_operand(loop_block, cond_expr) - ); - body_block = this.cfg.start_new_block(); - let false_block = this.cfg.start_new_block(); - let term = TerminatorKind::if_( - this.hir.tcx(), - cond, - body_block, - false_block, - ); - this.cfg.terminate(loop_block_end, source_info, term); + let cond_expr = this.hir.mirror(cond_expr); + let (true_block, false_block) + = this.test_bool(loop_block, cond_expr, source_info); + body_block = true_block; // if the test is false, there's no `break` to assign `destination`, so // we have to do it diff --git a/src/librustc_mir/build/matches/mod.rs b/src/librustc_mir/build/matches/mod.rs index 5a0f5a01a04..cebfa681b5c 100644 --- a/src/librustc_mir/build/matches/mod.rs +++ b/src/librustc_mir/build/matches/mod.rs @@ -1490,7 +1490,8 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { }; let source_info = self.source_info(guard.span); let guard_end = self.source_info(tcx.sess.source_map().end_point(guard.span)); - let cond = unpack!(block = self.as_local_operand(block, guard)); + let (post_guard_block, otherwise_post_guard_block) + = self.test_bool(block, guard, source_info); let guard_frame = self.guard_context.pop().unwrap(); debug!( "Exiting guard building context with locals: {:?}", @@ -1498,7 +1499,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { ); for &(_, temp) in fake_borrows { - self.cfg.push(block, Statement { + self.cfg.push(post_guard_block, Statement { source_info: guard_end, kind: StatementKind::FakeRead( FakeReadCause::ForMatchGuard, @@ -1507,6 +1508,13 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { }); } + self.exit_scope( + source_info.span, + region_scope, + otherwise_post_guard_block, + candidate.otherwise_block.unwrap(), + ); + // We want to ensure that the matched candidates are bound // after we have confirmed this candidate *and* any // associated guard; Binding them on `block` is too soon, @@ -1533,41 +1541,6 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { // ``` // // and that is clearly not correct. - let post_guard_block = self.cfg.start_new_block(); - let otherwise_post_guard_block = self.cfg.start_new_block(); - self.cfg.terminate( - block, - source_info, - TerminatorKind::if_( - self.hir.tcx(), - cond.clone(), - post_guard_block, - otherwise_post_guard_block, - ), - ); - - self.exit_scope( - source_info.span, - region_scope, - otherwise_post_guard_block, - candidate.otherwise_block.unwrap(), - ); - - if let Operand::Copy(cond_place) | Operand::Move(cond_place) = cond { - if let Place::Base(PlaceBase::Local(cond_temp)) = cond_place { - // We will call `clear_top_scope` if there's another guard. So - // we have to drop this variable now or it will be "storage - // leaked". - self.pop_variable( - post_guard_block, - region_scope.0, - cond_temp - ); - } else { - bug!("Expected as_local_operand to produce a temporary"); - } - } - let by_value_bindings = candidate.bindings.iter().filter(|binding| { if let BindingMode::ByValue = binding.binding_mode { true } else { false } }); @@ -1577,7 +1550,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { let local_id = self.var_local_id(binding.var_id, RefWithinGuard); let place = Place::from(local_id); self.cfg.push( - block, + post_guard_block, Statement { source_info: guard_end, kind: StatementKind::FakeRead(FakeReadCause::ForGuardBinding, place), diff --git a/src/librustc_mir/build/scope.rs b/src/librustc_mir/build/scope.rs index 32b69082557..ec2e970906c 100644 --- a/src/librustc_mir/build/scope.rs +++ b/src/librustc_mir/build/scope.rs @@ -83,7 +83,7 @@ should go to. */ use crate::build::{BlockAnd, BlockAndExtension, BlockFrame, Builder, CFG}; -use crate::hair::{ExprRef, LintLevel}; +use crate::hair::{Expr, ExprRef, LintLevel}; use rustc::middle::region; use rustc::ty::Ty; use rustc::hir; @@ -829,6 +829,78 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { // Other // ===== + /// Branch based on a boolean condition. + /// + /// This is a special case because the temporary for the condition needs to + /// be dropped on both the true and the false arm. + pub fn test_bool( + &mut self, + mut block: BasicBlock, + condition: Expr<'tcx>, + source_info: SourceInfo, + ) -> (BasicBlock, BasicBlock) { + let cond = unpack!(block = self.as_local_operand(block, condition)); + let true_block = self.cfg.start_new_block(); + let false_block = self.cfg.start_new_block(); + let term = TerminatorKind::if_( + self.hir.tcx(), + cond.clone(), + true_block, + false_block, + ); + self.cfg.terminate(block, source_info, term); + + match cond { + // Don't try to drop a constant + Operand::Constant(_) => (), + // If constants and statics, we don't generate StorageLive for this + // temporary, so don't try to generate StorageDead for it either. + _ if self.local_scope().is_none() => (), + Operand::Copy(Place::Base(PlaceBase::Local(cond_temp))) + | Operand::Move(Place::Base(PlaceBase::Local(cond_temp))) => { + // Manually drop the condition on both branches. + let top_scope = self.scopes.scopes.last_mut().unwrap(); + let top_drop_data = top_scope.drops.pop().unwrap(); + + match top_drop_data.kind { + DropKind::Value { .. } => { + bug!("Drop scheduled on top of condition variable") + } + DropKind::Storage => { + // Drop the storage for both value and storage drops. + // Only temps and vars need their storage dead. + match top_drop_data.location { + Place::Base(PlaceBase::Local(index)) => { + let source_info = top_scope.source_info(top_drop_data.span); + assert_eq!(index, cond_temp, "Drop scheduled on top of condition"); + self.cfg.push( + true_block, + Statement { + source_info, + kind: StatementKind::StorageDead(index) + }, + ); + self.cfg.push( + false_block, + Statement { + source_info, + kind: StatementKind::StorageDead(index) + }, + ); + } + _ => unreachable!(), + } + } + } + + top_scope.invalidate_cache(true, self.is_generator, true); + } + _ => bug!("Expected as_local_operand to produce a temporary"), + } + + (true_block, false_block) + } + /// Creates a path that performs all required cleanup for unwinding. /// /// This path terminates in Resume. Returns the start of the path. @@ -942,57 +1014,6 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { top_scope.drops.clear(); top_scope.invalidate_cache(false, self.is_generator, true); } - - /// Drops the single variable provided - /// - /// * The scope must be the top scope. - /// * The variable must be in that scope. - /// * The variable must be at the top of that scope: it's the next thing - /// scheduled to drop. - /// * The drop must be of `DropKind::Storage`. - /// - /// This is used for the boolean holding the result of the match guard. We - /// do this because: - /// - /// * The boolean is different for each pattern - /// * There is only one exit for the arm scope - /// * The guard expression scope is too short, it ends just before the - /// boolean is tested. - pub(crate) fn pop_variable( - &mut self, - block: BasicBlock, - region_scope: region::Scope, - variable: Local, - ) { - let top_scope = self.scopes.scopes.last_mut().unwrap(); - - assert_eq!(top_scope.region_scope, region_scope); - - let top_drop_data = top_scope.drops.pop().unwrap(); - - match top_drop_data.kind { - DropKind::Value { .. } => { - bug!("Should not be calling pop_top_variable on non-copy type!") - } - DropKind::Storage => { - // Drop the storage for both value and storage drops. - // Only temps and vars need their storage dead. - match top_drop_data.location { - Place::Base(PlaceBase::Local(index)) => { - let source_info = top_scope.source_info(top_drop_data.span); - assert_eq!(index, variable); - self.cfg.push(block, Statement { - source_info, - kind: StatementKind::StorageDead(index) - }); - } - _ => unreachable!(), - } - } - } - - top_scope.invalidate_cache(true, self.is_generator, true); - } } /// Builds drops for pop_scope and exit_scope. diff --git a/src/test/mir-opt/match-arm-scopes.rs b/src/test/mir-opt/match-arm-scopes.rs index a2bc238c68a..18e0642eb34 100644 --- a/src/test/mir-opt/match-arm-scopes.rs +++ b/src/test/mir-opt/match-arm-scopes.rs @@ -103,10 +103,6 @@ fn main() { // bb10: { // `else` block - first time // _9 = (*_6); // StorageDead(_10); -// FakeRead(ForMatchGuard, _3); -// FakeRead(ForMatchGuard, _4); -// FakeRead(ForGuardBinding, _6); -// FakeRead(ForGuardBinding, _8); // switchInt(move _9) -> [false: bb16, otherwise: bb15]; // } // bb11: { // `return 3` - first time @@ -128,6 +124,10 @@ fn main() { // } // bb15: { // StorageDead(_9); +// FakeRead(ForMatchGuard, _3); +// FakeRead(ForMatchGuard, _4); +// FakeRead(ForGuardBinding, _6); +// FakeRead(ForGuardBinding, _8); // StorageLive(_5); // _5 = (_2.1: bool); // StorageLive(_7); @@ -159,10 +159,6 @@ fn main() { // bb19: { // `else` block - second time // _12 = (*_6); // StorageDead(_13); -// FakeRead(ForMatchGuard, _3); -// FakeRead(ForMatchGuard, _4); -// FakeRead(ForGuardBinding, _6); -// FakeRead(ForGuardBinding, _8); // switchInt(move _12) -> [false: bb22, otherwise: bb21]; // } // bb20: { @@ -175,6 +171,10 @@ fn main() { // } // bb21: { // bindings for arm 1 // StorageDead(_12); +// FakeRead(ForMatchGuard, _3); +// FakeRead(ForMatchGuard, _4); +// FakeRead(ForGuardBinding, _6); +// FakeRead(ForGuardBinding, _8); // StorageLive(_5); // _5 = (_2.0: bool); // StorageLive(_7); diff --git a/src/test/mir-opt/match_false_edges.rs b/src/test/mir-opt/match_false_edges.rs index a62e1b21dd1..b275c06e05c 100644 --- a/src/test/mir-opt/match_false_edges.rs +++ b/src/test/mir-opt/match_false_edges.rs @@ -71,12 +71,12 @@ fn main() { // _7 = const guard() -> [return: bb7, unwind: bb1]; // } // bb7: { // end of guard -// FakeRead(ForMatchGuard, _4); -// FakeRead(ForGuardBinding, _6); // switchInt(move _7) -> [false: bb9, otherwise: bb8]; // } // bb8: { // arm1 // StorageDead(_7); +// FakeRead(ForMatchGuard, _4); +// FakeRead(ForGuardBinding, _6); // StorageLive(_5); // _5 = ((_2 as Some).0: i32); // StorageLive(_8); @@ -138,12 +138,12 @@ fn main() { // _7 = const guard() -> [return: bb6, unwind: bb1]; // } // bb6: { // end of guard -// FakeRead(ForMatchGuard, _4); -// FakeRead(ForGuardBinding, _6); // switchInt(move _7) -> [false: bb8, otherwise: bb7]; // } // bb7: { // StorageDead(_7); +// FakeRead(ForMatchGuard, _4); +// FakeRead(ForGuardBinding, _6); // StorageLive(_5); // _5 = ((_2 as Some).0: i32); // StorageLive(_8); @@ -209,12 +209,12 @@ fn main() { // _8 = const guard() -> [return: bb6, unwind: bb1]; // } // bb6: { //end of guard1 -// FakeRead(ForMatchGuard, _5); -// FakeRead(ForGuardBinding, _7); // switchInt(move _8) -> [false: bb8, otherwise: bb7]; // } // bb7: { // StorageDead(_8); +// FakeRead(ForMatchGuard, _5); +// FakeRead(ForGuardBinding, _7); // StorageLive(_6); // _6 = ((_2 as Some).0: i32); // _1 = const 1i32; @@ -245,12 +245,12 @@ fn main() { // } // bb11: { // end of guard2 // StorageDead(_13); -// FakeRead(ForMatchGuard, _5); -// FakeRead(ForGuardBinding, _11); // switchInt(move _12) -> [false: bb13, otherwise: bb12]; // } // bb12: { // binding4 & arm4 // StorageDead(_12); +// FakeRead(ForMatchGuard, _5); +// FakeRead(ForGuardBinding, _11); // StorageLive(_10); // _10 = ((_2 as Some).0: i32); // _1 = const 3i32; diff --git a/src/test/mir-opt/match_test.rs b/src/test/mir-opt/match_test.rs index aeb162772fa..1ca75b10041 100644 --- a/src/test/mir-opt/match_test.rs +++ b/src/test/mir-opt/match_test.rs @@ -54,11 +54,11 @@ fn main() { // _8 = &shallow _1; // StorageLive(_9); // _9 = _2; -// FakeRead(ForMatchGuard, _8); // switchInt(move _9) -> [false: bb11, otherwise: bb10]; // } // bb10: { // StorageDead(_9); +// FakeRead(ForMatchGuard, _8); // _3 = const 0i32; // goto -> bb14; // } diff --git a/src/test/mir-opt/remove_fake_borrows.rs b/src/test/mir-opt/remove_fake_borrows.rs index 0f9c6f62c2b..3245d38b258 100644 --- a/src/test/mir-opt/remove_fake_borrows.rs +++ b/src/test/mir-opt/remove_fake_borrows.rs @@ -38,14 +38,14 @@ fn main() { // _7 = &shallow (*(*((_1 as Some).0: &' &' i32))); // StorageLive(_8); // _8 = _2; -// FakeRead(ForMatchGuard, _4); -// FakeRead(ForMatchGuard, _5); -// FakeRead(ForMatchGuard, _6); -// FakeRead(ForMatchGuard, _7); // switchInt(move _8) -> [false: bb6, otherwise: bb5]; // } // bb5: { // StorageDead(_8); +// FakeRead(ForMatchGuard, _4); +// FakeRead(ForMatchGuard, _5); +// FakeRead(ForMatchGuard, _6); +// FakeRead(ForMatchGuard, _7); // _0 = const 0i32; // goto -> bb7; // } @@ -84,14 +84,14 @@ fn main() { // nop; // StorageLive(_8); // _8 = _2; -// nop; -// nop; -// nop; -// nop; // switchInt(move _8) -> [false: bb6, otherwise: bb5]; // } // bb5: { // StorageDead(_8); +// nop; +// nop; +// nop; +// nop; // _0 = const 0i32; // goto -> bb7; // } diff --git a/src/test/mir-opt/while-storage.rs b/src/test/mir-opt/while-storage.rs new file mode 100644 index 00000000000..a486bd49a77 --- /dev/null +++ b/src/test/mir-opt/while-storage.rs @@ -0,0 +1,59 @@ +// Test that we correctly generate StorageDead statements for while loop +// conditions on all branches + +fn get_bool(c: bool) -> bool { + c +} + +fn while_loop(c: bool) { + while get_bool(c) { + if get_bool(c) { + break; + } + } +} + +fn main() { + while_loop(false); +} + +// END RUST SOURCE + +// START rustc.while_loop.PreCodegen.after.mir +// bb0: { +// StorageLive(_2); +// StorageLive(_3); +// _3 = _1; +// _2 = const get_bool(move _3) -> bb2; +// } +// bb1: { +// return; +// } +// bb2: { +// StorageDead(_3); +// switchInt(move _2) -> [false: bb4, otherwise: bb3]; +// } +// bb3: { +// StorageDead(_2); +// StorageLive(_4); +// StorageLive(_5); +// _5 = _1; +// _4 = const get_bool(move _5) -> bb5; +// } +// bb4: { +// StorageDead(_2); +// goto -> bb1; +// } +// bb5: { +// StorageDead(_5); +// switchInt(_4) -> [false: bb6, otherwise: bb7]; +// } +// bb6: { +// StorageDead(_4); +// goto -> bb0; +// } +// bb7: { +// StorageDead(_4); +// goto -> bb1; +// } +// END rustc.while_loop.PreCodegen.after.mir -- cgit 1.4.1-3-g733a5