Skip to content

Commit f038882

Browse files
committed
Give collect_drop_flags and elaborate_drops closer structure.
1 parent 252c647 commit f038882

File tree

1 file changed

+64
-72
lines changed

1 file changed

+64
-72
lines changed

compiler/rustc_mir_transform/src/elaborate_drops.rs

Lines changed: 64 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -287,26 +287,36 @@ impl<'b, 'tcx> ElaborateDropsCtxt<'b, 'tcx> {
287287
fn collect_drop_flags(&mut self) {
288288
for (bb, data) in self.body.basic_blocks.iter_enumerated() {
289289
let terminator = data.terminator();
290-
let place = match terminator.kind {
291-
TerminatorKind::Drop { ref place, .. } => place,
292-
_ => continue,
293-
};
294-
295-
self.init_data.seek_before(self.body.terminator_loc(bb));
290+
let TerminatorKind::Drop { ref place, .. } = terminator.kind else { continue };
296291

297292
let path = self.move_data().rev_lookup.find(place.as_ref());
298293
debug!("collect_drop_flags: {:?}, place {:?} ({:?})", bb, place, path);
299294

300-
let path = match path {
301-
LookupResult::Exact(e) => e,
302-
LookupResult::Parent(None) => continue,
295+
match path {
296+
LookupResult::Exact(path) => {
297+
self.init_data.seek_before(self.body.terminator_loc(bb));
298+
on_all_drop_children_bits(self.tcx, self.body, self.env, path, |child| {
299+
let (maybe_live, maybe_dead) = self.init_data.maybe_live_dead(child);
300+
debug!(
301+
"collect_drop_flags: collecting {:?} from {:?}@{:?} - {:?}",
302+
child,
303+
place,
304+
path,
305+
(maybe_live, maybe_dead)
306+
);
307+
if maybe_live && maybe_dead {
308+
self.create_drop_flag(child, terminator.source_info.span)
309+
}
310+
});
311+
}
312+
LookupResult::Parent(None) => {}
303313
LookupResult::Parent(Some(parent)) => {
304-
let (_maybe_live, maybe_dead) = self.init_data.maybe_live_dead(parent);
305-
306314
if self.body.local_decls[place.local].is_deref_temp() {
307315
continue;
308316
}
309317

318+
self.init_data.seek_before(self.body.terminator_loc(bb));
319+
let (_maybe_live, maybe_dead) = self.init_data.maybe_live_dead(parent);
310320
if maybe_dead {
311321
self.tcx.sess.delay_span_bug(
312322
terminator.source_info.span,
@@ -315,80 +325,62 @@ impl<'b, 'tcx> ElaborateDropsCtxt<'b, 'tcx> {
315325
),
316326
);
317327
}
318-
continue;
319328
}
320329
};
321-
322-
on_all_drop_children_bits(self.tcx, self.body, self.env, path, |child| {
323-
let (maybe_live, maybe_dead) = self.init_data.maybe_live_dead(child);
324-
debug!(
325-
"collect_drop_flags: collecting {:?} from {:?}@{:?} - {:?}",
326-
child,
327-
place,
328-
path,
329-
(maybe_live, maybe_dead)
330-
);
331-
if maybe_live && maybe_dead {
332-
self.create_drop_flag(child, terminator.source_info.span)
333-
}
334-
});
335330
}
336331
}
337332

338333
fn elaborate_drops(&mut self) {
334+
// This function should mirror what `collect_drop_flags` does.
339335
for (bb, data) in self.body.basic_blocks.iter_enumerated() {
340-
let loc = Location { block: bb, statement_index: data.statements.len() };
341336
let terminator = data.terminator();
337+
let TerminatorKind::Drop { place, target, unwind, replace } = terminator.kind else {
338+
continue;
339+
};
342340

343-
match terminator.kind {
344-
TerminatorKind::Drop { place, target, unwind, replace } => {
345-
self.init_data.seek_before(loc);
346-
match self.move_data().rev_lookup.find(place.as_ref()) {
347-
LookupResult::Exact(path) => {
348-
let unwind = if data.is_cleanup {
349-
Unwind::InCleanup
350-
} else {
351-
match unwind {
352-
UnwindAction::Cleanup(cleanup) => Unwind::To(cleanup),
353-
UnwindAction::Continue => Unwind::To(self.patch.resume_block()),
354-
UnwindAction::Unreachable => {
355-
Unwind::To(self.patch.unreachable_cleanup_block())
356-
}
357-
UnwindAction::Terminate(reason) => {
358-
debug_assert_ne!(
359-
reason,
360-
UnwindTerminateReason::InCleanup,
361-
"we are not in a cleanup block, InCleanup reason should be impossible"
362-
);
363-
Unwind::To(self.patch.terminate_block(reason))
364-
}
365-
}
366-
};
367-
elaborate_drop(
368-
&mut Elaborator { ctxt: self },
369-
terminator.source_info,
370-
place,
371-
path,
372-
target,
373-
unwind,
374-
bb,
375-
)
341+
let path = self.move_data().rev_lookup.find(place.as_ref());
342+
match path {
343+
LookupResult::Exact(path) => {
344+
let unwind = match unwind {
345+
_ if data.is_cleanup => Unwind::InCleanup,
346+
UnwindAction::Cleanup(cleanup) => Unwind::To(cleanup),
347+
UnwindAction::Continue => Unwind::To(self.patch.resume_block()),
348+
UnwindAction::Unreachable => {
349+
Unwind::To(self.patch.unreachable_cleanup_block())
376350
}
377-
LookupResult::Parent(..) => {
378-
if !replace {
379-
self.tcx.sess.delay_span_bug(
380-
terminator.source_info.span,
381-
format!("drop of untracked value {bb:?}"),
382-
);
383-
}
384-
// A drop and replace behind a pointer/array/whatever.
385-
// The borrow checker requires that these locations are initialized before the assignment,
386-
// so we just leave an unconditional drop.
387-
assert!(!data.is_cleanup);
351+
UnwindAction::Terminate(reason) => {
352+
debug_assert_ne!(
353+
reason,
354+
UnwindTerminateReason::InCleanup,
355+
"we are not in a cleanup block, InCleanup reason should be impossible"
356+
);
357+
Unwind::To(self.patch.terminate_block(reason))
388358
}
359+
};
360+
self.init_data.seek_before(self.body.terminator_loc(bb));
361+
elaborate_drop(
362+
&mut Elaborator { ctxt: self },
363+
terminator.source_info,
364+
place,
365+
path,
366+
target,
367+
unwind,
368+
bb,
369+
)
370+
}
371+
LookupResult::Parent(None) => {}
372+
LookupResult::Parent(Some(_)) => {
373+
if !replace {
374+
self.tcx.sess.delay_span_bug(
375+
terminator.source_info.span,
376+
format!("drop of untracked value {bb:?}"),
377+
);
389378
}
379+
// A drop and replace behind a pointer/array/whatever.
380+
// The borrow checker requires that these locations are initialized before the assignment,
381+
// so we just leave an unconditional drop.
382+
assert!(!data.is_cleanup);
390383
}
391-
_ => continue,
392384
}
393385
}
394386
}

0 commit comments

Comments
 (0)