Skip to content

Commit cf5ab63

Browse files
authored
Merge pull request #18409 from Veykril/veykril/push-rkrkpvzvumvx
Only construct a resolver in macro descension when needed
2 parents e37c6dc + c6d1f78 commit cf5ab63

File tree

2 files changed

+20
-14
lines changed

2 files changed

+20
-14
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1043,12 +1043,12 @@ impl HasResolver for ModuleId {
10431043
fn resolver(self, db: &dyn DefDatabase) -> Resolver {
10441044
let mut def_map = self.def_map(db);
10451045
let mut module_id = self.local_id;
1046-
let mut modules: SmallVec<[_; 1]> = smallvec![];
10471046

10481047
if !self.is_block_module() {
10491048
return Resolver { scopes: vec![], module_scope: ModuleItemMap { def_map, module_id } };
10501049
}
10511050

1051+
let mut modules: SmallVec<[_; 1]> = smallvec![];
10521052
while let Some(parent) = def_map.parent() {
10531053
let block_def_map = mem::replace(&mut def_map, parent.def_map(db));
10541054
modules.push(block_def_map);

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

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -936,16 +936,7 @@ impl<'db> SemanticsImpl<'db> {
936936
}
937937
}
938938

939-
let (file_id, tokens) = stack.first()?;
940-
// make sure we pick the token in the expanded include if we encountered an include,
941-
// otherwise we'll get the wrong semantics
942-
let sa =
943-
tokens.first()?.0.parent().and_then(|parent| {
944-
self.analyze_impl(InFile::new(*file_id, &parent), None, false)
945-
})?;
946-
947939
let mut m_cache = self.macro_call_cache.borrow_mut();
948-
let def_map = sa.resolver.def_map();
949940

950941
// Filters out all tokens that contain the given range (usually the macro call), any such
951942
// token is redundant as the corresponding macro call has already been processed
@@ -1024,8 +1015,16 @@ impl<'db> SemanticsImpl<'db> {
10241015
) {
10251016
call.as_macro_file()
10261017
} else {
1027-
// FIXME: This is wrong, the SourceAnalyzer might be invalid here
1028-
sa.expand(self.db, mcall.as_ref())?
1018+
token
1019+
.parent()
1020+
.and_then(|parent| {
1021+
self.analyze_impl(
1022+
InFile::new(expansion, &parent),
1023+
None,
1024+
false,
1025+
)
1026+
})?
1027+
.expand(self.db, mcall.as_ref())?
10291028
};
10301029
m_cache.insert(mcall, it);
10311030
it
@@ -1095,9 +1094,16 @@ impl<'db> SemanticsImpl<'db> {
10951094
attr.path().and_then(|it| it.as_single_name_ref())?.as_name();
10961095
// Not an attribute, nor a derive, so it's either an intert attribute or a derive helper
10971096
// Try to resolve to a derive helper and downmap
1097+
let resolver = &token
1098+
.parent()
1099+
.and_then(|parent| {
1100+
self.analyze_impl(InFile::new(expansion, &parent), None, false)
1101+
})?
1102+
.resolver;
10981103
let id = self.db.ast_id_map(expansion).ast_id(&adt);
1099-
let helpers =
1100-
def_map.derive_helpers_in_scope(InFile::new(expansion, id))?;
1104+
let helpers = resolver
1105+
.def_map()
1106+
.derive_helpers_in_scope(InFile::new(expansion, id))?;
11011107

11021108
if !helpers.is_empty() {
11031109
let text_range = attr.syntax().text_range();

0 commit comments

Comments
 (0)