2020-05-28 09:30:19 -04:00
|
|
|
//! Provides validations for unsafe code. Currently checks if unsafe functions are missing
|
|
|
|
//! unsafe blocks.
|
|
|
|
|
|
|
|
use std::sync::Arc;
|
|
|
|
|
2020-05-29 08:55:47 -04:00
|
|
|
use hir_def::{DefWithBodyId, FunctionId};
|
2020-05-28 09:30:19 -04:00
|
|
|
use hir_expand::diagnostics::DiagnosticSink;
|
|
|
|
|
|
|
|
use crate::{
|
2020-05-29 08:55:47 -04:00
|
|
|
db::HirDatabase, diagnostics::MissingUnsafe, lower::CallableDef, ApplicationTy,
|
|
|
|
InferenceResult, Ty, TypeCtor,
|
2020-05-28 09:30:19 -04:00
|
|
|
};
|
|
|
|
|
2020-05-29 08:55:47 -04:00
|
|
|
use rustc_hash::FxHashSet;
|
|
|
|
|
2020-05-28 09:30:19 -04:00
|
|
|
pub use hir_def::{
|
|
|
|
body::{
|
|
|
|
scope::{ExprScopes, ScopeEntry, ScopeId},
|
|
|
|
Body, BodySourceMap, ExprPtr, ExprSource, PatPtr, PatSource,
|
|
|
|
},
|
|
|
|
expr::{
|
|
|
|
ArithOp, Array, BinaryOp, BindingAnnotation, CmpOp, Expr, ExprId, Literal, LogicOp,
|
|
|
|
MatchArm, Ordering, Pat, PatId, RecordFieldPat, RecordLitField, Statement, UnaryOp,
|
|
|
|
},
|
|
|
|
LocalFieldId, VariantId,
|
|
|
|
};
|
|
|
|
|
|
|
|
pub struct UnsafeValidator<'a, 'b: 'a> {
|
|
|
|
func: FunctionId,
|
|
|
|
infer: Arc<InferenceResult>,
|
|
|
|
sink: &'a mut DiagnosticSink<'b>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'b> UnsafeValidator<'a, 'b> {
|
|
|
|
pub fn new(
|
|
|
|
func: FunctionId,
|
|
|
|
infer: Arc<InferenceResult>,
|
|
|
|
sink: &'a mut DiagnosticSink<'b>,
|
|
|
|
) -> UnsafeValidator<'a, 'b> {
|
|
|
|
UnsafeValidator { func, infer, sink }
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn validate_body(&mut self, db: &dyn HirDatabase) {
|
|
|
|
let def = self.func.into();
|
|
|
|
let unsafe_expressions = unsafe_expressions(db, self.infer.as_ref(), def);
|
|
|
|
let func_data = db.function_data(self.func);
|
|
|
|
if func_data.is_unsafe
|
|
|
|
|| unsafe_expressions
|
|
|
|
.iter()
|
|
|
|
.filter(|unsafe_expr| !unsafe_expr.inside_unsafe_block)
|
|
|
|
.count()
|
|
|
|
== 0
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let (_, body_source) = db.body_with_source_map(def);
|
|
|
|
for unsafe_expr in unsafe_expressions {
|
|
|
|
if !unsafe_expr.inside_unsafe_block {
|
|
|
|
if let Ok(in_file) = body_source.as_ref().expr_syntax(unsafe_expr.expr) {
|
|
|
|
self.sink.push(MissingUnsafe { file: in_file.file_id, expr: in_file.value })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-05-29 08:55:47 -04:00
|
|
|
|
|
|
|
pub struct UnsafeExpr {
|
|
|
|
pub expr: ExprId,
|
|
|
|
pub inside_unsafe_block: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl UnsafeExpr {
|
|
|
|
fn new(expr: ExprId) -> Self {
|
|
|
|
Self { expr, inside_unsafe_block: false }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn unsafe_expressions(
|
|
|
|
db: &dyn HirDatabase,
|
|
|
|
infer: &InferenceResult,
|
|
|
|
def: DefWithBodyId,
|
|
|
|
) -> Vec<UnsafeExpr> {
|
|
|
|
let mut unsafe_exprs = vec![];
|
|
|
|
let mut unsafe_block_exprs = FxHashSet::default();
|
|
|
|
let body = db.body(def);
|
|
|
|
for (id, expr) in body.exprs.iter() {
|
|
|
|
match expr {
|
|
|
|
Expr::Unsafe { .. } => {
|
|
|
|
unsafe_block_exprs.insert(id);
|
|
|
|
}
|
|
|
|
Expr::Call { callee, .. } => {
|
|
|
|
let ty = &infer[*callee];
|
|
|
|
if let &Ty::Apply(ApplicationTy {
|
|
|
|
ctor: TypeCtor::FnDef(CallableDef::FunctionId(func)),
|
|
|
|
..
|
|
|
|
}) = ty
|
|
|
|
{
|
|
|
|
if db.function_data(func).is_unsafe {
|
|
|
|
unsafe_exprs.push(UnsafeExpr::new(id));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Expr::MethodCall { .. } => {
|
|
|
|
if infer
|
|
|
|
.method_resolution(id)
|
|
|
|
.map(|func| db.function_data(func).is_unsafe)
|
|
|
|
.unwrap_or(false)
|
|
|
|
{
|
|
|
|
unsafe_exprs.push(UnsafeExpr::new(id));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Expr::UnaryOp { expr, op: UnaryOp::Deref } => {
|
|
|
|
if let Ty::Apply(ApplicationTy { ctor: TypeCtor::RawPtr(..), .. }) = &infer[*expr] {
|
|
|
|
unsafe_exprs.push(UnsafeExpr::new(id));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
'unsafe_exprs: for unsafe_expr in &mut unsafe_exprs {
|
|
|
|
let mut child = unsafe_expr.expr;
|
|
|
|
while let Some(parent) = body.parent_map.get(child) {
|
|
|
|
if unsafe_block_exprs.contains(parent) {
|
|
|
|
unsafe_expr.inside_unsafe_block = true;
|
|
|
|
continue 'unsafe_exprs;
|
|
|
|
}
|
|
|
|
child = *parent;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
unsafe_exprs
|
|
|
|
}
|