Skip to content

Commit fa3e2fc

Browse files
matthewjasperAaron1011
authored andcommitted
Defer creating drop trees in MIR lowering until leaving that scope
1 parent 0d37dca commit fa3e2fc

File tree

8 files changed

+768
-730
lines changed

8 files changed

+768
-730
lines changed

compiler/rustc_mir/src/util/graphviz.rs

+12-1
Original file line numberDiff line numberDiff line change
@@ -111,13 +111,24 @@ where
111111
write!(w, r#"<table border="0" cellborder="1" cellspacing="0">"#)?;
112112

113113
// Basic block number at the top.
114+
let (blk, bgcolor) = if data.is_cleanup {
115+
(format!("{} (cleanup)", block.index()), "lightblue")
116+
} else {
117+
let color = if dark_mode {
118+
"dimgray"
119+
} else {
120+
"gray"
121+
};
122+
(format!("{}", block.index()), "gray")
123+
};
114124
write!(
115125
w,
116126
r#"<tr><td bgcolor="{bgcolor}" {attrs} colspan="{colspan}">{blk}</td></tr>"#,
117127
bgcolor = if dark_mode { "dimgray" } else { "gray" },
118128
attrs = r#"align="center""#,
119129
colspan = num_cols,
120-
blk = block.index()
130+
blk = blk,
131+
color = color
121132
)?;
122133

123134
init(w)?;

compiler/rustc_mir_build/src/build/block.rs

+10-8
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,16 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
2828
self.in_opt_scope(opt_destruction_scope.map(|de| (de, source_info)), move |this| {
2929
this.in_scope((region_scope, source_info), LintLevel::Inherited, move |this| {
3030
if targeted_by_break {
31-
// This is a `break`-able block
32-
let exit_block = this.cfg.start_new_block();
33-
let block_exit =
34-
this.in_breakable_scope(None, exit_block, destination, |this| {
35-
this.ast_block_stmts(destination, block, span, stmts, expr, safety_mode)
36-
});
37-
this.cfg.goto(unpack!(block_exit), source_info, exit_block);
38-
exit_block.unit()
31+
this.in_breakable_scope(None, destination, span, |this| {
32+
Some(this.ast_block_stmts(
33+
destination,
34+
block,
35+
span,
36+
stmts,
37+
expr,
38+
safety_mode,
39+
))
40+
})
3941
} else {
4042
this.ast_block_stmts(destination, block, span, stmts, expr, safety_mode)
4143
}

compiler/rustc_mir_build/src/build/expr/into.rs

+11-13
Original file line numberDiff line numberDiff line change
@@ -140,32 +140,30 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
140140
// body, even when the exact code in the body cannot unwind
141141

142142
let loop_block = this.cfg.start_new_block();
143-
let exit_block = this.cfg.start_new_block();
144143

145144
// Start the loop.
146145
this.cfg.goto(block, source_info, loop_block);
147146

148-
this.in_breakable_scope(Some(loop_block), exit_block, destination, move |this| {
147+
this.in_breakable_scope(Some(loop_block), destination, expr_span, move |this| {
149148
// conduct the test, if necessary
150149
let body_block = this.cfg.start_new_block();
151-
let diverge_cleanup = this.diverge_cleanup();
152150
this.cfg.terminate(
153151
loop_block,
154152
source_info,
155-
TerminatorKind::FalseUnwind {
156-
real_target: body_block,
157-
unwind: Some(diverge_cleanup),
158-
},
153+
TerminatorKind::FalseUnwind { real_target: body_block, unwind: None },
159154
);
155+
this.diverge_from(loop_block);
160156

161157
// The “return” value of the loop body must always be an unit. We therefore
162158
// introduce a unit temporary as the destination for the loop body.
163159
let tmp = this.get_unit_temp();
164160
// Execute the body, branching back to the test.
165161
let body_block_end = unpack!(this.into(tmp, body_block, body));
166162
this.cfg.goto(body_block_end, source_info, loop_block);
167-
});
168-
exit_block.unit()
163+
164+
// Loops are only exited by `break` expressions.
165+
None
166+
})
169167
}
170168
ExprKind::Call { ty, fun, args, from_hir_call, fn_span } => {
171169
let intrinsic = match *ty.kind() {
@@ -206,7 +204,6 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
206204
.collect();
207205

208206
let success = this.cfg.start_new_block();
209-
let cleanup = this.diverge_cleanup();
210207

211208
this.record_operands_moved(&args);
212209

@@ -218,7 +215,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
218215
TerminatorKind::Call {
219216
func: fun,
220217
args,
221-
cleanup: Some(cleanup),
218+
cleanup: None,
222219
// FIXME(varkor): replace this with an uninhabitedness-based check.
223220
// This requires getting access to the current module to call
224221
// `tcx.is_ty_uninhabited_from`, which is currently tricky to do.
@@ -231,6 +228,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
231228
fn_span,
232229
},
233230
);
231+
this.diverge_from(block);
234232
success.unit()
235233
}
236234
}
@@ -437,12 +435,12 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
437435
let scope = this.local_scope();
438436
let value = unpack!(block = this.as_operand(block, scope, value));
439437
let resume = this.cfg.start_new_block();
440-
let cleanup = this.generator_drop_cleanup();
441438
this.cfg.terminate(
442439
block,
443440
source_info,
444-
TerminatorKind::Yield { value, resume, resume_arg: destination, drop: cleanup },
441+
TerminatorKind::Yield { value, resume, resume_arg: destination, drop: None },
445442
);
443+
this.generator_drop_cleanup(block);
446444
resume.unit()
447445
}
448446

compiler/rustc_mir_build/src/build/matches/mod.rs

+5-12
Original file line numberDiff line numberDiff line change
@@ -228,8 +228,6 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
228228
outer_source_info: SourceInfo,
229229
fake_borrow_temps: Vec<(Place<'tcx>, Local)>,
230230
) -> BlockAnd<()> {
231-
let match_scope = self.scopes.topmost();
232-
233231
let arm_end_blocks: Vec<_> = arm_candidates
234232
.into_iter()
235233
.map(|(arm, candidate)| {
@@ -250,7 +248,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
250248
let arm_block = this.bind_pattern(
251249
outer_source_info,
252250
candidate,
253-
arm.guard.as_ref().map(|g| (g, match_scope)),
251+
arm.guard.as_ref(),
254252
&fake_borrow_temps,
255253
scrutinee_span,
256254
Some(arm.scope),
@@ -287,7 +285,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
287285
&mut self,
288286
outer_source_info: SourceInfo,
289287
candidate: Candidate<'_, 'tcx>,
290-
guard: Option<(&Guard<'tcx>, region::Scope)>,
288+
guard: Option<&Guard<'tcx>>,
291289
fake_borrow_temps: &Vec<(Place<'tcx>, Local)>,
292290
scrutinee_span: Span,
293291
arm_scope: Option<region::Scope>,
@@ -1592,7 +1590,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
15921590
&mut self,
15931591
candidate: Candidate<'pat, 'tcx>,
15941592
parent_bindings: &[(Vec<Binding<'tcx>>, Vec<Ascription<'tcx>>)],
1595-
guard: Option<(&Guard<'tcx>, region::Scope)>,
1593+
guard: Option<&Guard<'tcx>>,
15961594
fake_borrows: &Vec<(Place<'tcx>, Local)>,
15971595
scrutinee_span: Span,
15981596
schedule_drops: bool,
@@ -1704,7 +1702,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
17041702
// the reference that we create for the arm.
17051703
// * So we eagerly create the reference for the arm and then take a
17061704
// reference to that.
1707-
if let Some((guard, region_scope)) = guard {
1705+
if let Some(guard) = guard {
17081706
let tcx = self.hir.tcx();
17091707
let bindings = parent_bindings
17101708
.iter()
@@ -1748,12 +1746,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
17481746
unreachable
17491747
});
17501748
let outside_scope = self.cfg.start_new_block();
1751-
self.exit_scope(
1752-
source_info.span,
1753-
region_scope,
1754-
otherwise_post_guard_block,
1755-
outside_scope,
1756-
);
1749+
self.exit_top_scope(otherwise_post_guard_block, outside_scope, source_info);
17571750
self.false_edges(
17581751
outside_scope,
17591752
otherwise_block,

compiler/rustc_mir_build/src/build/matches/test.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,6 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
418418
let bool_ty = self.hir.bool_ty();
419419
let eq_result = self.temp(bool_ty, source_info.span);
420420
let eq_block = self.cfg.start_new_block();
421-
let cleanup = self.diverge_cleanup();
422421
self.cfg.terminate(
423422
block,
424423
source_info,
@@ -435,12 +434,13 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
435434
literal: method,
436435
}),
437436
args: vec![val, expect],
438-
destination: Some((eq_result, eq_block)),
439-
cleanup: Some(cleanup),
437+
destination: Some((eq_result.clone(), eq_block)),
438+
cleanup: None,
440439
from_hir_call: false,
441440
fn_span: source_info.span,
442441
},
443442
);
443+
self.diverge_from(block);
444444

445445
if let [success_block, fail_block] = *make_target_blocks(self) {
446446
// check the result

compiler/rustc_mir_build/src/build/mod.rs

+28-63
Original file line numberDiff line numberDiff line change
@@ -345,11 +345,6 @@ struct Builder<'a, 'tcx> {
345345

346346
var_debug_info: Vec<VarDebugInfo<'tcx>>,
347347

348-
/// Cached block with the `RESUME` terminator; this is created
349-
/// when first set of cleanups are built.
350-
cached_resume_block: Option<BasicBlock>,
351-
/// Cached block with the `RETURN` terminator.
352-
cached_return_block: Option<BasicBlock>,
353348
/// Cached block with the `UNREACHABLE` terminator.
354349
cached_unreachable_block: Option<BasicBlock>,
355350
}
@@ -609,50 +604,34 @@ where
609604
region::Scope { id: body.value.hir_id.local_id, data: region::ScopeData::CallSite };
610605
let arg_scope =
611606
region::Scope { id: body.value.hir_id.local_id, data: region::ScopeData::Arguments };
612-
let mut block = START_BLOCK;
613607
let source_info = builder.source_info(span);
614608
let call_site_s = (call_site_scope, source_info);
615-
unpack!(
616-
block = builder.in_scope(call_site_s, LintLevel::Inherited, |builder| {
617-
if should_abort_on_panic(tcx, fn_def_id, abi) {
618-
builder.schedule_abort();
619-
}
620-
621-
let arg_scope_s = (arg_scope, source_info);
622-
// `return_block` is called when we evaluate a `return` expression, so
623-
// we just use `START_BLOCK` here.
624-
unpack!(
625-
block = builder.in_breakable_scope(
626-
None,
627-
START_BLOCK,
628-
Place::return_place(),
629-
|builder| {
630-
builder.in_scope(arg_scope_s, LintLevel::Inherited, |builder| {
631-
builder.args_and_body(
632-
block,
633-
fn_def_id.to_def_id(),
634-
&arguments,
635-
arg_scope,
636-
&body.value,
637-
)
638-
})
639-
},
640-
)
641-
);
642-
// Attribute epilogue to function's closing brace
643-
let fn_end = span_with_body.shrink_to_hi();
644-
let source_info = builder.source_info(fn_end);
645-
let return_block = builder.return_block();
646-
builder.cfg.goto(block, source_info, return_block);
647-
builder.cfg.terminate(return_block, source_info, TerminatorKind::Return);
648-
// Attribute any unreachable codepaths to the function's closing brace
649-
if let Some(unreachable_block) = builder.cached_unreachable_block {
650-
builder.cfg.terminate(unreachable_block, source_info, TerminatorKind::Unreachable);
651-
}
652-
return_block.unit()
653-
})
654-
);
655-
assert_eq!(block, builder.return_block());
609+
unpack!(builder.in_scope(call_site_s, LintLevel::Inherited, |builder| {
610+
let arg_scope_s = (arg_scope, source_info);
611+
// Attribute epilogue to function's closing brace
612+
let fn_end = span_with_body.shrink_to_hi();
613+
let return_block =
614+
unpack!(builder.in_breakable_scope(None, Place::return_place(), fn_end, |builder| {
615+
Some(builder.in_scope(arg_scope_s, LintLevel::Inherited, |builder| {
616+
builder.args_and_body(
617+
START_BLOCK,
618+
fn_def_id.to_def_id(),
619+
&arguments,
620+
arg_scope,
621+
&body.value,
622+
)
623+
}))
624+
}));
625+
let source_info = builder.source_info(fn_end);
626+
builder.cfg.terminate(return_block, source_info, TerminatorKind::Return);
627+
let should_abort = should_abort_on_panic(tcx, fn_def_id, abi);
628+
builder.build_drop_trees(should_abort);
629+
// Attribute any unreachable codepaths to the function's closing brace
630+
if let Some(unreachable_block) = builder.cached_unreachable_block {
631+
builder.cfg.terminate(unreachable_block, source_info, TerminatorKind::Unreachable);
632+
}
633+
return_block.unit()
634+
}));
656635

657636
let spread_arg = if abi == Abi::RustCall {
658637
// RustCall pseudo-ABI untuples the last argument.
@@ -686,8 +665,7 @@ fn construct_const<'a, 'tcx>(
686665
let source_info = builder.source_info(span);
687666
builder.cfg.terminate(block, source_info, TerminatorKind::Return);
688667

689-
// Constants can't `return` so a return block should not be created.
690-
assert_eq!(builder.cached_return_block, None);
668+
builder.build_drop_trees(false);
691669

692670
// Constants may be match expressions in which case an unreachable block may
693671
// be created, so terminate it properly.
@@ -754,7 +732,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
754732
fn_span: span,
755733
arg_count,
756734
generator_kind,
757-
scopes: Default::default(),
735+
scopes: scope::Scopes::new(),
758736
block_context: BlockContext::new(),
759737
source_scopes: IndexVec::new(),
760738
source_scope: OUTERMOST_SOURCE_SCOPE,
@@ -767,8 +745,6 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
767745
var_indices: Default::default(),
768746
unit_temp: None,
769747
var_debug_info: vec![],
770-
cached_resume_block: None,
771-
cached_return_block: None,
772748
cached_unreachable_block: None,
773749
};
774750

@@ -1003,17 +979,6 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
1003979
}
1004980
}
1005981
}
1006-
1007-
fn return_block(&mut self) -> BasicBlock {
1008-
match self.cached_return_block {
1009-
Some(rb) => rb,
1010-
None => {
1011-
let rb = self.cfg.start_new_block();
1012-
self.cached_return_block = Some(rb);
1013-
rb
1014-
}
1015-
}
1016-
}
1017982
}
1018983

1019984
///////////////////////////////////////////////////////////////////////////

0 commit comments

Comments
 (0)