Make the THIR unsafeck use the thir_body
query
This commit is contained in:
parent
6f64eb1fe6
commit
6dfdea9800
5 changed files with 55 additions and 56 deletions
|
@ -46,6 +46,18 @@ fn mir_build(tcx: TyCtxt<'_>, def: ty::WithOptConstParam<LocalDefId>) -> Body<'_
|
|||
let body_owner_kind = tcx.hir().body_owner_kind(id);
|
||||
let typeck_results = tcx.typeck_opt_const_arg(def);
|
||||
|
||||
if tcx.sess.opts.debugging_opts.thir_unsafeck {
|
||||
// Ensure unsafeck is ran before we steal the THIR.
|
||||
match def {
|
||||
ty::WithOptConstParam { did, const_param_did: Some(const_param_did) } => {
|
||||
tcx.ensure().thir_check_unsafety_for_const_arg((did, const_param_did))
|
||||
}
|
||||
ty::WithOptConstParam { did, const_param_did: None } => {
|
||||
tcx.ensure().thir_check_unsafety(did)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Figure out what primary body this item has.
|
||||
let (body_id, return_ty_span, span_with_body) = match tcx.hir().get(id) {
|
||||
Node::Expr(hir::Expr { kind: hir::ExprKind::Closure(_, decl, body_id, _, _), .. }) => {
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
use crate::thir::visit::{self, Visitor};
|
||||
use crate::thir::*;
|
||||
|
||||
use rustc_errors::struct_span_err;
|
||||
use rustc_hir as hir;
|
||||
use rustc_middle::thir::*;
|
||||
use rustc_middle::ty::{self, TyCtxt};
|
||||
use rustc_session::lint::builtin::{UNSAFE_OP_IN_UNSAFE_FN, UNUSED_UNSAFE};
|
||||
use rustc_session::lint::Level;
|
||||
|
@ -328,13 +328,10 @@ impl UnsafeOpKind {
|
|||
|
||||
// FIXME: checking unsafety for closures should be handled by their parent body,
|
||||
// as they inherit their "safety context" from their declaration site.
|
||||
pub fn check_unsafety<'tcx>(
|
||||
tcx: TyCtxt<'tcx>,
|
||||
thir: &Thir<'tcx>,
|
||||
expr: ExprId,
|
||||
def_id: LocalDefId,
|
||||
hir_id: hir::HirId,
|
||||
) {
|
||||
pub fn check_unsafety<'tcx>(tcx: TyCtxt<'tcx>, def: ty::WithOptConstParam<LocalDefId>) {
|
||||
let (thir, expr) = tcx.thir_body(def);
|
||||
let thir = &thir.borrow();
|
||||
let hir_id = tcx.hir().local_def_id_to_hir_id(def.did);
|
||||
let body_unsafety = tcx.hir().fn_sig_by_hir_id(hir_id).map_or(BodyUnsafety::Safe, |fn_sig| {
|
||||
if fn_sig.header.unsafety == hir::Unsafety::Unsafe {
|
||||
BodyUnsafety::Unsafe(fn_sig.span)
|
||||
|
@ -342,12 +339,12 @@ pub fn check_unsafety<'tcx>(
|
|||
BodyUnsafety::Safe
|
||||
}
|
||||
});
|
||||
let body_target_features = &tcx.codegen_fn_attrs(def_id).target_features;
|
||||
let body_target_features = &tcx.codegen_fn_attrs(def.did).target_features;
|
||||
let safety_context =
|
||||
if body_unsafety.is_unsafe() { SafetyContext::UnsafeFn } else { SafetyContext::Safe };
|
||||
let is_const = match tcx.hir().body_owner_kind(hir_id) {
|
||||
hir::BodyOwnerKind::Closure => false,
|
||||
hir::BodyOwnerKind::Fn => tcx.is_const_fn_raw(def_id.to_def_id()),
|
||||
hir::BodyOwnerKind::Fn => tcx.is_const_fn_raw(def.did.to_def_id()),
|
||||
hir::BodyOwnerKind::Const | hir::BodyOwnerKind::Static(_) => true,
|
||||
};
|
||||
let mut visitor = UnsafetyVisitor {
|
||||
|
@ -362,22 +359,11 @@ pub fn check_unsafety<'tcx>(
|
|||
visitor.visit_expr(&thir[expr]);
|
||||
}
|
||||
|
||||
crate fn thir_check_unsafety_inner<'tcx>(
|
||||
tcx: TyCtxt<'tcx>,
|
||||
def: ty::WithOptConstParam<LocalDefId>,
|
||||
) {
|
||||
let hir_id = tcx.hir().local_def_id_to_hir_id(def.did);
|
||||
let body_id = tcx.hir().body_owned_by(hir_id);
|
||||
let body = tcx.hir().body(body_id);
|
||||
let (thir, expr) = cx::build_thir(tcx, def, &body.value);
|
||||
check_unsafety(tcx, &thir, expr, def.did, hir_id);
|
||||
}
|
||||
|
||||
crate fn thir_check_unsafety<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId) {
|
||||
if let Some(def) = ty::WithOptConstParam::try_lookup(def_id, tcx) {
|
||||
tcx.thir_check_unsafety_for_const_arg(def)
|
||||
} else {
|
||||
thir_check_unsafety_inner(tcx, ty::WithOptConstParam::unknown(def_id))
|
||||
check_unsafety(tcx, ty::WithOptConstParam::unknown(def_id))
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -385,5 +371,5 @@ crate fn thir_check_unsafety_for_const_arg<'tcx>(
|
|||
tcx: TyCtxt<'tcx>,
|
||||
(did, param_did): (LocalDefId, DefId),
|
||||
) {
|
||||
thir_check_unsafety_inner(tcx, ty::WithOptConstParam { did, const_param_did: Some(param_did) })
|
||||
check_unsafety(tcx, ty::WithOptConstParam { did, const_param_did: Some(param_did) })
|
||||
}
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
use crate::thir::*;
|
||||
use rustc_middle::thir::*;
|
||||
use rustc_middle::ty::Const;
|
||||
|
||||
pub trait Visitor<'a, 'tcx: 'a>: Sized {
|
||||
fn thir(&self) -> &'a Thir<'tcx>;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue