Skip to content

Commit 2fa0d4e

Browse files
When descending into macros in search, first check if there is a need to - i.e. if we are inside a macro call
This avoids the need to analyze the file when we are not inside a macro call. This is especially important for the optimization in the next commit(s), as there the common case will be to descent into macros but then not analyze.
1 parent 9b72445 commit 2fa0d4e

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

crates/hir/src/semantics.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,11 @@ use hir_expand::{
2323
builtin::{BuiltinFnLikeExpander, EagerExpander},
2424
db::ExpandDatabase,
2525
files::InRealFile,
26+
inert_attr_macro::find_builtin_attr_idx,
2627
name::AsName,
2728
FileRange, InMacroFile, MacroCallId, MacroFileId, MacroFileIdExt,
2829
};
30+
use intern::Symbol;
2931
use itertools::Itertools;
3032
use rustc_hash::{FxHashMap, FxHashSet};
3133
use smallvec::{smallvec, SmallVec};
@@ -674,6 +676,35 @@ impl<'db> SemanticsImpl<'db> {
674676
res
675677
}
676678

679+
fn is_inside_macro_call(token: &SyntaxToken) -> bool {
680+
token.parent_ancestors().any(|ancestor| {
681+
if ast::MacroCall::can_cast(ancestor.kind()) {
682+
return true;
683+
}
684+
// Check if it is an item (only items can have macro attributes) that has a non-builtin attribute.
685+
let Some(item) = ast::Item::cast(ancestor) else { return false };
686+
item.attrs().any(|attr| {
687+
let Some(meta) = attr.meta() else { return false };
688+
let Some(path) = meta.path() else { return false };
689+
let Some(attr_name) = path.as_single_name_ref() else { return true };
690+
let attr_name = attr_name.text();
691+
let attr_name = attr_name.as_str();
692+
attr_name == "derive" || find_builtin_attr_idx(&Symbol::intern(attr_name)).is_none()
693+
})
694+
})
695+
}
696+
697+
pub fn descend_into_macros_exact_if_in_macro(
698+
&self,
699+
token: SyntaxToken,
700+
) -> SmallVec<[SyntaxToken; 1]> {
701+
if Self::is_inside_macro_call(&token) {
702+
self.descend_into_macros_exact(token)
703+
} else {
704+
smallvec![token]
705+
}
706+
}
707+
677708
pub fn descend_into_macros_cb(
678709
&self,
679710
token: SyntaxToken,

0 commit comments

Comments
 (0)