Skip to content

Commit

Permalink
Extract shared logic into a new function
Browse files Browse the repository at this point in the history
  • Loading branch information
nbdd0121 committed Dec 13, 2022
1 parent d2830cb commit d6dc912
Showing 1 changed file with 14 additions and 20 deletions.
34 changes: 14 additions & 20 deletions compiler/rustc_mir_build/src/check_unsafety.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,18 @@ impl<'tcx> UnsafetyVisitor<'_, 'tcx> {
fn unsafe_op_in_unsafe_fn_allowed(&self) -> bool {
self.tcx.lint_level_at_node(UNSAFE_OP_IN_UNSAFE_FN, self.hir_context).0 == Level::Allow
}

/// Handle closures/generators/inline-consts, which is unsafecked with their parent body.
fn visit_inner_body(&mut self, def: ty::WithOptConstParam<LocalDefId>) {
if let Ok((inner_thir, expr)) = self.tcx.thir_body(def) {
let inner_thir = &inner_thir.borrow();
let hir_context = self.tcx.hir().local_def_id_to_hir_id(def.did);
let mut inner_visitor = UnsafetyVisitor { thir: inner_thir, hir_context, ..*self };
inner_visitor.visit_expr(&inner_thir[expr]);
// Unsafe blocks can be used in the inner body, make sure to take it into account
self.safety_context = inner_visitor.safety_context;
}
}
}

// Searches for accesses to layout constrained fields.
Expand Down Expand Up @@ -408,29 +420,11 @@ impl<'a, 'tcx> Visitor<'a, 'tcx> for UnsafetyVisitor<'a, 'tcx> {
} else {
ty::WithOptConstParam::unknown(closure_id)
};
if let Ok((closure_thir, expr)) = self.tcx.thir_body(closure_def) {
let closure_thir = &closure_thir.borrow();
let hir_context = self.tcx.hir().local_def_id_to_hir_id(closure_id);
let mut closure_visitor =
UnsafetyVisitor { thir: closure_thir, hir_context, ..*self };
closure_visitor.visit_expr(&closure_thir[expr]);
// Unsafe blocks can be used in closures, make sure to take it into account
self.safety_context = closure_visitor.safety_context;
}
self.visit_inner_body(closure_def);
}
ExprKind::ConstBlock { did, substs: _ } => {
let def_id = did.expect_local();
if let Ok((inner_thir, expr)) =
self.tcx.thir_body(ty::WithOptConstParam::unknown(def_id))
{
let inner_thir = &inner_thir.borrow();
let hir_context = self.tcx.hir().local_def_id_to_hir_id(def_id);
let mut inner_visitor =
UnsafetyVisitor { thir: inner_thir, hir_context, ..*self };
inner_visitor.visit_expr(&inner_thir[expr]);
// Unsafe blocks can be used in inline consts, make sure to take it into account
self.safety_context = inner_visitor.safety_context;
}
self.visit_inner_body(ty::WithOptConstParam::unknown(def_id));
}
ExprKind::Field { lhs, .. } => {
let lhs = &self.thir[lhs];
Expand Down

0 comments on commit d6dc912

Please sign in to comment.