Skip to content

Commit b22614f

Browse files
bors[bot]matklad
andcommitted
Merge #1268
1268: simplify r=matklad a=matklad Co-authored-by: Aleksey Kladov <[email protected]>
2 parents 0f57564 + 9cba67b commit b22614f

File tree

5 files changed

+15
-38
lines changed

5 files changed

+15
-38
lines changed

crates/ra_hir/src/expr.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use ra_syntax::{
1010
};
1111

1212
use crate::{
13-
Path, Name, HirDatabase, Resolver,DefWithBody, Either, HirFileId,
13+
Path, Name, HirDatabase, Resolver,DefWithBody, Either, HirFileId, MacroCallLoc,
1414
name::AsName,
1515
type_ref::{Mutability, TypeRef},
1616
};
@@ -828,7 +828,8 @@ where
828828
.ast_id(e)
829829
.with_file_id(self.current_file_id);
830830

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);
832833
if let Some(tt) = self.db.macro_expand(call_id).ok() {
833834
if let Some(expr) = mbe::token_tree_to_expr(&tt).ok() {
834835
log::debug!("macro expansion {}", expr.syntax().debug_dump());

crates/ra_hir/src/nameres.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -272,8 +272,8 @@ impl CrateDefMap {
272272
(res.resolved_def, res.segment_index)
273273
}
274274

275-
pub(crate) fn find_macro(&self, name: &Name) -> Option<&MacroDefId> {
276-
self.public_macros.get(name).or(self.local_macros.get(name))
275+
pub(crate) fn find_macro(&self, name: &Name) -> Option<MacroDefId> {
276+
self.public_macros.get(name).or(self.local_macros.get(name)).map(|it| *it)
277277
}
278278

279279
// Returns Yes if we are sure that additions to `ItemMap` wouldn't change

crates/ra_hir/src/resolve.rs

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
11
//! Name resolution.
22
use std::sync::Arc;
33

4-
use ra_syntax::ast;
5-
64
use rustc_hash::{FxHashMap, FxHashSet};
75

86
use crate::{
97
ModuleDef, Trait,
108
code_model_api::Crate,
11-
MacroCallId,
12-
MacroCallLoc,
13-
AstId,
9+
MacroDefId,
1410
db::HirDatabase,
1511
name::{Name, KnownName},
1612
nameres::{PerNs, CrateDefMap, CrateModuleId},
@@ -134,16 +130,9 @@ impl Resolver {
134130
resolution
135131
}
136132

137-
pub fn resolve_macro_call(
138-
&self,
139-
db: &impl HirDatabase,
140-
path: Option<Path>,
141-
ast_id: AstId<ast::MacroCall>,
142-
) -> Option<MacroCallId> {
133+
pub(crate) fn resolve_macro_call(&self, path: Option<Path>) -> Option<MacroDefId> {
143134
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))?;
145-
let call_loc = MacroCallLoc { def: *def_id, ast_id }.id(db);
146-
Some(call_loc)
135+
self.module()?.0.find_macro(&name)
147136
}
148137

149138
/// Returns the resolved path segments

crates/ra_hir/src/source_binder.rs

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use crate::{
2020
HirDatabase, Function, Struct, Enum, Const, Static, Either, DefWithBody, PerNs, Name,
2121
AsName, Module, HirFileId, Crate, Trait, Resolver, Ty,Path,
2222
expr::{BodySourceMap, scope::{ScopeId, ExprScopes}},
23-
ids::{LocationCtx,MacroCallId},
23+
ids::{LocationCtx, MacroDefId},
2424
docs::{docs_from_ast,Documentation},
2525
expr, AstId,
2626
};
@@ -191,13 +191,12 @@ pub enum PathResolution {
191191

192192
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
193193
pub struct MacroByExampleDef {
194-
pub(crate) id: MacroCallId,
194+
pub(crate) id: MacroDefId,
195195
}
196196

197197
impl MacroByExampleDef {
198198
pub fn source(&self, db: &impl HirDatabase) -> (HirFileId, TreeArc<ast::MacroCall>) {
199-
let loc = self.id.loc(db);
200-
(self.id.into(), loc.def.0.to_node(db))
199+
(self.id.0.file_id(), self.id.0.to_node(db))
201200
}
202201
}
203202

@@ -284,21 +283,9 @@ impl SourceAnalyzer {
284283
self.infer.as_ref()?.field_resolution(expr_id)
285284
}
286285

287-
pub fn resolve_macro_call(
288-
&self,
289-
db: &impl HirDatabase,
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 })
286+
pub fn resolve_macro_call(&self, macro_call: &ast::MacroCall) -> Option<MacroByExampleDef> {
287+
let id = self.resolver.resolve_macro_call(macro_call.path().and_then(Path::from_ast))?;
288+
Some(MacroByExampleDef { id })
302289
}
303290

304291
pub fn resolve_hir_path(

crates/ra_ide_api/src/goto_definition.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ pub(crate) fn reference_definition(
6969
.and_then(ast::MacroCall::cast)
7070
{
7171
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) {
7373
return Exact(NavigationTarget::from_macro_def(db, macro_call));
7474
}
7575
}

0 commit comments

Comments
 (0)