Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'crates/hir-ty/src/diagnostics/unsafe_check.rs')
-rw-r--r--crates/hir-ty/src/diagnostics/unsafe_check.rs61
1 files changed, 40 insertions, 21 deletions
diff --git a/crates/hir-ty/src/diagnostics/unsafe_check.rs b/crates/hir-ty/src/diagnostics/unsafe_check.rs
index 3f04b72c2f..6160962e3b 100644
--- a/crates/hir-ty/src/diagnostics/unsafe_check.rs
+++ b/crates/hir-ty/src/diagnostics/unsafe_check.rs
@@ -5,20 +5,21 @@ use std::mem;
use either::Either;
use hir_def::{
- AdtId, DefWithBodyId, FieldId, FunctionId, VariantId,
+ AdtId, CallableDefId, DefWithBodyId, FieldId, FunctionId, VariantId,
expr_store::{Body, path::Path},
hir::{AsmOperand, Expr, ExprId, ExprOrPatId, InlineAsmKind, Pat, PatId, Statement, UnaryOp},
resolver::{HasResolver, ResolveValueResult, Resolver, ValueNs},
signatures::StaticFlags,
type_ref::Rawness,
};
+use rustc_type_ir::inherent::IntoKind;
use span::Edition;
-use crate::utils::TargetFeatureIsSafeInTarget;
use crate::{
- InferenceResult, Interner, TargetFeatures, TyExt, TyKind,
+ InferenceResult, TargetFeatures,
db::HirDatabase,
- utils::{is_fn_unsafe_to_call, target_feature_is_safe_in_target},
+ next_solver::{CallableIdWrapper, TyKind, abi::Safety},
+ utils::{TargetFeatureIsSafeInTarget, is_fn_unsafe_to_call, target_feature_is_safe_in_target},
};
#[derive(Debug, Default)]
@@ -41,7 +42,7 @@ pub fn missing_unsafe(db: &dyn HirDatabase, def: DefWithBodyId) -> MissingUnsafe
let mut res = MissingUnsafeResult { fn_is_unsafe: is_unsafe, ..MissingUnsafeResult::default() };
let body = db.body(def);
- let infer = db.infer(def);
+ let infer = InferenceResult::for_body(db, def);
let mut callback = |diag| match diag {
UnsafeDiagnostic::UnsafeOperation { node, inside_unsafe_block, reason } => {
if inside_unsafe_block == InsideUnsafeBlock::No {
@@ -54,7 +55,7 @@ pub fn missing_unsafe(db: &dyn HirDatabase, def: DefWithBodyId) -> MissingUnsafe
}
}
};
- let mut visitor = UnsafeVisitor::new(db, &infer, &body, def, &mut callback);
+ let mut visitor = UnsafeVisitor::new(db, infer, &body, def, &mut callback);
visitor.walk_expr(body.body_expr);
if !is_unsafe {
@@ -96,9 +97,9 @@ enum UnsafeDiagnostic {
DeprecatedSafe2024 { node: ExprId, inside_unsafe_block: InsideUnsafeBlock },
}
-pub fn unsafe_operations_for_body(
- db: &dyn HirDatabase,
- infer: &InferenceResult,
+pub fn unsafe_operations_for_body<'db>(
+ db: &'db dyn HirDatabase,
+ infer: &InferenceResult<'db>,
def: DefWithBodyId,
body: &Body,
callback: &mut dyn FnMut(ExprOrPatId),
@@ -115,9 +116,9 @@ pub fn unsafe_operations_for_body(
}
}
-pub fn unsafe_operations(
- db: &dyn HirDatabase,
- infer: &InferenceResult,
+pub fn unsafe_operations<'db>(
+ db: &'db dyn HirDatabase,
+ infer: &InferenceResult<'db>,
def: DefWithBodyId,
body: &Body,
current: ExprId,
@@ -135,7 +136,7 @@ pub fn unsafe_operations(
struct UnsafeVisitor<'db> {
db: &'db dyn HirDatabase,
- infer: &'db InferenceResult,
+ infer: &'db InferenceResult<'db>,
body: &'db Body,
resolver: Resolver<'db>,
def: DefWithBodyId,
@@ -143,7 +144,7 @@ struct UnsafeVisitor<'db> {
inside_assignment: bool,
inside_union_destructure: bool,
callback: &'db mut dyn FnMut(UnsafeDiagnostic),
- def_target_features: TargetFeatures,
+ def_target_features: TargetFeatures<'db>,
// FIXME: This needs to be the edition of the span of each call.
edition: Edition,
/// On some targets (WASM), calling safe functions with `#[target_feature]` is always safe, even when
@@ -154,14 +155,14 @@ struct UnsafeVisitor<'db> {
impl<'db> UnsafeVisitor<'db> {
fn new(
db: &'db dyn HirDatabase,
- infer: &'db InferenceResult,
+ infer: &'db InferenceResult<'db>,
body: &'db Body,
def: DefWithBodyId,
unsafe_expr_cb: &'db mut dyn FnMut(UnsafeDiagnostic),
) -> Self {
let resolver = def.resolver(db);
let def_target_features = match def {
- DefWithBodyId::FunctionId(func) => TargetFeatures::from_attrs(&db.attrs(func.into())),
+ DefWithBodyId::FunctionId(func) => TargetFeatures::from_fn(db, func),
_ => TargetFeatures::default(),
};
let krate = resolver.module().krate();
@@ -285,12 +286,14 @@ impl<'db> UnsafeVisitor<'db> {
let inside_assignment = mem::replace(&mut self.inside_assignment, false);
match expr {
&Expr::Call { callee, .. } => {
- let callee = &self.infer[callee];
- if let Some(func) = callee.as_fn_def(self.db) {
+ let callee = self.infer[callee];
+ if let TyKind::FnDef(CallableIdWrapper(CallableDefId::FunctionId(func)), _) =
+ callee.kind()
+ {
self.check_call(current, func);
}
- if let TyKind::Function(fn_ptr) = callee.kind(Interner)
- && fn_ptr.sig.safety == chalk_ir::Safety::Unsafe
+ if let TyKind::FnPtr(_, hdr) = callee.kind()
+ && hdr.safety == Safety::Unsafe
{
self.on_unsafe_op(current.into(), UnsafetyReason::UnsafeFnCall);
}
@@ -315,6 +318,22 @@ impl<'db> UnsafeVisitor<'db> {
}
_ => (),
}
+
+ let mut peeled = *expr;
+ while let Expr::Field { expr: lhs, .. } = &self.body[peeled] {
+ if let Some(Either::Left(FieldId { parent: VariantId::UnionId(_), .. })) =
+ self.infer.field_resolution(peeled)
+ {
+ peeled = *lhs;
+ } else {
+ break;
+ }
+ }
+
+ // Walk the peeled expression (the LHS of the union field chain)
+ self.walk_expr(peeled);
+ // Return so we don't recurse directly onto the union field access(es)
+ return;
}
Expr::MethodCall { .. } => {
if let Some((func, _)) = self.infer.method_resolution(current) {
@@ -322,7 +341,7 @@ impl<'db> UnsafeVisitor<'db> {
}
}
Expr::UnaryOp { expr, op: UnaryOp::Deref } => {
- if let TyKind::Raw(..) = &self.infer[*expr].kind(Interner) {
+ if let TyKind::RawPtr(..) = self.infer[*expr].kind() {
self.on_unsafe_op(current.into(), UnsafetyReason::RawPtrDeref);
}
}