From 2fc92fa28cda858c25fea2e378c2eb73e7f65247 Mon Sep 17 00:00:00 2001 From: Paul Daniel Faria Date: Tue, 2 Jun 2020 18:44:04 -0400 Subject: Remove track_parent and parent_map, replace with simple walk in missign unsafe validator --- crates/ra_hir_ty/src/unsafe_validation.rs | 42 +++++++++++++++++-------------- 1 file changed, 23 insertions(+), 19 deletions(-) (limited to 'crates/ra_hir_ty/src') diff --git a/crates/ra_hir_ty/src/unsafe_validation.rs b/crates/ra_hir_ty/src/unsafe_validation.rs index 430182803..f3ce7112a 100644 --- a/crates/ra_hir_ty/src/unsafe_validation.rs +++ b/crates/ra_hir_ty/src/unsafe_validation.rs @@ -13,16 +13,9 @@ use crate::{ use rustc_hash::FxHashSet; -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, +use hir_def::{ + body::Body, + expr::{Expr, ExprId, UnaryOp}, }; pub struct UnsafeValidator<'a, 'b: 'a> { @@ -119,16 +112,27 @@ pub fn unsafe_expressions( } } - '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; - } + for unsafe_expr in &mut unsafe_exprs { + unsafe_expr.inside_unsafe_block = + is_in_unsafe(&body, body.body_expr, unsafe_expr.expr, false); } unsafe_exprs } + +fn is_in_unsafe(body: &Body, current: ExprId, needle: ExprId, within_unsafe: bool) -> bool { + if current == needle { + return within_unsafe; + } + + let expr = &body.exprs[current]; + if let &Expr::Unsafe { body: child } = expr { + return is_in_unsafe(body, child, needle, true); + } + + let mut found = false; + expr.walk_child_exprs(|child| { + found = found || is_in_unsafe(body, child, needle, within_unsafe); + }); + found +} -- cgit v1.2.3