Skip to content

Commit 608dc49

Browse files
committed
Move is_inside_unsafe to Semantics impl
1 parent 3bfe704 commit 608dc49

File tree

3 files changed

+74
-73
lines changed

3 files changed

+74
-73
lines changed

crates/hir/src/lib.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ use hir_def::{
5050
per_ns::PerNs,
5151
resolver::{HasResolver, Resolver},
5252
src::HasSource as _,
53-
AdtId, AssocItemId, AssocItemLoc, AttrDefId, ConstId, ConstParamId, EnumId, EnumVariantId,
54-
FunctionId, GenericDefId, HasModule, ImplId, ItemContainerId, LifetimeParamId,
53+
AdtId, AssocItemId, AssocItemLoc, AttrDefId, ConstId, ConstParamId, DefWithBodyId, EnumId,
54+
EnumVariantId, FunctionId, GenericDefId, HasModule, ImplId, ItemContainerId, LifetimeParamId,
5555
LocalEnumVariantId, LocalFieldId, Lookup, MacroExpander, MacroId, ModuleId, StaticId, StructId,
5656
TraitId, TypeAliasId, TypeOrConstParamId, TypeParamId, UnionId,
5757
};
@@ -107,16 +107,13 @@ pub use {
107107
hir_def::{
108108
adt::StructKind,
109109
attr::{Attr, Attrs, AttrsWithOwner, Documentation},
110-
body::{Body, BodySourceMap},
111110
builtin_attr::AttributeTemplate,
112-
expr::Expr,
113111
find_path::PrefixKind,
114112
import_map,
115113
nameres::ModuleSource,
116114
path::{ModPath, PathKind},
117115
type_ref::{Mutability, TypeRef},
118116
visibility::Visibility,
119-
DefWithBodyId,
120117
},
121118
hir_expand::{
122119
name::{known, Name},

crates/hir/src/semantics.rs

Lines changed: 69 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,14 @@ mod source_to_def;
55
use std::{cell::RefCell, fmt, iter, mem, ops};
66

77
use base_db::{FileId, FileRange};
8+
use either::Either;
89
use hir_def::{
9-
body, macro_id_to_def_id,
10+
body,
11+
expr::Expr,
12+
macro_id_to_def_id,
1013
resolver::{self, HasResolver, Resolver, TypeNs},
1114
type_ref::Mutability,
12-
AsMacroCall, FunctionId, MacroId, TraitId, VariantId,
15+
AsMacroCall, DefWithBodyId, FunctionId, MacroId, TraitId, VariantId,
1316
};
1417
use hir_expand::{
1518
db::AstDatabase,
@@ -438,8 +441,7 @@ impl<'db, DB: HirDatabase> Semantics<'db, DB> {
438441
}
439442

440443
pub fn to_def<T: ToDef>(&self, src: &T) -> Option<T::Def> {
441-
let src = self.imp.find_file(src.syntax()).with_value(src).cloned();
442-
T::to_def(&self.imp, src)
444+
self.imp.to_def(src)
443445
}
444446

445447
pub fn to_module_def(&self, file: FileId) -> Option<Module> {
@@ -481,6 +483,11 @@ impl<'db, DB: HirDatabase> Semantics<'db, DB> {
481483
pub fn is_unsafe_ident_pat(&self, ident_pat: &ast::IdentPat) -> bool {
482484
self.imp.is_unsafe_ident_pat(ident_pat)
483485
}
486+
487+
/// Returns `true` if the `node` is inside an `unsafe` context.
488+
pub fn is_inside_unsafe(&self, node: &SyntaxNode) -> bool {
489+
self.imp.is_inside_unsafe(node)
490+
}
484491
}
485492

486493
impl<'db> SemanticsImpl<'db> {
@@ -1243,6 +1250,11 @@ impl<'db> SemanticsImpl<'db> {
12431250
f(&mut ctx)
12441251
}
12451252

1253+
fn to_def<T: ToDef>(&self, src: &T) -> Option<T::Def> {
1254+
let src = self.find_file(src.syntax()).with_value(src).cloned();
1255+
T::to_def(&self, src)
1256+
}
1257+
12461258
fn to_module_def(&self, file: FileId) -> impl Iterator<Item = Module> {
12471259
self.with_ctx(|ctx| ctx.file_to_def(file)).into_iter().map(Module::from)
12481260
}
@@ -1458,6 +1470,59 @@ impl<'db> SemanticsImpl<'db> {
14581470
.map(|ty| ty.original.is_packed(self.db))
14591471
.unwrap_or(false)
14601472
}
1473+
1474+
fn is_inside_unsafe(&self, node: &SyntaxNode) -> bool {
1475+
let item_or_variant = |ancestor: SyntaxNode| {
1476+
if ast::Item::can_cast(ancestor.kind()) {
1477+
ast::Item::cast(ancestor).map(Either::Left)
1478+
} else {
1479+
ast::Variant::cast(ancestor).map(Either::Right)
1480+
}
1481+
};
1482+
let Some(enclosing_item) = node.ancestors().find_map(item_or_variant) else { return false };
1483+
1484+
let def = match &enclosing_item {
1485+
Either::Left(ast::Item::Fn(it)) => {
1486+
self.to_def(it).map(<_>::into).map(DefWithBodyId::FunctionId)
1487+
}
1488+
Either::Left(ast::Item::Const(it)) => {
1489+
self.to_def(it).map(<_>::into).map(DefWithBodyId::ConstId)
1490+
}
1491+
Either::Left(ast::Item::Static(it)) => {
1492+
self.to_def(it).map(<_>::into).map(DefWithBodyId::StaticId)
1493+
}
1494+
Either::Left(_) => None,
1495+
Either::Right(it) => self.to_def(it).map(<_>::into).map(DefWithBodyId::VariantId),
1496+
};
1497+
let Some(def) = def else { return false };
1498+
let enclosing_node = enclosing_item.as_ref().either(|i| i.syntax(), |v| v.syntax());
1499+
1500+
if ast::Fn::cast(enclosing_node.clone()).and_then(|f| f.unsafe_token()).is_some() {
1501+
return true;
1502+
}
1503+
1504+
let (body, source_map) = self.db.body_with_source_map(def);
1505+
1506+
let file_id = self.find_file(node).file_id;
1507+
1508+
let Some(mut parent) = node.parent() else { return false };
1509+
loop {
1510+
if &parent == enclosing_node {
1511+
break false;
1512+
}
1513+
1514+
if let Some(parent) = ast::Expr::cast(parent.clone()) {
1515+
if let Some(expr_id) = source_map.node_expr(InFile { file_id, value: &parent }) {
1516+
if let Expr::Unsafe { .. } = body[expr_id] {
1517+
break true;
1518+
}
1519+
}
1520+
}
1521+
1522+
let Some(parent_) = parent.parent() else { break false };
1523+
parent = parent_;
1524+
}
1525+
}
14611526
}
14621527

14631528
fn macro_call_to_macro_id(

crates/ide/src/inlay_hints/adjustment.rs

Lines changed: 3 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,9 @@
33
//! let _: u32 = /* <never-to-any> */ loop {};
44
//! let _: &u32 = /* &* */ &mut 0;
55
//! ```
6-
use either::Either;
7-
use hir::{
8-
db::DefDatabase, Adjust, AutoBorrow, InFile, Mutability, OverloadedDeref, PointerCast, Safety,
9-
Semantics,
10-
};
6+
use hir::{Adjust, AutoBorrow, Mutability, OverloadedDeref, PointerCast, Safety, Semantics};
117
use ide_db::RootDatabase;
12-
13-
use syntax::{
14-
ast::{self, AstNode},
15-
SyntaxNode,
16-
};
8+
use syntax::ast::{self, AstNode};
179

1810
use crate::{AdjustmentHints, InlayHint, InlayHintsConfig, InlayKind};
1911

@@ -23,7 +15,7 @@ pub(super) fn hints(
2315
config: &InlayHintsConfig,
2416
expr: &ast::Expr,
2517
) -> Option<()> {
26-
if config.adjustment_hints_hide_outside_unsafe && !is_inside_unsafe(sema, expr.syntax()) {
18+
if config.adjustment_hints_hide_outside_unsafe && !sema.is_inside_unsafe(expr.syntax()) {
2719
return None;
2820
}
2921

@@ -121,59 +113,6 @@ pub(super) fn hints(
121113
Some(())
122114
}
123115

124-
fn is_inside_unsafe(sema: &Semantics<'_, RootDatabase>, node: &SyntaxNode) -> bool {
125-
let item_or_variant = |ancestor: SyntaxNode| {
126-
if ast::Item::can_cast(ancestor.kind()) {
127-
ast::Item::cast(ancestor).map(Either::Left)
128-
} else {
129-
ast::Variant::cast(ancestor).map(Either::Right)
130-
}
131-
};
132-
let Some(enclosing_item) = node.ancestors().find_map(item_or_variant) else { return false };
133-
134-
let def = match &enclosing_item {
135-
Either::Left(ast::Item::Fn(it)) => {
136-
sema.to_def(it).map(<_>::into).map(hir::DefWithBodyId::FunctionId)
137-
}
138-
Either::Left(ast::Item::Const(it)) => {
139-
sema.to_def(it).map(<_>::into).map(hir::DefWithBodyId::ConstId)
140-
}
141-
Either::Left(ast::Item::Static(it)) => {
142-
sema.to_def(it).map(<_>::into).map(hir::DefWithBodyId::StaticId)
143-
}
144-
Either::Left(_) => None,
145-
Either::Right(it) => sema.to_def(it).map(<_>::into).map(hir::DefWithBodyId::VariantId),
146-
};
147-
let Some(def) = def else { return false };
148-
let enclosing_node = enclosing_item.as_ref().either(|i| i.syntax(), |v| v.syntax());
149-
150-
if ast::Fn::cast(enclosing_node.clone()).and_then(|f| f.unsafe_token()).is_some() {
151-
return true;
152-
}
153-
154-
let (body, source_map) = sema.db.body_with_source_map(def);
155-
156-
let file_id = sema.hir_file_for(node);
157-
158-
let Some(mut parent) = node.parent() else { return false };
159-
loop {
160-
if &parent == enclosing_node {
161-
break false;
162-
}
163-
164-
if let Some(parent) = ast::Expr::cast(parent.clone()) {
165-
if let Some(expr_id) = source_map.node_expr(InFile { file_id, value: &parent }) {
166-
if let hir::Expr::Unsafe { .. } = body[expr_id] {
167-
break true;
168-
}
169-
}
170-
}
171-
172-
let Some(parent_) = parent.parent() else { break false };
173-
parent = parent_;
174-
}
175-
}
176-
177116
#[cfg(test)]
178117
mod tests {
179118
use crate::{

0 commit comments

Comments
 (0)