From 9664c57e60ec5662b3e8b063324d9ab7879d5570 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 13 Aug 2020 23:52:14 +0200 Subject: Make hygiene private to hir --- crates/hir/src/code_model.rs | 7 +++++++ crates/hir/src/lib.rs | 8 ++++++-- crates/hir/src/semantics.rs | 16 +++++++++++++--- 3 files changed, 26 insertions(+), 5 deletions(-) (limited to 'crates/hir/src') diff --git a/crates/hir/src/code_model.rs b/crates/hir/src/code_model.rs index 8ffb9e99b..5dc3ae3b1 100644 --- a/crates/hir/src/code_model.rs +++ b/crates/hir/src/code_model.rs @@ -883,6 +883,13 @@ where } impl AssocItem { + pub fn name(self, db: &dyn HirDatabase) -> Option { + match self { + AssocItem::Function(it) => Some(it.name(db)), + AssocItem::Const(it) => it.name(db), + AssocItem::TypeAlias(it) => Some(it.name(db)), + } + } pub fn module(self, db: &dyn HirDatabase) -> Module { match self { AssocItem::Function(f) => f.module(db), diff --git a/crates/hir/src/lib.rs b/crates/hir/src/lib.rs index 24a0f6b4b..4ae2bd085 100644 --- a/crates/hir/src/lib.rs +++ b/crates/hir/src/lib.rs @@ -52,8 +52,12 @@ pub use hir_def::{ type_ref::{Mutability, TypeRef}, }; pub use hir_expand::{ - hygiene::Hygiene, name::Name, HirFileId, InFile, MacroCallId, MacroCallLoc, - MacroDefId, /* FIXME */ + name::Name, HirFileId, InFile, MacroCallId, MacroCallLoc, /* FIXME */ MacroDefId, MacroFile, Origin, }; pub use hir_ty::display::HirDisplay; + +// These are negative re-exports: pub using these names is forbidden, they +// should remain private to hir internals. +#[allow(unused)] +use hir_expand::hygiene::Hygiene; diff --git a/crates/hir/src/semantics.rs b/crates/hir/src/semantics.rs index 1467d825d..d8beac98a 100644 --- a/crates/hir/src/semantics.rs +++ b/crates/hir/src/semantics.rs @@ -502,18 +502,19 @@ impl<'db> SemanticsImpl<'db> { fn scope(&self, node: &SyntaxNode) -> SemanticsScope<'db> { let node = self.find_file(node.clone()); let resolver = self.analyze2(node.as_ref(), None).resolver; - SemanticsScope { db: self.db, resolver } + SemanticsScope { db: self.db, file_id: node.file_id, resolver } } fn scope_at_offset(&self, node: &SyntaxNode, offset: TextSize) -> SemanticsScope<'db> { let node = self.find_file(node.clone()); let resolver = self.analyze2(node.as_ref(), Some(offset)).resolver; - SemanticsScope { db: self.db, resolver } + SemanticsScope { db: self.db, file_id: node.file_id, resolver } } fn scope_for_def(&self, def: Trait) -> SemanticsScope<'db> { + let file_id = self.db.lookup_intern_trait(def.id).id.file_id; let resolver = def.id.resolver(self.db.upcast()); - SemanticsScope { db: self.db, resolver } + SemanticsScope { db: self.db, file_id, resolver } } fn analyze(&self, node: &SyntaxNode) -> SourceAnalyzer { @@ -709,6 +710,7 @@ fn find_root(node: &SyntaxNode) -> SyntaxNode { #[derive(Debug)] pub struct SemanticsScope<'a> { pub db: &'a dyn HirDatabase, + file_id: HirFileId, resolver: Resolver, } @@ -752,6 +754,14 @@ impl<'a> SemanticsScope<'a> { }) } + /// Resolve a path as-if it was written at the given scope. This is + /// necessary a heuristic, as it doesn't take hygiene into account. + pub fn resolve_hypothetical(&self, path: &ast::Path) -> Option { + let hygiene = Hygiene::new(self.db.upcast(), self.file_id); + let path = Path::from_src(path.clone(), &hygiene)?; + self.resolve_hir_path(&path) + } + pub fn resolve_hir_path(&self, path: &Path) -> Option { resolve_hir_path(self.db, &self.resolver, path) } -- cgit v1.2.3