Skip to content

Commit fac7b0e

Browse files
committed
Don't guess macro expansion crate
1 parent d8a5d39 commit fac7b0e

File tree

9 files changed

+89
-62
lines changed

9 files changed

+89
-62
lines changed

crates/ra_hir/src/semantics.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,9 @@ impl<'db, DB: HirDatabase> Semantics<'db, DB> {
122122
let macro_call =
123123
self.find_file(actual_macro_call.syntax().clone()).with_value(actual_macro_call);
124124
let sa = self.analyze2(macro_call.map(|it| it.syntax()), None);
125+
let krate = sa.resolver.krate()?;
125126
let macro_call_id = macro_call
126-
.as_call_id(self.db, |path| sa.resolver.resolve_path_as_macro(self.db, &path))?;
127+
.as_call_id(self.db, krate, |path| sa.resolver.resolve_path_as_macro(self.db, &path))?;
127128
hir_expand::db::expand_hypothetical(self.db, macro_call_id, hypothetical_args, token_to_map)
128129
}
129130

crates/ra_hir/src/source_analyzer.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,8 @@ impl SourceAnalyzer {
307307
db: &dyn HirDatabase,
308308
macro_call: InFile<&ast::MacroCall>,
309309
) -> Option<HirFileId> {
310-
let macro_call_id = macro_call.as_call_id(db.upcast(), |path| {
310+
let krate = self.resolver.krate()?;
311+
let macro_call_id = macro_call.as_call_id(db.upcast(), krate, |path| {
311312
self.resolver.resolve_path_as_macro(db.upcast(), &path)
312313
})?;
313314
Some(macro_call_id.as_file())

crates/ra_hir_def/src/body.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ impl Expander {
9797

9898
let macro_call = InFile::new(self.current_file_id, &macro_call);
9999

100-
if let Some(call_id) = macro_call.as_call_id(db, |path| {
100+
if let Some(call_id) = macro_call.as_call_id(db, self.crate_def_map.krate, |path| {
101101
if let Some(local_scope) = local_scope {
102102
if let Some(def) = path.as_ident().and_then(|n| local_scope.get_legacy_macro(n)) {
103103
return Some(def);

crates/ra_hir_def/src/lib.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,7 @@ pub trait AsMacroCall {
417417
fn as_call_id(
418418
&self,
419419
db: &dyn db::DefDatabase,
420+
krate: CrateId,
420421
resolver: impl Fn(path::ModPath) -> Option<MacroDefId>,
421422
) -> Option<MacroCallId>;
422423
}
@@ -425,13 +426,14 @@ impl AsMacroCall for InFile<&ast::MacroCall> {
425426
fn as_call_id(
426427
&self,
427428
db: &dyn db::DefDatabase,
429+
krate: CrateId,
428430
resolver: impl Fn(path::ModPath) -> Option<MacroDefId>,
429431
) -> Option<MacroCallId> {
430432
let ast_id = AstId::new(self.file_id, db.ast_id_map(self.file_id).ast_id(self.value));
431433
let h = Hygiene::new(db.upcast(), self.file_id);
432434
let path = path::ModPath::from_src(self.value.path()?, &h)?;
433435

434-
AstIdWithPath::new(ast_id.file_id, ast_id.value, path).as_call_id(db, resolver)
436+
AstIdWithPath::new(ast_id.file_id, ast_id.value, path).as_call_id(db, krate, resolver)
435437
}
436438
}
437439

@@ -452,6 +454,7 @@ impl AsMacroCall for AstIdWithPath<ast::MacroCall> {
452454
fn as_call_id(
453455
&self,
454456
db: &dyn db::DefDatabase,
457+
krate: CrateId,
455458
resolver: impl Fn(path::ModPath) -> Option<MacroDefId>,
456459
) -> Option<MacroCallId> {
457460
let def: MacroDefId = resolver(self.path.clone())?;
@@ -461,13 +464,13 @@ impl AsMacroCall for AstIdWithPath<ast::MacroCall> {
461464
let hygiene = Hygiene::new(db.upcast(), self.ast_id.file_id);
462465

463466
Some(
464-
expand_eager_macro(db.upcast(), macro_call, def, &|path: ast::Path| {
467+
expand_eager_macro(db.upcast(), krate, macro_call, def, &|path: ast::Path| {
465468
resolver(path::ModPath::from_src(path, &hygiene)?)
466469
})?
467470
.into(),
468471
)
469472
} else {
470-
Some(def.as_lazy_macro(db.upcast(), MacroCallKind::FnLike(self.ast_id)).into())
473+
Some(def.as_lazy_macro(db.upcast(), krate, MacroCallKind::FnLike(self.ast_id)).into())
471474
}
472475
}
473476
}
@@ -476,12 +479,14 @@ impl AsMacroCall for AstIdWithPath<ast::ModuleItem> {
476479
fn as_call_id(
477480
&self,
478481
db: &dyn db::DefDatabase,
482+
krate: CrateId,
479483
resolver: impl Fn(path::ModPath) -> Option<MacroDefId>,
480484
) -> Option<MacroCallId> {
481485
let def = resolver(self.path.clone())?;
482486
Some(
483487
def.as_lazy_macro(
484488
db.upcast(),
489+
krate,
485490
MacroCallKind::Attr(self.ast_id, self.path.segments.last()?.to_string()),
486491
)
487492
.into(),

crates/ra_hir_def/src/nameres/collector.rs

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -571,16 +571,18 @@ impl DefCollector<'_> {
571571
return false;
572572
}
573573

574-
if let Some(call_id) = directive.ast_id.as_call_id(self.db, |path| {
575-
let resolved_res = self.def_map.resolve_path_fp_with_macro(
576-
self.db,
577-
ResolveMode::Other,
578-
directive.module_id,
579-
&path,
580-
BuiltinShadowMode::Module,
581-
);
582-
resolved_res.resolved_def.take_macros()
583-
}) {
574+
if let Some(call_id) =
575+
directive.ast_id.as_call_id(self.db, self.def_map.krate, |path| {
576+
let resolved_res = self.def_map.resolve_path_fp_with_macro(
577+
self.db,
578+
ResolveMode::Other,
579+
directive.module_id,
580+
&path,
581+
BuiltinShadowMode::Module,
582+
);
583+
resolved_res.resolved_def.take_macros()
584+
})
585+
{
584586
resolved.push((directive.module_id, call_id, directive.depth));
585587
res = ReachedFixedPoint::No;
586588
return false;
@@ -589,9 +591,10 @@ impl DefCollector<'_> {
589591
true
590592
});
591593
attribute_macros.retain(|directive| {
592-
if let Some(call_id) = directive
593-
.ast_id
594-
.as_call_id(self.db, |path| self.resolve_attribute_macro(&directive, &path))
594+
if let Some(call_id) =
595+
directive.ast_id.as_call_id(self.db, self.def_map.krate, |path| {
596+
self.resolve_attribute_macro(&directive, &path)
597+
})
595598
{
596599
resolved.push((directive.module_id, call_id, 0));
597600
res = ReachedFixedPoint::No;
@@ -957,11 +960,13 @@ impl ModCollector<'_, '_> {
957960
}
958961

959962
// Case 2: try to resolve in legacy scope and expand macro_rules
960-
if let Some(macro_call_id) = ast_id.as_call_id(self.def_collector.db, |path| {
961-
path.as_ident().and_then(|name| {
962-
self.def_collector.def_map[self.module_id].scope.get_legacy_macro(&name)
963+
if let Some(macro_call_id) =
964+
ast_id.as_call_id(self.def_collector.db, self.def_collector.def_map.krate, |path| {
965+
path.as_ident().and_then(|name| {
966+
self.def_collector.def_map[self.module_id].scope.get_legacy_macro(&name)
967+
})
963968
})
964-
}) {
969+
{
965970
self.def_collector.unexpanded_macros.push(MacroDirective {
966971
module_id: self.module_id,
967972
ast_id,

crates/ra_hir_expand/src/builtin_derive.rs

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ use ra_syntax::{
88
match_ast,
99
};
1010

11-
use crate::db::AstDatabase;
12-
use crate::{guess_crate, name, quote, LazyMacroId, MacroCallId, MacroDefId, MacroDefKind};
11+
use crate::{db::AstDatabase, name, quote, LazyMacroId, MacroDefId, MacroDefKind};
1312

1413
macro_rules! register_builtin {
1514
( $($trait:ident => $expand:ident),* ) => {
@@ -156,17 +155,8 @@ fn expand_simple_derive(
156155
fn find_builtin_crate(db: &dyn AstDatabase, id: LazyMacroId) -> tt::TokenTree {
157156
// FIXME: make hygiene works for builtin derive macro
158157
// such that $crate can be used here.
159-
160-
let m: MacroCallId = id.into();
161-
let file_id = m.as_file().original_file(db);
162158
let cg = db.crate_graph();
163-
let krate = match guess_crate(db, file_id) {
164-
Some(krate) => krate,
165-
None => {
166-
let tt = quote! { core };
167-
return tt.token_trees[0].clone();
168-
}
169-
};
159+
let krate = db.lookup_intern_macro(id).krate;
170160

171161
// XXX
172162
// All crates except core itself should have a dependency on core,
@@ -263,10 +253,12 @@ fn partial_ord_expand(
263253

264254
#[cfg(test)]
265255
mod tests {
266-
use super::*;
267-
use crate::{test_db::TestDB, AstId, MacroCallId, MacroCallKind, MacroCallLoc};
268256
use name::{known, Name};
269-
use ra_db::{fixture::WithFixture, SourceDatabase};
257+
use ra_db::{fixture::WithFixture, CrateId, SourceDatabase};
258+
259+
use crate::{test_db::TestDB, AstId, MacroCallId, MacroCallKind, MacroCallLoc};
260+
261+
use super::*;
270262

271263
fn expand_builtin_derive(s: &str, name: Name) -> String {
272264
let def = find_builtin_derive(&name).unwrap();
@@ -290,7 +282,11 @@ mod tests {
290282

291283
let attr_id = AstId::new(file_id.into(), ast_id_map.ast_id(&items[0]));
292284

293-
let loc = MacroCallLoc { def, kind: MacroCallKind::Attr(attr_id, name.to_string()) };
285+
let loc = MacroCallLoc {
286+
def,
287+
krate: CrateId(0),
288+
kind: MacroCallKind::Attr(attr_id, name.to_string()),
289+
};
294290

295291
let id: MacroCallId = db.intern_macro(loc).into();
296292
let parsed = db.parse_or_expand(id.as_file()).unwrap();

crates/ra_hir_expand/src/builtin_macro.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
//! Builtin macro
2-
use crate::db::AstDatabase;
32
use crate::{
4-
ast::{self, AstToken, HasStringValue},
5-
name, AstId, CrateId, MacroDefId, MacroDefKind, TextSize,
3+
db::AstDatabase, name, quote, AstId, CrateId, EagerMacroId, LazyMacroId, MacroCallId,
4+
MacroDefId, MacroDefKind, TextSize,
65
};
76

8-
use crate::{guess_crate, quote, EagerMacroId, LazyMacroId, MacroCallId};
97
use either::Either;
108
use mbe::parse_to_token_tree;
119
use ra_db::FileId;
1210
use ra_parser::FragmentKind;
11+
use ra_syntax::ast::{self, AstToken, HasStringValue};
1312

1413
macro_rules! register_builtin {
1514
( LAZY: $(($name:ident, $kind: ident) => $expand:ident),* , EAGER: $(($e_name:ident, $e_kind: ident) => $e_expand:ident),* ) => {
@@ -333,9 +332,7 @@ fn include_expand(
333332
}
334333

335334
fn get_env_inner(db: &dyn AstDatabase, arg_id: EagerMacroId, key: &str) -> Option<String> {
336-
let call_id: MacroCallId = arg_id.into();
337-
let original_file = call_id.as_file().original_file(db);
338-
let krate = guess_crate(db, original_file)?;
335+
let krate = db.lookup_intern_eager_expansion(arg_id).krate;
339336
db.crate_graph()[krate].env.get(key)
340337
}
341338

@@ -394,6 +391,7 @@ mod tests {
394391

395392
let expander = find_by_name(&macro_calls[0].name().unwrap().as_name()).unwrap();
396393

394+
let krate = CrateId(0);
397395
let file_id = match expander {
398396
Either::Left(expander) => {
399397
// the first one should be a macro_rules
@@ -406,6 +404,7 @@ mod tests {
406404

407405
let loc = MacroCallLoc {
408406
def,
407+
krate,
409408
kind: MacroCallKind::FnLike(AstId::new(
410409
file_id.into(),
411410
ast_id_map.ast_id(&macro_calls[1]),
@@ -418,7 +417,7 @@ mod tests {
418417
Either::Right(expander) => {
419418
// the first one should be a macro_rules
420419
let def = MacroDefId {
421-
krate: Some(CrateId(0)),
420+
krate: Some(krate),
422421
ast_id: Some(AstId::new(file_id.into(), ast_id_map.ast_id(&macro_calls[0]))),
423422
kind: MacroDefKind::BuiltInEager(expander),
424423
local_inner: false,
@@ -432,6 +431,7 @@ mod tests {
432431
def,
433432
fragment: FragmentKind::Expr,
434433
subtree: Arc::new(parsed_args.clone()),
434+
krate,
435435
file_id: file_id.into(),
436436
}
437437
});
@@ -441,6 +441,7 @@ mod tests {
441441
def,
442442
fragment,
443443
subtree: Arc::new(subtree),
444+
krate,
444445
file_id: file_id.into(),
445446
};
446447

crates/ra_hir_expand/src/eager.rs

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,14 @@ use crate::{
2525
EagerCallLoc, EagerMacroId, InFile, MacroCallId, MacroCallKind, MacroDefId, MacroDefKind,
2626
};
2727

28+
use ra_db::CrateId;
2829
use ra_parser::FragmentKind;
2930
use ra_syntax::{algo::SyntaxRewriter, SyntaxNode};
3031
use std::sync::Arc;
3132

3233
pub fn expand_eager_macro(
3334
db: &dyn AstDatabase,
35+
krate: CrateId,
3436
macro_call: InFile<ast::MacroCall>,
3537
def: MacroDefId,
3638
resolver: &dyn Fn(ast::Path) -> Option<MacroDefId>,
@@ -47,6 +49,7 @@ pub fn expand_eager_macro(
4749
def,
4850
fragment: FragmentKind::Expr,
4951
subtree: Arc::new(parsed_args.clone()),
52+
krate,
5053
file_id: macro_call.file_id,
5154
}
5255
});
@@ -56,14 +59,20 @@ pub fn expand_eager_macro(
5659
let result = eager_macro_recur(
5760
db,
5861
InFile::new(arg_file_id.as_file(), parsed_args.syntax_node()),
62+
krate,
5963
resolver,
6064
)?;
6165
let subtree = to_subtree(&result)?;
6266

6367
if let MacroDefKind::BuiltInEager(eager) = def.kind {
6468
let (subtree, fragment) = eager.expand(db, arg_id, &subtree).ok()?;
65-
let eager =
66-
EagerCallLoc { def, fragment, subtree: Arc::new(subtree), file_id: macro_call.file_id };
69+
let eager = EagerCallLoc {
70+
def,
71+
fragment,
72+
subtree: Arc::new(subtree),
73+
krate,
74+
file_id: macro_call.file_id,
75+
};
6776

6877
Some(db.intern_eager_expansion(eager))
6978
} else {
@@ -81,18 +90,20 @@ fn lazy_expand(
8190
db: &dyn AstDatabase,
8291
def: &MacroDefId,
8392
macro_call: InFile<ast::MacroCall>,
93+
krate: CrateId,
8494
) -> Option<InFile<SyntaxNode>> {
8595
let ast_id = db.ast_id_map(macro_call.file_id).ast_id(&macro_call.value);
8696

8797
let id: MacroCallId =
88-
def.as_lazy_macro(db, MacroCallKind::FnLike(macro_call.with_value(ast_id))).into();
98+
def.as_lazy_macro(db, krate, MacroCallKind::FnLike(macro_call.with_value(ast_id))).into();
8999

90100
db.parse_or_expand(id.as_file()).map(|node| InFile::new(id.as_file(), node))
91101
}
92102

93103
fn eager_macro_recur(
94104
db: &dyn AstDatabase,
95105
curr: InFile<SyntaxNode>,
106+
krate: CrateId,
96107
macro_resolver: &dyn Fn(ast::Path) -> Option<MacroDefId>,
97108
) -> Option<SyntaxNode> {
98109
let original = curr.value.clone();
@@ -105,18 +116,23 @@ fn eager_macro_recur(
105116
let def: MacroDefId = macro_resolver(child.path()?)?;
106117
let insert = match def.kind {
107118
MacroDefKind::BuiltInEager(_) => {
108-
let id: MacroCallId =
109-
expand_eager_macro(db, curr.with_value(child.clone()), def, macro_resolver)?
110-
.into();
119+
let id: MacroCallId = expand_eager_macro(
120+
db,
121+
krate,
122+
curr.with_value(child.clone()),
123+
def,
124+
macro_resolver,
125+
)?
126+
.into();
111127
db.parse_or_expand(id.as_file())?
112128
}
113129
MacroDefKind::Declarative
114130
| MacroDefKind::BuiltIn(_)
115131
| MacroDefKind::BuiltInDerive(_)
116132
| MacroDefKind::CustomDerive(_) => {
117-
let expanded = lazy_expand(db, &def, curr.with_value(child.clone()))?;
133+
let expanded = lazy_expand(db, &def, curr.with_value(child.clone()), krate)?;
118134
// replace macro inside
119-
eager_macro_recur(db, expanded, macro_resolver)?
135+
eager_macro_recur(db, expanded, krate, macro_resolver)?
120136
}
121137
};
122138

0 commit comments

Comments
 (0)