Skip to content

Commit cfe488c

Browse files
committed
fix: Fix IDE layer not resolving some macro calls
1 parent f67ee9c commit cfe488c

File tree

5 files changed

+62
-35
lines changed

5 files changed

+62
-35
lines changed

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/src/semantics.rs

Lines changed: 5 additions & 27 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!(
@@ -1097,16 +1090,7 @@ impl<'db> SemanticsImpl<'db> {
10971090
let file_id = match m_cache.get(&mcall) {
10981091
Some(&it) => it,
10991092
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())?;
1093+
let it = ast::MacroCall::to_def(self, mcall.as_ref())?;
11101094
m_cache.insert(mcall, it);
11111095
it
11121096
}
@@ -1562,14 +1546,8 @@ impl<'db> SemanticsImpl<'db> {
15621546
}
15631547

15641548
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
1549+
let file_id = self.to_def(macro_call)?;
1550+
self.db.parse_macro_expansion(file_id).value.1.matched_arm
15731551
}
15741552

15751553
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/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)