Skip to content

Commit da14081

Browse files
committed
Auto merge of #102906 - nbdd0121:mir, r=wesleywiser,tmiasko
Refactor unwind in MIR This makes unwinding from current `Option<BasicBlock>` into ```rust enum UnwindAction { Continue, Cleanup(BasicBlock), Unreachable, Terminate, } ``` cc `@JakobDegen` `@RalfJung` `@Amanieu`
2 parents b6f6104 + ea69dad commit da14081

File tree

294 files changed

+1218
-926
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

294 files changed

+1218
-926
lines changed

compiler/rustc_borrowck/src/diagnostics/find_use.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use crate::{
1111
};
1212
use rustc_data_structures::fx::FxIndexSet;
1313
use rustc_middle::mir::visit::{MirVisitable, PlaceContext, Visitor};
14-
use rustc_middle::mir::{Body, Local, Location};
14+
use rustc_middle::mir::{self, Body, Local, Location};
1515
use rustc_middle::ty::{RegionVid, TyCtxt};
1616

1717
pub(crate) fn find<'tcx>(
@@ -70,7 +70,10 @@ impl<'cx, 'tcx> UseFinder<'cx, 'tcx> {
7070
block_data
7171
.terminator()
7272
.successors()
73-
.filter(|&bb| Some(&Some(bb)) != block_data.terminator().unwind())
73+
.filter(|&bb| {
74+
Some(&mir::UnwindAction::Cleanup(bb))
75+
!= block_data.terminator().unwind()
76+
})
7477
.map(|bb| Location { statement_index: 0, block: bb }),
7578
);
7679
}

