Skip to content

Commit 37a8770

Browse files
committed
internal: Don't kick off inference in Semantics::descend_into_macros_impl
1 parent 2ca3834 commit 37a8770

File tree

2 files changed

+48
-13
lines changed

2 files changed

+48
-13
lines changed

crates/hir/src/semantics.rs

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -528,7 +528,7 @@ impl<'db> SemanticsImpl<'db> {
528528
if first == last {
529529
self.descend_into_macros_impl(
530530
first,
531-
|InFile { value, .. }| {
531+
&mut |InFile { value, .. }| {
532532
if let Some(node) = value.ancestors().find_map(N::cast) {
533533
res.push(node)
534534
}
@@ -540,7 +540,7 @@ impl<'db> SemanticsImpl<'db> {
540540
let mut scratch: SmallVec<[_; 1]> = smallvec![];
541541
self.descend_into_macros_impl(
542542
first,
543-
|token| {
543+
&mut |token| {
544544
scratch.push(token);
545545
},
546546
false,
@@ -549,7 +549,7 @@ impl<'db> SemanticsImpl<'db> {
549549
let mut scratch = scratch.into_iter();
550550
self.descend_into_macros_impl(
551551
last,
552-
|InFile { value: last, file_id: last_fid }| {
552+
&mut |InFile { value: last, file_id: last_fid }| {
553553
if let Some(InFile { value: first, file_id: first_fid }) = scratch.next() {
554554
if first_fid == last_fid {
555555
if let Some(p) = first.parent() {
@@ -574,28 +574,28 @@ impl<'db> SemanticsImpl<'db> {
574574

575575
fn descend_into_macros(&self, token: SyntaxToken) -> SmallVec<[SyntaxToken; 1]> {
576576
let mut res = smallvec![];
577-
self.descend_into_macros_impl(token, |InFile { value, .. }| res.push(value), false);
577+
self.descend_into_macros_impl(token, &mut |InFile { value, .. }| res.push(value), false);
578578
res
579579
}
580580

581581
fn descend_into_macros_single(&self, token: SyntaxToken) -> SyntaxToken {
582582
let mut res = token.clone();
583-
self.descend_into_macros_impl(token, |InFile { value, .. }| res = value, true);
583+
self.descend_into_macros_impl(token, &mut |InFile { value, .. }| res = value, true);
584584
res
585585
}
586586

587587
fn descend_into_macros_impl(
588588
&self,
589589
token: SyntaxToken,
590-
mut f: impl FnMut(InFile<SyntaxToken>),
590+
f: &mut dyn FnMut(InFile<SyntaxToken>),
591591
single: bool,
592592
) {
593593
let _p = profile::span("descend_into_macros");
594594
let parent = match token.parent() {
595595
Some(it) => it,
596596
None => return,
597597
};
598-
let sa = self.analyze(&parent);
598+
let sa = self.analyze_no_infer(&parent);
599599
let mut stack: SmallVec<[_; 4]> = smallvec![InFile::new(sa.file_id, token)];
600600
let mut cache = self.expansion_info_cache.borrow_mut();
601601
let mut mcache = self.macro_call_cache.borrow_mut();
@@ -927,14 +927,23 @@ impl<'db> SemanticsImpl<'db> {
927927
}
928928

929929
fn analyze(&self, node: &SyntaxNode) -> SourceAnalyzer {
930-
self.analyze_impl(node, None)
930+
self.analyze_impl(node, None, true)
931931
}
932932

933933
fn analyze_with_offset(&self, node: &SyntaxNode, offset: TextSize) -> SourceAnalyzer {
934-
self.analyze_impl(node, Some(offset))
934+
self.analyze_impl(node, Some(offset), true)
935935
}
936936

937-
fn analyze_impl(&self, node: &SyntaxNode, offset: Option<TextSize>) -> SourceAnalyzer {
937+
fn analyze_no_infer(&self, node: &SyntaxNode) -> SourceAnalyzer {
938+
self.analyze_impl(node, None, false)
939+
}
940+
941+
fn analyze_impl(
942+
&self,
943+
node: &SyntaxNode,
944+
offset: Option<TextSize>,
945+
infer_body: bool,
946+
) -> SourceAnalyzer {
938947
let _p = profile::span("Semantics::analyze_impl");
939948
let node = self.find_file(node.clone());
940949
let node = node.as_ref();
@@ -946,7 +955,11 @@ impl<'db> SemanticsImpl<'db> {
946955

947956
let resolver = match container {
948957
ChildContainer::DefWithBodyId(def) => {
949-
return SourceAnalyzer::new_for_body(self.db, def, node, offset)
958+
return if infer_body {
959+
SourceAnalyzer::new_for_body(self.db, def, node, offset)
960+
} else {
961+
SourceAnalyzer::new_for_body_no_infer(self.db, def, node, offset)
962+
}
950963
}
951964
ChildContainer::TraitId(it) => it.resolver(self.db.upcast()),
952965
ChildContainer::ImplId(it) => it.resolver(self.db.upcast()),

crates/hir/src/source_analyzer.rs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ impl SourceAnalyzer {
5050
pub(crate) fn new_for_body(
5151
db: &dyn HirDatabase,
5252
def: DefWithBodyId,
53-
node: InFile<&SyntaxNode>,
53+
node @ InFile { file_id, .. }: InFile<&SyntaxNode>,
5454
offset: Option<TextSize>,
5555
) -> SourceAnalyzer {
5656
let (body, source_map) = db.body_with_source_map(def);
@@ -65,7 +65,29 @@ impl SourceAnalyzer {
6565
body: Some(body),
6666
body_source_map: Some(source_map),
6767
infer: Some(db.infer(def)),
68-
file_id: node.file_id,
68+
file_id,
69+
}
70+
}
71+
72+
pub(crate) fn new_for_body_no_infer(
73+
db: &dyn HirDatabase,
74+
def: DefWithBodyId,
75+
node @ InFile { file_id, .. }: InFile<&SyntaxNode>,
76+
offset: Option<TextSize>,
77+
) -> SourceAnalyzer {
78+
let (body, source_map) = db.body_with_source_map(def);
79+
let scopes = db.expr_scopes(def);
80+
let scope = match offset {
81+
None => scope_for(&scopes, &source_map, node),
82+
Some(offset) => scope_for_offset(db, &scopes, &source_map, node.with_value(offset)),
83+
};
84+
let resolver = resolver_for_scope(db.upcast(), def, scope);
85+
SourceAnalyzer {
86+
resolver,
87+
body: Some(body),
88+
body_source_map: Some(source_map),
89+
infer: None,
90+
file_id,
6991
}
7092
}
7193

0 commit comments

Comments
 (0)