Skip to content

Commit 85a7a11

Browse files
bjorn3folkertdev
authored andcommitted
Handle drop in unwind paths for #[const_continue]
1 parent 0a48c4c commit 85a7a11

File tree

2 files changed

+50
-38
lines changed

2 files changed

+50
-38
lines changed

compiler/rustc_mir_build/src/builder/scope.rs

+1-38
Original file line numberDiff line numberDiff line change
@@ -919,44 +919,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
919919
// (See `<ExitScopes as DropTreeBuilder>::link_entry_point`.)
920920
self.cfg.terminate(drop_and_continue_block, source_info, TerminatorKind::UnwindResume);
921921

922-
{
923-
let this = &mut *self;
924-
let blocks = drops.build_mir::<ExitScopes>(&mut this.cfg, Some(real_target));
925-
//let is_coroutine = this.coroutine.is_some();
926-
927-
/*// Link the exit drop tree to unwind drop tree.
928-
if drops.drops.iter().any(|drop_node| drop_node.data.kind == DropKind::Value) {
929-
let unwind_target = this.diverge_cleanup_target(region_scope, span);
930-
let mut unwind_indices = IndexVec::from_elem_n(unwind_target, 1);
931-
for (drop_idx, drop_node) in drops.drops.iter_enumerated().skip(1) {
932-
match drop_node.data.kind {
933-
DropKind::Storage | DropKind::ForLint => {
934-
if is_coroutine {
935-
let unwind_drop = this.scopes.unwind_drops.add_drop(
936-
drop_node.data,
937-
unwind_indices[drop_node.next],
938-
);
939-
unwind_indices.push(unwind_drop);
940-
} else {
941-
unwind_indices.push(unwind_indices[drop_node.next]);
942-
}
943-
}
944-
DropKind::Value => {
945-
let unwind_drop = this
946-
.scopes
947-
.unwind_drops
948-
.add_drop(drop_node.data, unwind_indices[drop_node.next]);
949-
this.scopes.unwind_drops.add_entry_point(
950-
blocks[drop_idx].unwrap(),
951-
unwind_indices[drop_node.next],
952-
);
953-
unwind_indices.push(unwind_drop);
954-
}
955-
}
956-
}
957-
}*/
958-
blocks[ROOT_NODE].map(BasicBlock::unit)
959-
};
922+
self.build_exit_tree(drops, region_scope, span, Some(real_target));
960923

961924
return self.cfg.start_new_block().unit();
962925
}

tests/ui/loop-match/unwind.rs

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Test that #[const_continue] correctly emits cleanup paths for drops.
2+
3+
//@ run-pass
4+
//@ needs-unwind
5+
6+
#![allow(incomplete_features)]
7+
#![feature(loop_match)]
8+
9+
enum State {
10+
A,
11+
B,
12+
}
13+
14+
struct ExitOnDrop;
15+
16+
impl Drop for ExitOnDrop {
17+
fn drop(&mut self) {
18+
std::process::exit(0);
19+
}
20+
}
21+
22+
struct DropBomb;
23+
24+
impl Drop for DropBomb {
25+
fn drop(&mut self) {
26+
panic!("this must unwind");
27+
}
28+
}
29+
30+
fn main() {
31+
let mut state = State::A;
32+
#[loop_match]
33+
'a: loop {
34+
state = 'blk: {
35+
match state {
36+
State::A => {
37+
let _exit = ExitOnDrop;
38+
let _bomb = DropBomb;
39+
40+
#[const_continue]
41+
break 'blk State::B;
42+
}
43+
State::B => break 'a,
44+
}
45+
};
46+
}
47+
48+
unreachable!();
49+
}

0 commit comments

Comments
 (0)