Skip to content

Commit 737631c

Browse files
committed
Avoid modifying existing blocks.
1 parent e982162 commit 737631c

24 files changed

Lines changed: 1504 additions & 1115 deletions

File tree

compiler/rustc_mir_transform/src/elaborate_drop.rs

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ where
277277
(sig.output(), drop_fn_def_id, trait_args)
278278
};
279279

280-
let fut = Place::from(self.new_temp(fut_ty));
280+
let fut = self.new_temp(fut_ty);
281281

282282
// #1:pin_obj_bb >>> obj_ref = &mut obj
283283
let obj_ref_ty = Ty::new_mut_ref(tcx, tcx.lifetimes.re_erased, drop_ty);
@@ -312,20 +312,34 @@ where
312312
// causing StorageLive(fut) to fire again without a preceding StorageDead.
313313
let succ_with_dead = self.new_block_with_statements(
314314
unwind,
315-
vec![Statement::new(self.source_info, StatementKind::StorageDead(fut.local))],
315+
vec![self.storage_dead(fut)],
316316
TerminatorKind::Goto { target: succ },
317317
);
318+
let dropline_with_dead = dropline.map(|target| {
319+
self.new_block_with_statements(
320+
unwind,
321+
vec![self.storage_dead(fut)],
322+
TerminatorKind::Goto { target },
323+
)
324+
});
325+
let unwind_with_dead = unwind.map(|target| {
326+
self.new_block_with_statements(
327+
Unwind::InCleanup,
328+
vec![self.storage_dead(fut)],
329+
TerminatorKind::Goto { target },
330+
)
331+
});
318332

319333
// #3:drop_term_bb
320334
let drop_term_bb = self.new_block(
321335
unwind,
322336
TerminatorKind::Drop {
323337
place,
324338
target: succ_with_dead,
325-
unwind: unwind.into_action(),
339+
unwind: unwind_with_dead.into_action(),
326340
replace: false,
327-
drop: dropline,
328-
async_fut: Some(fut.local),
341+
drop: dropline_with_dead,
342+
async_fut: Some(fut),
329343
},
330344
);
331345

@@ -350,36 +364,21 @@ where
350364

351365
obj_ref_place
352366
};
353-
call_statements
354-
.push(Statement::new(self.source_info, StatementKind::StorageLive(fut.local)));
367+
call_statements.push(self.storage_live(fut));
355368

356369
let call_drop_bb = self.new_block_with_statements(
357370
unwind,
358371
call_statements,
359372
TerminatorKind::Call {
360373
func: Operand::function_handle(tcx, drop_fn_def_id, trait_args, span),
361374
args: [Spanned { node: Operand::Move(drop_arg), span: DUMMY_SP }].into(),
362-
destination: fut,
375+
destination: fut.into(),
363376
target: Some(drop_term_bb),
364-
unwind: unwind.into_action(),
377+
unwind: unwind_with_dead.into_action(),
365378
call_source: CallSource::Misc,
366379
fn_span: self.source_info.span,
367380
},
368381
);
369-
// StorageDead(fut) in unwind block (at the begin)
370-
if let Unwind::To(block) = unwind {
371-
self.elaborator.patch().add_statement(
372-
Location { block, statement_index: 0 },
373-
StatementKind::StorageDead(fut.local),
374-
);
375-
}
376-
// StorageDead(fut) in dropline block (at the begin)
377-
if let Some(block) = dropline {
378-
self.elaborator.patch().add_statement(
379-
Location { block, statement_index: 0 },
380-
StatementKind::StorageDead(fut.local),
381-
);
382-
}
383382

384383
// #1:pin_obj_bb >>> call Pin<ObjTy>::new_unchecked(&mut obj)
385384
self.new_block_with_statements(
@@ -1399,4 +1398,12 @@ where
13991398
fn assign(&self, lhs: Place<'tcx>, rhs: Rvalue<'tcx>) -> Statement<'tcx> {
14001399
Statement::new(self.source_info, StatementKind::Assign(Box::new((lhs, rhs))))
14011400
}
1401+
1402+
fn storage_live(&self, local: Local) -> Statement<'tcx> {
1403+
Statement::new(self.source_info, StatementKind::StorageLive(local))
1404+
}
1405+
1406+
fn storage_dead(&self, local: Local) -> Statement<'tcx> {
1407+
Statement::new(self.source_info, StatementKind::StorageDead(local))
1408+
}
14021409
}

tests/mir-opt/coroutine/async_drop.core.future-async_drop-async_drop_in_place-{closure#0}.AsyncEnum.MentionedItems.after.mir

Lines changed: 56 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,14 @@ yields ()
2222

2323
bb0: {
2424
_3 = move (_1.0: &mut AsyncEnum);
25-
goto -> bb24;
25+
goto -> bb29;
2626
}
2727

2828
bb1: {
29-
StorageDead(_8);
3029
return;
3130
}
3231

3332
bb2 (cleanup): {
34-
StorageDead(_4);
35-
StorageDead(_8);
3633
resume;
3734
}
3835

@@ -53,90 +50,113 @@ yields ()
5350
goto -> bb1;
5451
}
5552

