aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--crates/ra_hir/src/expr.rs5
-rw-r--r--crates/ra_hir/src/nameres.rs4
-rw-r--r--crates/ra_hir/src/resolve.rs17
-rw-r--r--crates/ra_hir/src/source_binder.rs25
-rw-r--r--crates/ra_ide_api/src/goto_definition.rs2
5 files changed, 15 insertions, 38 deletions
diff --git a/crates/ra_hir/src/expr.rs b/crates/ra_hir/src/expr.rs
index 480eaf171..a2b5db1a1 100644
--- a/crates/ra_hir/src/expr.rs
+++ b/crates/ra_hir/src/expr.rs
@@ -10,7 +10,7 @@ use ra_syntax::{
10}; 10};
11 11
12use crate::{ 12use crate::{
13 Path, Name, HirDatabase, Resolver,DefWithBody, Either, HirFileId, 13 Path, Name, HirDatabase, Resolver,DefWithBody, Either, HirFileId, MacroCallLoc,
14 name::AsName, 14 name::AsName,
15 type_ref::{Mutability, TypeRef}, 15 type_ref::{Mutability, TypeRef},
16}; 16};
@@ -828,7 +828,8 @@ where
828 .ast_id(e) 828 .ast_id(e)
829 .with_file_id(self.current_file_id); 829 .with_file_id(self.current_file_id);
830 830
831 if let Some(call_id) = self.resolver.resolve_macro_call(self.db, path, ast_id) { 831 if let Some(def) = self.resolver.resolve_macro_call(path) {
832 let call_id = MacroCallLoc { def, ast_id }.id(self.db);
832 if let Some(tt) = self.db.macro_expand(call_id).ok() { 833 if let Some(tt) = self.db.macro_expand(call_id).ok() {
833 if let Some(expr) = mbe::token_tree_to_expr(&tt).ok() { 834 if let Some(expr) = mbe::token_tree_to_expr(&tt).ok() {
834 log::debug!("macro expansion {}", expr.syntax().debug_dump()); 835 log::debug!("macro expansion {}", expr.syntax().debug_dump());
diff --git a/crates/ra_hir/src/nameres.rs b/crates/ra_hir/src/nameres.rs
index a450d7b84..0290b3474 100644
--- a/crates/ra_hir/src/nameres.rs
+++ b/crates/ra_hir/src/nameres.rs
@@ -272,8 +272,8 @@ impl CrateDefMap {
272 (res.resolved_def, res.segment_index) 272 (res.resolved_def, res.segment_index)
273 } 273 }
274 274
275 pub(crate) fn find_macro(&self, name: &Name) -> Option<&MacroDefId> { 275 pub(crate) fn find_macro(&self, name: &Name) -> Option<MacroDefId> {
276 self.public_macros.get(name).or(self.local_macros.get(name)) 276 self.public_macros.get(name).or(self.local_macros.get(name)).map(|it| *it)
277 } 277 }
278 278
279 // Returns Yes if we are sure that additions to `ItemMap` wouldn't change 279 // Returns Yes if we are sure that additions to `ItemMap` wouldn't change
diff --git a/crates/ra_hir/src/resolve.rs b/crates/ra_hir/src/resolve.rs
index 2fb219908..3874e28bf 100644
--- a/crates/ra_hir/src/resolve.rs
+++ b/crates/ra_hir/src/resolve.rs
@@ -1,16 +1,12 @@
1//! Name resolution. 1//! Name resolution.
2use std::sync::Arc; 2use std::sync::Arc;
3 3
4use ra_syntax::ast;
5
6use rustc_hash::{FxHashMap, FxHashSet}; 4use rustc_hash::{FxHashMap, FxHashSet};
7 5
8use crate::{ 6use crate::{
9 ModuleDef, Trait, 7 ModuleDef, Trait,
10 code_model_api::Crate, 8 code_model_api::Crate,
11 MacroCallId, 9 MacroDefId,
12 MacroCallLoc,
13 AstId,
14 db::HirDatabase, 10 db::HirDatabase,
15 name::{Name, KnownName}, 11 name::{Name, KnownName},
16 nameres::{PerNs, CrateDefMap, CrateModuleId}, 12 nameres::{PerNs, CrateDefMap, CrateModuleId},
@@ -134,16 +130,9 @@ impl Resolver {
134 resolution 130 resolution
135 } 131 }
136 132
137 pub fn resolve_macro_call( 133 pub(crate) fn resolve_macro_call(&self, path: Option<Path>) -> Option<MacroDefId> {
138 &self,
139 db: &impl HirDatabase,
140 path: Option<Path>,
141 ast_id: AstId<ast::MacroCall>,
142 ) -> Option<MacroCallId> {
143 let name = path.and_then(|path| path.expand_macro_expr()).unwrap_or_else(Name::missing); 134 let name = path.and_then(|path| path.expand_macro_expr()).unwrap_or_else(Name::missing);
144 let def_id = self.module().and_then(|(module, _)| module.find_macro(&name))?; 135 self.module()?.0.find_macro(&name)
145 let call_loc = MacroCallLoc { def: *def_id, ast_id }.id(db);
146 Some(call_loc)
147 } 136 }
148 137
149 /// Returns the resolved path segments 138 /// Returns the resolved path segments
diff --git a/crates/ra_hir/src/source_binder.rs b/crates/ra_hir/src/source_binder.rs
index 31bf13425..179faebfb 100644
--- a/crates/ra_hir/src/source_binder.rs
+++ b/crates/ra_hir/src/source_binder.rs
@@ -20,7 +20,7 @@ use crate::{
20 HirDatabase, Function, Struct, Enum, Const, Static, Either, DefWithBody, PerNs, Name, 20 HirDatabase, Function, Struct, Enum, Const, Static, Either, DefWithBody, PerNs, Name,
21 AsName, Module, HirFileId, Crate, Trait, Resolver, Ty,Path, 21 AsName, Module, HirFileId, Crate, Trait, Resolver, Ty,Path,
22 expr::{BodySourceMap, scope::{ScopeId, ExprScopes}}, 22 expr::{BodySourceMap, scope::{ScopeId, ExprScopes}},
23 ids::{LocationCtx,MacroCallId}, 23 ids::{LocationCtx, MacroDefId},
24 docs::{docs_from_ast,Documentation}, 24 docs::{docs_from_ast,Documentation},
25 expr, AstId, 25 expr, AstId,
26}; 26};
@@ -191,13 +191,12 @@ pub enum PathResolution {
191 191
192#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 192#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
193pub struct MacroByExampleDef { 193pub struct MacroByExampleDef {
194 pub(crate) id: MacroCallId, 194 pub(crate) id: MacroDefId,
195} 195}
196 196
197impl MacroByExampleDef { 197impl MacroByExampleDef {
198 pub fn source(&self, db: &impl HirDatabase) -> (HirFileId, TreeArc<ast::MacroCall>) { 198 pub fn source(&self, db: &impl HirDatabase) -> (HirFileId, TreeArc<ast::MacroCall>) {
199 let loc = self.id.loc(db); 199 (self.id.0.file_id(), self.id.0.to_node(db))
200 (self.id.into(), loc.def.0.to_node(db))
201 } 200 }
202} 201}
203 202
@@ -284,21 +283,9 @@ impl SourceAnalyzer {
284 self.infer.as_ref()?.field_resolution(expr_id) 283 self.infer.as_ref()?.field_resolution(expr_id)
285 } 284 }
286 285
287 pub fn resolve_macro_call( 286 pub fn resolve_macro_call(&self, macro_call: &ast::MacroCall) -> Option<MacroByExampleDef> {
288 &self, 287 let id = self.resolver.resolve_macro_call(macro_call.path().and_then(Path::from_ast))?;
289 db: &impl HirDatabase, 288 Some(MacroByExampleDef { id })
290 file_id: FileId,
291 macro_call: &ast::MacroCall,
292 ) -> Option<MacroByExampleDef> {
293 let hir_id = file_id.into();
294 let ast_id = db.ast_id_map(hir_id).ast_id(macro_call).with_file_id(hir_id);
295 let call_id = self.resolver.resolve_macro_call(
296 db,
297 macro_call.path().and_then(Path::from_ast),
298 ast_id,
299 );
300
301 call_id.map(|id| MacroByExampleDef { id })
302 } 289 }
303 290
304 pub fn resolve_hir_path( 291 pub fn resolve_hir_path(
diff --git a/crates/ra_ide_api/src/goto_definition.rs b/crates/ra_ide_api/src/goto_definition.rs
index 533c229fe..adae29e9c 100644
--- a/crates/ra_ide_api/src/goto_definition.rs
+++ b/crates/ra_ide_api/src/goto_definition.rs
@@ -69,7 +69,7 @@ pub(crate) fn reference_definition(
69 .and_then(ast::MacroCall::cast) 69 .and_then(ast::MacroCall::cast)
70 { 70 {
71 tested_by!(goto_definition_works_for_macros); 71 tested_by!(goto_definition_works_for_macros);
72 if let Some(macro_call) = analyzer.resolve_macro_call(db, file_id, macro_call) { 72 if let Some(macro_call) = analyzer.resolve_macro_call(macro_call) {
73 return Exact(NavigationTarget::from_macro_def(db, macro_call)); 73 return Exact(NavigationTarget::from_macro_def(db, macro_call));
74 } 74 }
75 } 75 }