5 use std::{cell::RefCell, fmt, iter::successors};
7 use base_db::{FileId, FileRange};
9 resolver::{self, HasResolver, Resolver, TypeNs},
10 AsMacroCall, FunctionId, TraitId, VariantId,
12 use hir_expand::{hygiene::Hygiene, name::AsName, ExpansionInfo};
13 use hir_ty::associated_type_shorthand_candidates;
14 use itertools::Itertools;
15 use rustc_hash::{FxHashMap, FxHashSet};
17 algo::{find_node_at_offset, skip_trivia_token},
18 ast, AstNode, Direction, SyntaxNode, SyntaxToken, TextRange, TextSize,
23 diagnostics::Diagnostic,
24 semantics::source_to_def::{ChildContainer, SourceToDefCache, SourceToDefCtx},
25 source_analyzer::{resolve_hir_path, SourceAnalyzer},
26 AssocItem, Callable, Crate, Field, Function, HirFileId, ImplDef, InFile, Local, MacroDef,
27 Module, ModuleDef, Name, Origin, Path, ScopeDef, Trait, Type, TypeAlias, TypeParam, TypeRef,
31 #[derive(Debug, Clone, PartialEq, Eq)]
32 pub enum PathResolution {
35 /// A local binding (only value namespace)
37 /// A generic parameter
45 fn in_type_ns(&self) -> Option<TypeNs> {
47 PathResolution::Def(ModuleDef::Adt(adt)) => Some(TypeNs::AdtId((*adt).into())),
48 PathResolution::Def(ModuleDef::BuiltinType(builtin)) => {
49 Some(TypeNs::BuiltinType(*builtin))
51 PathResolution::Def(ModuleDef::Const(_))
52 | PathResolution::Def(ModuleDef::EnumVariant(_))
53 | PathResolution::Def(ModuleDef::Function(_))
54 | PathResolution::Def(ModuleDef::Module(_))
55 | PathResolution::Def(ModuleDef::Static(_))
56 | PathResolution::Def(ModuleDef::Trait(_)) => None,
57 PathResolution::Def(ModuleDef::TypeAlias(alias)) => {
58 Some(TypeNs::TypeAliasId((*alias).into()))
60 PathResolution::Local(_) | PathResolution::Macro(_) => None,
61 PathResolution::TypeParam(param) => Some(TypeNs::GenericParam((*param).into())),
62 PathResolution::SelfType(impl_def) => Some(TypeNs::SelfType((*impl_def).into())),
63 PathResolution::AssocItem(AssocItem::Const(_))
64 | PathResolution::AssocItem(AssocItem::Function(_)) => None,
65 PathResolution::AssocItem(AssocItem::TypeAlias(alias)) => {
66 Some(TypeNs::TypeAliasId((*alias).into()))
71 /// Returns an iterator over associated types that may be specified after this path (using
72 /// `Ty::Assoc` syntax).
73 pub fn assoc_type_shorthand_candidates<R>(
76 mut cb: impl FnMut(TypeAlias) -> Option<R>,
78 associated_type_shorthand_candidates(db, self.in_type_ns()?, |_, _, id| cb(id.into()))
82 /// Primary API to get semantic information, like types, from syntax trees.
83 pub struct Semantics<'db, DB> {
85 imp: SemanticsImpl<'db>,
88 pub struct SemanticsImpl<'db> {
89 pub db: &'db dyn HirDatabase,
90 s2d_cache: RefCell<SourceToDefCache>,
91 expansion_info_cache: RefCell<FxHashMap<HirFileId, Option<ExpansionInfo>>>,
92 cache: RefCell<FxHashMap<SyntaxNode, HirFileId>>,
95 impl<DB> fmt::Debug for Semantics<'_, DB> {
96 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
97 write!(f, "Semantics {{ ... }}")
101 impl<'db, DB: HirDatabase> Semantics<'db, DB> {
102 pub fn new(db: &DB) -> Semantics<DB> {
103 let impl_ = SemanticsImpl::new(db);
104 Semantics { db, imp: impl_ }
107 pub fn parse(&self, file_id: FileId) -> ast::SourceFile {
108 self.imp.parse(file_id)
111 pub fn expand(&self, macro_call: &ast::MacroCall) -> Option<SyntaxNode> {
112 self.imp.expand(macro_call)
114 pub fn speculative_expand(
116 actual_macro_call: &ast::MacroCall,
117 hypothetical_args: &ast::TokenTree,
118 token_to_map: SyntaxToken,
119 ) -> Option<(SyntaxNode, SyntaxToken)> {
120 self.imp.speculative_expand(actual_macro_call, hypothetical_args, token_to_map)
123 pub fn descend_into_macros(&self, token: SyntaxToken) -> SyntaxToken {
124 self.imp.descend_into_macros(token)
127 pub fn descend_node_at_offset<N: ast::AstNode>(
132 self.imp.descend_node_at_offset(node, offset).find_map(N::cast)
135 pub fn original_range(&self, node: &SyntaxNode) -> FileRange {
136 self.imp.original_range(node)
139 pub fn diagnostics_display_range(&self, diagnostics: &dyn Diagnostic) -> FileRange {
140 self.imp.diagnostics_display_range(diagnostics)
143 pub fn ancestors_with_macros(&self, node: SyntaxNode) -> impl Iterator<Item = SyntaxNode> + '_ {
144 self.imp.ancestors_with_macros(node)
147 pub fn ancestors_at_offset_with_macros(
151 ) -> impl Iterator<Item = SyntaxNode> + '_ {
152 self.imp.ancestors_at_offset_with_macros(node, offset)
155 /// Find a AstNode by offset inside SyntaxNode, if it is inside *Macrofile*,
156 /// search up until it is of the target AstNode type
157 pub fn find_node_at_offset_with_macros<N: AstNode>(
162 self.imp.ancestors_at_offset_with_macros(node, offset).find_map(N::cast)
165 /// Find a AstNode by offset inside SyntaxNode, if it is inside *MacroCall*,
166 /// descend it and find again
167 pub fn find_node_at_offset_with_descend<N: AstNode>(
172 if let Some(it) = find_node_at_offset(&node, offset) {
176 self.imp.descend_node_at_offset(node, offset).find_map(N::cast)
179 pub fn type_of_expr(&self, expr: &ast::Expr) -> Option<Type> {
180 self.imp.type_of_expr(expr)
183 pub fn type_of_pat(&self, pat: &ast::Pat) -> Option<Type> {
184 self.imp.type_of_pat(pat)
187 pub fn type_of_self(&self, param: &ast::SelfParam) -> Option<Type> {
188 self.imp.type_of_self(param)
191 pub fn resolve_method_call(&self, call: &ast::MethodCallExpr) -> Option<Function> {
192 self.imp.resolve_method_call(call).map(Function::from)
195 pub fn resolve_method_call_as_callable(&self, call: &ast::MethodCallExpr) -> Option<Callable> {
196 self.imp.resolve_method_call_as_callable(call)
199 pub fn resolve_field(&self, field: &ast::FieldExpr) -> Option<Field> {
200 self.imp.resolve_field(field)
203 pub fn resolve_record_field(
205 field: &ast::RecordExprField,
206 ) -> Option<(Field, Option<Local>)> {
207 self.imp.resolve_record_field(field)
210 pub fn resolve_record_field_pat(&self, field: &ast::RecordPatField) -> Option<Field> {
211 self.imp.resolve_record_field_pat(field)
214 pub fn resolve_macro_call(&self, macro_call: &ast::MacroCall) -> Option<MacroDef> {
215 self.imp.resolve_macro_call(macro_call)
218 pub fn resolve_path(&self, path: &ast::Path) -> Option<PathResolution> {
219 self.imp.resolve_path(path)
222 pub fn resolve_extern_crate(&self, extern_crate: &ast::ExternCrate) -> Option<Crate> {
223 self.imp.resolve_extern_crate(extern_crate)
226 pub fn resolve_variant(&self, record_lit: ast::RecordExpr) -> Option<VariantDef> {
227 self.imp.resolve_variant(record_lit).map(VariantDef::from)
230 pub fn resolve_bind_pat_to_const(&self, pat: &ast::IdentPat) -> Option<ModuleDef> {
231 self.imp.resolve_bind_pat_to_const(pat)
234 // FIXME: use this instead?
235 // pub fn resolve_name_ref(&self, name_ref: &ast::NameRef) -> Option<???>;
237 pub fn record_literal_missing_fields(&self, literal: &ast::RecordExpr) -> Vec<(Field, Type)> {
238 self.imp.record_literal_missing_fields(literal)
241 pub fn record_pattern_missing_fields(&self, pattern: &ast::RecordPat) -> Vec<(Field, Type)> {
242 self.imp.record_pattern_missing_fields(pattern)
245 pub fn to_def<T: ToDef>(&self, src: &T) -> Option<T::Def> {
246 let src = self.imp.find_file(src.syntax().clone()).with_value(src).cloned();
247 T::to_def(&self.imp, src)
250 pub fn to_module_def(&self, file: FileId) -> Option<Module> {
251 self.imp.to_module_def(file)
254 pub fn scope(&self, node: &SyntaxNode) -> SemanticsScope<'db> {
258 pub fn scope_at_offset(&self, node: &SyntaxNode, offset: TextSize) -> SemanticsScope<'db> {
259 self.imp.scope_at_offset(node, offset)
262 pub fn scope_for_def(&self, def: Trait) -> SemanticsScope<'db> {
263 self.imp.scope_for_def(def)
266 pub fn assert_contains_node(&self, node: &SyntaxNode) {
267 self.imp.assert_contains_node(node)
270 pub fn is_unsafe_method_call(&self, method_call_expr: ast::MethodCallExpr) -> bool {
271 self.imp.is_unsafe_method_call(method_call_expr)
274 pub fn is_unsafe_ref_expr(&self, ref_expr: &ast::RefExpr) -> bool {
275 self.imp.is_unsafe_ref_expr(ref_expr)
278 pub fn is_unsafe_ident_pat(&self, ident_pat: &ast::IdentPat) -> bool {
279 self.imp.is_unsafe_ident_pat(ident_pat)
283 impl<'db> SemanticsImpl<'db> {
284 fn new(db: &'db dyn HirDatabase) -> Self {
287 s2d_cache: Default::default(),
288 cache: Default::default(),
289 expansion_info_cache: Default::default(),
293 fn parse(&self, file_id: FileId) -> ast::SourceFile {
294 let tree = self.db.parse(file_id).tree();
295 self.cache(tree.syntax().clone(), file_id.into());
299 fn expand(&self, macro_call: &ast::MacroCall) -> Option<SyntaxNode> {
300 let macro_call = self.find_file(macro_call.syntax().clone()).with_value(macro_call);
301 let sa = self.analyze2(macro_call.map(|it| it.syntax()), None);
302 let file_id = sa.expand(self.db, macro_call)?;
303 let node = self.db.parse_or_expand(file_id)?;
304 self.cache(node.clone(), file_id);
308 fn speculative_expand(
310 actual_macro_call: &ast::MacroCall,
311 hypothetical_args: &ast::TokenTree,
312 token_to_map: SyntaxToken,
313 ) -> Option<(SyntaxNode, SyntaxToken)> {
315 self.find_file(actual_macro_call.syntax().clone()).with_value(actual_macro_call);
316 let sa = self.analyze2(macro_call.map(|it| it.syntax()), None);
317 let krate = sa.resolver.krate()?;
318 let macro_call_id = macro_call.as_call_id(self.db.upcast(), krate, |path| {
319 sa.resolver.resolve_path_as_macro(self.db.upcast(), &path)
321 hir_expand::db::expand_hypothetical(
329 fn descend_into_macros(&self, token: SyntaxToken) -> SyntaxToken {
330 let _p = profile::span("descend_into_macros");
331 let parent = token.parent();
332 let parent = self.find_file(parent);
333 let sa = self.analyze2(parent.as_ref(), None);
335 let token = successors(Some(parent.with_value(token)), |token| {
336 self.db.check_canceled();
337 let macro_call = token.value.ancestors().find_map(ast::MacroCall::cast)?;
338 let tt = macro_call.token_tree()?;
339 if !tt.syntax().text_range().contains_range(token.value.text_range()) {
342 let file_id = sa.expand(self.db, token.with_value(¯o_call))?;
344 .expansion_info_cache
347 .or_insert_with(|| file_id.expansion_info(self.db.upcast()))
349 .map_token_down(token.as_ref())?;
351 self.cache(find_root(&token.value.parent()), token.file_id);
361 fn descend_node_at_offset(
365 ) -> impl Iterator<Item = SyntaxNode> + '_ {
366 // Handle macro token cases
367 node.token_at_offset(offset)
368 .map(|token| self.descend_into_macros(token))
369 .map(|it| self.ancestors_with_macros(it.parent()))
373 fn original_range(&self, node: &SyntaxNode) -> FileRange {
374 let node = self.find_file(node.clone());
375 original_range(self.db, node.as_ref())
378 fn diagnostics_display_range(&self, diagnostics: &dyn Diagnostic) -> FileRange {
379 let src = diagnostics.display_source();
380 let root = self.db.parse_or_expand(src.file_id).unwrap();
381 let node = src.value.to_node(&root);
382 self.cache(root, src.file_id);
383 original_range(self.db, src.with_value(&node))
386 fn ancestors_with_macros(&self, node: SyntaxNode) -> impl Iterator<Item = SyntaxNode> + '_ {
387 let node = self.find_file(node);
388 node.ancestors_with_macros(self.db.upcast()).map(|it| it.value)
391 fn ancestors_at_offset_with_macros(
395 ) -> impl Iterator<Item = SyntaxNode> + '_ {
396 node.token_at_offset(offset)
397 .map(|token| self.ancestors_with_macros(token.parent()))
398 .kmerge_by(|node1, node2| node1.text_range().len() < node2.text_range().len())
401 fn type_of_expr(&self, expr: &ast::Expr) -> Option<Type> {
402 self.analyze(expr.syntax()).type_of_expr(self.db, &expr)
405 fn type_of_pat(&self, pat: &ast::Pat) -> Option<Type> {
406 self.analyze(pat.syntax()).type_of_pat(self.db, &pat)
409 fn type_of_self(&self, param: &ast::SelfParam) -> Option<Type> {
410 self.analyze(param.syntax()).type_of_self(self.db, ¶m)
413 fn resolve_method_call(&self, call: &ast::MethodCallExpr) -> Option<FunctionId> {
414 self.analyze(call.syntax()).resolve_method_call(self.db, call)
417 fn resolve_method_call_as_callable(&self, call: &ast::MethodCallExpr) -> Option<Callable> {
418 // FIXME: this erases Substs
419 let func = self.resolve_method_call(call)?;
420 let ty = self.db.value_ty(func.into());
421 let resolver = self.analyze(call.syntax()).resolver;
422 let ty = Type::new_with_resolver(self.db, &resolver, ty.value)?;
423 let mut res = ty.as_callable(self.db)?;
424 res.is_bound_method = true;
428 fn resolve_field(&self, field: &ast::FieldExpr) -> Option<Field> {
429 self.analyze(field.syntax()).resolve_field(self.db, field)
432 fn resolve_record_field(&self, field: &ast::RecordExprField) -> Option<(Field, Option<Local>)> {
433 self.analyze(field.syntax()).resolve_record_field(self.db, field)
436 fn resolve_record_field_pat(&self, field: &ast::RecordPatField) -> Option<Field> {
437 self.analyze(field.syntax()).resolve_record_field_pat(self.db, field)
440 fn resolve_macro_call(&self, macro_call: &ast::MacroCall) -> Option<MacroDef> {
441 let sa = self.analyze(macro_call.syntax());
442 let macro_call = self.find_file(macro_call.syntax().clone()).with_value(macro_call);
443 sa.resolve_macro_call(self.db, macro_call)
446 fn resolve_path(&self, path: &ast::Path) -> Option<PathResolution> {
447 self.analyze(path.syntax()).resolve_path(self.db, path)
450 fn resolve_extern_crate(&self, extern_crate: &ast::ExternCrate) -> Option<Crate> {
451 let krate = self.scope(extern_crate.syntax()).krate()?;
452 krate.dependencies(self.db).into_iter().find_map(|dep| {
453 if dep.name == extern_crate.name_ref()?.as_name() {
461 fn resolve_variant(&self, record_lit: ast::RecordExpr) -> Option<VariantId> {
462 self.analyze(record_lit.syntax()).resolve_variant(self.db, record_lit)
465 fn resolve_bind_pat_to_const(&self, pat: &ast::IdentPat) -> Option<ModuleDef> {
466 self.analyze(pat.syntax()).resolve_bind_pat_to_const(self.db, pat)
469 fn record_literal_missing_fields(&self, literal: &ast::RecordExpr) -> Vec<(Field, Type)> {
470 self.analyze(literal.syntax())
471 .record_literal_missing_fields(self.db, literal)
475 fn record_pattern_missing_fields(&self, pattern: &ast::RecordPat) -> Vec<(Field, Type)> {
476 self.analyze(pattern.syntax())
477 .record_pattern_missing_fields(self.db, pattern)
481 fn with_ctx<F: FnOnce(&mut SourceToDefCtx) -> T, T>(&self, f: F) -> T {
482 let mut cache = self.s2d_cache.borrow_mut();
483 let mut ctx = SourceToDefCtx { db: self.db, cache: &mut *cache };
487 fn to_module_def(&self, file: FileId) -> Option<Module> {
488 self.with_ctx(|ctx| ctx.file_to_def(file)).map(Module::from)
491 fn scope(&self, node: &SyntaxNode) -> SemanticsScope<'db> {
492 let node = self.find_file(node.clone());
493 let resolver = self.analyze2(node.as_ref(), None).resolver;
494 SemanticsScope { db: self.db, file_id: node.file_id, resolver }
497 fn scope_at_offset(&self, node: &SyntaxNode, offset: TextSize) -> SemanticsScope<'db> {
498 let node = self.find_file(node.clone());
499 let resolver = self.analyze2(node.as_ref(), Some(offset)).resolver;
500 SemanticsScope { db: self.db, file_id: node.file_id, resolver }
503 fn scope_for_def(&self, def: Trait) -> SemanticsScope<'db> {
504 let file_id = self.db.lookup_intern_trait(def.id).id.file_id;
505 let resolver = def.id.resolver(self.db.upcast());
506 SemanticsScope { db: self.db, file_id, resolver }
509 fn analyze(&self, node: &SyntaxNode) -> SourceAnalyzer {
510 let src = self.find_file(node.clone());
511 self.analyze2(src.as_ref(), None)
514 fn analyze2(&self, src: InFile<&SyntaxNode>, offset: Option<TextSize>) -> SourceAnalyzer {
515 let _p = profile::span("Semantics::analyze2");
517 let container = match self.with_ctx(|ctx| ctx.find_container(src)) {
519 None => return SourceAnalyzer::new_for_resolver(Resolver::default(), src),
522 let resolver = match container {
523 ChildContainer::DefWithBodyId(def) => {
524 return SourceAnalyzer::new_for_body(self.db, def, src, offset)
526 ChildContainer::TraitId(it) => it.resolver(self.db.upcast()),
527 ChildContainer::ImplId(it) => it.resolver(self.db.upcast()),
528 ChildContainer::ModuleId(it) => it.resolver(self.db.upcast()),
529 ChildContainer::EnumId(it) => it.resolver(self.db.upcast()),
530 ChildContainer::VariantId(it) => it.resolver(self.db.upcast()),
531 ChildContainer::TypeAliasId(it) => it.resolver(self.db.upcast()),
532 ChildContainer::GenericDefId(it) => it.resolver(self.db.upcast()),
534 SourceAnalyzer::new_for_resolver(resolver, src)
537 fn cache(&self, root_node: SyntaxNode, file_id: HirFileId) {
538 assert!(root_node.parent().is_none());
539 let mut cache = self.cache.borrow_mut();
540 let prev = cache.insert(root_node, file_id);
541 assert!(prev == None || prev == Some(file_id))
544 fn assert_contains_node(&self, node: &SyntaxNode) {
545 self.find_file(node.clone());
548 fn lookup(&self, root_node: &SyntaxNode) -> Option<HirFileId> {
549 let cache = self.cache.borrow();
550 cache.get(root_node).copied()
553 fn find_file(&self, node: SyntaxNode) -> InFile<SyntaxNode> {
554 let root_node = find_root(&node);
555 let file_id = self.lookup(&root_node).unwrap_or_else(|| {
557 "\n\nFailed to lookup {:?} in this Semantics.\n\
558 Make sure to use only query nodes, derived from this instance of Semantics.\n\
560 known nodes: {}\n\n",
566 .map(|it| format!("{:?}", it))
571 InFile::new(file_id, node)
574 pub fn is_unsafe_method_call(&self, method_call_expr: ast::MethodCallExpr) -> bool {
578 let field_expr = if let ast::Expr::FieldExpr(field_expr) = expr {
583 let ty = self.type_of_expr(&field_expr.expr()?)?;
584 if !ty.is_packed(self.db) {
588 let func = self.resolve_method_call(&method_call_expr).map(Function::from)?;
589 let is_unsafe = func.has_self_param(self.db)
590 && matches!(func.params(self.db).first(), Some(TypeRef::Reference(..)));
596 pub fn is_unsafe_ref_expr(&self, ref_expr: &ast::RefExpr) -> bool {
600 let field_expr = match expr {
601 ast::Expr::FieldExpr(field_expr) => field_expr,
604 let expr = field_expr.expr()?;
605 self.type_of_expr(&expr)
607 // Binding a reference to a packed type is possibly unsafe.
608 .map(|ty| ty.is_packed(self.db))
611 // FIXME This needs layout computation to be correct. It will highlight
612 // more than it should with the current implementation.
615 pub fn is_unsafe_ident_pat(&self, ident_pat: &ast::IdentPat) -> bool {
616 if !ident_pat.ref_token().is_some() {
624 // `IdentPat` can live under `RecordPat` directly under `RecordPatField` or
625 // `RecordPatFieldList`. `RecordPatField` also lives under `RecordPatFieldList`,
626 // so this tries to lookup the `IdentPat` anywhere along that structure to the
627 // `RecordPat` so we can get the containing type.
628 let record_pat = ast::RecordPatField::cast(parent.clone())
629 .and_then(|record_pat| record_pat.syntax().parent())
630 .or_else(|| Some(parent.clone()))
632 ast::RecordPatFieldList::cast(parent)?
635 .and_then(ast::RecordPat::cast)
638 // If this doesn't match a `RecordPat`, fallback to a `LetStmt` to see if
639 // this is initialized from a `FieldExpr`.
640 if let Some(record_pat) = record_pat {
641 self.type_of_pat(&ast::Pat::RecordPat(record_pat))
642 } else if let Some(let_stmt) = ast::LetStmt::cast(parent) {
643 let field_expr = match let_stmt.initializer()? {
644 ast::Expr::FieldExpr(field_expr) => field_expr,
648 self.type_of_expr(&field_expr.expr()?)
653 // Binding a reference to a packed type is possibly unsafe.
654 .map(|ty| ty.is_packed(self.db))
659 pub trait ToDef: AstNode + Clone {
662 fn to_def(sema: &SemanticsImpl, src: InFile<Self>) -> Option<Self::Def>;
665 macro_rules! to_def_impls {
666 ($(($def:path, $ast:path, $meth:ident)),* ,) => {$(
667 impl ToDef for $ast {
669 fn to_def(sema: &SemanticsImpl, src: InFile<Self>) -> Option<Self::Def> {
670 sema.with_ctx(|ctx| ctx.$meth(src)).map(<$def>::from)
677 (crate::Module, ast::Module, module_to_def),
678 (crate::Struct, ast::Struct, struct_to_def),
679 (crate::Enum, ast::Enum, enum_to_def),
680 (crate::Union, ast::Union, union_to_def),
681 (crate::Trait, ast::Trait, trait_to_def),
682 (crate::ImplDef, ast::Impl, impl_to_def),
683 (crate::TypeAlias, ast::TypeAlias, type_alias_to_def),
684 (crate::Const, ast::Const, const_to_def),
685 (crate::Static, ast::Static, static_to_def),
686 (crate::Function, ast::Fn, fn_to_def),
687 (crate::Field, ast::RecordField, record_field_to_def),
688 (crate::Field, ast::TupleField, tuple_field_to_def),
689 (crate::EnumVariant, ast::Variant, enum_variant_to_def),
690 (crate::TypeParam, ast::TypeParam, type_param_to_def),
691 (crate::MacroDef, ast::MacroCall, macro_call_to_def), // this one is dubious, not all calls are macros
692 (crate::Local, ast::IdentPat, bind_pat_to_def),
695 fn find_root(node: &SyntaxNode) -> SyntaxNode {
696 node.ancestors().last().unwrap()
700 pub struct SemanticsScope<'a> {
701 pub db: &'a dyn HirDatabase,
706 impl<'a> SemanticsScope<'a> {
707 pub fn module(&self) -> Option<Module> {
708 Some(Module { id: self.resolver.module()? })
711 pub fn krate(&self) -> Option<Crate> {
712 Some(Crate { id: self.resolver.krate()? })
715 /// Note: `FxHashSet<TraitId>` should be treated as an opaque type, passed into `Type
716 // FIXME: rename to visible_traits to not repeat scope?
717 pub fn traits_in_scope(&self) -> FxHashSet<TraitId> {
718 let resolver = &self.resolver;
719 resolver.traits_in_scope(self.db.upcast())
722 pub fn process_all_names(&self, f: &mut dyn FnMut(Name, ScopeDef)) {
723 let resolver = &self.resolver;
725 resolver.process_all_names(self.db.upcast(), &mut |name, def| {
726 let def = match def {
727 resolver::ScopeDef::PerNs(it) => {
728 let items = ScopeDef::all_items(it);
730 f(name.clone(), item);
734 resolver::ScopeDef::ImplSelfType(it) => ScopeDef::ImplSelfType(it.into()),
735 resolver::ScopeDef::AdtSelfType(it) => ScopeDef::AdtSelfType(it.into()),
736 resolver::ScopeDef::GenericParam(id) => ScopeDef::GenericParam(TypeParam { id }),
737 resolver::ScopeDef::Local(pat_id) => {
738 let parent = resolver.body_owner().unwrap().into();
739 ScopeDef::Local(Local { parent, pat_id })
746 /// Resolve a path as-if it was written at the given scope. This is
747 /// necessary a heuristic, as it doesn't take hygiene into account.
748 pub fn speculative_resolve(&self, path: &ast::Path) -> Option<PathResolution> {
749 let hygiene = Hygiene::new(self.db.upcast(), self.file_id);
750 let path = Path::from_src(path.clone(), &hygiene)?;
751 resolve_hir_path(self.db, &self.resolver, &path)
755 // FIXME: Change `HasSource` trait to work with `Semantics` and remove this?
756 pub fn original_range(db: &dyn HirDatabase, node: InFile<&SyntaxNode>) -> FileRange {
757 if let Some(range) = original_range_opt(db, node) {
758 let original_file = range.file_id.original_file(db.upcast());
759 if range.file_id == original_file.into() {
760 return FileRange { file_id: original_file, range: range.value };
763 log::error!("Fail to mapping up more for {:?}", range);
764 return FileRange { file_id: range.file_id.original_file(db.upcast()), range: range.value };
767 // Fall back to whole macro call
768 if let Some(expansion) = node.file_id.expansion_info(db.upcast()) {
769 if let Some(call_node) = expansion.call_node() {
771 file_id: call_node.file_id.original_file(db.upcast()),
772 range: call_node.value.text_range(),
777 FileRange { file_id: node.file_id.original_file(db.upcast()), range: node.value.text_range() }
780 fn original_range_opt(
781 db: &dyn HirDatabase,
782 node: InFile<&SyntaxNode>,
783 ) -> Option<InFile<TextRange>> {
784 let expansion = node.file_id.expansion_info(db.upcast())?;
786 // the input node has only one token ?
787 let single = skip_trivia_token(node.value.first_token()?, Direction::Next)?
788 == skip_trivia_token(node.value.last_token()?, Direction::Prev)?;
790 Some(node.value.descendants().find_map(|it| {
791 let first = skip_trivia_token(it.first_token()?, Direction::Next)?;
792 let first = ascend_call_token(db, &expansion, node.with_value(first))?;
794 let last = skip_trivia_token(it.last_token()?, Direction::Prev)?;
795 let last = ascend_call_token(db, &expansion, node.with_value(last))?;
797 if (!single && first == last) || (first.file_id != last.file_id) {
801 Some(first.with_value(first.value.text_range().cover(last.value.text_range())))
805 fn ascend_call_token(
806 db: &dyn HirDatabase,
807 expansion: &ExpansionInfo,
808 token: InFile<SyntaxToken>,
809 ) -> Option<InFile<SyntaxToken>> {
810 let (mapped, origin) = expansion.map_token_up(token.as_ref())?;
811 if origin != Origin::Call {
814 if let Some(info) = mapped.file_id.expansion_info(db.upcast()) {
815 return ascend_call_token(db, &info, mapped);