Skip to content

Commit bb9ae88

Browse files
When descending into macros, 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 fa00326 commit bb9ae88

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

crates/hir/src/semantics.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,11 @@ use hir_expand::{
2222
builtin::{BuiltinFnLikeExpander, EagerExpander},
2323
db::ExpandDatabase,
2424
files::InRealFile,
25+
inert_attr_macro::find_builtin_attr_idx,
2526
name::AsName,
2627
FileRange, InMacroFile, MacroCallId, MacroFileId, MacroFileIdExt,
2728
};
29+
use intern::Symbol;
2830
use itertools::Itertools;
2931
use rustc_hash::{FxHashMap, FxHashSet};
3032
use smallvec::{smallvec, SmallVec};
@@ -666,6 +668,36 @@ impl<'db> SemanticsImpl<'db> {
666668
res
667669
}
668670

671+
fn is_inside_macro_call(token: &SyntaxToken) -> bool {
672+
token.parent_ancestors().any(|ancestor| {
673+
if ast::MacroCall::can_cast(ancestor.kind()) {
674+
return true;
675+
}
676+
// Check if it is an item (only items can have macro attributes) that has a non-builtin attribute.
677+
let Some(item) = ast::Item::cast(ancestor) else { return false };
678+
item.attrs().any(|attr| {
679+
let Some(meta) = attr.meta() else { return false };
680+
let Some(path) = meta.path() else { return false };
681+
let Some(attr_name) = path.as_single_name_ref() else { return true };
682+
let attr_name = attr_name.text();
683+
let attr_name = attr_name.as_str();
684+
attr_name == "derive" || find_builtin_attr_idx(&Symbol::intern(attr_name)).is_none()
685+
})
686+
})
687+
}
688+
689+
pub fn descend_into_macros_if_in_macro(
690+
&self,
691+
mode: DescendPreference,
692+
token: SyntaxToken,
693+
) -> SmallVec<[SyntaxToken; 1]> {
694+
if Self::is_inside_macro_call(&token) {
695+
self.descend_into_macros(mode, token)
696+
} else {
697+
smallvec![token]
698+
}
699+
}
700+
669701
/// Descend the token into its macro call if it is part of one, returning the tokens in the
670702
/// expansion that it is associated with.
671703
pub fn descend_into_macros(
@@ -678,6 +710,7 @@ impl<'db> SemanticsImpl<'db> {
678710
SameKind(SyntaxKind),
679711
None,
680712
}
713+
681714
let fetch_kind = |token: &SyntaxToken| match token.parent() {
682715
Some(node) => match node.kind() {
683716
kind @ (SyntaxKind::NAME | SyntaxKind::NAME_REF) => kind,
@@ -723,6 +756,7 @@ impl<'db> SemanticsImpl<'db> {
723756
SameKind(SyntaxKind),
724757
None,
725758
}
759+
726760
let fetch_kind = |token: &SyntaxToken| match token.parent() {
727761
Some(node) => match node.kind() {
728762
kind @ (SyntaxKind::NAME | SyntaxKind::NAME_REF) => kind,

0 commit comments

Comments
 (0)