diff options
Diffstat (limited to 'crates/hir/src/semantics.rs')
-rw-r--r-- | crates/hir/src/semantics.rs | 46 |
1 files changed, 7 insertions, 39 deletions
diff --git a/crates/hir/src/semantics.rs b/crates/hir/src/semantics.rs index d8beac98a..c693176fa 100644 --- a/crates/hir/src/semantics.rs +++ b/crates/hir/src/semantics.rs | |||
@@ -6,7 +6,7 @@ use std::{cell::RefCell, fmt, iter::successors}; | |||
6 | 6 | ||
7 | use base_db::{FileId, FileRange}; | 7 | use base_db::{FileId, FileRange}; |
8 | use hir_def::{ | 8 | use hir_def::{ |
9 | resolver::{self, HasResolver, Resolver}, | 9 | resolver::{self, HasResolver, Resolver, TypeNs}, |
10 | AsMacroCall, FunctionId, TraitId, VariantId, | 10 | AsMacroCall, FunctionId, TraitId, VariantId, |
11 | }; | 11 | }; |
12 | use hir_expand::{hygiene::Hygiene, name::AsName, ExpansionInfo}; | 12 | use hir_expand::{hygiene::Hygiene, name::AsName, ExpansionInfo}; |
@@ -22,12 +22,11 @@ use crate::{ | |||
22 | db::HirDatabase, | 22 | db::HirDatabase, |
23 | diagnostics::Diagnostic, | 23 | diagnostics::Diagnostic, |
24 | semantics::source_to_def::{ChildContainer, SourceToDefCache, SourceToDefCtx}, | 24 | semantics::source_to_def::{ChildContainer, SourceToDefCache, SourceToDefCtx}, |
25 | source_analyzer::{resolve_hir_path, resolve_hir_path_qualifier, SourceAnalyzer}, | 25 | source_analyzer::{resolve_hir_path, SourceAnalyzer}, |
26 | AssocItem, Callable, Crate, Field, Function, HirFileId, ImplDef, InFile, Local, MacroDef, | 26 | AssocItem, Callable, Crate, Field, Function, HirFileId, ImplDef, InFile, Local, MacroDef, |
27 | Module, ModuleDef, Name, Origin, Path, ScopeDef, Trait, Type, TypeAlias, TypeParam, TypeRef, | 27 | Module, ModuleDef, Name, Origin, Path, ScopeDef, Trait, Type, TypeAlias, TypeParam, TypeRef, |
28 | VariantDef, | 28 | VariantDef, |
29 | }; | 29 | }; |
30 | use resolver::TypeNs; | ||
31 | 30 | ||
32 | #[derive(Debug, Clone, PartialEq, Eq)] | 31 | #[derive(Debug, Clone, PartialEq, Eq)] |
33 | pub enum PathResolution { | 32 | pub enum PathResolution { |
@@ -112,14 +111,13 @@ impl<'db, DB: HirDatabase> Semantics<'db, DB> { | |||
112 | pub fn expand(&self, macro_call: &ast::MacroCall) -> Option<SyntaxNode> { | 111 | pub fn expand(&self, macro_call: &ast::MacroCall) -> Option<SyntaxNode> { |
113 | self.imp.expand(macro_call) | 112 | self.imp.expand(macro_call) |
114 | } | 113 | } |
115 | 114 | pub fn speculative_expand( | |
116 | pub fn expand_hypothetical( | ||
117 | &self, | 115 | &self, |
118 | actual_macro_call: &ast::MacroCall, | 116 | actual_macro_call: &ast::MacroCall, |
119 | hypothetical_args: &ast::TokenTree, | 117 | hypothetical_args: &ast::TokenTree, |
120 | token_to_map: SyntaxToken, | 118 | token_to_map: SyntaxToken, |
121 | ) -> Option<(SyntaxNode, SyntaxToken)> { | 119 | ) -> Option<(SyntaxNode, SyntaxToken)> { |
122 | self.imp.expand_hypothetical(actual_macro_call, hypothetical_args, token_to_map) | 120 | self.imp.speculative_expand(actual_macro_call, hypothetical_args, token_to_map) |
123 | } | 121 | } |
124 | 122 | ||
125 | pub fn descend_into_macros(&self, token: SyntaxToken) -> SyntaxToken { | 123 | pub fn descend_into_macros(&self, token: SyntaxToken) -> SyntaxToken { |
@@ -229,10 +227,6 @@ impl<'db, DB: HirDatabase> Semantics<'db, DB> { | |||
229 | self.imp.resolve_variant(record_lit).map(VariantDef::from) | 227 | self.imp.resolve_variant(record_lit).map(VariantDef::from) |
230 | } | 228 | } |
231 | 229 | ||
232 | pub fn lower_path(&self, path: &ast::Path) -> Option<Path> { | ||
233 | self.imp.lower_path(path) | ||
234 | } | ||
235 | |||
236 | pub fn resolve_bind_pat_to_const(&self, pat: &ast::IdentPat) -> Option<ModuleDef> { | 230 | pub fn resolve_bind_pat_to_const(&self, pat: &ast::IdentPat) -> Option<ModuleDef> { |
237 | self.imp.resolve_bind_pat_to_const(pat) | 231 | self.imp.resolve_bind_pat_to_const(pat) |
238 | } | 232 | } |
@@ -311,7 +305,7 @@ impl<'db> SemanticsImpl<'db> { | |||
311 | Some(node) | 305 | Some(node) |
312 | } | 306 | } |
313 | 307 | ||
314 | fn expand_hypothetical( | 308 | fn speculative_expand( |
315 | &self, | 309 | &self, |
316 | actual_macro_call: &ast::MacroCall, | 310 | actual_macro_call: &ast::MacroCall, |
317 | hypothetical_args: &ast::TokenTree, | 311 | hypothetical_args: &ast::TokenTree, |
@@ -468,11 +462,6 @@ impl<'db> SemanticsImpl<'db> { | |||
468 | self.analyze(record_lit.syntax()).resolve_variant(self.db, record_lit) | 462 | self.analyze(record_lit.syntax()).resolve_variant(self.db, record_lit) |
469 | } | 463 | } |
470 | 464 | ||
471 | fn lower_path(&self, path: &ast::Path) -> Option<Path> { | ||
472 | let src = self.find_file(path.syntax().clone()); | ||
473 | Path::from_src(path.clone(), &Hygiene::new(self.db.upcast(), src.file_id.into())) | ||
474 | } | ||
475 | |||
476 | fn resolve_bind_pat_to_const(&self, pat: &ast::IdentPat) -> Option<ModuleDef> { | 465 | fn resolve_bind_pat_to_const(&self, pat: &ast::IdentPat) -> Option<ModuleDef> { |
477 | self.analyze(pat.syntax()).resolve_bind_pat_to_const(self.db, pat) | 466 | self.analyze(pat.syntax()).resolve_bind_pat_to_const(self.db, pat) |
478 | } | 467 | } |
@@ -756,31 +745,10 @@ impl<'a> SemanticsScope<'a> { | |||
756 | 745 | ||
757 | /// Resolve a path as-if it was written at the given scope. This is | 746 | /// Resolve a path as-if it was written at the given scope. This is |
758 | /// necessary a heuristic, as it doesn't take hygiene into account. | 747 | /// necessary a heuristic, as it doesn't take hygiene into account. |
759 | pub fn resolve_hypothetical(&self, path: &ast::Path) -> Option<PathResolution> { | 748 | pub fn speculative_resolve(&self, path: &ast::Path) -> Option<PathResolution> { |
760 | let hygiene = Hygiene::new(self.db.upcast(), self.file_id); | 749 | let hygiene = Hygiene::new(self.db.upcast(), self.file_id); |
761 | let path = Path::from_src(path.clone(), &hygiene)?; | 750 | let path = Path::from_src(path.clone(), &hygiene)?; |
762 | self.resolve_hir_path(&path) | 751 | resolve_hir_path(self.db, &self.resolver, &path) |
763 | } | ||
764 | |||
765 | pub fn resolve_hir_path(&self, path: &Path) -> Option<PathResolution> { | ||
766 | resolve_hir_path(self.db, &self.resolver, path) | ||
767 | } | ||
768 | |||
769 | /// Resolves a path where we know it is a qualifier of another path. | ||
770 | /// | ||
771 | /// For example, if we have: | ||
772 | /// ``` | ||
773 | /// mod my { | ||
774 | /// pub mod foo { | ||
775 | /// struct Bar; | ||
776 | /// } | ||
777 | /// | ||
778 | /// pub fn foo() {} | ||
779 | /// } | ||
780 | /// ``` | ||
781 | /// then we know that `foo` in `my::foo::Bar` refers to the module, not the function. | ||
782 | pub fn resolve_hir_path_qualifier(&self, path: &Path) -> Option<PathResolution> { | ||
783 | resolve_hir_path_qualifier(self.db, &self.resolver, path) | ||
784 | } | 752 | } |
785 | } | 753 | } |
786 | 754 | ||