Skip to content

Commit 1c32387

Browse files
committed
Auto merge of #16526 - Veykril:item-loc, r=Veykril
internal: Cleanup 🧹
2 parents 0878cde + 36fb140 commit 1c32387

File tree

12 files changed

+552
-480
lines changed

12 files changed

+552
-480
lines changed

crates/hir-def/src/attr.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ use crate::{
2828
lang_item::LangItem,
2929
nameres::{ModuleOrigin, ModuleSource},
3030
src::{HasChildSource, HasSource},
31-
AdtId, AttrDefId, GenericParamId, ItemTreeLoc, LocalFieldId, Lookup, MacroId, VariantId,
31+
AdtId, AttrDefId, GenericParamId, HasModule, ItemTreeLoc, LocalFieldId, Lookup, MacroId,
32+
VariantId,
3233
};
3334

3435
/// Desugared attributes of an item post `cfg_attr` expansion.

crates/hir-def/src/child_by_source.rs

+53-21
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
77
use either::Either;
88
use hir_expand::{attrs::collect_attrs, HirFileId};
9+
use syntax::ast;
910

1011
use crate::{
1112
db::DefDatabase,
@@ -17,8 +18,9 @@ use crate::{
1718
item_tree::ItemTreeNode,
1819
nameres::DefMap,
1920
src::{HasChildSource, HasSource},
20-
AdtId, AssocItemId, DefWithBodyId, EnumId, FieldId, ImplId, ItemTreeLoc, Lookup, MacroId,
21-
ModuleDefId, ModuleId, TraitId, VariantId,
21+
AdtId, AssocItemId, DefWithBodyId, EnumId, FieldId, GenericDefId, ImplId, ItemTreeLoc,
22+
LifetimeParamId, Lookup, MacroId, ModuleDefId, ModuleId, TraitId, TypeOrConstParamId,
23+
VariantId,
2224
};
2325

2426
pub trait ChildBySource {
@@ -59,14 +61,6 @@ impl ChildBySource for ImplId {
5961
}
6062
}
6163

62-
fn add_assoc_item(db: &dyn DefDatabase, res: &mut DynMap, file_id: HirFileId, item: AssocItemId) {
63-
match item {
64-
AssocItemId::FunctionId(func) => insert_item_loc(db, res, file_id, func, keys::FUNCTION),
65-
AssocItemId::ConstId(konst) => insert_item_loc(db, res, file_id, konst, keys::CONST),
66-
AssocItemId::TypeAliasId(ty) => insert_item_loc(db, res, file_id, ty, keys::TYPE_ALIAS),
67-
}
68-
}
69-
7064
impl ChildBySource for ModuleId {
7165
fn child_by_source_to(&self, db: &dyn DefDatabase, res: &mut DynMap, file_id: HirFileId) {
7266
let def_map = self.def_map(db);
@@ -118,14 +112,6 @@ impl ChildBySource for ItemScope {
118112
file_id: HirFileId,
119113
item: ModuleDefId,
120114
) {
121-
macro_rules! insert {
122-
($map:ident[$key:path].$insert:ident($id:ident)) => {{
123-
let loc = $id.lookup(db);
124-
if loc.id.file_id() == file_id {
125-
$map[$key].$insert(loc.source(db).value, $id)
126-
}
127-
}};
128-
}
129115
match item {
130116
ModuleDefId::FunctionId(id) => {
131117
insert_item_loc(db, map, file_id, id, keys::FUNCTION)
@@ -145,9 +131,13 @@ impl ChildBySource for ItemScope {
145131
AdtId::EnumId(id) => insert_item_loc(db, map, file_id, id, keys::ENUM),
146132
},
147133
ModuleDefId::MacroId(id) => match id {
148-
MacroId::Macro2Id(id) => insert!(map[keys::MACRO2].insert(id)),
149-
MacroId::MacroRulesId(id) => insert!(map[keys::MACRO_RULES].insert(id)),
150-
MacroId::ProcMacroId(id) => insert!(map[keys::PROC_MACRO].insert(id)),
134+
MacroId::Macro2Id(id) => insert_item_loc(db, map, file_id, id, keys::MACRO2),
135+
MacroId::MacroRulesId(id) => {
136+
insert_item_loc(db, map, file_id, id, keys::MACRO_RULES)
137+
}
138+
MacroId::ProcMacroId(id) => {
139+
insert_item_loc(db, map, file_id, id, keys::PROC_MACRO)
140+
}
151141
},
152142
ModuleDefId::ModuleId(_)
153143
| ModuleDefId::EnumVariantId(_)
@@ -207,6 +197,40 @@ impl ChildBySource for DefWithBodyId {
207197
}
208198
}
209199

200+
impl ChildBySource for GenericDefId {
201+
fn child_by_source_to(&self, db: &dyn DefDatabase, res: &mut DynMap, file_id: HirFileId) {
202+
let (gfile_id, generic_params_list) = self.file_id_and_params_of(db);
203+
if gfile_id != file_id {
204+
return;
205+
}
206+
207+
let generic_params = db.generic_params(*self);
208+
let mut toc_idx_iter = generic_params.type_or_consts.iter().map(|(idx, _)| idx);
209+
let lts_idx_iter = generic_params.lifetimes.iter().map(|(idx, _)| idx);
210+
211+
// For traits the first type index is `Self`, skip it.
212+
if let GenericDefId::TraitId(_) = *self {
213+
toc_idx_iter.next().unwrap(); // advance_by(1);
214+
}
215+
216+
if let Some(generic_params_list) = generic_params_list {
217+
for (local_id, ast_param) in
218+
toc_idx_iter.zip(generic_params_list.type_or_const_params())
219+
{
220+
let id = TypeOrConstParamId { parent: *self, local_id };
221+
match ast_param {
222+
ast::TypeOrConstParam::Type(a) => res[keys::TYPE_PARAM].insert(a, id),
223+
ast::TypeOrConstParam::Const(a) => res[keys::CONST_PARAM].insert(a, id),
224+
}
225+
}
226+
for (local_id, ast_param) in lts_idx_iter.zip(generic_params_list.lifetime_params()) {
227+
let id = LifetimeParamId { parent: *self, local_id };
228+
res[keys::LIFETIME_PARAM].insert(ast_param, id);
229+
}
230+
}
231+
}
232+
}
233+
210234
fn insert_item_loc<ID, N, Data>(
211235
db: &dyn DefDatabase,
212236
res: &mut DynMap,
@@ -224,3 +248,11 @@ fn insert_item_loc<ID, N, Data>(
224248
res[key].insert(loc.source(db).value, id)
225249
}
226250
}
251+
252+
fn add_assoc_item(db: &dyn DefDatabase, res: &mut DynMap, file_id: HirFileId, item: AssocItemId) {
253+
match item {
254+
AssocItemId::FunctionId(func) => insert_item_loc(db, res, file_id, func, keys::FUNCTION),
255+
AssocItemId::ConstId(konst) => insert_item_loc(db, res, file_id, konst, keys::CONST),
256+
AssocItemId::TypeAliasId(ty) => insert_item_loc(db, res, file_id, ty, keys::TYPE_ALIAS),
257+
}
258+
}

crates/hir-def/src/data/adt.rs

+3-45
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use hir_expand::{
1010
HirFileId, InFile,
1111
};
1212
use intern::Interned;
13-
use la_arena::{Arena, ArenaMap};
13+
use la_arena::Arena;
1414
use rustc_abi::{Align, Integer, IntegerType, ReprFlags, ReprOptions};
1515
use syntax::ast::{self, HasName, HasVisibility};
1616
use triomphe::Arc;
@@ -22,13 +22,11 @@ use crate::{
2222
lang_item::LangItem,
2323
lower::LowerCtx,
2424
nameres::diagnostics::{DefDiagnostic, DefDiagnostics},
25-
src::HasChildSource,
26-
src::HasSource,
2725
trace::Trace,
2826
tt::{Delimiter, DelimiterKind, Leaf, Subtree, TokenTree},
2927
type_ref::TypeRef,
3028
visibility::RawVisibility,
31-
EnumId, EnumVariantId, LocalFieldId, LocalModuleId, Lookup, StructId, UnionId, VariantId,
29+
EnumId, EnumVariantId, LocalFieldId, LocalModuleId, Lookup, StructId, UnionId,
3230
};
3331

3432
/// Note that we use `StructData` for unions as well!
@@ -387,54 +385,14 @@ impl VariantData {
387385
}
388386
}
389387

390-
impl HasChildSource<LocalFieldId> for VariantId {
391-
type Value = Either<ast::TupleField, ast::RecordField>;
392-
393-
fn child_source(&self, db: &dyn DefDatabase) -> InFile<ArenaMap<LocalFieldId, Self::Value>> {
394-
let item_tree;
395-
let (src, fields, container) = match *self {
396-
VariantId::EnumVariantId(it) => {
397-
let lookup = it.lookup(db);
398-
item_tree = lookup.id.item_tree(db);
399-
(
400-
lookup.source(db).map(|it| it.kind()),
401-
&item_tree[lookup.id.value].fields,
402-
lookup.parent.lookup(db).container,
403-
)
404-
}
405-
VariantId::StructId(it) => {
406-
let lookup = it.lookup(db);
407-
item_tree = lookup.id.item_tree(db);
408-
(
409-
lookup.source(db).map(|it| it.kind()),
410-
&item_tree[lookup.id.value].fields,
411-
lookup.container,
412-
)
413-
}
414-
VariantId::UnionId(it) => {
415-
let lookup = it.lookup(db);
416-
item_tree = lookup.id.item_tree(db);
417-
(
418-
lookup.source(db).map(|it| it.kind()),
419-
&item_tree[lookup.id.value].fields,
420-
lookup.container,
421-
)
422-
}
423-
};
424-
let mut trace = Trace::new_for_map();
425-
lower_struct(db, &mut trace, &src, container.krate, &item_tree, fields);
426-
src.with_value(trace.into_map())
427-
}
428-
}
429-
430388
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
431389
pub enum StructKind {
432390
Tuple,
433391
Record,
434392
Unit,
435393
}
436394

437-
fn lower_struct(
395+
pub(crate) fn lower_struct(
438396
db: &dyn DefDatabase,
439397
trace: &mut Trace<FieldData, Either<ast::TupleField, ast::RecordField>>,
440398
ast: &InFile<ast::StructKind>,

0 commit comments

Comments
 (0)