提交 bdc37aa0 编写于 作者: B bobtwinkles

mir: Add TerminatorKind::FalseUnwind

Sometimes a simple goto misses the cleanup/unwind edges. Specifically, in the
case of infinite loops such as those introduced by a loop statement without any
other out edges. Analogous to TerminatorKind::FalseEdges; this new terminator
kind is used when we want borrowck to consider an unwind path, but real control
flow should never actually take it.
上级 5ca88ae6
......@@ -201,6 +201,10 @@ fn hash_stable<W: StableHasherResult>(&self,
target.hash_stable(hcx, hasher);
}
}
mir::TerminatorKind::FalseUnwind { ref real_target, ref unwind } => {
real_target.hash_stable(hcx, hasher);
unwind.hash_stable(hcx, hasher);
}
}
}
}
......
......@@ -803,9 +803,28 @@ pub enum TerminatorKind<'tcx> {
/// Indicates the end of the dropping of a generator
GeneratorDrop,
/// A block where control flow only ever takes one real path, but borrowck
/// needs to be more conservative.
FalseEdges {
/// The target normal control flow will take
real_target: BasicBlock,
imaginary_targets: Vec<BasicBlock>
/// The list of blocks control flow could conceptually take, but won't
/// in practice
imaginary_targets: Vec<BasicBlock>,
},
/// A terminator for blocks that only take one path in reality, but where we
/// reserve the right to unwind in borrowck, even if it won't happen in practice.
/// This can arise in infinite loops with no function calls for example.
FalseUnwind {
/// The target normal control flow will take
real_target: BasicBlock,
/// The imaginary cleanup block link. This particular path will never be taken
/// in practice, but in order to avoid fragility we want to always
/// consider it in borrowck. We don't want to accept programs which
/// pass borrowck only when panic=abort or some assertions are disabled
/// due to release vs. debug mode builds. This needs to be an Option because
/// of the remove_noop_landing_pads and no_landing_pads passes
unwind: Option<BasicBlock>,
},
}
......@@ -865,6 +884,8 @@ pub fn successors(&self) -> Cow<[BasicBlock]> {
s.extend_from_slice(imaginary_targets);
s.into_cow()
}
FalseUnwind { real_target: t, unwind: Some(u) } => vec![t, u].into_cow(),
FalseUnwind { real_target: ref t, unwind: None } => slice::from_ref(t).into_cow(),
}
}
......@@ -897,6 +918,8 @@ pub fn successors_mut(&mut self) -> Vec<&mut BasicBlock> {
s.extend(imaginary_targets.iter_mut());
s
}
FalseUnwind { real_target: ref mut t, unwind: Some(ref mut u) } => vec![t, u],
FalseUnwind { ref mut real_target, unwind: None } => vec![real_target],
}
}
......@@ -916,7 +939,8 @@ pub fn unwind_mut(&mut self) -> Option<&mut Option<BasicBlock>> {
TerminatorKind::Call { cleanup: ref mut unwind, .. } |
TerminatorKind::Assert { cleanup: ref mut unwind, .. } |
TerminatorKind::DropAndReplace { ref mut unwind, .. } |
TerminatorKind::Drop { ref mut unwind, .. } => {
TerminatorKind::Drop { ref mut unwind, .. } |
TerminatorKind::FalseUnwind { ref mut unwind, .. } => {
Some(unwind)
}
}
......@@ -1045,7 +1069,8 @@ pub fn fmt_head<W: Write>(&self, fmt: &mut W) -> fmt::Result {
write!(fmt, ")")
},
FalseEdges { .. } => write!(fmt, "falseEdges")
FalseEdges { .. } => write!(fmt, "falseEdges"),
FalseUnwind { .. } => write!(fmt, "falseUnwind"),
}
}
......@@ -1087,6 +1112,8 @@ pub fn fmt_successor_labels(&self) -> Vec<Cow<'static, str>> {
l.resize(imaginary_targets.len() + 1, "imaginary".into());
l
}
FalseUnwind { unwind: Some(_), .. } => vec!["real".into(), "cleanup".into()],
FalseUnwind { unwind: None, .. } => vec!["real".into()],
}
}
}
......@@ -2189,7 +2216,8 @@ fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F)
Return => Return,
Unreachable => Unreachable,
FalseEdges { real_target, ref imaginary_targets } =>
FalseEdges { real_target, imaginary_targets: imaginary_targets.clone() }
FalseEdges { real_target, imaginary_targets: imaginary_targets.clone() },
FalseUnwind { real_target, unwind } => FalseUnwind { real_target, unwind },
};
Terminator {
source_info: self.source_info,
......@@ -2231,7 +2259,8 @@ fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
Return |
GeneratorDrop |
Unreachable |
FalseEdges { .. } => false
FalseEdges { .. } |
FalseUnwind { .. } => false
}
}
}
......
......@@ -495,15 +495,21 @@ fn super_terminator_kind(&mut self,
self.visit_operand(value, source_location);
self.visit_branch(block, resume);
drop.map(|t| self.visit_branch(block, t));
}
TerminatorKind::FalseEdges { real_target, ref imaginary_targets } => {
TerminatorKind::FalseEdges { real_target, ref imaginary_targets} => {
self.visit_branch(block, real_target);
for target in imaginary_targets {
self.visit_branch(block, *target);
}
}
TerminatorKind::FalseUnwind { real_target, unwind } => {
self.visit_branch(block, real_target);
if let Some(unwind) = unwind {
self.visit_branch(block, unwind);
}
}
}
}
......
......@@ -576,7 +576,8 @@ fn visit_terminator_entry(
TerminatorKind::Goto { target: _ }
| TerminatorKind::Abort
| TerminatorKind::Unreachable
| TerminatorKind::FalseEdges { .. } => {
| TerminatorKind::FalseEdges { real_target: _, imaginary_targets: _ }
| TerminatorKind::FalseUnwind { real_target: _, unwind: _ } => {
// no data used, thus irrelevant to borrowck
}
}
......
......@@ -796,7 +796,8 @@ fn check_terminator(
| TerminatorKind::GeneratorDrop
| TerminatorKind::Unreachable
| TerminatorKind::Drop { .. }
| TerminatorKind::FalseEdges { .. } => {
| TerminatorKind::FalseEdges { .. }
| TerminatorKind::FalseUnwind { .. } => {
// no checks needed for these
}
......@@ -1152,6 +1153,18 @@ fn check_iscleanup(&mut self, mir: &Mir<'tcx>, block_data: &BasicBlockData<'tcx>
self.assert_iscleanup(mir, block_data, *target, is_cleanup);
}
}
TerminatorKind::FalseUnwind {
real_target,
unwind
} => {
self.assert_iscleanup(mir, block_data, real_target, is_cleanup);
if let Some(unwind) = unwind {
if is_cleanup {
span_mirbug!(self, block_data, "cleanup in cleanup block via false unwind");
}
self.assert_iscleanup(mir, block_data, unwind, true);
}
}
}
}
......
......@@ -728,7 +728,8 @@ fn bind_and_guard_matched_candidate<'pat>(&mut self,
TerminatorKind::FalseEdges {
real_target: block,
imaginary_targets:
vec![candidate.next_candidate_pre_binding_block]});
vec![candidate.next_candidate_pre_binding_block],
});
self.bind_matched_candidate(block, candidate.bindings);
......@@ -749,7 +750,8 @@ fn bind_and_guard_matched_candidate<'pat>(&mut self,
TerminatorKind::FalseEdges {
real_target: otherwise,
imaginary_targets:
vec![candidate.next_candidate_pre_binding_block] });
vec![candidate.next_candidate_pre_binding_block],
});
Some(otherwise)
} else {
self.cfg.terminate(block, candidate_source_info,
......
......@@ -517,6 +517,7 @@ fn terminator_effect_on_borrows(&self,
mir::TerminatorKind::Yield {..} |
mir::TerminatorKind::Goto {..} |
mir::TerminatorKind::FalseEdges {..} |
mir::TerminatorKind::FalseUnwind {..} |
mir::TerminatorKind::Unreachable => {}
}
}
......
......@@ -864,6 +864,14 @@ fn propagate_bits_into_graph_successors_of(
self.propagate_bits_into_entry_set_for(in_out, changed, target);
}
}
mir::TerminatorKind::FalseUnwind { ref real_target, unwind } => {
self.propagate_bits_into_entry_set_for(in_out, changed, real_target);
if let Some(ref unwind) = unwind {
if !self.dead_unwinds.contains(&bb) {
self.propagate_bits_into_entry_set_for(in_out, changed, unwind);
}
}
}
}
}
......
......@@ -346,6 +346,7 @@ fn gather_terminator(&mut self, term: &Terminator<'tcx>) {
TerminatorKind::Abort |
TerminatorKind::GeneratorDrop |
TerminatorKind::FalseEdges { .. } |
TerminatorKind::FalseUnwind { .. } |
TerminatorKind::Unreachable => { }
TerminatorKind::Return => {
......
......@@ -165,6 +165,7 @@ pub(super) fn eval_terminator(
Resume => unimplemented!(),
Abort => unimplemented!(),
FalseEdges { .. } => bug!("should have been eliminated by `simplify_branches` mir pass"),
FalseUnwind { .. } => bug!("should have been eliminated by `simplify_branches` mir pass"),
Unreachable => return err!(Unreachable),
}
......
......@@ -636,7 +636,8 @@ fn visit_terminator_kind(&mut self,
mir::TerminatorKind::Assert { .. } => {}
mir::TerminatorKind::GeneratorDrop |
mir::TerminatorKind::Yield { .. } |
mir::TerminatorKind::FalseEdges { .. } => bug!(),
mir::TerminatorKind::FalseEdges { .. } |
mir::TerminatorKind::FalseUnwind { .. } => bug!(),
}
self.super_terminator_kind(block, kind, location);
......
......@@ -76,7 +76,8 @@ fn visit_terminator(&mut self,
TerminatorKind::Abort |
TerminatorKind::Return |
TerminatorKind::Unreachable |
TerminatorKind::FalseEdges { .. } => {
TerminatorKind::FalseEdges { .. } |
TerminatorKind::FalseUnwind { .. } => {
// safe (at least as emitted during MIR construction)
}
......
......@@ -813,6 +813,9 @@ fn visit_terminator_kind(&mut self, block: BasicBlock,
*target = self.update_target(*target);
}
}
TerminatorKind::FalseUnwind { real_target: _ , unwind: _ } =>
// see the ordering of passes in the optimized_mir query.
bug!("False unwinds should have been removed before inlining")
}
}
......
......@@ -327,7 +327,8 @@ fn qualify_const(&mut self) -> (Qualif, Rc<IdxSetBuf<Local>>) {
TerminatorKind::GeneratorDrop |
TerminatorKind::Yield { .. } |
TerminatorKind::Unreachable |
TerminatorKind::FalseEdges { .. } => None,
TerminatorKind::FalseEdges { .. } |
TerminatorKind::FalseUnwind { .. } => None,
TerminatorKind::Return => {
// Check for unused values. This usually means
......
......@@ -75,7 +75,8 @@ fn is_nop_landing_pad(&self, bb: BasicBlock, mir: &Mir, nop_landing_pads: &BitVe
TerminatorKind::Goto { .. } |
TerminatorKind::Resume |
TerminatorKind::SwitchInt { .. } |
TerminatorKind::FalseEdges { .. } => {
TerminatorKind::FalseEdges { .. } |
TerminatorKind::FalseUnwind { .. } => {
terminator.successors().iter().all(|succ| {
nop_landing_pads.contains(succ.index())
})
......
......@@ -64,6 +64,9 @@ fn run_pass<'a, 'tcx>(&self,
TerminatorKind::FalseEdges { real_target, .. } => {
TerminatorKind::Goto { target: real_target }
},
TerminatorKind::FalseUnwind { real_target, .. } => {
TerminatorKind::Goto { target: real_target }
},
_ => continue
};
}
......
......@@ -123,6 +123,7 @@ fn visit_terminator_kind(&mut self,
TerminatorKind::GeneratorDrop => "TerminatorKind::GeneratorDrop",
TerminatorKind::Yield { .. } => "TerminatorKind::Yield",
TerminatorKind::FalseEdges { .. } => "TerminatorKind::FalseEdges",
TerminatorKind::FalseUnwind { .. } => "TerminatorKind::FalseUnwind",
}, kind);
self.super_terminator_kind(block, kind, location);
}
......
......@@ -242,7 +242,8 @@ fn discover_masters<'tcx>(result: &mut IndexVec<mir::BasicBlock, CleanupKind>,
TerminatorKind::Unreachable |
TerminatorKind::SwitchInt { .. } |
TerminatorKind::Yield { .. } |
TerminatorKind::FalseEdges { .. } => {
TerminatorKind::FalseEdges { .. } |
TerminatorKind::FalseUnwind { .. } => {
/* nothing to do */
}
TerminatorKind::Call { cleanup: unwind, .. } |
......
......@@ -608,8 +608,9 @@ fn trans_terminator(&mut self,
cleanup);
}
mir::TerminatorKind::GeneratorDrop |
mir::TerminatorKind::Yield { .. } |
mir::TerminatorKind::FalseEdges { .. } => bug!("generator ops in trans"),
mir::TerminatorKind::Yield { .. } => bug!("generator ops in trans"),
mir::TerminatorKind::FalseEdges { .. } |
mir::TerminatorKind::FalseUnwind { .. } => bug!("borrowck false edges in trans"),
}
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册