Skip to content

Commit d6dc912

Browse files
committed
Extract shared logic into a new function
1 parent d2830cb commit d6dc912

File tree

1 file changed

+14
-20
lines changed

1 file changed

+14
-20
lines changed

compiler/rustc_mir_build/src/check_unsafety.rs

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,18 @@ impl<'tcx> UnsafetyVisitor<'_, 'tcx> {
132132
fn unsafe_op_in_unsafe_fn_allowed(&self) -> bool {
133133
self.tcx.lint_level_at_node(UNSAFE_OP_IN_UNSAFE_FN, self.hir_context).0 == Level::Allow
134134
}
135+
136+
/// Handle closures/generators/inline-consts, which is unsafecked with their parent body.
137+
fn visit_inner_body(&mut self, def: ty::WithOptConstParam<LocalDefId>) {
138+
if let Ok((inner_thir, expr)) = self.tcx.thir_body(def) {
139+
let inner_thir = &inner_thir.borrow();
140+
let hir_context = self.tcx.hir().local_def_id_to_hir_id(def.did);
141+
let mut inner_visitor = UnsafetyVisitor { thir: inner_thir, hir_context, ..*self };
142+
inner_visitor.visit_expr(&inner_thir[expr]);
143+
// Unsafe blocks can be used in the inner body, make sure to take it into account
144+
self.safety_context = inner_visitor.safety_context;
145+
}
146+
}
135147
}
136148

137149
// Searches for accesses to layout constrained fields.
@@ -408,29 +420,11 @@ impl<'a, 'tcx> Visitor<'a, 'tcx> for UnsafetyVisitor<'a, 'tcx> {
408420
} else {
409421
ty::WithOptConstParam::unknown(closure_id)
410422
};
411-
if let Ok((closure_thir, expr)) = self.tcx.thir_body(closure_def) {
412-
let closure_thir = &closure_thir.borrow();
413-
let hir_context = self.tcx.hir().local_def_id_to_hir_id(closure_id);
414-
let mut closure_visitor =
415-
UnsafetyVisitor { thir: closure_thir, hir_context, ..*self };
416-
closure_visitor.visit_expr(&closure_thir[expr]);
417-
// Unsafe blocks can be used in closures, make sure to take it into account
418-
self.safety_context = closure_visitor.safety_context;
419-
}
423+
self.visit_inner_body(closure_def);
420424
}
421425
ExprKind::ConstBlock { did, substs: _ } => {
422426
let def_id = did.expect_local();
423-
if let Ok((inner_thir, expr)) =
424-
self.tcx.thir_body(ty::WithOptConstParam::unknown(def_id))
425-
{
426-
let inner_thir = &inner_thir.borrow();
427-
let hir_context = self.tcx.hir().local_def_id_to_hir_id(def_id);
428-
let mut inner_visitor =
429-
UnsafetyVisitor { thir: inner_thir, hir_context, ..*self };
430-
inner_visitor.visit_expr(&inner_thir[expr]);
431-
// Unsafe blocks can be used in inline consts, make sure to take it into account
432-
self.safety_context = inner_visitor.safety_context;
433-
}
427+
self.visit_inner_body(ty::WithOptConstParam::unknown(def_id));
434428
}
435429
ExprKind::Field { lhs, .. } => {
436430
let lhs = &self.thir[lhs];

0 commit comments

Comments
 (0)