Skip to content

Commit 14ff3d7

Browse files
bors[bot]Veykril
andauthored
Merge #11065
11065: internal: Don't kick off inference in `Semantics::descend_into_macros_impl` r=Veykril a=Veykril We do not need inference info here so there is no point in calculating it bors r+ Co-authored-by: Lukas Wirth <[email protected]>
2 parents 48d6cef + a574434 commit 14ff3d7

File tree

4 files changed

+148
-127
lines changed

4 files changed

+148
-127
lines changed

crates/hir/src/semantics.rs

Lines changed: 25 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()),
@@ -1117,6 +1130,7 @@ to_def_impls![
11171130
(crate::TypeParam, ast::TypeParam, type_param_to_def),
11181131
(crate::LifetimeParam, ast::LifetimeParam, lifetime_param_to_def),
11191132
(crate::ConstParam, ast::ConstParam, const_param_to_def),
1133+
(crate::GenericParam, ast::GenericParam, generic_param_to_def),
11201134
(crate::MacroDef, ast::Macro, macro_to_def),
11211135
(crate::Local, ast::IdentPat, bind_pat_to_def),
11221136
(crate::Local, ast::SelfParam, self_param_to_def),

crates/hir/src/semantics/source_to_def.rs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,8 @@ use hir_def::{
9292
expr::{LabelId, PatId},
9393
keys::{self, Key},
9494
AdtId, ConstId, ConstParamId, DefWithBodyId, EnumId, EnumVariantId, FieldId, FunctionId,
95-
GenericDefId, ImplId, LifetimeParamId, ModuleId, StaticId, StructId, TraitId, TypeAliasId,
96-
TypeParamId, UnionId, VariantId,
95+
GenericDefId, GenericParamId, ImplId, LifetimeParamId, ModuleId, StaticId, StructId, TraitId,
96+
TypeAliasId, TypeParamId, UnionId, VariantId,
9797
};
9898
use hir_expand::{name::AsName, AstId, HirFileId, MacroCallId, MacroDefId, MacroDefKind};
9999
use rustc_hash::FxHashMap;
@@ -299,6 +299,23 @@ impl SourceToDefCtx<'_, '_> {
299299
dyn_map[keys::CONST_PARAM].get(&src).copied()
300300
}
301301

302+
pub(super) fn generic_param_to_def(
303+
&mut self,
304+
InFile { file_id, value }: InFile<ast::GenericParam>,
305+
) -> Option<GenericParamId> {
306+
match value {
307+
ast::GenericParam::ConstParam(it) => {
308+
self.const_param_to_def(InFile::new(file_id, it)).map(GenericParamId::ConstParamId)
309+
}
310+
ast::GenericParam::LifetimeParam(it) => self
311+
.lifetime_param_to_def(InFile::new(file_id, it))
312+
.map(GenericParamId::LifetimeParamId),
313+
ast::GenericParam::TypeParam(it) => {
314+
self.type_param_to_def(InFile::new(file_id, it)).map(GenericParamId::TypeParamId)
315+
}
316+
}
317+
}
318+
302319
pub(super) fn macro_to_def(&mut self, src: InFile<ast::Macro>) -> Option<MacroDefId> {
303320
let makro = self.dyn_map(src.as_ref()).and_then(|it| it[keys::MACRO].get(&src).copied());
304321
if let res @ Some(_) = makro {

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

crates/ide_db/src/defs.rs

Lines changed: 80 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -224,125 +224,93 @@ impl NameClass {
224224

225225
let parent = name.syntax().parent()?;
226226

227-
if let Some(bind_pat) = ast::IdentPat::cast(parent.clone()) {
228-
if let Some(def) = sema.resolve_bind_pat_to_const(&bind_pat) {
227+
let def = if let Some(item) = ast::Item::cast(parent.clone()) {
228+
match item {
229+
ast::Item::MacroRules(it) => {
230+
Definition::Macro(sema.to_def(&ast::Macro::MacroRules(it))?)
231+
}
232+
ast::Item::MacroDef(it) => {
233+
Definition::Macro(sema.to_def(&ast::Macro::MacroDef(it))?)
234+
}
235+
ast::Item::Const(it) => Definition::Const(sema.to_def(&it)?),
236+
ast::Item::Fn(it) => Definition::Function(sema.to_def(&it)?),
237+
ast::Item::Module(it) => Definition::Module(sema.to_def(&it)?),
238+
ast::Item::Static(it) => Definition::Static(sema.to_def(&it)?),
239+
ast::Item::Trait(it) => Definition::Trait(sema.to_def(&it)?),
240+
ast::Item::TypeAlias(it) => Definition::TypeAlias(sema.to_def(&it)?),
241+
ast::Item::Enum(it) => Definition::Adt(hir::Adt::Enum(sema.to_def(&it)?)),
242+
ast::Item::Struct(it) => Definition::Adt(hir::Adt::Struct(sema.to_def(&it)?)),
243+
ast::Item::Union(it) => Definition::Adt(hir::Adt::Union(sema.to_def(&it)?)),
244+
_ => return None,
245+
}
246+
} else if let Some(it) = ast::IdentPat::cast(parent.clone()) {
247+
if let Some(def) = sema.resolve_bind_pat_to_const(&it) {
229248
return Some(NameClass::ConstReference(Definition::from(def)));
230249
}
231-
}
232250

233-
match_ast! {
234-
match parent {
235-
ast::Rename(it) => {
236-
if let Some(use_tree) = it.syntax().parent().and_then(ast::UseTree::cast) {
237-
let path = use_tree.path()?;
238-
let path_segment = path.segment()?;
239-
let name_ref = path_segment.name_ref()?;
240-
let name_ref = if name_ref.self_token().is_some() {
241-
use_tree
242-
.syntax()
243-
.parent()
244-
.as_ref()
245-
// Skip over UseTreeList
246-
.and_then(|it| {
247-
let use_tree = it.parent().and_then(ast::UseTree::cast)?;
248-
let path = use_tree.path()?;
249-
let path_segment = path.segment()?;
250-
path_segment.name_ref()
251-
}).unwrap_or(name_ref)
252-
} else {
253-
name_ref
254-
};
255-
let name_ref_class = NameRefClass::classify(sema, &name_ref)?;
256-
257-
Some(NameClass::Definition(match name_ref_class {
258-
NameRefClass::Definition(def) => def,
259-
NameRefClass::FieldShorthand { local_ref: _, field_ref } => {
260-
Definition::Field(field_ref)
261-
}
262-
}))
263-
} else {
264-
let extern_crate = it.syntax().parent().and_then(ast::ExternCrate::cast)?;
265-
let krate = sema.resolve_extern_crate(&extern_crate)?;
266-
let root_module = krate.root_module(sema.db);
267-
Some(NameClass::Definition(Definition::Module(root_module)))
251+
let local = sema.to_def(&it)?;
252+
let pat_parent = it.syntax().parent();
253+
if let Some(record_pat_field) = pat_parent.and_then(ast::RecordPatField::cast) {
254+
if record_pat_field.name_ref().is_none() {
255+
if let Some(field) = sema.resolve_record_pat_field(&record_pat_field) {
256+
return Some(NameClass::PatFieldShorthand {
257+
local_def: local,
258+
field_ref: field,
259+
});
268260
}
269-
},
270-
ast::IdentPat(it) => {
271-
let local = sema.to_def(&it)?;
261+
}
262+
}
272263

273-
if let Some(record_pat_field) = it.syntax().parent().and_then(ast::RecordPatField::cast) {
274-
if record_pat_field.name_ref().is_none() {
275-
if let Some(field) = sema.resolve_record_pat_field(&record_pat_field) {
276-
return Some(NameClass::PatFieldShorthand { local_def: local, field_ref: field });
277-
}
278-
}
279-
}
264+
Definition::Local(local)
265+
} else if let Some(it) = ast::Rename::cast(parent.clone()) {
266+
if let Some(use_tree) = it.syntax().parent().and_then(ast::UseTree::cast) {
267+
let path = use_tree.path()?;
268+
let path_segment = path.segment()?;
269+
let name_ref = path_segment.name_ref()?;
270+
let name_ref = if name_ref.self_token().is_some() {
271+
use_tree
272+
.syntax()
273+
.parent()
274+
.as_ref()
275+
// Skip over UseTreeList
276+
.and_then(|it| {
277+
let use_tree = it.parent().and_then(ast::UseTree::cast)?;
278+
let path = use_tree.path()?;
279+
let path_segment = path.segment()?;
280+
path_segment.name_ref()
281+
})
282+
.unwrap_or(name_ref)
283+
} else {
284+
name_ref
285+
};
286+
let name_ref_class = NameRefClass::classify(sema, &name_ref)?;
280287

281-
Some(NameClass::Definition(Definition::Local(local)))
282-
},
283-
ast::SelfParam(it) => {
284-
let def = sema.to_def(&it)?;
285-
Some(NameClass::Definition(Definition::Local(def)))
286-
},
287-
ast::RecordField(it) => {
288-
let field: hir::Field = sema.to_def(&it)?;
289-
Some(NameClass::Definition(Definition::Field(field)))
290-
},
291-
ast::Module(it) => {
292-
let def = sema.to_def(&it)?;
293-
Some(NameClass::Definition(Definition::Module(def)))
294-
},
295-
ast::Struct(it) => {
296-
let def: hir::Struct = sema.to_def(&it)?;
297-
Some(NameClass::Definition(Definition::Adt(def.into())))
298-
},
299-
ast::Union(it) => {
300-
let def: hir::Union = sema.to_def(&it)?;
301-
Some(NameClass::Definition(Definition::Adt(def.into())))
302-
},
303-
ast::Enum(it) => {
304-
let def: hir::Enum = sema.to_def(&it)?;
305-
Some(NameClass::Definition(Definition::Adt(def.into())))
306-
},
307-
ast::Trait(it) => {
308-
let def: hir::Trait = sema.to_def(&it)?;
309-
Some(NameClass::Definition(Definition::Trait(def)))
310-
},
311-
ast::Static(it) => {
312-
let def: hir::Static = sema.to_def(&it)?;
313-
Some(NameClass::Definition(Definition::Static(def)))
314-
},
315-
ast::Variant(it) => {
316-
let def: hir::Variant = sema.to_def(&it)?;
317-
Some(NameClass::Definition(Definition::Variant(def)))
318-
},
319-
ast::Fn(it) => {
320-
let def: hir::Function = sema.to_def(&it)?;
321-
Some(NameClass::Definition(Definition::Function(def)))
322-
},
323-
ast::Const(it) => {
324-
let def: hir::Const = sema.to_def(&it)?;
325-
Some(NameClass::Definition(Definition::Const(def)))
326-
},
327-
ast::TypeAlias(it) => {
328-
let def: hir::TypeAlias = sema.to_def(&it)?;
329-
Some(NameClass::Definition(Definition::TypeAlias(def)))
330-
},
331-
ast::Macro(it) => {
332-
let def = sema.to_def(&it)?;
333-
Some(NameClass::Definition(Definition::Macro(def)))
334-
},
335-
ast::TypeParam(it) => {
336-
let def = sema.to_def(&it)?;
337-
Some(NameClass::Definition(Definition::GenericParam(def.into())))
338-
},
339-
ast::ConstParam(it) => {
340-
let def = sema.to_def(&it)?;
341-
Some(NameClass::Definition(Definition::GenericParam(def.into())))
342-
},
343-
_ => None,
288+
match name_ref_class {
289+
NameRefClass::Definition(def) => def,
290+
NameRefClass::FieldShorthand { local_ref: _, field_ref } => {
291+
Definition::Field(field_ref)
292+
}
293+
}
294+
} else {
295+
let extern_crate = it.syntax().parent().and_then(ast::ExternCrate::cast)?;
296+
let krate = sema.resolve_extern_crate(&extern_crate)?;
297+
let root_module = krate.root_module(sema.db);
298+
Definition::Module(root_module)
344299
}
345-
}
300+
} else {
301+
match_ast! {
302+
match parent {
303+
ast::SelfParam(it) => Definition::Local(sema.to_def(&it)?),
304+
ast::RecordField(it) => Definition::Field(sema.to_def(&it)?),
305+
ast::Variant(it) => Definition::Variant(sema.to_def(&it)?),
306+
ast::TypeParam(it) => Definition::GenericParam(sema.to_def(&it)?.into()),
307+
ast::ConstParam(it) => Definition::GenericParam(sema.to_def(&it)?.into()),
308+
_ => return None,
309+
}
310+
}
311+
};
312+
313+
Some(NameClass::Definition(def))
346314
}
347315

348316
pub fn classify_lifetime(

0 commit comments

Comments
 (0)