56-
bb7: {
57-
async drop((((*_3) as A).0: AsyncInt); poll=_4) -> [return: bb6, unwind: bb2];
53+
bb7 (cleanup): {
54+
StorageDead(_4);
55+
goto -> bb2;
5856
}
5957

6058
bb8: {
59+
async drop((((*_3) as A).0: AsyncInt); poll=_4) -> [return: bb6, unwind: bb7];
60+
}
61+
62+
bb9: {
6163
_7 = copy (_6.0: &mut AsyncInt);
6264
StorageLive(_4);
63-
_4 = async_drop_in_place::<AsyncInt>(move _7) -> [return: bb7, unwind: bb2];
65+
_4 = async_drop_in_place::<AsyncInt>(move _7) -> [return: bb8, unwind: bb7];
6466
}
6567

66-
bb9: {
68+
bb10: {
6769
_5 = &mut (((*_3) as A).0: AsyncInt);
68-
_6 = Pin::<&mut AsyncInt>::new_unchecked(move _5) -> [return: bb8, unwind: bb2];
70+
_6 = Pin::<&mut AsyncInt>::new_unchecked(move _5) -> [return: bb9, unwind: bb2];
6971
}
7072

71-
bb10: {
73+
bb11: {
7274
StorageDead(_8);
7375
goto -> bb3;
7476
}
7577

76-
bb11: {
77-
async drop((((*_3) as A).0: AsyncInt); poll=_8) -> [return: bb10, unwind: bb2, drop: bb1];
78+
bb12: {
79+
StorageDead(_8);
80+
goto -> bb1;
7881
}
7982

80-
bb12: {
83+
bb13 (cleanup): {
84+
StorageDead(_8);
85+
goto -> bb2;
86+
}
87+
88+
bb14: {
89+
async drop((((*_3) as A).0: AsyncInt); poll=_8) -> [return: bb11, unwind: bb13, drop: bb12];
90+
}
91+
92+
bb15: {
8193
_11 = copy (_10.0: &mut AsyncInt);
8294
StorageLive(_8);
83-
_8 = async_drop_in_place::<AsyncInt>(move _11) -> [return: bb11, unwind: bb2];
95+
_8 = async_drop_in_place::<AsyncInt>(move _11) -> [return: bb14, unwind: bb13];
8496
}
8597

86-
bb13: {
98+
bb16: {
8799
_9 = &mut (((*_3) as A).0: AsyncInt);
88-
_10 = Pin::<&mut AsyncInt>::new_unchecked(move _9) -> [return: bb12, unwind: bb2];
100+
_10 = Pin::<&mut AsyncInt>::new_unchecked(move _9) -> [return: bb15, unwind: bb2];
89101
}
90102

91-
bb14 (cleanup): {
103+
bb17 (cleanup): {
92104
drop((((*_3) as B).0: SyncInt)) -> [return: bb2, unwind terminate(cleanup)];
93105
}
94106

95-
bb15 (cleanup): {
107+
bb18 (cleanup): {
96108
drop((((*_3) as B).0: SyncInt)) -> [return: bb2, unwind terminate(cleanup)];
97109
}
98110

99-
bb16: {
111+
bb19: {
100112
drop((((*_3) as B).0: SyncInt)) -> [return: bb1, unwind: bb2];
101113
}
102114

103-
bb17: {
115+
bb20: {
104116
drop((((*_3) as B).0: SyncInt)) -> [return: bb3, unwind: bb2];
105117
}
106118

107-
bb18: {
119+
bb21: {
108120
_12 = discriminant((*_3));
109-
switchInt(move _12) -> [0: bb13, otherwise: bb17];
121+
switchInt(move _12) -> [0: bb16, otherwise: bb20];
110122
}
111123

112-
bb19 (cleanup): {
113-
StorageDead(_15);
124+
bb22 (cleanup): {
114125
_13 = discriminant((*_3));
115-
switchInt(move _13) -> [0: bb4, otherwise: bb14];
126+
switchInt(move _13) -> [0: bb4, otherwise: bb17];
116127
}
117128

118-
bb20: {
119-
StorageDead(_15);
129+
bb23: {
120130
_14 = discriminant((*_3));
121-
switchInt(move _14) -> [0: bb9, otherwise: bb16];
131+
switchInt(move _14) -> [0: bb10, otherwise: bb19];
122132
}
123133

124-
bb21: {
134+
bb24: {
125135
StorageDead(_15);
126-
goto -> bb18;
136+
goto -> bb21;
127137
}
128138

129-
bb22: {
130-
async drop((*_3); poll=_15) -> [return: bb21, unwind: bb19, drop: bb20];
139+
bb25: {
140+
StorageDead(_15);
141+
goto -> bb23;
131142
}
132143

133-
bb23: {
144+
bb26 (cleanup): {
145+
StorageDead(_15);
146+
goto -> bb22;
147+
}
148+
149+
bb27: {
150+
async drop((*_3); poll=_15) -> [return: bb24, unwind: bb26, drop: bb25];
151+
}
152+
153+
bb28: {
134154
StorageLive(_15);
135-
_15 = <AsyncEnum as AsyncDrop>::drop(move _17) -> [return: bb22, unwind: bb19];
155+
_15 = <AsyncEnum as AsyncDrop>::drop(move _17) -> [return: bb27, unwind: bb26];
136156
}
137157

138-
bb24: {
158+
bb29: {
139159
_16 = &mut (*_3);
140-
_17 = Pin::<&mut AsyncEnum>::new_unchecked(move _16) -> [return: bb23, unwind: bb19];
160+
_17 = Pin::<&mut AsyncEnum>::new_unchecked(move _16) -> [return: bb28, unwind: bb22];
141161
}
142162
}

0 commit comments

Comments
 (0)