Skip to content

Commit

Permalink
rescope temp lifetime in let-chain into IfElse
Browse files Browse the repository at this point in the history
  • Loading branch information
dingxiangfei2009 committed Apr 25, 2024
1 parent b9be3c4 commit 3d420e9
Show file tree
Hide file tree
Showing 23 changed files with 253 additions and 65 deletions.
2 changes: 2 additions & 0 deletions compiler/rustc_feature/src/unstable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,8 @@ declare_features! (
(unstable, half_open_range_patterns_in_slices, "1.66.0", Some(67264)),
/// Allows `if let` guard in match arms.
(unstable, if_let_guard, "1.47.0", Some(51114)),
/// Rescoping temporaries in `if let` to align with Rust 2024.
(unstable, if_let_rescope, "1.78.0", Some(124085)),
/// Allows `impl Trait` to be used inside associated types (RFC 2515).
(unstable, impl_trait_in_assoc_type, "1.70.0", Some(63063)),
/// Allows `impl Trait` as output type in `Fn` traits in return position of functions.
Expand Down
3 changes: 2 additions & 1 deletion compiler/rustc_hir_typeck/src/rvalue_scopes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ pub fn resolve_rvalue_scopes<'a, 'tcx>(
def_id: DefId,
) -> RvalueScopes {
let tcx = &fcx.tcx;
let mut rvalue_scopes = RvalueScopes::new();
let mut rvalue_scopes =
RvalueScopes::new(tcx.features().if_let_rescope && tcx.sess.at_least_rust_2024());
debug!("start resolving rvalue scopes, def_id={def_id:?}");
debug!("rvalue_scope: rvalue_candidates={:?}", scope_tree.rvalue_candidates);
for (&hir_id, candidate) in &scope_tree.rvalue_candidates {
Expand Down
17 changes: 14 additions & 3 deletions compiler/rustc_middle/src/ty/rvalue_scopes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ use rustc_hir::ItemLocalMap;
/// rules laid out in `rustc_hir_analysis::check::rvalue_scopes`.
#[derive(TyEncodable, TyDecodable, Clone, Debug, Default, Eq, PartialEq, HashStable)]
pub struct RvalueScopes {
rescope_if_let: bool,
map: ItemLocalMap<Option<Scope>>,
}

impl RvalueScopes {
pub fn new() -> Self {
Self { map: <_>::default() }
pub fn new(rescope_if_let: bool) -> Self {
Self { rescope_if_let, map: <_>::default() }
}

/// Returns the scope when the temp created by `expr_id` will be cleaned up.
Expand All @@ -38,7 +39,17 @@ impl RvalueScopes {
debug!("temporary_scope({expr_id:?}) = {id:?} [enclosing]");
return Some(id);
}
_ => id = p,
ScopeData::IfThen => {
if self.rescope_if_let {
debug!("temporary_scope({expr_id:?}) = {p:?} [enclosing]");
return Some(p);
}
id = p;
}
ScopeData::Node
| ScopeData::CallSite
| ScopeData::Arguments
| ScopeData::Remainder(_) => id = p,
}
}

Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_span/src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -964,6 +964,7 @@ symbols! {
ident,
if_let,
if_let_guard,
if_let_rescope,
if_while_or_patterns,
ignore,
impl_header_lifetime_elision,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ fn test_complex() -> () {
bb0: {
StorageLive(_1);
StorageLive(_2);
_2 = E::f() -> [return: bb1, unwind: bb38];
_2 = E::f() -> [return: bb1, unwind: bb39];
}

bb1: {
Expand Down Expand Up @@ -51,7 +51,7 @@ fn test_complex() -> () {

bb7: {
StorageLive(_4);
_4 = always_true() -> [return: bb8, unwind: bb38];
_4 = always_true() -> [return: bb8, unwind: bb39];
}

bb8: {
Expand All @@ -73,7 +73,7 @@ fn test_complex() -> () {
}

bb11: {
drop(_7) -> [return: bb13, unwind: bb38];
drop(_7) -> [return: bb13, unwind: bb39];
}

bb12: {
Expand All @@ -87,7 +87,7 @@ fn test_complex() -> () {
}

bb14: {
drop(_7) -> [return: bb15, unwind: bb38];
drop(_7) -> [return: bb15, unwind: bb39];
}

bb15: {
Expand All @@ -107,7 +107,7 @@ fn test_complex() -> () {
}

bb17: {
drop(_10) -> [return: bb19, unwind: bb38];
drop(_10) -> [return: bb19, unwind: bb39];
}

bb18: {
Expand All @@ -122,11 +122,12 @@ fn test_complex() -> () {

bb20: {
_1 = const ();
StorageDead(_2);
goto -> bb24;
}

bb21: {
drop(_10) -> [return: bb22, unwind: bb38];
drop(_10) -> [return: bb22, unwind: bb39];
}

bb22: {
Expand All @@ -136,6 +137,7 @@ fn test_complex() -> () {
}

bb23: {
StorageDead(_2);
_1 = const ();
goto -> bb24;
}
Expand All @@ -144,18 +146,17 @@ fn test_complex() -> () {
StorageDead(_8);
StorageDead(_5);
StorageDead(_4);
StorageDead(_2);
StorageDead(_1);
StorageLive(_11);
_11 = always_true() -> [return: bb25, unwind: bb38];
_11 = always_true() -> [return: bb25, unwind: bb39];
}

bb25: {
switchInt(move _11) -> [0: bb27, otherwise: bb26];
}

bb26: {
goto -> bb36;
goto -> bb37;
}

bb27: {
Expand All @@ -164,7 +165,7 @@ fn test_complex() -> () {

bb28: {
StorageLive(_12);
_12 = E::f() -> [return: bb29, unwind: bb38];
_12 = E::f() -> [return: bb29, unwind: bb39];
}

bb29: {
Expand Down Expand Up @@ -196,21 +197,26 @@ fn test_complex() -> () {

bb35: {
_0 = const ();
goto -> bb37;
StorageDead(_12);
goto -> bb38;
}

bb36: {
_0 = const ();
StorageDead(_12);
goto -> bb37;
}

bb37: {
_0 = const ();
goto -> bb38;
}

bb38: {
StorageDead(_11);
StorageDead(_12);
return;
}

bb38 (cleanup): {
bb39 (cleanup): {
resume;
}
}
3 changes: 2 additions & 1 deletion tests/mir-opt/const_prop/discriminant.main.GVN.32bit.diff
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,19 @@

bb2: {
_2 = const 42_i32;
StorageDead(_3);
goto -> bb4;
}

bb3: {
StorageDead(_3);
_2 = const 10_i32;
goto -> bb4;
}

bb4: {
_1 = Add(move _2, const 0_i32);
StorageDead(_2);
StorageDead(_3);
_0 = const ();
StorageDead(_1);
return;
Expand Down
3 changes: 2 additions & 1 deletion tests/mir-opt/const_prop/discriminant.main.GVN.64bit.diff
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,19 @@

bb2: {
_2 = const 42_i32;
StorageDead(_3);
goto -> bb4;
}

bb3: {
StorageDead(_3);
_2 = const 10_i32;
goto -> bb4;
}

bb4: {
_1 = Add(move _2, const 0_i32);
StorageDead(_2);
StorageDead(_3);
_0 = const ();
StorageDead(_1);
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,19 @@
bb2: {
_1 = const 1_i32;
_0 = const ();
StorageDead(_3);
goto -> bb4;
}

bb3: {
StorageDead(_3);
StorageLive(_7);
_0 = const ();
StorageDead(_7);
goto -> bb4;
}

bb4: {
StorageDead(_3);
StorageDead(_1);
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,12 @@
StorageDead(_9);
- StorageDead(_8);
+ nop;
StorageDead(_6);
goto -> bb8;
}

bb6: {
StorageDead(_6);
StorageLive(_16);
_16 = _1;
StorageLive(_17);
Expand All @@ -130,7 +132,6 @@
bb8: {
StorageDead(_5);
StorageDead(_4);
StorageDead(_6);
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,12 @@
StorageDead(_9);
- StorageDead(_8);
+ nop;
StorageDead(_6);
goto -> bb8;
}

bb6: {
StorageDead(_6);
StorageLive(_16);
_16 = _1;
StorageLive(_17);
Expand All @@ -130,7 +132,6 @@
bb8: {
StorageDead(_5);
StorageDead(_4);
StorageDead(_6);
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,19 @@
StorageDead(_3);
StorageDead(_2);
_5 = discriminant((_1.0: std::option::Option<u8>));
switchInt(move _5) -> [1: bb1, 0: bb3, otherwise: bb5];
switchInt(move _5) -> [1: bb1, 0: bb3, otherwise: bb7];
}

bb1: {
_4 = discriminant((_1.1: std::option::Option<T>));
switchInt(move _4) -> [0: bb2, 1: bb3, otherwise: bb5];
switchInt(move _4) -> [0: bb2, 1: bb3, otherwise: bb7];
}

bb2: {
StorageLive(_6);
_6 = (((_1.0: std::option::Option<u8>) as Some).0: u8);
StorageDead(_6);
goto -> bb3;
drop(_1) -> [return: bb5, unwind unreachable];
}

bb3: {
Expand All @@ -44,10 +44,19 @@

bb4: {
StorageDead(_1);
return;
goto -> bb6;
}

bb5: {
StorageDead(_1);
goto -> bb6;
}

bb6: {
return;
}

bb7: {
unreachable;
}
}
Expand Down
Loading

0 comments on commit 3d420e9

Please sign in to comment.