From 882adea25bc2709725e7d4c9b0825f2640b7f339 Mon Sep 17 00:00:00 2001 From: Ding Xiang Fei Date: Tue, 24 Jan 2023 16:06:35 +0800 Subject: [PATCH] rescope temp lifetime in let-chain into IfElse --- compiler/rustc_middle/src/ty/rvalue_scopes.rs | 9 ++++- ...n_conditional.test_complex.built.after.mir | 34 +++++++++++-------- .../discriminant.main.ConstProp.32bit.diff | 3 +- .../discriminant.main.ConstProp.64bit.diff | 3 +- ..._let_loops.change_loop_body.ConstProp.diff | 3 +- ...onential_common.ConstProp.panic-abort.diff | 3 +- ...nential_common.ConstProp.panic-unwind.diff | 3 +- ...e_prop.debuginfo.ReferencePropagation.diff | 6 ++-- ....foo.SimplifyLocals-final.panic-abort.diff | 11 +++++- ...foo.SimplifyLocals-final.panic-unwind.diff | 11 +++++- ...s.enums.ScalarReplacementOfAggregates.diff | 3 +- ...in.UnreachablePropagation.panic-abort.diff | 2 +- ...n.UnreachablePropagation.panic-unwind.diff | 2 +- ...in.UnreachablePropagation.panic-abort.diff | 2 +- ...n.UnreachablePropagation.panic-unwind.diff | 2 +- tests/ui/drop/drop_order.rs | 8 ++--- tests/ui/mir/mir_let_chains_drop_order.rs | 4 +-- tests/ui/nll/issue-54556-niconii.rs | 3 +- tests/ui/nll/issue-54556-niconii.stderr | 26 -------------- 19 files changed, 76 insertions(+), 62 deletions(-) delete mode 100644 tests/ui/nll/issue-54556-niconii.stderr diff --git a/compiler/rustc_middle/src/ty/rvalue_scopes.rs b/compiler/rustc_middle/src/ty/rvalue_scopes.rs index 17eabec257e93..e78c7dfd2bd0d 100644 --- a/compiler/rustc_middle/src/ty/rvalue_scopes.rs +++ b/compiler/rustc_middle/src/ty/rvalue_scopes.rs @@ -38,7 +38,14 @@ impl RvalueScopes { debug!("temporary_scope({expr_id:?}) = {id:?} [enclosing]"); return Some(id); } - _ => id = p, + ScopeData::IfThen => { + debug!("temporary_scope({expr_id:?}) = {p:?} [enclosing]"); + return Some(p); + } + ScopeData::Node + | ScopeData::CallSite + | ScopeData::Arguments + | ScopeData::Remainder(_) => id = p, } } diff --git a/tests/mir-opt/building/logical_or_in_conditional.test_complex.built.after.mir b/tests/mir-opt/building/logical_or_in_conditional.test_complex.built.after.mir index 096aaec4a3888..04a2478afd4ce 100644 --- a/tests/mir-opt/building/logical_or_in_conditional.test_complex.built.after.mir +++ b/tests/mir-opt/building/logical_or_in_conditional.test_complex.built.after.mir @@ -19,7 +19,7 @@ fn test_complex() -> () { bb0: { StorageLive(_1); StorageLive(_2); - _2 = E::f() -> [return: bb1, unwind: bb31]; + _2 = E::f() -> [return: bb1, unwind: bb32]; } bb1: { @@ -38,7 +38,7 @@ fn test_complex() -> () { bb4: { StorageLive(_4); - _4 = always_true() -> [return: bb5, unwind: bb31]; + _4 = always_true() -> [return: bb5, unwind: bb32]; } bb5: { @@ -60,7 +60,7 @@ fn test_complex() -> () { } bb8: { - drop(_7) -> [return: bb10, unwind: bb31]; + drop(_7) -> [return: bb10, unwind: bb32]; } bb9: { @@ -74,7 +74,7 @@ fn test_complex() -> () { } bb11: { - drop(_7) -> [return: bb12, unwind: bb31]; + drop(_7) -> [return: bb12, unwind: bb32]; } bb12: { @@ -94,7 +94,7 @@ fn test_complex() -> () { } bb14: { - drop(_10) -> [return: bb16, unwind: bb31]; + drop(_10) -> [return: bb16, unwind: bb32]; } bb15: { @@ -105,11 +105,12 @@ fn test_complex() -> () { StorageDead(_10); StorageDead(_9); _1 = const (); + StorageDead(_2); goto -> bb20; } bb17: { - drop(_10) -> [return: bb18, unwind: bb31]; + drop(_10) -> [return: bb18, unwind: bb32]; } bb18: { @@ -119,6 +120,7 @@ fn test_complex() -> () { } bb19: { + StorageDead(_2); _1 = const (); goto -> bb20; } @@ -127,10 +129,9 @@ fn test_complex() -> () { StorageDead(_8); StorageDead(_5); StorageDead(_4); - StorageDead(_2); StorageDead(_1); StorageLive(_11); - _11 = always_true() -> [return: bb21, unwind: bb31]; + _11 = always_true() -> [return: bb21, unwind: bb32]; } bb21: { @@ -138,7 +139,7 @@ fn test_complex() -> () { } bb22: { - goto -> bb29; + goto -> bb30; } bb23: { @@ -147,7 +148,7 @@ fn test_complex() -> () { bb24: { StorageLive(_12); - _12 = E::f() -> [return: bb25, unwind: bb31]; + _12 = E::f() -> [return: bb25, unwind: bb32]; } bb25: { @@ -166,21 +167,26 @@ fn test_complex() -> () { bb28: { _0 = const (); - goto -> bb30; + StorageDead(_12); + goto -> bb31; } bb29: { - _0 = const (); + StorageDead(_12); goto -> bb30; } bb30: { + _0 = const (); + goto -> bb31; + } + + bb31: { StorageDead(_11); - StorageDead(_12); return; } - bb31 (cleanup): { + bb32 (cleanup): { resume; } } diff --git a/tests/mir-opt/const_prop/discriminant.main.ConstProp.32bit.diff b/tests/mir-opt/const_prop/discriminant.main.ConstProp.32bit.diff index e02e7f320b865..2b9feb79e06ed 100644 --- a/tests/mir-opt/const_prop/discriminant.main.ConstProp.32bit.diff +++ b/tests/mir-opt/const_prop/discriminant.main.ConstProp.32bit.diff @@ -32,10 +32,12 @@ bb2: { _2 = const 42_i32; + StorageDead(_3); goto -> bb4; } bb3: { + StorageDead(_3); _2 = const 10_i32; goto -> bb4; } @@ -43,7 +45,6 @@ bb4: { _1 = Add(move _2, const 0_i32); StorageDead(_2); - StorageDead(_3); _0 = const (); StorageDead(_1); return; diff --git a/tests/mir-opt/const_prop/discriminant.main.ConstProp.64bit.diff b/tests/mir-opt/const_prop/discriminant.main.ConstProp.64bit.diff index e02e7f320b865..2b9feb79e06ed 100644 --- a/tests/mir-opt/const_prop/discriminant.main.ConstProp.64bit.diff +++ b/tests/mir-opt/const_prop/discriminant.main.ConstProp.64bit.diff @@ -32,10 +32,12 @@ bb2: { _2 = const 42_i32; + StorageDead(_3); goto -> bb4; } bb3: { + StorageDead(_3); _2 = const 10_i32; goto -> bb4; } @@ -43,7 +45,6 @@ bb4: { _1 = Add(move _2, const 0_i32); StorageDead(_2); - StorageDead(_3); _0 = const (); StorageDead(_1); return; diff --git a/tests/mir-opt/const_prop/while_let_loops.change_loop_body.ConstProp.diff b/tests/mir-opt/const_prop/while_let_loops.change_loop_body.ConstProp.diff index f54908b4a38a6..2d0d339894d15 100644 --- a/tests/mir-opt/const_prop/while_let_loops.change_loop_body.ConstProp.diff +++ b/tests/mir-opt/const_prop/while_let_loops.change_loop_body.ConstProp.diff @@ -35,10 +35,12 @@ bb2: { _1 = const 1_i32; _0 = const (); + StorageDead(_3); goto -> bb4; } bb3: { + StorageDead(_3); StorageLive(_7); _0 = const (); StorageDead(_7); @@ -46,7 +48,6 @@ } bb4: { - StorageDead(_3); StorageDead(_1); return; } diff --git a/tests/mir-opt/funky_arms.float_to_exponential_common.ConstProp.panic-abort.diff b/tests/mir-opt/funky_arms.float_to_exponential_common.ConstProp.panic-abort.diff index 298a608489937..34d3874d2974f 100644 --- a/tests/mir-opt/funky_arms.float_to_exponential_common.ConstProp.panic-abort.diff +++ b/tests/mir-opt/funky_arms.float_to_exponential_common.ConstProp.panic-abort.diff @@ -87,10 +87,12 @@ bb5: { StorageDead(_12); StorageDead(_11); + StorageDead(_6); goto -> bb8; } bb6: { + StorageDead(_6); StorageLive(_18); _18 = _5; _0 = float_to_exponential_common_shortest::(_1, _2, move _18, _3) -> [return: bb7, unwind unreachable]; @@ -104,7 +106,6 @@ bb8: { StorageDead(_5); StorageDead(_4); - StorageDead(_6); return; } } diff --git a/tests/mir-opt/funky_arms.float_to_exponential_common.ConstProp.panic-unwind.diff b/tests/mir-opt/funky_arms.float_to_exponential_common.ConstProp.panic-unwind.diff index 037f4f7cfac25..25ad5d25be117 100644 --- a/tests/mir-opt/funky_arms.float_to_exponential_common.ConstProp.panic-unwind.diff +++ b/tests/mir-opt/funky_arms.float_to_exponential_common.ConstProp.panic-unwind.diff @@ -87,10 +87,12 @@ bb5: { StorageDead(_12); StorageDead(_11); + StorageDead(_6); goto -> bb8; } bb6: { + StorageDead(_6); StorageLive(_18); _18 = _5; _0 = float_to_exponential_common_shortest::(_1, _2, move _18, _3) -> [return: bb7, unwind continue]; @@ -104,7 +106,6 @@ bb8: { StorageDead(_5); StorageDead(_4); - StorageDead(_6); return; } } diff --git a/tests/mir-opt/reference_prop.debuginfo.ReferencePropagation.diff b/tests/mir-opt/reference_prop.debuginfo.ReferencePropagation.diff index 1648f5dd8ca63..fc15321ac16fb 100644 --- a/tests/mir-opt/reference_prop.debuginfo.ReferencePropagation.diff +++ b/tests/mir-opt/reference_prop.debuginfo.ReferencePropagation.diff @@ -127,17 +127,19 @@ StorageDead(_21); StorageDead(_20); StorageDead(_19); +- StorageDead(_12); +- StorageDead(_11); goto -> bb8; } bb7: { +- StorageDead(_12); +- StorageDead(_11); - _10 = const (); goto -> bb8; } bb8: { -- StorageDead(_12); -- StorageDead(_11); - StorageDead(_10); StorageLive(_22); StorageLive(_23); diff --git a/tests/mir-opt/simplify_locals_fixedpoint.foo.SimplifyLocals-final.panic-abort.diff b/tests/mir-opt/simplify_locals_fixedpoint.foo.SimplifyLocals-final.panic-abort.diff index 1566d7197acd8..3e5f44fc8185f 100644 --- a/tests/mir-opt/simplify_locals_fixedpoint.foo.SimplifyLocals-final.panic-abort.diff +++ b/tests/mir-opt/simplify_locals_fixedpoint.foo.SimplifyLocals-final.panic-abort.diff @@ -35,7 +35,7 @@ StorageLive(_6); _6 = (((_1.0: std::option::Option) as Some).0: u8); StorageDead(_6); - goto -> bb3; + drop(_1) -> [return: bb5, unwind unreachable]; } bb3: { @@ -44,6 +44,15 @@ bb4: { StorageDead(_1); + goto -> bb6; + } + + bb5: { + StorageDead(_1); + goto -> bb6; + } + + bb6: { return; } } diff --git a/tests/mir-opt/simplify_locals_fixedpoint.foo.SimplifyLocals-final.panic-unwind.diff b/tests/mir-opt/simplify_locals_fixedpoint.foo.SimplifyLocals-final.panic-unwind.diff index ba5262b0ee143..dadbe57eef6af 100644 --- a/tests/mir-opt/simplify_locals_fixedpoint.foo.SimplifyLocals-final.panic-unwind.diff +++ b/tests/mir-opt/simplify_locals_fixedpoint.foo.SimplifyLocals-final.panic-unwind.diff @@ -35,7 +35,7 @@ StorageLive(_6); _6 = (((_1.0: std::option::Option) as Some).0: u8); StorageDead(_6); - goto -> bb3; + drop(_1) -> [return: bb5, unwind continue]; } bb3: { @@ -44,6 +44,15 @@ bb4: { StorageDead(_1); + goto -> bb6; + } + + bb5: { + StorageDead(_1); + goto -> bb6; + } + + bb6: { return; } } diff --git a/tests/mir-opt/sroa/structs.enums.ScalarReplacementOfAggregates.diff b/tests/mir-opt/sroa/structs.enums.ScalarReplacementOfAggregates.diff index b5e39e632476c..3c610cb7b0f3b 100644 --- a/tests/mir-opt/sroa/structs.enums.ScalarReplacementOfAggregates.diff +++ b/tests/mir-opt/sroa/structs.enums.ScalarReplacementOfAggregates.diff @@ -27,16 +27,17 @@ _5 = ((_2 as Some).0: usize); _0 = _5; StorageDead(_5); + StorageDead(_2); goto -> bb3; } bb2: { + StorageDead(_2); _0 = const 0_usize; goto -> bb3; } bb3: { - StorageDead(_2); return; } } diff --git a/tests/mir-opt/unreachable.main.UnreachablePropagation.panic-abort.diff b/tests/mir-opt/unreachable.main.UnreachablePropagation.panic-abort.diff index eb5a0c39b0b2f..81460795abb76 100644 --- a/tests/mir-opt/unreachable.main.UnreachablePropagation.panic-abort.diff +++ b/tests/mir-opt/unreachable.main.UnreachablePropagation.panic-abort.diff @@ -59,8 +59,8 @@ - bb6: { + bb3: { - _0 = const (); StorageDead(_1); + _0 = const (); return; } } diff --git a/tests/mir-opt/unreachable.main.UnreachablePropagation.panic-unwind.diff b/tests/mir-opt/unreachable.main.UnreachablePropagation.panic-unwind.diff index 906dce9819fdc..708154fe0cf79 100644 --- a/tests/mir-opt/unreachable.main.UnreachablePropagation.panic-unwind.diff +++ b/tests/mir-opt/unreachable.main.UnreachablePropagation.panic-unwind.diff @@ -59,8 +59,8 @@ - bb6: { + bb3: { - _0 = const (); StorageDead(_1); + _0 = const (); return; } } diff --git a/tests/mir-opt/unreachable_diverging.main.UnreachablePropagation.panic-abort.diff b/tests/mir-opt/unreachable_diverging.main.UnreachablePropagation.panic-abort.diff index 713757ce6e080..9d29f49454f7c 100644 --- a/tests/mir-opt/unreachable_diverging.main.UnreachablePropagation.panic-abort.diff +++ b/tests/mir-opt/unreachable_diverging.main.UnreachablePropagation.panic-abort.diff @@ -56,9 +56,9 @@ } bb6: { + StorageDead(_2); _0 = const (); StorageDead(_1); - StorageDead(_2); return; } } diff --git a/tests/mir-opt/unreachable_diverging.main.UnreachablePropagation.panic-unwind.diff b/tests/mir-opt/unreachable_diverging.main.UnreachablePropagation.panic-unwind.diff index a0479fb91304b..49fa9769a023d 100644 --- a/tests/mir-opt/unreachable_diverging.main.UnreachablePropagation.panic-unwind.diff +++ b/tests/mir-opt/unreachable_diverging.main.UnreachablePropagation.panic-unwind.diff @@ -56,9 +56,9 @@ } bb6: { + StorageDead(_2); _0 = const (); StorageDead(_1); - StorageDead(_2); return; } } diff --git a/tests/ui/drop/drop_order.rs b/tests/ui/drop/drop_order.rs index 5ce1fd54a9e62..0666326ccf9ca 100644 --- a/tests/ui/drop/drop_order.rs +++ b/tests/ui/drop/drop_order.rs @@ -55,10 +55,10 @@ impl DropOrderCollector { } fn if_let(&self) { - if let None = self.option_loud_drop(2) { + if let None = self.option_loud_drop(1) { unreachable!(); } else { - self.print(1); + self.print(2); } if let Some(_) = self.option_loud_drop(4) { @@ -197,10 +197,10 @@ impl DropOrderCollector { // take the "else" branch if self.option_loud_drop(5).is_some() // 1 && self.option_loud_drop(6).is_some() // 2 - && let None = self.option_loud_drop(8) { // 4 + && let None = self.option_loud_drop(7) { // 3 unreachable!(); } else { - self.print(7); // 3 + self.print(8); // 4 } // let exprs interspersed diff --git a/tests/ui/mir/mir_let_chains_drop_order.rs b/tests/ui/mir/mir_let_chains_drop_order.rs index 6471553e93fd1..8128d38ac8b6c 100644 --- a/tests/ui/mir/mir_let_chains_drop_order.rs +++ b/tests/ui/mir/mir_let_chains_drop_order.rs @@ -57,7 +57,7 @@ fn main() { d(10, None) }, ); - assert_eq!(get(), vec![8, 7, 1, 3, 2]); + assert_eq!(get(), vec![3, 2, 8, 7, 1]); } assert_eq!(get(), vec![0, 4, 6, 9, 5]); @@ -89,5 +89,5 @@ fn main() { panic::panic_any(InjectedFailure) ); }); - assert_eq!(get(), vec![20, 17, 15, 11, 19, 18, 16, 12, 14, 13]); + assert_eq!(get(), vec![14, 13, 19, 18, 20, 17, 15, 11, 16, 12]); } diff --git a/tests/ui/nll/issue-54556-niconii.rs b/tests/ui/nll/issue-54556-niconii.rs index cae389e8ccb52..8e8759c674d39 100644 --- a/tests/ui/nll/issue-54556-niconii.rs +++ b/tests/ui/nll/issue-54556-niconii.rs @@ -1,3 +1,4 @@ +// check-pass // This is a reduction of a concrete test illustrating a case that was // annoying to Rust developer niconii (see comment thread on #21114). // @@ -19,7 +20,7 @@ impl Mutex { fn main() { let counter = Mutex; - if let Ok(_) = counter.lock() { } //~ ERROR does not live long enough + if let Ok(_) = counter.lock() { } // With this code as written, the dynamic semantics here implies // that `Mutex::drop` for `counter` runs *before* diff --git a/tests/ui/nll/issue-54556-niconii.stderr b/tests/ui/nll/issue-54556-niconii.stderr deleted file mode 100644 index ad0a2d1e3243e..0000000000000 --- a/tests/ui/nll/issue-54556-niconii.stderr +++ /dev/null @@ -1,26 +0,0 @@ -error[E0597]: `counter` does not live long enough - --> $DIR/issue-54556-niconii.rs:22:20 - | -LL | let counter = Mutex; - | ------- binding `counter` declared here -LL | -LL | if let Ok(_) = counter.lock() { } - | ^^^^^^^------- - | | - | borrowed value does not live long enough - | a temporary with access to the borrow is created here ... -... -LL | } - | - - | | - | `counter` dropped here while still borrowed - | ... and the borrow might be used here, when that temporary is dropped and runs the destructor for type `Result, ()>` - | -help: consider adding semicolon after the expression so its temporaries are dropped sooner, before the local variables declared by the block are dropped - | -LL | if let Ok(_) = counter.lock() { }; - | + - -error: aborting due to previous error - -For more information about this error, try `rustc --explain E0597`.