Skip to content

Commit b6a2ae9

Browse files
committed
Temp: Cleanup 1
1 parent 179fc42 commit b6a2ae9

File tree

1 file changed

+65
-39
lines changed

1 file changed

+65
-39
lines changed

src/librustc_mir/build/scope.rs

Lines changed: 65 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ struct BreakableScope<'tcx> {
160160

161161
/// The target of an expression that breaks out of a scope
162162
#[derive(Clone, Copy, Debug)]
163-
pub enum BreakableTarget {
163+
pub(crate) enum BreakableTarget {
164164
Continue(region::Scope),
165165
Break(region::Scope),
166166
Return,
@@ -287,7 +287,7 @@ impl<'tcx> Scopes<'tcx> {
287287

288288
/// Returns the topmost active scope, which is known to be alive until
289289
/// the next scope expression.
290-
pub(super) fn topmost(&self) -> region::Scope {
290+
fn topmost(&self) -> region::Scope {
291291
self.scopes.last().expect("topmost_scope: no scopes present").region_scope
292292
}
293293

@@ -301,7 +301,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
301301
// ==========================
302302
// Start a breakable scope, which tracks where `continue`, `break` and
303303
// `return` should branch to.
304-
pub fn in_breakable_scope<F>(
304+
crate fn in_breakable_scope<F>(
305305
&mut self,
306306
loop_block: Option<BasicBlock>,
307307
break_destination: Place<'tcx>,
@@ -342,7 +342,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
342342
}
343343
}
344344

345-
pub fn in_opt_scope<F, R>(&mut self,
345+
crate fn in_opt_scope<F, R>(&mut self,
346346
opt_scope: Option<(region::Scope, SourceInfo)>,
347347
f: F)
348348
-> BlockAnd<R>
@@ -361,7 +361,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
361361

362362
/// Convenience wrapper that pushes a scope and then executes `f`
363363
/// to build its contents, popping the scope afterwards.
364-
pub fn in_scope<F, R>(&mut self,
364+
crate fn in_scope<F, R>(&mut self,
365365
region_scope: (region::Scope, SourceInfo),
366366
lint_level: LintLevel,
367367
f: F)
@@ -406,14 +406,14 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
406406
/// scope and call `pop_scope` afterwards. Note that these two
407407
/// calls must be paired; using `in_scope` as a convenience
408408
/// wrapper maybe preferable.
409-
pub fn push_scope(&mut self, region_scope: (region::Scope, SourceInfo)) {
409+
crate fn push_scope(&mut self, region_scope: (region::Scope, SourceInfo)) {
410410
self.scopes.push_scope(region_scope, self.source_scope);
411411
}
412412

413413
/// Pops a scope, which should have region scope `region_scope`,
414414
/// adding any drops onto the end of `block` that are needed.
415415
/// This must match 1-to-1 with `push_scope`.
416-
pub fn pop_scope(&mut self,
416+
crate fn pop_scope(&mut self,
417417
region_scope: (region::Scope, SourceInfo),
418418
mut block: BasicBlock)
419419
-> BlockAnd<()> {
@@ -427,7 +427,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
427427
}
428428

429429
/// Sets up the drops for breaking from `block` to `target`.
430-
pub fn break_scope(
430+
crate fn break_scope(
431431
&mut self,
432432
mut block: BasicBlock,
433433
value: Option<ExprRef<'tcx>>,
@@ -496,7 +496,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
496496
}
497497

498498
// TODO: use in pop_top_scope.
499-
pub fn exit_top_scope(
499+
crate fn exit_top_scope(
500500
&mut self,
501501
mut block: BasicBlock,
502502
target: BasicBlock,
@@ -546,7 +546,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
546546
///
547547
/// This path terminates in GeneratorDrop. Returns the start of the path.
548548
/// None indicates there’s no cleanup to do at this point.
549-
pub fn generator_drop_cleanup(&mut self, yield_block: BasicBlock) {
549+
crate fn generator_drop_cleanup(&mut self, yield_block: BasicBlock) {
550550
let drops = self.scopes.scopes.iter().flat_map(|scope| &scope.drops);
551551
let mut next_drop = ROOT_NODE;
552552
for drop in drops {
@@ -556,7 +556,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
556556
}
557557

558558
/// Creates a new source scope, nested in the current one.
559-
pub fn new_source_scope(&mut self,
559+
crate fn new_source_scope(&mut self,
560560
span: Span,
561561
lint_level: LintLevel,
562562
safety: Option<Safety>) -> SourceScope {
@@ -583,7 +583,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
583583
}
584584

585585
/// Given a span and the current source scope, make a SourceInfo.
586-
pub fn source_info(&self, span: Span) -> SourceInfo {
586+
crate fn source_info(&self, span: Span) -> SourceInfo {
587587
SourceInfo {
588588
span,
589589
scope: self.source_scope
@@ -614,7 +614,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
614614
///
615615
/// When building statics/constants, returns `None` since
616616
/// intermediate values do not have to be dropped in that case.
617-
pub fn local_scope(&self) -> Option<region::Scope> {
617+
crate fn local_scope(&self) -> Option<region::Scope> {
618618
match self.hir.body_owner_kind {
619619
hir::BodyOwnerKind::Const |
620620
hir::BodyOwnerKind::Static(_) =>
@@ -628,7 +628,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
628628

629629
// Scheduling drops
630630
// ================
631-
pub fn schedule_drop_storage_and_value(
631+
crate fn schedule_drop_storage_and_value(
632632
&mut self,
633633
span: Span,
634634
region_scope: region::Scope,
@@ -642,7 +642,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
642642
///
643643
/// When called with `DropKind::Storage`, `place` shouldn't be the return
644644
/// place, or a function parameter.
645-
pub fn schedule_drop(
645+
crate fn schedule_drop(
646646
&mut self,
647647
span: Span,
648648
region_scope: region::Scope,
@@ -720,7 +720,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
720720
/// spurious borrow-check errors -- the problem, ironically, is
721721
/// not the `DROP(_X)` itself, but the (spurious) unwind pathways
722722
/// that it creates. See #64391 for an example.
723-
pub fn record_operands_moved(&mut self, operands: &[Operand<'tcx>]) {
723+
crate fn record_operands_moved(&mut self, operands: &[Operand<'tcx>]) {
724724
let scope = match self.local_scope() {
725725
None => {
726726
// if there is no local scope, operands won't be dropped anyway
@@ -756,7 +756,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
756756
///
757757
/// This is a special case because the temporary for the condition needs to
758758
/// be dropped on both the true and the false arm.
759-
pub fn test_bool(
759+
crate fn test_bool(
760760
&mut self,
761761
mut block: BasicBlock,
762762
condition: Expr<'tcx>,
@@ -836,13 +836,13 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
836836
///
837837
/// This path terminates in Resume. The path isn't created until after all
838838
/// of the non-unwind paths in this item have been lowered.
839-
pub fn diverge_from(&mut self, start: BasicBlock) {
839+
crate fn diverge_from(&mut self, start: BasicBlock) {
840840
let next_drop = self.diverge_cleanup();
841841
self.scopes.unwind_drops.add_entry(start, next_drop);
842842
}
843843

844844
/// Utility function for *non*-scope code to build their own drops
845-
pub fn build_drop_and_replace(&mut self,
845+
crate fn build_drop_and_replace(&mut self,
846846
block: BasicBlock,
847847
span: Span,
848848
location: Place<'tcx>,
@@ -863,7 +863,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
863863
/// Creates an Assert terminator and return the success block.
864864
/// If the boolean condition operand is not the expected value,
865865
/// a runtime panic will be caused with the given message.
866-
pub fn assert(&mut self, block: BasicBlock,
866+
crate fn assert(&mut self, block: BasicBlock,
867867
cond: Operand<'tcx>,
868868
expected: bool,
869869
msg: AssertMessage<'tcx>,
@@ -892,7 +892,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
892892
///
893893
/// This is only needed for `match` arm scopes, because they have one
894894
/// entrance per pattern, but only one exit.
895-
pub(crate) fn clear_top_scope(&mut self, region_scope: region::Scope) {
895+
crate fn clear_top_scope(&mut self, region_scope: region::Scope) {
896896
let top_scope = self.scopes.scopes.last_mut().unwrap();
897897

898898
assert_eq!(top_scope.region_scope, region_scope);
@@ -1027,30 +1027,26 @@ impl<'a, 'tcx: 'a> Builder<'a, 'tcx> {
10271027

10281028
crate fn build_drop_trees(&mut self, should_abort: bool) {
10291029
if self.is_generator {
1030-
Self::build_generator_drop_tree(
1030+
self.build_generator_drop_trees(
1031+
should_abort,
1032+
);
1033+
} else {
1034+
Self::build_unwind_tree(
10311035
&mut self.cfg,
1032-
&mut self.scopes.generator_drops,
1036+
&mut self.scopes.unwind_drops,
10331037
self.fn_span,
10341038
should_abort,
10351039
);
10361040
}
1037-
Self::build_unwind_tree(
1038-
&mut self.cfg,
1039-
&mut self.scopes.unwind_drops,
1040-
self.fn_span,
1041-
should_abort,
1042-
);
10431041
}
10441042

1045-
fn build_generator_drop_tree(
1046-
cfg: &mut CFG<'tcx>,
1047-
drops: &mut DropTree,
1048-
fn_span: Span,
1049-
should_abort: bool,
1050-
) {
1043+
fn build_generator_drop_trees(&mut self, should_abort: bool) {
1044+
// Build the drop tree for dropping the generator while it's suspended.
1045+
let drops = &mut self.scopes.generator_drops;
1046+
let cfg = &mut self.cfg;
1047+
let fn_span = self.fn_span;
10511048
let mut blocks = IndexVec::from_elem(None, &drops.drops);
10521049
build_drop_tree::<GeneratorDrop>(cfg, drops, &mut blocks);
1053-
// TODO: unwind?
10541050
if let Some(root_block) = blocks[ROOT_NODE] {
10551051
cfg.terminate(
10561052
root_block,
@@ -1061,7 +1057,17 @@ impl<'a, 'tcx: 'a> Builder<'a, 'tcx> {
10611057
TerminatorKind::GeneratorDrop,
10621058
);
10631059
}
1064-
// Reuse the generator drop tree as the unwind tree.
1060+
1061+
// Build the drop tree for unwinding in the normal control flow paths.
1062+
let resume_block = Self::build_unwind_tree(
1063+
cfg,
1064+
&mut self.scopes.unwind_drops,
1065+
fn_span,
1066+
should_abort,
1067+
);
1068+
1069+
// Build the drop tree for unwinding when dropping a suspended
1070+
// generator.
10651071
//
10661072
// This is a different tree to the standard unwind paths here to
10671073
// prevent drop elaboration from creating drop flags that would have
@@ -1072,15 +1078,32 @@ impl<'a, 'tcx: 'a> Builder<'a, 'tcx> {
10721078
drops.entry_points.push((drop_data.1, blocks[drop_idx].unwrap()));
10731079
}
10741080
}
1075-
Self::build_unwind_tree(cfg, drops, fn_span, should_abort);
1081+
let mut blocks = IndexVec::from_elem(None, &drops.drops);
1082+
blocks[ROOT_NODE] = resume_block;
1083+
build_drop_tree::<Unwind>(cfg, drops, &mut blocks);
1084+
if let (None, Some(new_resume_block)) = (resume_block, blocks[ROOT_NODE]) {
1085+
let terminator = if should_abort {
1086+
TerminatorKind::Abort
1087+
} else {
1088+
TerminatorKind::Resume
1089+
};
1090+
cfg.terminate(
1091+
new_resume_block,
1092+
SourceInfo {
1093+
scope: OUTERMOST_SOURCE_SCOPE,
1094+
span: fn_span
1095+
},
1096+
terminator,
1097+
);
1098+
}
10761099
}
10771100

10781101
fn build_unwind_tree(
10791102
cfg: &mut CFG<'tcx>,
10801103
drops: &mut DropTree,
10811104
fn_span: Span,
10821105
should_abort: bool,
1083-
) {
1106+
) -> Option<BasicBlock> {
10841107
let mut blocks = IndexVec::from_elem(None, &drops.drops);
10851108
build_drop_tree::<Unwind>(cfg, drops, &mut blocks);
10861109
if let Some(resume_block) = blocks[ROOT_NODE] {
@@ -1097,6 +1120,9 @@ impl<'a, 'tcx: 'a> Builder<'a, 'tcx> {
10971120
},
10981121
terminator,
10991122
);
1123+
Some(resume_block)
1124+
} else {
1125+
None
11001126
}
11011127
}
11021128
}

0 commit comments

Comments
 (0)