Skip to content

Commit 5afe7c2

Browse files
committed
Fix variable deallocation order in panic unwinding paths
1 parent cc3eee7 commit 5afe7c2

File tree

1 file changed

+8
-23
lines changed
  • compiler/rustc_mir_build/src/builder

1 file changed

+8
-23
lines changed

compiler/rustc_mir_build/src/builder/scope.rs

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -256,21 +256,10 @@ struct DropNodeKey {
256256

257257
impl Scope {
258258
/// Whether there's anything to do for the cleanup path, that is,
259-
/// when unwinding through this scope. This includes destructors,
260-
/// but not StorageDead statements, which don't get emitted at all
261-
/// for unwinding, for several reasons:
262-
/// * clang doesn't emit llvm.lifetime.end for C++ unwinding
263-
/// * LLVM's memory dependency analysis can't handle it atm
264-
/// * polluting the cleanup MIR with StorageDead creates
265-
/// landing pads even though there's no actual destructors
266-
/// * freeing up stack space has no effect during unwinding
267-
/// Note that for coroutines we do emit StorageDeads, for the
268-
/// use of optimizations in the MIR coroutine transform.
259+
/// when unwinding through this scope. This includes destructors
260+
/// and StorageDead statements to maintain proper drop ordering.
269261
fn needs_cleanup(&self) -> bool {
270-
self.drops.iter().any(|drop| match drop.kind {
271-
DropKind::Value | DropKind::ForLint => true,
272-
DropKind::Storage => false,
273-
})
262+
!self.drops.is_empty()
274263
}
275264

276265
fn invalidate_cache(&mut self) {
@@ -1986,15 +1975,11 @@ impl<'a, 'tcx: 'a> Builder<'a, 'tcx> {
19861975
for (drop_idx, drop_node) in drops.drop_nodes.iter_enumerated().skip(1) {
19871976
match drop_node.data.kind {
19881977
DropKind::Storage | DropKind::ForLint => {
1989-
if is_coroutine {
1990-
let unwind_drop = self
1991-
.scopes
1992-
.unwind_drops
1993-
.add_drop(drop_node.data, unwind_indices[drop_node.next]);
1994-
unwind_indices.push(unwind_drop);
1995-
} else {
1996-
unwind_indices.push(unwind_indices[drop_node.next]);
1997-
}
1978+
let unwind_drop = self
1979+
.scopes
1980+
.unwind_drops
1981+
.add_drop(drop_node.data, unwind_indices[drop_node.next]);
1982+
unwind_indices.push(unwind_drop);
19981983
}
19991984
DropKind::Value => {
20001985
let unwind_drop = self

0 commit comments

Comments
 (0)