diff --git a/compiler/rustc_mir/src/transform/inline.rs b/compiler/rustc_mir/src/transform/inline.rs index 96df6023c71..947466c87fa 100644 --- a/compiler/rustc_mir/src/transform/inline.rs +++ b/compiler/rustc_mir/src/transform/inline.rs @@ -435,6 +435,11 @@ impl Inliner<'tcx> { let mut scope_map = IndexVec::with_capacity(callee_body.source_scopes.len()); for mut scope in callee_body.source_scopes.iter().cloned() { + // Map the callee scopes into the caller. + // FIXME(eddyb) this may ICE if the scopes are out of order. + scope.parent_scope = scope.parent_scope.map(|s| scope_map[s]); + scope.inlined_parent_scope = scope.inlined_parent_scope.map(|s| scope_map[s]); + if scope.parent_scope.is_none() { let callsite_scope = &caller_body.source_scopes[callsite.source_info.scope]; diff --git a/src/test/mir-opt/inline/inline_closure_borrows_arg.foo.Inline.after.mir b/src/test/mir-opt/inline/inline_closure_borrows_arg.foo.Inline.after.mir index 390b2320ef6..cb2cd785b48 100644 --- a/src/test/mir-opt/inline/inline_closure_borrows_arg.foo.Inline.after.mir +++ b/src/test/mir-opt/inline/inline_closure_borrows_arg.foo.Inline.after.mir @@ -17,11 +17,11 @@ fn foo(_1: T, _2: &i32) -> i32 { debug r => _9; // in scope 2 at $DIR/inline-closure-borrows-arg.rs:12:14: 12:15 debug _s => _10; // in scope 2 at $DIR/inline-closure-borrows-arg.rs:12:23: 12:25 let _8: &i32; // in scope 2 at $DIR/inline-closure-borrows-arg.rs:16:5: 16:12 + scope 3 { + debug variable => _8; // in scope 3 at $DIR/inline-closure-borrows-arg.rs:13:13: 13:21 + } } } - scope 3 { - debug variable => _8; // in scope 3 at $DIR/inline-closure-borrows-arg.rs:13:13: 13:21 - } bb0: { StorageLive(_3); // scope 0 at $DIR/inline-closure-borrows-arg.rs:12:9: 12:10 diff --git a/src/test/mir-opt/inline/issue-76997-inline-scopes-parenting.rs b/src/test/mir-opt/inline/issue-76997-inline-scopes-parenting.rs new file mode 100644 index 00000000000..76d806acc63 --- /dev/null +++ b/src/test/mir-opt/inline/issue-76997-inline-scopes-parenting.rs @@ -0,0 +1,7 @@ +// Tests that MIR inliner can handle `SourceScopeData` parenting correctly. (#76997) + +// EMIT_MIR issue_76997_inline_scopes_parenting.main.Inline.after.mir +fn main() { + let f = |x| { let y = x; y }; + f(()) +} diff --git a/src/test/mir-opt/inline/issue_76997_inline_scopes_parenting.main.Inline.after.mir b/src/test/mir-opt/inline/issue_76997_inline_scopes_parenting.main.Inline.after.mir new file mode 100644 index 00000000000..9d1e8aca408 --- /dev/null +++ b/src/test/mir-opt/inline/issue_76997_inline_scopes_parenting.main.Inline.after.mir @@ -0,0 +1,41 @@ +// MIR for `main` after Inline + +fn main() -> () { + let mut _0: (); // return place in scope 0 at $DIR/issue-76997-inline-scopes-parenting.rs:4:11: 4:11 + let _1: [closure@$DIR/issue-76997-inline-scopes-parenting.rs:5:13: 5:33]; // in scope 0 at $DIR/issue-76997-inline-scopes-parenting.rs:5:9: 5:10 + let mut _2: &[closure@$DIR/issue-76997-inline-scopes-parenting.rs:5:13: 5:33]; // in scope 0 at $DIR/issue-76997-inline-scopes-parenting.rs:6:5: 6:6 + let mut _3: ((),); // in scope 0 at $DIR/issue-76997-inline-scopes-parenting.rs:6:5: 6:10 + let mut _4: (); // in scope 0 at $DIR/issue-76997-inline-scopes-parenting.rs:6:7: 6:9 + let mut _6: (); // in scope 0 at $DIR/issue-76997-inline-scopes-parenting.rs:6:5: 6:10 + scope 1 { + debug f => _1; // in scope 1 at $DIR/issue-76997-inline-scopes-parenting.rs:5:9: 5:10 + scope 2 (inlined main::{closure#0}) { // at $DIR/issue-76997-inline-scopes-parenting.rs:6:5: 6:10 + debug x => _6; // in scope 2 at $DIR/issue-76997-inline-scopes-parenting.rs:5:14: 5:15 + let _5: (); // in scope 2 at $DIR/issue-76997-inline-scopes-parenting.rs:6:5: 6:10 + scope 3 { + debug y => _5; // in scope 3 at $DIR/issue-76997-inline-scopes-parenting.rs:5:23: 5:24 + } + } + } + + bb0: { + StorageLive(_1); // scope 0 at $DIR/issue-76997-inline-scopes-parenting.rs:5:9: 5:10 + StorageLive(_2); // scope 1 at $DIR/issue-76997-inline-scopes-parenting.rs:6:5: 6:6 + _2 = &_1; // scope 1 at $DIR/issue-76997-inline-scopes-parenting.rs:6:5: 6:6 + StorageLive(_3); // scope 1 at $DIR/issue-76997-inline-scopes-parenting.rs:6:5: 6:10 + StorageLive(_4); // scope 1 at $DIR/issue-76997-inline-scopes-parenting.rs:6:7: 6:9 + (_3.0: ()) = move _4; // scope 1 at $DIR/issue-76997-inline-scopes-parenting.rs:6:5: 6:10 + StorageLive(_6); // scope 1 at $DIR/issue-76997-inline-scopes-parenting.rs:6:5: 6:10 + _6 = move (_3.0: ()); // scope 1 at $DIR/issue-76997-inline-scopes-parenting.rs:6:5: 6:10 + StorageLive(_5); // scope 2 at $DIR/issue-76997-inline-scopes-parenting.rs:5:23: 5:24 + _5 = const (); // scope 2 at $DIR/issue-76997-inline-scopes-parenting.rs:5:27: 5:28 + _0 = const (); // scope 3 at $DIR/issue-76997-inline-scopes-parenting.rs:5:30: 5:31 + StorageDead(_5); // scope 2 at $DIR/issue-76997-inline-scopes-parenting.rs:5:32: 5:33 + StorageDead(_6); // scope 1 at $DIR/issue-76997-inline-scopes-parenting.rs:6:5: 6:10 + StorageDead(_4); // scope 1 at $DIR/issue-76997-inline-scopes-parenting.rs:6:9: 6:10 + StorageDead(_3); // scope 1 at $DIR/issue-76997-inline-scopes-parenting.rs:6:9: 6:10 + StorageDead(_2); // scope 1 at $DIR/issue-76997-inline-scopes-parenting.rs:6:9: 6:10 + StorageDead(_1); // scope 0 at $DIR/issue-76997-inline-scopes-parenting.rs:7:1: 7:2 + return; // scope 0 at $DIR/issue-76997-inline-scopes-parenting.rs:7:2: 7:2 + } +} diff --git a/src/test/mir-opt/issue_73223.main.PreCodegen.32bit.diff b/src/test/mir-opt/issue_73223.main.PreCodegen.32bit.diff index ac08a4512d0..82d17488782 100644 --- a/src/test/mir-opt/issue_73223.main.PreCodegen.32bit.diff +++ b/src/test/mir-opt/issue_73223.main.PreCodegen.32bit.diff @@ -41,6 +41,8 @@ let mut _23: for<'r, 's, 't0> fn(&'r core::fmt::Opaque, &'s mut std::fmt::Formatter<'t0>) -> std::result::Result<(), std::fmt::Error>; // in scope 6 at $SRC_DIR/std/src/macros.rs:LL:COL let mut _24: for<'r, 's, 't0> fn(&'r &i32, &'s mut std::fmt::Formatter<'t0>) -> std::result::Result<(), std::fmt::Error>; // in scope 6 at $SRC_DIR/std/src/macros.rs:LL:COL let mut _25: &&i32; // in scope 6 at $SRC_DIR/std/src/macros.rs:LL:COL + scope 7 { + } } scope 8 (inlined ArgumentV1::new::<&i32>) { // at $SRC_DIR/std/src/macros.rs:LL:COL debug x => _28; // in scope 8 at $SRC_DIR/core/src/fmt/mod.rs:LL:COL @@ -48,6 +50,8 @@ let mut _26: for<'r, 's, 't0> fn(&'r core::fmt::Opaque, &'s mut std::fmt::Formatter<'t0>) -> std::result::Result<(), std::fmt::Error>; // in scope 8 at $SRC_DIR/std/src/macros.rs:LL:COL let mut _27: for<'r, 's, 't0> fn(&'r &i32, &'s mut std::fmt::Formatter<'t0>) -> std::result::Result<(), std::fmt::Error>; // in scope 8 at $SRC_DIR/std/src/macros.rs:LL:COL let mut _28: &&i32; // in scope 8 at $SRC_DIR/std/src/macros.rs:LL:COL + scope 9 { + } } } scope 10 (inlined Arguments::new_v1) { // at $SRC_DIR/std/src/macros.rs:LL:COL @@ -63,10 +67,6 @@ scope 2 { debug v => _3; // in scope 2 at $DIR/issue-73223.rs:3:14: 3:15 } - scope 7 { - } - scope 9 { - } bb0: { StorageLive(_1); // scope 0 at $DIR/issue-73223.rs:2:9: 2:14 diff --git a/src/test/mir-opt/issue_73223.main.PreCodegen.64bit.diff b/src/test/mir-opt/issue_73223.main.PreCodegen.64bit.diff index ac08a4512d0..82d17488782 100644 --- a/src/test/mir-opt/issue_73223.main.PreCodegen.64bit.diff +++ b/src/test/mir-opt/issue_73223.main.PreCodegen.64bit.diff @@ -41,6 +41,8 @@ let mut _23: for<'r, 's, 't0> fn(&'r core::fmt::Opaque, &'s mut std::fmt::Formatter<'t0>) -> std::result::Result<(), std::fmt::Error>; // in scope 6 at $SRC_DIR/std/src/macros.rs:LL:COL let mut _24: for<'r, 's, 't0> fn(&'r &i32, &'s mut std::fmt::Formatter<'t0>) -> std::result::Result<(), std::fmt::Error>; // in scope 6 at $SRC_DIR/std/src/macros.rs:LL:COL let mut _25: &&i32; // in scope 6 at $SRC_DIR/std/src/macros.rs:LL:COL + scope 7 { + } } scope 8 (inlined ArgumentV1::new::<&i32>) { // at $SRC_DIR/std/src/macros.rs:LL:COL debug x => _28; // in scope 8 at $SRC_DIR/core/src/fmt/mod.rs:LL:COL @@ -48,6 +50,8 @@ let mut _26: for<'r, 's, 't0> fn(&'r core::fmt::Opaque, &'s mut std::fmt::Formatter<'t0>) -> std::result::Result<(), std::fmt::Error>; // in scope 8 at $SRC_DIR/std/src/macros.rs:LL:COL let mut _27: for<'r, 's, 't0> fn(&'r &i32, &'s mut std::fmt::Formatter<'t0>) -> std::result::Result<(), std::fmt::Error>; // in scope 8 at $SRC_DIR/std/src/macros.rs:LL:COL let mut _28: &&i32; // in scope 8 at $SRC_DIR/std/src/macros.rs:LL:COL + scope 9 { + } } } scope 10 (inlined Arguments::new_v1) { // at $SRC_DIR/std/src/macros.rs:LL:COL @@ -63,10 +67,6 @@ scope 2 { debug v => _3; // in scope 2 at $DIR/issue-73223.rs:3:14: 3:15 } - scope 7 { - } - scope 9 { - } bb0: { StorageLive(_1); // scope 0 at $DIR/issue-73223.rs:2:9: 2:14 diff --git a/src/test/mir-opt/issue_73223.main.SimplifyArmIdentity.32bit.diff b/src/test/mir-opt/issue_73223.main.SimplifyArmIdentity.32bit.diff index b302114fa17..f4295b278e6 100644 --- a/src/test/mir-opt/issue_73223.main.SimplifyArmIdentity.32bit.diff +++ b/src/test/mir-opt/issue_73223.main.SimplifyArmIdentity.32bit.diff @@ -65,6 +65,8 @@ let mut _47: for<'r, 's, 't0> fn(&'r &i32, &'s mut std::fmt::Formatter<'t0>) -> std::result::Result<(), std::fmt::Error>; // in scope 6 at $SRC_DIR/std/src/macros.rs:LL:COL let mut _48: &core::fmt::Opaque; // in scope 6 at $SRC_DIR/std/src/macros.rs:LL:COL let mut _49: &&i32; // in scope 6 at $SRC_DIR/std/src/macros.rs:LL:COL + scope 7 { + } } scope 8 (inlined ArgumentV1::new::<&i32>) { // at $SRC_DIR/std/src/macros.rs:LL:COL debug x => _42; // in scope 8 at $SRC_DIR/core/src/fmt/mod.rs:LL:COL @@ -73,6 +75,8 @@ let mut _51: for<'r, 's, 't0> fn(&'r &i32, &'s mut std::fmt::Formatter<'t0>) -> std::result::Result<(), std::fmt::Error>; // in scope 8 at $SRC_DIR/std/src/macros.rs:LL:COL let mut _52: &core::fmt::Opaque; // in scope 8 at $SRC_DIR/std/src/macros.rs:LL:COL let mut _53: &&i32; // in scope 8 at $SRC_DIR/std/src/macros.rs:LL:COL + scope 9 { + } } } scope 10 (inlined Arguments::new_v1) { // at $SRC_DIR/std/src/macros.rs:LL:COL @@ -88,10 +92,6 @@ scope 2 { debug v => _4; // in scope 2 at $DIR/issue-73223.rs:3:14: 3:15 } - scope 7 { - } - scope 9 { - } bb0: { StorageLive(_1); // scope 0 at $DIR/issue-73223.rs:2:9: 2:14 diff --git a/src/test/mir-opt/issue_73223.main.SimplifyArmIdentity.64bit.diff b/src/test/mir-opt/issue_73223.main.SimplifyArmIdentity.64bit.diff index b302114fa17..f4295b278e6 100644 --- a/src/test/mir-opt/issue_73223.main.SimplifyArmIdentity.64bit.diff +++ b/src/test/mir-opt/issue_73223.main.SimplifyArmIdentity.64bit.diff @@ -65,6 +65,8 @@ let mut _47: for<'r, 's, 't0> fn(&'r &i32, &'s mut std::fmt::Formatter<'t0>) -> std::result::Result<(), std::fmt::Error>; // in scope 6 at $SRC_DIR/std/src/macros.rs:LL:COL let mut _48: &core::fmt::Opaque; // in scope 6 at $SRC_DIR/std/src/macros.rs:LL:COL let mut _49: &&i32; // in scope 6 at $SRC_DIR/std/src/macros.rs:LL:COL + scope 7 { + } } scope 8 (inlined ArgumentV1::new::<&i32>) { // at $SRC_DIR/std/src/macros.rs:LL:COL debug x => _42; // in scope 8 at $SRC_DIR/core/src/fmt/mod.rs:LL:COL @@ -73,6 +75,8 @@ let mut _51: for<'r, 's, 't0> fn(&'r &i32, &'s mut std::fmt::Formatter<'t0>) -> std::result::Result<(), std::fmt::Error>; // in scope 8 at $SRC_DIR/std/src/macros.rs:LL:COL let mut _52: &core::fmt::Opaque; // in scope 8 at $SRC_DIR/std/src/macros.rs:LL:COL let mut _53: &&i32; // in scope 8 at $SRC_DIR/std/src/macros.rs:LL:COL + scope 9 { + } } } scope 10 (inlined Arguments::new_v1) { // at $SRC_DIR/std/src/macros.rs:LL:COL @@ -88,10 +92,6 @@ scope 2 { debug v => _4; // in scope 2 at $DIR/issue-73223.rs:3:14: 3:15 } - scope 7 { - } - scope 9 { - } bb0: { StorageLive(_1); // scope 0 at $DIR/issue-73223.rs:2:9: 2:14