fix: Fix expand macro recursively not working correctly for nested macro calls

This commit is contained in:
Lukas Wirth 2025-09-05 08:25:28 +02:00
parent 14872a5332
commit bd57ea0871

View File

@ -1,5 +1,5 @@
use hir::db::ExpandDatabase;
use hir::{ExpandResult, InFile, InRealFile, Semantics};
use hir::{ExpandResult, InFile, Semantics};
use ide_db::{
FileId, RootDatabase, base_db::Crate, helpers::pick_best_token,
syntax_helpers::prettify_macro_expansion,
@ -87,14 +87,15 @@ pub(crate) fn expand_macro(db: &RootDatabase, position: FilePosition) -> Option<
return derive;
}
let mut anc = sema
.descend_token_into_include_expansion(InRealFile::new(file_id, tok))
.value
.parent_ancestors();
let syntax_token = sema.descend_into_macros_exact(tok);
'tokens: for syntax_token in syntax_token {
let mut anc = syntax_token.parent_ancestors();
let mut span_map = SpanMap::empty();
let mut error = String::new();
let (name, expanded, kind) = loop {
let node = anc.next()?;
let Some(node) = anc.next() else {
continue 'tokens;
};
if let Some(item) = ast::Item::cast(node.clone())
&& let Some(def) = sema.resolve_attr_macro_call(&item)
@ -132,7 +133,9 @@ pub(crate) fn expand_macro(db: &RootDatabase, position: FilePosition) -> Option<
if !error.is_empty() {
expansion.insert_str(0, &format!("Expansion had errors:{error}\n\n"));
}
Some(ExpandedMacro { name, expansion })
return Some(ExpandedMacro { name, expansion });
}
None
}
fn expand_macro_recur(
@ -752,8 +755,8 @@ fn test() {
}
"#,
expect![[r#"
my_concat!
"<>hi""#]],
concat!
"<>""#]],
);
}