Skip to content

Commit 978b140

Browse files
bors[bot]Veykril
andauthored
Merge #9921
9921: Only add entries to SourceToDef dynmaps when they come from the same file r=matklad a=Veykril Fixes #9919 Running the test as described in the issue I do not get any eprintln output at all anymore. Co-authored-by: Lukas Wirth <lukastw97@gmail.com>
2 parents bae22f9 + 6523a09 commit 978b140

File tree

3 files changed

+124
-74
lines changed

3 files changed

+124
-74
lines changed

crates/hir/src/semantics/source_to_def.rs

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ use hir_def::{
9595
GenericDefId, ImplId, LifetimeParamId, ModuleId, StaticId, StructId, TraitId, TypeAliasId,
9696
TypeParamId, UnionId, VariantId,
9797
};
98-
use hir_expand::{name::AsName, AstId, MacroCallId, MacroDefId, MacroDefKind};
98+
use hir_expand::{name::AsName, AstId, HirFileId, MacroCallId, MacroDefId, MacroDefKind};
9999
use rustc_hash::FxHashMap;
100100
use smallvec::SmallVec;
101101
use stdx::impl_from;
@@ -106,7 +106,7 @@ use syntax::{
106106

107107
use crate::{db::HirDatabase, InFile};
108108

109-
pub(super) type SourceToDefCache = FxHashMap<ChildContainer, DynMap>;
109+
pub(super) type SourceToDefCache = FxHashMap<(ChildContainer, HirFileId), DynMap>;
110110

111111
pub(super) struct SourceToDefCtx<'a, 'b> {
112112
pub(super) db: &'b dyn HirDatabase,
@@ -252,17 +252,19 @@ impl SourceToDefCtx<'_, '_> {
252252

253253
fn dyn_map<Ast: AstNode + 'static>(&mut self, src: InFile<&Ast>) -> Option<&DynMap> {
254254
let container = self.find_container(src.map(|it| it.syntax()))?;
255+
Some(self.cache_for(container, src.file_id))
256+
}
257+
258+
fn cache_for(&mut self, container: ChildContainer, file_id: HirFileId) -> &DynMap {
255259
let db = self.db;
256-
let dyn_map =
257-
&*self.cache.entry(container).or_insert_with(|| container.child_by_source(db));
258-
Some(dyn_map)
260+
self.cache
261+
.entry((container, file_id))
262+
.or_insert_with(|| container.child_by_source(db, file_id))
259263
}
260264

261265
pub(super) fn type_param_to_def(&mut self, src: InFile<ast::TypeParam>) -> Option<TypeParamId> {
262266
let container: ChildContainer = self.find_generic_param_container(src.syntax())?.into();
263-
let db = self.db;
264-
let dyn_map =
265-
&*self.cache.entry(container).or_insert_with(|| container.child_by_source(db));
267+
let dyn_map = self.cache_for(container, src.file_id);
266268
dyn_map[keys::TYPE_PARAM].get(&src).copied()
267269
}
268270

@@ -271,9 +273,7 @@ impl SourceToDefCtx<'_, '_> {
271273
src: InFile<ast::LifetimeParam>,
272274
) -> Option<LifetimeParamId> {
273275
let container: ChildContainer = self.find_generic_param_container(src.syntax())?.into();
274-
let db = self.db;
275-
let dyn_map =
276-
&*self.cache.entry(container).or_insert_with(|| container.child_by_source(db));
276+
let dyn_map = self.cache_for(container, src.file_id);
277277
dyn_map[keys::LIFETIME_PARAM].get(&src).copied()
278278
}
279279

@@ -282,9 +282,7 @@ impl SourceToDefCtx<'_, '_> {
282282
src: InFile<ast::ConstParam>,
283283
) -> Option<ConstParamId> {
284284
let container: ChildContainer = self.find_generic_param_container(src.syntax())?.into();
285-
let db = self.db;
286-
let dyn_map =
287-
&*self.cache.entry(container).or_insert_with(|| container.child_by_source(db));
285+
let dyn_map = self.cache_for(container, src.file_id);
288286
dyn_map[keys::CONST_PARAM].get(&src).copied()
289287
}
290288

@@ -422,17 +420,17 @@ impl_from! {
422420
}
423421

424422
impl ChildContainer {
425-
fn child_by_source(self, db: &dyn HirDatabase) -> DynMap {
423+
fn child_by_source(self, db: &dyn HirDatabase, file_id: HirFileId) -> DynMap {
426424
let db = db.upcast();
427425
match self {
428-
ChildContainer::DefWithBodyId(it) => it.child_by_source(db),
429-
ChildContainer::ModuleId(it) => it.child_by_source(db),
430-
ChildContainer::TraitId(it) => it.child_by_source(db),
431-
ChildContainer::ImplId(it) => it.child_by_source(db),
432-
ChildContainer::EnumId(it) => it.child_by_source(db),
433-
ChildContainer::VariantId(it) => it.child_by_source(db),
426+
ChildContainer::DefWithBodyId(it) => it.child_by_source(db, file_id),
427+
ChildContainer::ModuleId(it) => it.child_by_source(db, file_id),
428+
ChildContainer::TraitId(it) => it.child_by_source(db, file_id),
429+
ChildContainer::ImplId(it) => it.child_by_source(db, file_id),
430+
ChildContainer::EnumId(it) => it.child_by_source(db, file_id),
431+
ChildContainer::VariantId(it) => it.child_by_source(db, file_id),
434432
ChildContainer::TypeAliasId(_) => DynMap::default(),
435-
ChildContainer::GenericDefId(it) => it.child_by_source(db),
433+
ChildContainer::GenericDefId(it) => it.child_by_source(db, file_id),
436434
}
437435
}
438436
}

crates/hir_def/src/child_by_source.rs

Lines changed: 102 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
//! node for a *child*, and get its hir.
66
77
use either::Either;
8+
use hir_expand::HirFileId;
89

910
use crate::{
1011
db::DefDatabase,
@@ -17,145 +18,196 @@ use crate::{
1718
};
1819

1920
pub trait ChildBySource {
20-
fn child_by_source(&self, db: &dyn DefDatabase) -> DynMap {
21+
fn child_by_source(&self, db: &dyn DefDatabase, file_id: HirFileId) -> DynMap {
2122
let mut res = DynMap::default();
22-
self.child_by_source_to(db, &mut res);
23+
self.child_by_source_to(db, &mut res, file_id);
2324
res
2425
}
25-
fn child_by_source_to(&self, db: &dyn DefDatabase, map: &mut DynMap);
26+
fn child_by_source_to(&self, db: &dyn DefDatabase, map: &mut DynMap, file_id: HirFileId);
2627
}
2728

2829
impl ChildBySource for TraitId {
29-
fn child_by_source_to(&self, db: &dyn DefDatabase, res: &mut DynMap) {
30+
fn child_by_source_to(&self, db: &dyn DefDatabase, res: &mut DynMap, file_id: HirFileId) {
3031
let data = db.trait_data(*self);
3132
for (_name, item) in data.items.iter() {
3233
match *item {
3334
AssocItemId::FunctionId(func) => {
34-
let src = func.lookup(db).source(db);
35-
res[keys::FUNCTION].insert(src, func)
35+
let loc = func.lookup(db);
36+
if loc.id.file_id() == file_id {
37+
let src = loc.source(db);
38+
res[keys::FUNCTION].insert(src, func)
39+
}
3640
}
3741
AssocItemId::ConstId(konst) => {
38-
let src = konst.lookup(db).source(db);
39-
res[keys::CONST].insert(src, konst)
42+
let loc = konst.lookup(db);
43+
if loc.id.file_id() == file_id {
44+
let src = loc.source(db);
45+
res[keys::CONST].insert(src, konst)
46+
}
4047
}
4148
AssocItemId::TypeAliasId(ty) => {
42-
let src = ty.lookup(db).source(db);
43-
res[keys::TYPE_ALIAS].insert(src, ty)
49+
let loc = ty.lookup(db);
50+
if loc.id.file_id() == file_id {
51+
let src = loc.source(db);
52+
res[keys::TYPE_ALIAS].insert(src, ty)
53+
}
4454
}
4555
}
4656
}
4757
}
4858
}
4959

5060
impl ChildBySource for ImplId {
51-
fn child_by_source_to(&self, db: &dyn DefDatabase, res: &mut DynMap) {
61+
fn child_by_source_to(&self, db: &dyn DefDatabase, res: &mut DynMap, file_id: HirFileId) {
5262
let data = db.impl_data(*self);
5363
for &item in data.items.iter() {
5464
match item {
5565
AssocItemId::FunctionId(func) => {
56-
let src = func.lookup(db).source(db);
57-
res[keys::FUNCTION].insert(src, func)
66+
let loc = func.lookup(db);
67+
if loc.id.file_id() == file_id {
68+
let src = loc.source(db);
69+
res[keys::FUNCTION].insert(src, func)
70+
}
5871
}
5972
AssocItemId::ConstId(konst) => {
60-
let src = konst.lookup(db).source(db);
61-
res[keys::CONST].insert(src, konst)
73+
let loc = konst.lookup(db);
74+
if loc.id.file_id() == file_id {
75+
let src = loc.source(db);
76+
res[keys::CONST].insert(src, konst)
77+
}
6278
}
6379
AssocItemId::TypeAliasId(ty) => {
64-
let src = ty.lookup(db).source(db);
65-
res[keys::TYPE_ALIAS].insert(src, ty)
80+
let loc = ty.lookup(db);
81+
if loc.id.file_id() == file_id {
82+
let src = loc.source(db);
83+
res[keys::TYPE_ALIAS].insert(src, ty)
84+
}
6685
}
6786
}
6887
}
6988
}
7089
}
7190

7291
impl ChildBySource for ModuleId {
73-
fn child_by_source_to(&self, db: &dyn DefDatabase, res: &mut DynMap) {
92+
fn child_by_source_to(&self, db: &dyn DefDatabase, res: &mut DynMap, file_id: HirFileId) {
7493
let def_map = self.def_map(db);
7594
let module_data = &def_map[self.local_id];
76-
module_data.scope.child_by_source_to(db, res);
95+
module_data.scope.child_by_source_to(db, res, file_id);
7796
}
7897
}
7998

8099
impl ChildBySource for ItemScope {
81-
fn child_by_source_to(&self, db: &dyn DefDatabase, res: &mut DynMap) {
82-
self.declarations().for_each(|item| add_module_def(db, res, item));
100+
fn child_by_source_to(&self, db: &dyn DefDatabase, res: &mut DynMap, file_id: HirFileId) {
101+
self.declarations().for_each(|item| add_module_def(db, file_id, res, item));
83102
self.unnamed_consts().for_each(|konst| {
84103
let src = konst.lookup(db).source(db);
85104
res[keys::CONST].insert(src, konst);
86105
});
87-
self.impls().for_each(|imp| add_impl(db, res, imp));
106+
self.impls().for_each(|imp| add_impl(db, file_id, res, imp));
88107
self.attr_macro_invocs().for_each(|(ast_id, call_id)| {
89108
let item = ast_id.with_value(ast_id.to_node(db.upcast()));
90109
res[keys::ATTR_MACRO].insert(item, call_id);
91110
});
92111

93-
fn add_module_def(db: &dyn DefDatabase, map: &mut DynMap, item: ModuleDefId) {
112+
fn add_module_def(
113+
db: &dyn DefDatabase,
114+
file_id: HirFileId,
115+
map: &mut DynMap,
116+
item: ModuleDefId,
117+
) {
94118
match item {
95119
ModuleDefId::FunctionId(func) => {
96-
let src = func.lookup(db).source(db);
97-
map[keys::FUNCTION].insert(src, func)
120+
let loc = func.lookup(db);
121+
if loc.id.file_id() == file_id {
122+
let src = loc.source(db);
123+
map[keys::FUNCTION].insert(src, func)
124+
}
98125
}
99126
ModuleDefId::ConstId(konst) => {
100-
let src = konst.lookup(db).source(db);
101-
map[keys::CONST].insert(src, konst)
127+
let loc = konst.lookup(db);
128+
if loc.id.file_id() == file_id {
129+
let src = loc.source(db);
130+
map[keys::CONST].insert(src, konst)
131+
}
102132
}
103133
ModuleDefId::StaticId(statik) => {
104-
let src = statik.lookup(db).source(db);
105-
map[keys::STATIC].insert(src, statik)
134+
let loc = statik.lookup(db);
135+
if loc.id.file_id() == file_id {
136+
let src = loc.source(db);
137+
map[keys::STATIC].insert(src, statik)
138+
}
106139
}
107140
ModuleDefId::TypeAliasId(ty) => {
108-
let src = ty.lookup(db).source(db);
109-
map[keys::TYPE_ALIAS].insert(src, ty)
141+
let loc = ty.lookup(db);
142+
if loc.id.file_id() == file_id {
143+
let src = loc.source(db);
144+
map[keys::TYPE_ALIAS].insert(src, ty)
145+
}
110146
}
111147
ModuleDefId::TraitId(trait_) => {
112-
let src = trait_.lookup(db).source(db);
113-
map[keys::TRAIT].insert(src, trait_)
148+
let loc = trait_.lookup(db);
149+
if loc.id.file_id() == file_id {
150+
let src = loc.source(db);
151+
map[keys::TRAIT].insert(src, trait_)
152+
}
114153
}
115154
ModuleDefId::AdtId(adt) => match adt {
116155
AdtId::StructId(strukt) => {
117-
let src = strukt.lookup(db).source(db);
118-
map[keys::STRUCT].insert(src, strukt)
156+
let loc = strukt.lookup(db);
157+
if loc.id.file_id() == file_id {
158+
let src = loc.source(db);
159+
map[keys::STRUCT].insert(src, strukt)
160+
}
119161
}
120162
AdtId::UnionId(union_) => {
121-
let src = union_.lookup(db).source(db);
122-
map[keys::UNION].insert(src, union_)
163+
let loc = union_.lookup(db);
164+
if loc.id.file_id() == file_id {
165+
let src = loc.source(db);
166+
map[keys::UNION].insert(src, union_)
167+
}
123168
}
124169
AdtId::EnumId(enum_) => {
125-
let src = enum_.lookup(db).source(db);
126-
map[keys::ENUM].insert(src, enum_)
170+
let loc = enum_.lookup(db);
171+
if loc.id.file_id() == file_id {
172+
let src = loc.source(db);
173+
map[keys::ENUM].insert(src, enum_)
174+
}
127175
}
128176
},
129177
_ => (),
130178
}
131179
}
132-
fn add_impl(db: &dyn DefDatabase, map: &mut DynMap, imp: ImplId) {
133-
let src = imp.lookup(db).source(db);
134-
map[keys::IMPL].insert(src, imp)
180+
fn add_impl(db: &dyn DefDatabase, file_id: HirFileId, map: &mut DynMap, imp: ImplId) {
181+
let loc = imp.lookup(db);
182+
if loc.id.file_id() == file_id {
183+
let src = loc.source(db);
184+
map[keys::IMPL].insert(src, imp)
185+
}
135186
}
136187
}
137188
}
138189

139190
impl ChildBySource for VariantId {
140-
fn child_by_source_to(&self, db: &dyn DefDatabase, res: &mut DynMap) {
191+
fn child_by_source_to(&self, db: &dyn DefDatabase, res: &mut DynMap, _: HirFileId) {
141192
let arena_map = self.child_source(db);
142193
let arena_map = arena_map.as_ref();
194+
let parent = *self;
143195
for (local_id, source) in arena_map.value.iter() {
144-
let id = FieldId { parent: *self, local_id };
145-
match source {
196+
let id = FieldId { parent, local_id };
197+
match source.clone() {
146198
Either::Left(source) => {
147-
res[keys::TUPLE_FIELD].insert(arena_map.with_value(source.clone()), id)
199+
res[keys::TUPLE_FIELD].insert(arena_map.with_value(source), id)
148200
}
149201
Either::Right(source) => {
150-
res[keys::RECORD_FIELD].insert(arena_map.with_value(source.clone()), id)
202+
res[keys::RECORD_FIELD].insert(arena_map.with_value(source), id)
151203
}
152204
}
153205
}
154206
}
155207
}
156208

157209
impl ChildBySource for EnumId {
158-
fn child_by_source_to(&self, db: &dyn DefDatabase, res: &mut DynMap) {
210+
fn child_by_source_to(&self, db: &dyn DefDatabase, res: &mut DynMap, _: HirFileId) {
159211
let arena_map = self.child_source(db);
160212
let arena_map = arena_map.as_ref();
161213
for (local_id, source) in arena_map.value.iter() {
@@ -166,12 +218,12 @@ impl ChildBySource for EnumId {
166218
}
167219

168220
impl ChildBySource for DefWithBodyId {
169-
fn child_by_source_to(&self, db: &dyn DefDatabase, res: &mut DynMap) {
221+
fn child_by_source_to(&self, db: &dyn DefDatabase, res: &mut DynMap, file_id: HirFileId) {
170222
let body = db.body(*self);
171223
for (_, def_map) in body.blocks(db) {
172224
// All block expressions are merged into the same map, because they logically all add
173225
// inner items to the containing `DefWithBodyId`.
174-
def_map[def_map.root()].scope.child_by_source_to(db, res);
226+
def_map[def_map.root()].scope.child_by_source_to(db, res, file_id);
175227
}
176228
}
177229
}

crates/hir_def/src/generics.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use base_db::FileId;
77
use either::Either;
88
use hir_expand::{
99
name::{name, AsName, Name},
10-
InFile,
10+
HirFileId, InFile,
1111
};
1212
use la_arena::{Arena, ArenaMap};
1313
use syntax::ast::{self, GenericParamsOwner, NameOwner, TypeBoundsOwner};
@@ -438,7 +438,7 @@ impl HasChildSource<LocalConstParamId> for GenericDefId {
438438
}
439439

440440
impl ChildBySource for GenericDefId {
441-
fn child_by_source_to(&self, db: &dyn DefDatabase, res: &mut DynMap) {
441+
fn child_by_source_to(&self, db: &dyn DefDatabase, res: &mut DynMap, _: HirFileId) {
442442
let (_, sm) = GenericParams::new(db, *self);
443443

444444
let sm = sm.as_ref();

0 commit comments

Comments
 (0)