diff --git a/compiler/rustc_mir_transform/src/elaborate_drops.rs b/compiler/rustc_mir_transform/src/elaborate_drops.rs index c69d5d8b03b..79b27b2e1a4 100644 --- a/compiler/rustc_mir_transform/src/elaborate_drops.rs +++ b/compiler/rustc_mir_transform/src/elaborate_drops.rs @@ -54,7 +54,10 @@ impl<'tcx> MirPass<'tcx> for ElaborateDrops { let def_id = body.source.def_id(); let param_env = tcx.param_env_reveal_all_normalized(def_id); - let move_data = MoveData::gather_moves(&body, tcx, param_env, |_| true); + // For types that do not need dropping, the behaviour is trivial. So we only need to track + // init/uninit for types that do need dropping. + let move_data = + MoveData::gather_moves(&body, tcx, param_env, |ty| ty.needs_drop(tcx, param_env)); let elaborate_patch = { let env = MoveDataParamEnv { move_data, param_env }; @@ -338,6 +341,18 @@ impl<'b, 'tcx> ElaborateDropsCtxt<'b, 'tcx> { continue; }; + // This place does not need dropping. It is not have an associated move-path, so the + // match below will conservatively keep an unconditional drop. As that drop is useless, + // just remove it here and now. + if !place + .ty(&self.body.local_decls, self.tcx) + .ty + .needs_drop(self.tcx, self.env.param_env) + { + self.patch.patch_terminator(bb, TerminatorKind::Goto { target }); + continue; + } + let path = self.move_data().rev_lookup.find(place.as_ref()); match path { LookupResult::Exact(path) => { diff --git a/compiler/rustc_mir_transform/src/remove_uninit_drops.rs b/compiler/rustc_mir_transform/src/remove_uninit_drops.rs index a0c1969ac3c..87fee2410ec 100644 --- a/compiler/rustc_mir_transform/src/remove_uninit_drops.rs +++ b/compiler/rustc_mir_transform/src/remove_uninit_drops.rs @@ -24,7 +24,8 @@ pub struct RemoveUninitDrops; impl<'tcx> MirPass<'tcx> for RemoveUninitDrops { fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) { let param_env = tcx.param_env(body.source.def_id()); - let move_data = MoveData::gather_moves(&body, tcx, param_env, |_| true); + let move_data = + MoveData::gather_moves(&body, tcx, param_env, |ty| ty.needs_drop(tcx, param_env)); let mdpe = MoveDataParamEnv { move_data, param_env }; let mut maybe_inits = MaybeInitializedPlaces::new(tcx, body, &mdpe) diff --git a/tests/mir-opt/inline/unsized_argument.caller.Inline.diff b/tests/mir-opt/inline/unsized_argument.caller.Inline.diff index ab81f707148..37083973fd1 100644 --- a/tests/mir-opt/inline/unsized_argument.caller.Inline.diff +++ b/tests/mir-opt/inline/unsized_argument.caller.Inline.diff @@ -6,24 +6,18 @@ let mut _0: (); let _2: (); let mut _3: std::boxed::Box<[i32]>; - let mut _4: &mut std::boxed::Box<[i32]>; - let mut _5: (); - let mut _6: &mut std::boxed::Box<[i32]>; - let mut _7: (); - let mut _8: &mut std::boxed::Box<[i32]>; - let mut _9: (); - let mut _10: *const [i32]; + let mut _4: *const [i32]; bb0: { StorageLive(_2); StorageLive(_3); _3 = move _1; - _10 = (((_3.0: std::ptr::Unique<[i32]>).0: std::ptr::NonNull<[i32]>).0: *const [i32]); - _2 = callee(move (*_10)) -> [return: bb3, unwind: bb4]; + _4 = (((_3.0: std::ptr::Unique<[i32]>).0: std::ptr::NonNull<[i32]>).0: *const [i32]); + _2 = callee(move (*_4)) -> [return: bb1, unwind: bb3]; } - bb1 (cleanup): { - resume; + bb1: { + drop(_3) -> [return: bb2, unwind: bb4]; } bb2: { @@ -33,14 +27,12 @@ return; } - bb3: { - _4 = &mut _3; - _5 = as Drop>::drop(move _4) -> [return: bb2, unwind: bb1]; + bb3 (cleanup): { + drop(_3) -> [return: bb4, unwind terminate(cleanup)]; } bb4 (cleanup): { - _8 = &mut _3; - _9 = as Drop>::drop(move _8) -> [return: bb1, unwind terminate(cleanup)]; + resume; } } diff --git a/tests/mir-opt/issue_41888.main.ElaborateDrops.panic-abort.diff b/tests/mir-opt/issue_41888.main.ElaborateDrops.panic-abort.diff index b57fe348c2d..ae18ddc8366 100644 --- a/tests/mir-opt/issue_41888.main.ElaborateDrops.panic-abort.diff +++ b/tests/mir-opt/issue_41888.main.ElaborateDrops.panic-abort.diff @@ -10,9 +10,8 @@ let mut _5: isize; + let mut _7: bool; + let mut _8: bool; -+ let mut _9: bool; ++ let mut _9: isize; + let mut _10: isize; -+ let mut _11: isize; scope 1 { debug e => _1; scope 2 { @@ -24,7 +23,6 @@ bb0: { + _7 = const false; + _8 = const false; -+ _9 = const false; StorageLive(_1); StorageLive(_2); _2 = cond() -> [return: bb1, unwind: bb11]; @@ -47,7 +45,6 @@ bb3: { + _7 = const true; + _8 = const true; -+ _9 = const true; _1 = move _3; - drop(_3) -> [return: bb5, unwind: bb11]; + goto -> bb5; @@ -56,7 +53,6 @@ bb4 (cleanup): { + _7 = const true; + _8 = const true; -+ _9 = const true; _1 = move _3; - drop(_3) -> [return: bb11, unwind terminate(cleanup)]; + goto -> bb11; @@ -70,7 +66,6 @@ bb6: { StorageLive(_6); -+ _9 = const false; _6 = move ((_1 as F).0: K); _0 = const (); StorageDead(_6); @@ -90,13 +85,12 @@ bb9: { StorageDead(_2); - drop(_1) -> [return: bb10, unwind: bb12]; -+ goto -> bb18; ++ goto -> bb19; } bb10: { + _7 = const false; + _8 = const false; -+ _9 = const false; StorageDead(_1); return; } @@ -116,33 +110,37 @@ + } + + bb14 (cleanup): { -+ goto -> bb12; ++ drop(((_1 as F).0: K)) -> [return: bb12, unwind terminate(cleanup)]; + } + -+ bb15: { ++ bb15 (cleanup): { ++ switchInt(_7) -> [0: bb12, otherwise: bb14]; ++ } ++ ++ bb16: { + drop(_1) -> [return: bb13, unwind: bb12]; + } + -+ bb16 (cleanup): { ++ bb17 (cleanup): { + drop(_1) -> [return: bb12, unwind terminate(cleanup)]; + } + -+ bb17: { -+ _10 = discriminant(_1); -+ switchInt(move _10) -> [0: bb13, otherwise: bb15]; -+ } -+ + bb18: { -+ switchInt(_7) -> [0: bb13, otherwise: bb17]; ++ _9 = discriminant(_1); ++ switchInt(move _9) -> [0: bb13, otherwise: bb16]; + } + -+ bb19 (cleanup): { -+ _11 = discriminant(_1); -+ switchInt(move _11) -> [0: bb14, otherwise: bb16]; ++ bb19: { ++ switchInt(_7) -> [0: bb13, otherwise: bb18]; + } + + bb20 (cleanup): { -+ switchInt(_7) -> [0: bb12, otherwise: bb19]; ++ _10 = discriminant(_1); ++ switchInt(move _10) -> [0: bb15, otherwise: bb17]; ++ } ++ ++ bb21 (cleanup): { ++ switchInt(_7) -> [0: bb12, otherwise: bb20]; } } diff --git a/tests/mir-opt/issue_41888.main.ElaborateDrops.panic-unwind.diff b/tests/mir-opt/issue_41888.main.ElaborateDrops.panic-unwind.diff index 2156850e38c..d08113c0ba5 100644 --- a/tests/mir-opt/issue_41888.main.ElaborateDrops.panic-unwind.diff +++ b/tests/mir-opt/issue_41888.main.ElaborateDrops.panic-unwind.diff @@ -10,9 +10,8 @@ let mut _5: isize; + let mut _7: bool; + let mut _8: bool; -+ let mut _9: bool; ++ let mut _9: isize; + let mut _10: isize; -+ let mut _11: isize; scope 1 { debug e => _1; scope 2 { @@ -24,7 +23,6 @@ bb0: { + _7 = const false; + _8 = const false; -+ _9 = const false; StorageLive(_1); StorageLive(_2); _2 = cond() -> [return: bb1, unwind: bb11]; @@ -47,7 +45,6 @@ bb3: { + _7 = const true; + _8 = const true; -+ _9 = const true; _1 = move _3; - drop(_3) -> [return: bb5, unwind: bb11]; + goto -> bb5; @@ -56,7 +53,6 @@ bb4 (cleanup): { + _7 = const true; + _8 = const true; -+ _9 = const true; _1 = move _3; - drop(_3) -> [return: bb11, unwind terminate(cleanup)]; + goto -> bb11; @@ -70,7 +66,6 @@ bb6: { StorageLive(_6); -+ _9 = const false; _6 = move ((_1 as F).0: K); _0 = const (); StorageDead(_6); @@ -90,13 +85,12 @@ bb9: { StorageDead(_2); - drop(_1) -> [return: bb10, unwind continue]; -+ goto -> bb18; ++ goto -> bb19; } bb10: { + _7 = const false; + _8 = const false; -+ _9 = const false; StorageDead(_1); return; } @@ -116,33 +110,37 @@ + } + + bb14 (cleanup): { -+ goto -> bb12; ++ drop(((_1 as F).0: K)) -> [return: bb12, unwind terminate(cleanup)]; + } + -+ bb15: { ++ bb15 (cleanup): { ++ switchInt(_7) -> [0: bb12, otherwise: bb14]; ++ } ++ ++ bb16: { + drop(_1) -> [return: bb13, unwind: bb12]; + } + -+ bb16 (cleanup): { ++ bb17 (cleanup): { + drop(_1) -> [return: bb12, unwind terminate(cleanup)]; + } + -+ bb17: { -+ _10 = discriminant(_1); -+ switchInt(move _10) -> [0: bb13, otherwise: bb15]; -+ } -+ + bb18: { -+ switchInt(_7) -> [0: bb13, otherwise: bb17]; ++ _9 = discriminant(_1); ++ switchInt(move _9) -> [0: bb13, otherwise: bb16]; + } + -+ bb19 (cleanup): { -+ _11 = discriminant(_1); -+ switchInt(move _11) -> [0: bb14, otherwise: bb16]; ++ bb19: { ++ switchInt(_7) -> [0: bb13, otherwise: bb18]; + } + + bb20 (cleanup): { -+ switchInt(_7) -> [0: bb12, otherwise: bb19]; ++ _10 = discriminant(_1); ++ switchInt(move _10) -> [0: bb15, otherwise: bb17]; ++ } ++ ++ bb21 (cleanup): { ++ switchInt(_7) -> [0: bb12, otherwise: bb20]; } }