Skip to content

Commit 321a72c

Browse files
committed
closure unsafety check: stop moving up when we hit an item
1 parent a8129d1 commit 321a72c

File tree

1 file changed

+15
-15
lines changed

1 file changed

+15
-15
lines changed

src/librustc_mir/transform/add_validation.rs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -128,29 +128,29 @@ fn fn_contains_unsafe<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, src: MirSource) ->
128128
let mut cur = fn_like.id();
129129
loop {
130130
// Go further upwards.
131-
let parent = tcx.hir.get_parent_node(cur);
132-
if cur == parent {
133-
bug!("Closures muts be inside a non-closure fn_like");
134-
}
135-
cur = parent;
136-
// Check if this is an unsafe block
137-
match tcx.hir.find(cur) {
138-
Some(Node::NodeExpr(&hir::Expr { node: hir::ExprBlock(ref block), ..})) => {
139-
if block_is_unsafe(&*block) {
140-
// Found an unsafe block, we can bail out here.
131+
cur = tcx.hir.get_parent_node(cur);
132+
let node = tcx.hir.get(cur);
133+
// Check if this is an unsafe function
134+
if let Some(fn_like) = FnLikeNode::from_node(node) {
135+
if !fn_is_closure(fn_like) {
136+
if fn_like.unsafety() == hir::Unsafety::Unsafe {
141137
return true;
142138
}
143139
}
144-
_ => {},
145140
}
146-
// Check if this is a non-closure fn_like, at which point we have to stop moving up
147-
if let Some(fn_like) = FnLikeNode::from_node(tcx.hir.get(cur)) {
148-
if !fn_is_closure(fn_like) {
149-
if fn_like.unsafety() == hir::Unsafety::Unsafe {
141+
// Check if this is an unsafe block, or an item
142+
match node {
143+
Node::NodeExpr(&hir::Expr { node: hir::ExprBlock(ref block), ..}) => {
144+
if block_is_unsafe(&*block) {
145+
// Found an unsafe block, we can bail out here.
150146
return true;
151147
}
148+
}
149+
Node::NodeItem(..) => {
150+
// No walking up beyond items. This makes sure the loop always terminates.
152151
break;
153152
}
153+
_ => {},
154154
}
155155
}
156156
}

0 commit comments

Comments
 (0)