compiler/rustc_borrowck/src/invalidation.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ impl<'cx, 'tcx> Visitor<'tcx> for InvalidationGenerator<'cx, 'tcx> {
125125
args,
126126
destination,
127127
target: _,
128-
cleanup: _,
128+
unwind: _,
129129
from_hir_call: _,
130130
fn_span: _,
131131
} => {
@@ -135,7 +135,7 @@ impl<'cx, 'tcx> Visitor<'tcx> for InvalidationGenerator<'cx, 'tcx> {
135135
}
136136
self.mutate_place(location, *destination, Deep);
137137
}
138-
TerminatorKind::Assert { cond, expected: _, msg, target: _, cleanup: _ } => {
138+
TerminatorKind::Assert { cond, expected: _, msg, target: _, unwind: _ } => {
139139
self.consume_operand(location, cond);
140140
use rustc_middle::mir::AssertKind;
141141
if let AssertKind::BoundsCheck { len, index } = msg {
@@ -173,7 +173,7 @@ impl<'cx, 'tcx> Visitor<'tcx> for InvalidationGenerator<'cx, 'tcx> {
173173
options: _,
174174
line_spans: _,
175175
destination: _,
176-
cleanup: _,
176+
unwind: _,
177177
} => {
178178
for op in operands {
179179
match op {
@@ -198,7 +198,7 @@ impl<'cx, 'tcx> Visitor<'tcx> for InvalidationGenerator<'cx, 'tcx> {
198198
}
199199
}
200200
TerminatorKind::Goto { target: _ }
201-
| TerminatorKind::Abort
201+
| TerminatorKind::Terminate
202202
| TerminatorKind::Unreachable
203203
| TerminatorKind::FalseEdge { real_target: _, imaginary_target: _ }
204204
| TerminatorKind::FalseUnwind { real_target: _, unwind: _ } => {

compiler/rustc_borrowck/src/lib.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -740,7 +740,7 @@ impl<'cx, 'tcx> rustc_mir_dataflow::ResultsVisitor<'cx, 'tcx> for MirBorrowckCtx
740740
args,
741741
destination,
742742
target: _,
743-
cleanup: _,
743+
unwind: _,
744744
from_hir_call: _,
745745
fn_span: _,
746746
} => {
@@ -750,7 +750,7 @@ impl<'cx, 'tcx> rustc_mir_dataflow::ResultsVisitor<'cx, 'tcx> for MirBorrowckCtx
750750
}
751751
self.mutate_place(loc, (*destination, span), Deep, flow_state);
752752
}
753-
TerminatorKind::Assert { cond, expected: _, msg, target: _, cleanup: _ } => {
753+
TerminatorKind::Assert { cond, expected: _, msg, target: _, unwind: _ } => {
754754
self.consume_operand(loc, (cond, span), flow_state);
755755
use rustc_middle::mir::AssertKind;
756756
if let AssertKind::BoundsCheck { len, index } = msg {
@@ -770,7 +770,7 @@ impl<'cx, 'tcx> rustc_mir_dataflow::ResultsVisitor<'cx, 'tcx> for MirBorrowckCtx
770770
options: _,
771771
line_spans: _,
772772
destination: _,
773-
cleanup: _,
773+
unwind: _,
774774
} => {
775775
for op in operands {
776776
match op {
@@ -801,7 +801,7 @@ impl<'cx, 'tcx> rustc_mir_dataflow::ResultsVisitor<'cx, 'tcx> for MirBorrowckCtx
801801
}
802802

803803
TerminatorKind::Goto { target: _ }
804-
| TerminatorKind::Abort
804+
| TerminatorKind::Terminate
805805
| TerminatorKind::Unreachable
806806
| TerminatorKind::Resume
807807
| TerminatorKind::Return
@@ -845,7 +845,7 @@ impl<'cx, 'tcx> rustc_mir_dataflow::ResultsVisitor<'cx, 'tcx> for MirBorrowckCtx
845845
}
846846
}
847847

848-
TerminatorKind::Abort
848+
TerminatorKind::Terminate
849849
| TerminatorKind::Assert { .. }
850850
| TerminatorKind::Call { .. }
851851
| TerminatorKind::Drop { .. }

compiler/rustc_borrowck/src/type_check/mod.rs

+32-29
Original file line numberDiff line numberDiff line change
@@ -1300,7 +1300,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
13001300
match &term.kind {
13011301
TerminatorKind::Goto { .. }
13021302
| TerminatorKind::Resume
1303-
| TerminatorKind::Abort
1303+
| TerminatorKind::Terminate
13041304
| TerminatorKind::Return
13051305
| TerminatorKind::GeneratorDrop
13061306
| TerminatorKind::Unreachable
@@ -1584,7 +1584,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
15841584
span_mirbug!(self, block_data, "resume on non-cleanup block!")
15851585
}
15861586
}
1587-
TerminatorKind::Abort => {
1587+
TerminatorKind::Terminate => {
15881588
if !is_cleanup {
15891589
span_mirbug!(self, block_data, "abort on non-cleanup block!")
15901590
}
@@ -1610,49 +1610,29 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
16101610
}
16111611
TerminatorKind::Unreachable => {}
16121612
TerminatorKind::Drop { target, unwind, .. }
1613-
| TerminatorKind::Assert { target, cleanup: unwind, .. } => {
1613+
| TerminatorKind::Assert { target, unwind, .. } => {
16141614
self.assert_iscleanup(body, block_data, target, is_cleanup);
1615-
if let Some(unwind) = unwind {
1616-
if is_cleanup {
1617-
span_mirbug!(self, block_data, "unwind on cleanup block")
1618-
}
1619-
self.assert_iscleanup(body, block_data, unwind, true);
1620-
}
1615+
self.assert_iscleanup_unwind(body, block_data, unwind, is_cleanup);
16211616
}
1622-
TerminatorKind::Call { ref target, cleanup, .. } => {
1617+
TerminatorKind::Call { ref target, unwind, .. } => {
16231618
if let &Some(target) = target {
16241619
self.assert_iscleanup(body, block_data, target, is_cleanup);
16251620
}
1626-
if let Some(cleanup) = cleanup {
1627-
if is_cleanup {
1628-
span_mirbug!(self, block_data, "cleanup on cleanup block")
1629-
}
1630-
self.assert_iscleanup(body, block_data, cleanup, true);
1631-
}
1621+
self.assert_iscleanup_unwind(body, block_data, unwind, is_cleanup);
16321622
}
16331623
TerminatorKind::FalseEdge { real_target, imaginary_target } => {
16341624
self.assert_iscleanup(body, block_data, real_target, is_cleanup);
16351625
self.assert_iscleanup(body, block_data, imaginary_target, is_cleanup);
16361626
}
16371627
TerminatorKind::FalseUnwind { real_target, unwind } => {
16381628
self.assert_iscleanup(body, block_data, real_target, is_cleanup);
1639-
if let Some(unwind) = unwind {
1640-
if is_cleanup {
1641-
span_mirbug!(self, block_data, "cleanup in cleanup block via false unwind");
1642-
}
1643-
self.assert_iscleanup(body, block_data, unwind, true);
1644-
}
1629+
self.assert_iscleanup_unwind(body, block_data, unwind, is_cleanup);
16451630
}
1646-
TerminatorKind::InlineAsm { destination, cleanup, .. } => {
1631+
TerminatorKind::InlineAsm { destination, unwind, .. } => {
16471632
if let Some(target) = destination {
16481633
self.assert_iscleanup(body, block_data, target, is_cleanup);
16491634
}
1650-
if let Some(cleanup) = cleanup {
1651-
if is_cleanup {
1652-
span_mirbug!(self, block_data, "cleanup on cleanup block")
1653-
}
1654-
self.assert_iscleanup(body, block_data, cleanup, true);
1655-
}
1635+
self.assert_iscleanup_unwind(body, block_data, unwind, is_cleanup);
16561636
}
16571637
}
16581638
}
@@ -1669,6 +1649,29 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
16691649
}
16701650
}
16711651

1652+
fn assert_iscleanup_unwind(
1653+
&mut self,
1654+
body: &Body<'tcx>,
1655+
ctxt: &dyn fmt::Debug,
1656+
unwind: UnwindAction,
1657+
is_cleanup: bool,
1658+
) {
1659+
match unwind {
1660+
UnwindAction::Cleanup(unwind) => {
1661+
if is_cleanup {
1662+
span_mirbug!(self, ctxt, "unwind on cleanup block")
1663+
}
1664+
self.assert_iscleanup(body, ctxt, unwind, true);
1665+
}
1666+
UnwindAction::Continue => {
1667+
if is_cleanup {
1668+
span_mirbug!(self, ctxt, "unwind on cleanup block")
1669+
}
1670+
}
1671+
UnwindAction::Unreachable | UnwindAction::Terminate => (),
1672+
}
1673+
}
1674+
16721675
fn check_local(&mut self, body: &Body<'tcx>, local: Local, local_decl: &LocalDecl<'tcx>) {
16731676
match body.local_kind(local) {
16741677
LocalKind::ReturnPointer | LocalKind::Arg => {

compiler/rustc_codegen_cranelift/src/base.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ fn codegen_fn_body(fx: &mut FunctionCx<'_, '_, '_>, start_block: Block) {
345345
TerminatorKind::Return => {
346346
crate::abi::codegen_return(fx);
347347
}
348-
TerminatorKind::Assert { cond, expected, msg, target, cleanup: _ } => {
348+
TerminatorKind::Assert { cond, expected, msg, target, unwind: _ } => {
349349
if !fx.tcx.sess.overflow_checks() && msg.is_optional_overflow_check() {
350350
let target = fx.get_block(*target);
351351
fx.bcx.ins().jump(target, &[]);
@@ -450,7 +450,7 @@ fn codegen_fn_body(fx: &mut FunctionCx<'_, '_, '_>, start_block: Block) {
450450
destination,
451451
target,
452452
fn_span,
453-
cleanup: _,
453+
unwind: _,
454454
from_hir_call: _,
455455
} => {
456456
fx.tcx.prof.generic_activity("codegen call").run(|| {
@@ -470,7 +470,7 @@ fn codegen_fn_body(fx: &mut FunctionCx<'_, '_, '_>, start_block: Block) {
470470
options,
471471
destination,
472472
line_spans: _,
473-
cleanup: _,
473+
unwind: _,
474474
} => {
475475
if options.contains(InlineAsmOptions::MAY_UNWIND) {
476476
fx.tcx.sess.span_fatal(
@@ -488,7 +488,7 @@ fn codegen_fn_body(fx: &mut FunctionCx<'_, '_, '_>, start_block: Block) {
488488
*destination,
489489
);
490490
}
491-
TerminatorKind::Abort => {
491+
TerminatorKind::Terminate => {
492492
codegen_panic_cannot_unwind(fx, source_info);
493493
}
494494
TerminatorKind::Resume => {

compiler/rustc_codegen_cranelift/src/constant.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -549,7 +549,7 @@ pub(crate) fn mir_operand_get_const_val<'tcx>(
549549
TerminatorKind::Goto { .. }
550550
| TerminatorKind::SwitchInt { .. }
551551
| TerminatorKind::Resume
552-
| TerminatorKind::Abort
552+
| TerminatorKind::Terminate
553553
| TerminatorKind::Return
554554
| TerminatorKind::Unreachable
555555
| TerminatorKind::Drop { .. }

compiler/rustc_codegen_ssa/src/mir/analyze.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -284,19 +284,19 @@ pub fn cleanup_kinds(mir: &mir::Body<'_>) -> IndexVec<mir::BasicBlock, CleanupKi
284284
match data.terminator().kind {
285285
TerminatorKind::Goto { .. }
286286
| TerminatorKind::Resume
287-
| TerminatorKind::Abort
287+
| TerminatorKind::Terminate
288288
| TerminatorKind::Return
289289
| TerminatorKind::GeneratorDrop
290290
| TerminatorKind::Unreachable
291291
| TerminatorKind::SwitchInt { .. }
292292
| TerminatorKind::Yield { .. }
293293
| TerminatorKind::FalseEdge { .. }
294294
| TerminatorKind::FalseUnwind { .. } => { /* nothing to do */ }
295-
TerminatorKind::Call { cleanup: unwind, .. }
296-
| TerminatorKind::InlineAsm { cleanup: unwind, .. }
297-
| TerminatorKind::Assert { cleanup: unwind, .. }
295+
TerminatorKind::Call { unwind, .. }
296+
| TerminatorKind::InlineAsm { unwind, .. }
297+
| TerminatorKind::Assert { unwind, .. }
298298
| TerminatorKind::Drop { unwind, .. } => {
299-
if let Some(unwind) = unwind {
299+
if let mir::UnwindAction::Cleanup(unwind) = unwind {
300300
debug!(
301301
"cleanup_kinds: {:?}/{:?} registering {:?} as funclet",
302302
bb, data, unwind

0 commit comments

Comments
 (0)