Skip to content

Commit 931ac5f

Browse files
committed
Do not create move paths that do not need dropping.
1 parent 44c7ae9 commit 931ac5f

5 files changed

+64
-73
lines changed

compiler/rustc_mir_transform/src/elaborate_drops.rs

+16-14
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ impl<'tcx> MirPass<'tcx> for ElaborateDrops {
5454

5555
let def_id = body.source.def_id();
5656
let param_env = tcx.param_env_reveal_all_normalized(def_id);
57-
let move_data = MoveData::gather_moves(&body, tcx, param_env, |_| true);
57+
let move_data =
58+
MoveData::gather_moves(&body, tcx, param_env, |ty| ty.needs_drop(tcx, param_env));
5859
let elaborate_patch = {
5960
let env = MoveDataParamEnv { move_data, param_env };
6061

@@ -340,7 +341,16 @@ impl<'b, 'tcx> ElaborateDropsCtxt<'b, 'tcx> {
340341
let terminator = data.terminator();
341342

342343
match terminator.kind {
343-
TerminatorKind::Drop { place, target, unwind, replace } => {
344+
TerminatorKind::Drop { place, target, unwind, replace: _ } => {
345+
if !place
346+
.ty(&self.body.local_decls, self.tcx)
347+
.ty
348+
.needs_drop(self.tcx, self.env.param_env)
349+
{
350+
self.patch.patch_terminator(bb, TerminatorKind::Goto { target });
351+
continue;
352+
}
353+
344354
self.init_data.seek_before(loc);
345355
match self.move_data().rev_lookup.find(place.as_ref()) {
346356
LookupResult::Exact(path) => {
@@ -373,18 +383,10 @@ impl<'b, 'tcx> ElaborateDropsCtxt<'b, 'tcx> {
373383
bb,
374384
)
375385
}
376-
LookupResult::Parent(..) => {
377-
if !replace {
378-
self.tcx.sess.delay_span_bug(
379-
terminator.source_info.span,
380-
format!("drop of untracked value {bb:?}"),
381-
);
382-
}
383-
// A drop and replace behind a pointer/array/whatever.
384-
// The borrow checker requires that these locations are initialized before the assignment,
385-
// so we just leave an unconditional drop.
386-
assert!(!data.is_cleanup);
387-
}
386+
// A drop and replace behind a pointer/array/whatever.
387+
// The borrow checker requires that these locations are initialized before the assignment,
388+
// so we just leave an unconditional drop.
389+
LookupResult::Parent(..) => {}
388390
}
389391
}
390392
_ => continue,

compiler/rustc_mir_transform/src/remove_uninit_drops.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ pub struct RemoveUninitDrops;
2424
impl<'tcx> MirPass<'tcx> for RemoveUninitDrops {
2525
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
2626
let param_env = tcx.param_env(body.source.def_id());
27-
let move_data = MoveData::gather_moves(&body, tcx, param_env, |_| true);
27+
let move_data =
28+
MoveData::gather_moves(&body, tcx, param_env, |ty| ty.needs_drop(tcx, param_env));
2829

2930
let mdpe = MoveDataParamEnv { move_data, param_env };
3031
let mut maybe_inits = MaybeInitializedPlaces::new(tcx, body, &mdpe)

tests/mir-opt/inline/unsized_argument.caller.Inline.diff

+8-16
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,18 @@
66
let mut _0: ();
77
let _2: ();
88
let mut _3: std::boxed::Box<[i32]>;
9-
let mut _4: &mut std::boxed::Box<[i32]>;
10-
let mut _5: ();
11-
let mut _6: &mut std::boxed::Box<[i32]>;
12-
let mut _7: ();
13-
let mut _8: &mut std::boxed::Box<[i32]>;
14-
let mut _9: ();
15-
let mut _10: *const [i32];
9+
let mut _4: *const [i32];
1610

1711
bb0: {
1812
StorageLive(_2);
1913
StorageLive(_3);
2014
_3 = move _1;
21-
_10 = (((_3.0: std::ptr::Unique<[i32]>).0: std::ptr::NonNull<[i32]>).0: *const [i32]);
22-
_2 = callee(move (*_10)) -> [return: bb3, unwind: bb4];
15+
_4 = (((_3.0: std::ptr::Unique<[i32]>).0: std::ptr::NonNull<[i32]>).0: *const [i32]);
16+
_2 = callee(move (*_4)) -> [return: bb1, unwind: bb3];
2317
}
2418

25-
bb1 (cleanup): {
26-
resume;
19+
bb1: {
20+
drop(_3) -> [return: bb2, unwind: bb4];
2721
}
2822

2923
bb2: {
@@ -33,14 +27,12 @@
3327
return;
3428
}
3529

36-
bb3: {
37-
_4 = &mut _3;
38-
_5 = <Box<[i32]> as Drop>::drop(move _4) -> [return: bb2, unwind: bb1];
30+
bb3 (cleanup): {
31+
drop(_3) -> [return: bb4, unwind terminate(cleanup)];
3932
}
4033

4134
bb4 (cleanup): {
42-
_8 = &mut _3;
43-
_9 = <Box<[i32]> as Drop>::drop(move _8) -> [return: bb1, unwind terminate(cleanup)];
35+
resume;
4436
}
4537
}
4638

tests/mir-opt/issue_41888.main.ElaborateDrops.panic-abort.diff

+19-21
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,8 @@
1010
let mut _5: isize;
1111
+ let mut _7: bool;
1212
+ let mut _8: bool;
13-
+ let mut _9: bool;
13+
+ let mut _9: isize;
1414
+ let mut _10: isize;
15-
+ let mut _11: isize;
1615
scope 1 {
1716
debug e => _1;
1817
scope 2 {
@@ -24,7 +23,6 @@
2423
bb0: {
2524
+ _7 = const false;
2625
+ _8 = const false;
27-
+ _9 = const false;
2826
StorageLive(_1);
2927
StorageLive(_2);
3028
_2 = cond() -> [return: bb1, unwind: bb11];
@@ -47,7 +45,6 @@
4745
bb3: {
4846
+ _7 = const true;
4947
+ _8 = const true;
50-
+ _9 = const true;
5148
_1 = move _3;
5249
- drop(_3) -> [return: bb5, unwind: bb11];
5350
+ goto -> bb5;
@@ -56,7 +53,6 @@
5653
bb4 (cleanup): {
5754
+ _7 = const true;
5855
+ _8 = const true;
59-
+ _9 = const true;
6056
_1 = move _3;
6157
- drop(_3) -> [return: bb11, unwind terminate(cleanup)];
6258
+ goto -> bb11;
@@ -70,7 +66,6 @@
7066

7167
bb6: {
7268
StorageLive(_6);
73-
+ _9 = const false;
7469
_6 = move ((_1 as F).0: K);
7570
_0 = const ();
7671
StorageDead(_6);
@@ -90,13 +85,12 @@
9085
bb9: {
9186
StorageDead(_2);
9287
- drop(_1) -> [return: bb10, unwind: bb12];
93-
+ goto -> bb18;
88+
+ goto -> bb19;
9489
}
9590

9691
bb10: {
9792
+ _7 = const false;
9893
+ _8 = const false;
99-
+ _9 = const false;
10094
StorageDead(_1);
10195
return;
10296
}
@@ -116,33 +110,37 @@
116110
+ }
117111
+
118112
+ bb14 (cleanup): {
119-
+ goto -> bb12;
113+
+ drop(((_1 as F).0: K)) -> [return: bb12, unwind terminate(cleanup)];
120114
+ }
121115
+
122-
+ bb15: {
123-
+ drop(_1) -> [return: bb13, unwind: bb12];
116+
+ bb15 (cleanup): {
117+
+ switchInt(_7) -> [0: bb12, otherwise: bb14];
124118
+ }
125119
+
126-
+ bb16 (cleanup): {
127-
+ drop(_1) -> [return: bb12, unwind terminate(cleanup)];
120+
+ bb16: {
121+
+ drop(_1) -> [return: bb13, unwind: bb12];
128122
+ }
129123
+
130-
+ bb17: {
131-
+ _10 = discriminant(_1);
132-
+ switchInt(move _10) -> [0: bb13, otherwise: bb15];
124+
+ bb17 (cleanup): {
125+
+ drop(_1) -> [return: bb12, unwind terminate(cleanup)];
133126
+ }
134127
+
135128
+ bb18: {
136-
+ switchInt(_7) -> [0: bb13, otherwise: bb17];
129+
+ _9 = discriminant(_1);
130+
+ switchInt(move _9) -> [0: bb13, otherwise: bb16];
137131
+ }
138132
+
139-
+ bb19 (cleanup): {
140-
+ _11 = discriminant(_1);
141-
+ switchInt(move _11) -> [0: bb14, otherwise: bb16];
133+
+ bb19: {
134+
+ switchInt(_7) -> [0: bb13, otherwise: bb18];
142135
+ }
143136
+
144137
+ bb20 (cleanup): {
145-
+ switchInt(_7) -> [0: bb12, otherwise: bb19];
138+
+ _10 = discriminant(_1);
139+
+ switchInt(move _10) -> [0: bb15, otherwise: bb17];
140+
+ }
141+
+
142+
+ bb21 (cleanup): {
143+
+ switchInt(_7) -> [0: bb12, otherwise: bb20];
146144
}
147145
}
148146

tests/mir-opt/issue_41888.main.ElaborateDrops.panic-unwind.diff

+19-21
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,8 @@
1010
let mut _5: isize;
1111
+ let mut _7: bool;
1212
+ let mut _8: bool;
13-
+ let mut _9: bool;
13+
+ let mut _9: isize;
1414
+ let mut _10: isize;
15-
+ let mut _11: isize;
1615
scope 1 {
1716
debug e => _1;
1817
scope 2 {
@@ -24,7 +23,6 @@
2423
bb0: {
2524
+ _7 = const false;
2625
+ _8 = const false;
27-
+ _9 = const false;
2826
StorageLive(_1);
2927
StorageLive(_2);
3028
_2 = cond() -> [return: bb1, unwind: bb11];
@@ -47,7 +45,6 @@
4745
bb3: {
4846
+ _7 = const true;
4947
+ _8 = const true;
50-
+ _9 = const true;
5148
_1 = move _3;
5249
- drop(_3) -> [return: bb5, unwind: bb11];
5350
+ goto -> bb5;
@@ -56,7 +53,6 @@
5653
bb4 (cleanup): {
5754
+ _7 = const true;
5855
+ _8 = const true;
59-
+ _9 = const true;
6056
_1 = move _3;
6157
- drop(_3) -> [return: bb11, unwind terminate(cleanup)];
6258
+ goto -> bb11;
@@ -70,7 +66,6 @@
7066

7167
bb6: {
7268
StorageLive(_6);
73-
+ _9 = const false;
7469
_6 = move ((_1 as F).0: K);
7570
_0 = const ();
7671
StorageDead(_6);
@@ -90,13 +85,12 @@
9085
bb9: {
9186
StorageDead(_2);
9287
- drop(_1) -> [return: bb10, unwind continue];
93-
+ goto -> bb18;
88+
+ goto -> bb19;
9489
}
9590

9691
bb10: {
9792
+ _7 = const false;
9893
+ _8 = const false;
99-
+ _9 = const false;
10094
StorageDead(_1);
10195
return;
10296
}
@@ -116,33 +110,37 @@
116110
+ }
117111
+
118112
+ bb14 (cleanup): {
119-
+ goto -> bb12;
113+
+ drop(((_1 as F).0: K)) -> [return: bb12, unwind terminate(cleanup)];
120114
+ }
121115
+
122-
+ bb15: {
123-
+ drop(_1) -> [return: bb13, unwind: bb12];
116+
+ bb15 (cleanup): {
117+
+ switchInt(_7) -> [0: bb12, otherwise: bb14];
124118
+ }
125119
+
126-
+ bb16 (cleanup): {
127-
+ drop(_1) -> [return: bb12, unwind terminate(cleanup)];
120+
+ bb16: {
121+
+ drop(_1) -> [return: bb13, unwind: bb12];
128122
+ }
129123
+
130-
+ bb17: {
131-
+ _10 = discriminant(_1);
132-
+ switchInt(move _10) -> [0: bb13, otherwise: bb15];
124+
+ bb17 (cleanup): {
125+
+ drop(_1) -> [return: bb12, unwind terminate(cleanup)];
133126
+ }
134127
+
135128
+ bb18: {
136-
+ switchInt(_7) -> [0: bb13, otherwise: bb17];
129+
+ _9 = discriminant(_1);
130+
+ switchInt(move _9) -> [0: bb13, otherwise: bb16];
137131
+ }
138132
+
139-
+ bb19 (cleanup): {
140-
+ _11 = discriminant(_1);
141-
+ switchInt(move _11) -> [0: bb14, otherwise: bb16];
133+
+ bb19: {
134+
+ switchInt(_7) -> [0: bb13, otherwise: bb18];
142135
+ }
143136
+
144137
+ bb20 (cleanup): {
145-
+ switchInt(_7) -> [0: bb12, otherwise: bb19];
138+
+ _10 = discriminant(_1);
139+
+ switchInt(move _10) -> [0: bb15, otherwise: bb17];
140+
+ }
141+
+
142+
+ bb21 (cleanup): {
143+
+ switchInt(_7) -> [0: bb12, otherwise: bb20];
146144
}
147145
}
148146

0 commit comments

Comments
 (0)