Skip to content

Commit 0766029

Browse files
authored
Merge pull request rust-lang#19879 from Veykril/push-mqykxnqtktuw
fix: Fix IDE layer not resolving some macro calls
2 parents 70b911d + 8b886b4 commit 0766029

File tree

9 files changed

+65
-84
lines changed

9 files changed

+65
-84
lines changed

src/tools/rust-analyzer/crates/hir-def/src/db.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,6 @@ fn macro_def(db: &dyn DefDatabase, id: MacroId) -> MacroDefId {
422422
let makro = &item_tree[loc.id.value];
423423
MacroDefId {
424424
krate: loc.container.krate,
425-
block: loc.container.block.map(|block| salsa::plumbing::AsId::as_id(&block)),
426425
kind: kind(loc.expander, loc.id.file_id(), makro.ast_id.upcast()),
427426
local_inner: false,
428427
allow_internal_unsafe: loc.allow_internal_unsafe,
@@ -436,7 +435,6 @@ fn macro_def(db: &dyn DefDatabase, id: MacroId) -> MacroDefId {
436435
let makro = &item_tree[loc.id.value];
437436
MacroDefId {
438437
krate: loc.container.krate,
439-
block: loc.container.block.map(|block| salsa::plumbing::AsId::as_id(&block)),
440438
kind: kind(loc.expander, loc.id.file_id(), makro.ast_id.upcast()),
441439
local_inner: loc.flags.contains(MacroRulesLocFlags::LOCAL_INNER),
442440
allow_internal_unsafe: loc
@@ -452,7 +450,6 @@ fn macro_def(db: &dyn DefDatabase, id: MacroId) -> MacroDefId {
452450
let makro = &item_tree[loc.id.value];
453451
MacroDefId {
454452
krate: loc.container.krate,
455-
block: None,
456453
kind: MacroDefKind::ProcMacro(
457454
InFile::new(loc.id.file_id(), makro.ast_id),
458455
loc.expander,

src/tools/rust-analyzer/crates/hir-def/src/nameres/assoc.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ impl TraitItems {
7575
})
7676
}
7777

78-
pub fn attribute_calls(&self) -> impl Iterator<Item = (AstId<ast::Item>, MacroCallId)> + '_ {
78+
pub fn macro_calls(&self) -> impl Iterator<Item = (AstId<ast::Item>, MacroCallId)> + '_ {
7979
self.macro_calls.iter().flat_map(|it| it.iter()).copied()
8080
}
8181
}
@@ -109,7 +109,7 @@ impl ImplItems {
109109
(Arc::new(ImplItems { items, macro_calls }), DefDiagnostics::new(diagnostics))
110110
}
111111

112-
pub fn attribute_calls(&self) -> impl Iterator<Item = (AstId<ast::Item>, MacroCallId)> + '_ {
112+
pub fn macro_calls(&self) -> impl Iterator<Item = (AstId<ast::Item>, MacroCallId)> + '_ {
113113
self.macro_calls.iter().flat_map(|it| it.iter()).copied()
114114
}
115115
}

src/tools/rust-analyzer/crates/hir-def/src/resolver.rs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -696,15 +696,6 @@ impl<'db> Resolver<'db> {
696696
&def_map[local_id].scope
697697
}
698698

699-
pub fn item_scopes(&self) -> impl Iterator<Item = &ItemScope> {
700-
self.scopes()
701-
.filter_map(move |scope| match scope {
702-
Scope::BlockScope(m) => Some(&m.def_map[m.module_id].scope),
703-
_ => None,
704-
})
705-
.chain(std::iter::once(&self.module_scope.def_map[self.module_scope.module_id].scope))
706-
}
707-
708699
pub fn krate(&self) -> Crate {
709700
self.module_scope.def_map.krate()
710701
}

src/tools/rust-analyzer/crates/hir-expand/src/lib.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -258,8 +258,6 @@ pub struct MacroCallLoc {
258258
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
259259
pub struct MacroDefId {
260260
pub krate: Crate,
261-
// FIXME: In `hir-expand` we can't refer to `BlockId`.
262-
pub block: Option<salsa::Id>,
263261
pub edition: Edition,
264262
pub kind: MacroDefKind,
265263
pub local_inner: bool,

src/tools/rust-analyzer/crates/hir/src/semantics.rs

Lines changed: 6 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -408,11 +408,7 @@ impl<'db> SemanticsImpl<'db> {
408408
}
409409

410410
pub fn expand_macro_call(&self, macro_call: &ast::MacroCall) -> Option<InFile<SyntaxNode>> {
411-
let sa = self.analyze_no_infer(macro_call.syntax())?;
412-
413-
let macro_call = InFile::new(sa.file_id, macro_call);
414-
let file_id = sa.expansion(self.db, macro_call)?;
415-
411+
let file_id = self.to_def(macro_call)?;
416412
let node = self.parse_or_expand(file_id.into());
417413
Some(InFile::new(file_id.into(), node))
418414
}
@@ -434,10 +430,7 @@ impl<'db> SemanticsImpl<'db> {
434430
&self,
435431
macro_call: &ast::MacroCall,
436432
) -> Option<ExpandResult<SyntaxNode>> {
437-
let sa = self.analyze_no_infer(macro_call.syntax())?;
438-
439-
let macro_call = InFile::new(sa.file_id, macro_call);
440-
let file_id = sa.expansion(self.db, macro_call)?;
433+
let file_id = self.to_def(macro_call)?;
441434
let macro_call = self.db.lookup_intern_macro_call(file_id);
442435

443436
let skip = matches!(
@@ -574,9 +567,7 @@ impl<'db> SemanticsImpl<'db> {
574567
speculative_args: &ast::TokenTree,
575568
token_to_map: SyntaxToken,
576569
) -> Option<(SyntaxNode, Vec<(SyntaxToken, u8)>)> {
577-
let analyzer = self.analyze_no_infer(actual_macro_call.syntax())?;
578-
let macro_call = InFile::new(analyzer.file_id, actual_macro_call);
579-
let macro_file = analyzer.expansion(self.db, macro_call)?;
570+
let macro_file = self.to_def(actual_macro_call)?;
580571
hir_expand::db::expand_speculative(
581572
self.db,
582573
macro_file,
@@ -1097,16 +1088,7 @@ impl<'db> SemanticsImpl<'db> {
10971088
let file_id = match m_cache.get(&mcall) {
10981089
Some(&it) => it,
10991090
None => {
1100-
let it = token
1101-
.parent()
1102-
.and_then(|parent| {
1103-
self.analyze_impl(
1104-
InFile::new(expansion, &parent),
1105-
None,
1106-
false,
1107-
)
1108-
})?
1109-
.expansion(self.db, mcall.as_ref())?;
1091+
let it = ast::MacroCall::to_def(self, mcall.as_ref())?;
11101092
m_cache.insert(mcall, it);
11111093
it
11121094
}
@@ -1551,9 +1533,6 @@ impl<'db> SemanticsImpl<'db> {
15511533
.and_then(|call| macro_call_to_macro_id(ctx, call))
15521534
.map(Into::into)
15531535
})
1554-
.or_else(|| {
1555-
self.analyze(macro_call.value.syntax())?.resolve_macro_call(self.db, macro_call)
1556-
})
15571536
}
15581537

15591538
pub fn is_proc_macro_call(&self, macro_call: InFile<&ast::MacroCall>) -> bool {
@@ -1562,14 +1541,8 @@ impl<'db> SemanticsImpl<'db> {
15621541
}
15631542

15641543
pub fn resolve_macro_call_arm(&self, macro_call: &ast::MacroCall) -> Option<u32> {
1565-
let sa = self.analyze(macro_call.syntax())?;
1566-
self.db
1567-
.parse_macro_expansion(
1568-
sa.expansion(self.db, self.wrap_node_infile(macro_call.clone()).as_ref())?,
1569-
)
1570-
.value
1571-
.1
1572-
.matched_arm
1544+
let file_id = self.to_def(macro_call)?;
1545+
self.db.parse_macro_expansion(file_id).value.1.matched_arm
15731546
}
15741547

15751548
pub fn get_unsafe_ops(&self, def: DefWithBody) -> FxHashSet<ExprOrPatSource> {

src/tools/rust-analyzer/crates/hir/src/semantics/child_by_source.rs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,14 @@ impl ChildBySource for TraitId {
3636
fn child_by_source_to(&self, db: &dyn DefDatabase, res: &mut DynMap, file_id: HirFileId) {
3737
let data = db.trait_items(*self);
3838

39-
data.attribute_calls().filter(|(ast_id, _)| ast_id.file_id == file_id).for_each(
39+
data.macro_calls().filter(|(ast_id, _)| ast_id.file_id == file_id).for_each(
4040
|(ast_id, call_id)| {
41-
res[keys::ATTR_MACRO_CALL].insert(ast_id.to_ptr(db), call_id);
41+
let ptr = ast_id.to_ptr(db);
42+
if let Some(ptr) = ptr.cast::<ast::MacroCall>() {
43+
res[keys::MACRO_CALL].insert(ptr, call_id);
44+
} else {
45+
res[keys::ATTR_MACRO_CALL].insert(ptr, call_id);
46+
}
4247
},
4348
);
4449
data.items.iter().for_each(|&(_, item)| {
@@ -50,10 +55,14 @@ impl ChildBySource for TraitId {
5055
impl ChildBySource for ImplId {
5156
fn child_by_source_to(&self, db: &dyn DefDatabase, res: &mut DynMap, file_id: HirFileId) {
5257
let data = db.impl_items(*self);
53-
// FIXME: Macro calls
54-
data.attribute_calls().filter(|(ast_id, _)| ast_id.file_id == file_id).for_each(
58+
data.macro_calls().filter(|(ast_id, _)| ast_id.file_id == file_id).for_each(
5559
|(ast_id, call_id)| {
56-
res[keys::ATTR_MACRO_CALL].insert(ast_id.to_ptr(db), call_id);
60+
let ptr = ast_id.to_ptr(db);
61+
if let Some(ptr) = ptr.cast::<ast::MacroCall>() {
62+
res[keys::MACRO_CALL].insert(ptr, call_id);
63+
} else {
64+
res[keys::ATTR_MACRO_CALL].insert(ptr, call_id);
65+
}
5766
},
5867
);
5968
data.items.iter().for_each(|&(_, item)| {

src/tools/rust-analyzer/crates/hir/src/source_analyzer.rs

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,12 @@ use hir_def::{
2626
},
2727
hir::{BindingId, Expr, ExprId, ExprOrPatId, Pat},
2828
lang_item::LangItem,
29-
nameres::{MacroSubNs, block_def_map, crate_def_map},
29+
nameres::MacroSubNs,
3030
resolver::{HasResolver, Resolver, TypeNs, ValueNs, resolver_for_scope},
3131
type_ref::{Mutability, TypeRefId},
3232
};
3333
use hir_expand::{
34-
HirFileId, InFile, MacroCallId,
34+
HirFileId, InFile,
3535
mod_path::{ModPath, PathKind, path},
3636
name::{AsName, Name},
3737
};
@@ -218,18 +218,6 @@ impl<'db> SourceAnalyzer<'db> {
218218
})
219219
}
220220

221-
pub(crate) fn expansion(
222-
&self,
223-
db: &dyn HirDatabase,
224-
macro_call: InFile<&ast::MacroCall>,
225-
) -> Option<MacroCallId> {
226-
self.store_sm().and_then(|sm| sm.expansion(macro_call)).or_else(|| {
227-
let ast_id_map = db.ast_id_map(macro_call.file_id);
228-
let call_ast_id = macro_call.with_value(ast_id_map.ast_id(macro_call.value));
229-
self.resolver.item_scopes().find_map(|scope| scope.macro_invoc(call_ast_id))
230-
})
231-
}
232-
233221
fn trait_environment(&self, db: &'db dyn HirDatabase) -> Arc<TraitEnvironment> {
234222
self.body_().map(|(def, ..)| def).map_or_else(
235223
|| TraitEnvironment::empty(self.resolver.krate()),
@@ -753,21 +741,6 @@ impl<'db> SourceAnalyzer<'db> {
753741
))
754742
}
755743

756-
pub(crate) fn resolve_macro_call(
757-
&self,
758-
db: &dyn HirDatabase,
759-
macro_call: InFile<&ast::MacroCall>,
760-
) -> Option<Macro> {
761-
self.expansion(db, macro_call).and_then(|it| {
762-
let def = it.lookup(db).def;
763-
let def_map = match def.block {
764-
Some(block) => block_def_map(db, base_db::salsa::plumbing::FromId::from_id(block)),
765-
None => crate_def_map(db, def.krate),
766-
};
767-
def_map.macro_def_to_macro_id.get(&def.kind.erased_ast_id()).map(|it| (*it).into())
768-
})
769-
}
770-
771744
pub(crate) fn resolve_bind_pat_to_const(
772745
&self,
773746
db: &'db dyn HirDatabase,

src/tools/rust-analyzer/crates/ide/src/expand_macro.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -719,4 +719,44 @@ __log!(written:%; "Test"$0);
719719
"#]],
720720
);
721721
}
722+
723+
#[test]
724+
fn assoc_call() {
725+
check(
726+
r#"
727+
macro_rules! mac {
728+
() => { fn assoc() {} }
729+
}
730+
impl () {
731+
mac$0!();
732+
}
733+
"#,
734+
expect![[r#"
735+
mac!
736+
fn assoc(){}"#]],
737+
);
738+
}
739+
740+
#[test]
741+
fn eager() {
742+
check(
743+
r#"
744+
//- minicore: concat
745+
macro_rules! my_concat {
746+
($head:expr, $($tail:tt)*) => { concat!($head, $($tail)*) };
747+
}
748+
749+
750+
fn test() {
751+
_ = my_concat!(
752+
conc$0at!("<", ">"),
753+
"hi",
754+
);
755+
}
756+
"#,
757+
expect![[r#"
758+
my_concat!
759+
"<>hi""#]],
760+
);
761+
}
722762
}

src/tools/rust-analyzer/crates/rust-analyzer/src/cli/analysis_stats.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1023,7 +1023,7 @@ impl flags::AnalysisStats {
10231023
percentage(num_pats_partially_unknown, num_pats),
10241024
num_pat_type_mismatches
10251025
);
1026-
eprintln!(" panics: {}", panics);
1026+
eprintln!(" panics: {panics}");
10271027
eprintln!("{:<20} {}", "Inference:", inference_time);
10281028
report_metric("unknown type", num_exprs_unknown, "#");
10291029
report_metric("type mismatches", num_expr_type_mismatches, "#");

0 commit comments

Comments
 (0)