Skip to content

Commit 5df53c9

Browse files
committed
Auto merge of rust-lang#16351 - Veykril:eager-enum-variant, r=Veykril
internal: Eagerly lower enum variants in CrateDefMap construction
2 parents 4de8954 + 180e9b2 commit 5df53c9

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+673
-709
lines changed

crates/hir-def/src/attr.rs

+18-77
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@ use triomphe::Arc;
2424

2525
use crate::{
2626
db::DefDatabase,
27-
item_tree::{AttrOwner, Fields, ItemTreeId, ItemTreeNode},
27+
item_tree::{AttrOwner, Fields, ItemTreeId, ItemTreeModItemNode},
2828
lang_item::LangItem,
2929
nameres::{ModuleOrigin, ModuleSource},
3030
src::{HasChildSource, HasSource},
31-
AdtId, AssocItemLoc, AttrDefId, EnumId, GenericParamId, ItemLoc, LocalEnumVariantId,
32-
LocalFieldId, Lookup, MacroId, VariantId,
31+
AdtId, AssocItemLoc, AttrDefId, GenericParamId, ItemLoc, LocalFieldId, Lookup, MacroId,
32+
VariantId,
3333
};
3434

3535
#[derive(Default, Debug, Clone, PartialEq, Eq)]
@@ -70,33 +70,6 @@ impl ops::Deref for AttrsWithOwner {
7070
impl Attrs {
7171
pub const EMPTY: Self = Self(RawAttrs::EMPTY);
7272

73-
pub(crate) fn variants_attrs_query(
74-
db: &dyn DefDatabase,
75-
e: EnumId,
76-
) -> Arc<ArenaMap<LocalEnumVariantId, Attrs>> {
77-
let _p = profile::span("variants_attrs_query");
78-
// FIXME: There should be some proper form of mapping between item tree enum variant ids and hir enum variant ids
79-
let mut res = ArenaMap::default();
80-
81-
let loc = e.lookup(db);
82-
let krate = loc.container.krate;
83-
let item_tree = loc.id.item_tree(db);
84-
let enum_ = &item_tree[loc.id.value];
85-
let crate_graph = db.crate_graph();
86-
let cfg_options = &crate_graph[krate].cfg_options;
87-
88-
let mut idx = 0;
89-
for variant in enum_.variants.clone() {
90-
let attrs = item_tree.attrs(db, krate, variant.into());
91-
if attrs.is_cfg_enabled(cfg_options) {
92-
res.insert(Idx::from_raw(RawIdx::from(idx)), attrs);
93-
idx += 1;
94-
}
95-
}
96-
97-
Arc::new(res)
98-
}
99-
10073
pub(crate) fn fields_attrs_query(
10174
db: &dyn DefDatabase,
10275
v: VariantId,
@@ -108,29 +81,11 @@ impl Attrs {
10881
let crate_graph = db.crate_graph();
10982
let (fields, item_tree, krate) = match v {
11083
VariantId::EnumVariantId(it) => {
111-
let e = it.parent;
112-
let loc = e.lookup(db);
113-
let krate = loc.container.krate;
84+
let loc = it.lookup(db);
85+
let krate = loc.parent.lookup(db).container.krate;
11486
let item_tree = loc.id.item_tree(db);
115-
let enum_ = &item_tree[loc.id.value];
116-
117-
let cfg_options = &crate_graph[krate].cfg_options;
118-
119-
let Some(variant) = enum_
120-
.variants
121-
.clone()
122-
.filter(|variant| {
123-
let attrs = item_tree.attrs(db, krate, (*variant).into());
124-
attrs.is_cfg_enabled(cfg_options)
125-
})
126-
.zip(0u32..)
127-
.find(|(_variant, idx)| it.local_id == Idx::from_raw(RawIdx::from(*idx)))
128-
.map(|(variant, _idx)| variant)
129-
else {
130-
return Arc::new(res);
131-
};
132-
133-
(item_tree[variant].fields.clone(), item_tree, krate)
87+
let variant = &item_tree[loc.id.value];
88+
(variant.fields.clone(), item_tree, krate)
13489
}
13590
VariantId::StructId(it) => {
13691
let loc = it.lookup(db);
@@ -401,10 +356,12 @@ impl AttrsWithOwner {
401356
AttrDefId::FieldId(it) => {
402357
return db.fields_attrs(it.parent)[it.local_id].clone();
403358
}
359+
// FIXME: DRY this up
404360
AttrDefId::EnumVariantId(it) => {
405-
return db.variants_attrs(it.parent)[it.local_id].clone();
361+
let id = it.lookup(db).id;
362+
let tree = id.item_tree(db);
363+
tree.raw_attrs(id.value.into()).clone()
406364
}
407-
// FIXME: DRY this up
408365
AttrDefId::AdtId(it) => match it {
409366
AdtId::StructId(it) => attrs_from_item_tree_loc(db, it),
410367
AdtId::EnumId(it) => attrs_from_item_tree_loc(db, it),
@@ -503,12 +460,7 @@ impl AttrsWithOwner {
503460
AdtId::EnumId(id) => any_has_attrs(db, id),
504461
},
505462
AttrDefId::FunctionId(id) => any_has_attrs(db, id),
506-
AttrDefId::EnumVariantId(id) => {
507-
let map = db.variants_attrs_source_map(id.parent);
508-
let file_id = id.parent.lookup(db).id.file_id();
509-
let root = db.parse_or_expand(file_id);
510-
InFile::new(file_id, ast::AnyHasAttrs::new(map[id.local_id].to_node(&root)))
511-
}
463+
AttrDefId::EnumVariantId(id) => any_has_attrs(db, id),
512464
AttrDefId::StaticId(id) => any_has_attrs(db, id),
513465
AttrDefId::ConstId(id) => any_has_attrs(db, id),
514466
AttrDefId::TraitId(id) => any_has_attrs(db, id),
@@ -654,42 +606,31 @@ fn any_has_attrs<'db>(
654606
id.lookup(db).source(db).map(ast::AnyHasAttrs::new)
655607
}
656608

657-
fn attrs_from_item_tree<N: ItemTreeNode>(db: &dyn DefDatabase, id: ItemTreeId<N>) -> RawAttrs {
609+
fn attrs_from_item_tree<N: ItemTreeModItemNode>(
610+
db: &dyn DefDatabase,
611+
id: ItemTreeId<N>,
612+
) -> RawAttrs {
658613
let tree = id.item_tree(db);
659614
let mod_item = N::id_to_mod_item(id.value);
660615
tree.raw_attrs(mod_item.into()).clone()
661616
}
662617

663-
fn attrs_from_item_tree_loc<'db, N: ItemTreeNode>(
618+
fn attrs_from_item_tree_loc<'db, N: ItemTreeModItemNode>(
664619
db: &(dyn DefDatabase + 'db),
665620
lookup: impl Lookup<Database<'db> = dyn DefDatabase + 'db, Data = ItemLoc<N>>,
666621
) -> RawAttrs {
667622
let id = lookup.lookup(db).id;
668623
attrs_from_item_tree(db, id)
669624
}
670625

671-
fn attrs_from_item_tree_assoc<'db, N: ItemTreeNode>(
626+
fn attrs_from_item_tree_assoc<'db, N: ItemTreeModItemNode>(
672627
db: &(dyn DefDatabase + 'db),
673628
lookup: impl Lookup<Database<'db> = dyn DefDatabase + 'db, Data = AssocItemLoc<N>>,
674629
) -> RawAttrs {
675630
let id = lookup.lookup(db).id;
676631
attrs_from_item_tree(db, id)
677632
}
678633

679-
pub(crate) fn variants_attrs_source_map(
680-
db: &dyn DefDatabase,
681-
def: EnumId,
682-
) -> Arc<ArenaMap<LocalEnumVariantId, AstPtr<ast::Variant>>> {
683-
let mut res = ArenaMap::default();
684-
let child_source = def.child_source(db);
685-
686-
for (idx, variant) in child_source.value.iter() {
687-
res.insert(idx, AstPtr::new(variant));
688-
}
689-
690-
Arc::new(res)
691-
}
692-
693634
pub(crate) fn fields_attrs_source_map(
694635
db: &dyn DefDatabase,
695636
def: VariantId,

crates/hir-def/src/body.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use crate::{
2626
},
2727
nameres::DefMap,
2828
path::{ModPath, Path},
29-
src::{HasChildSource, HasSource},
29+
src::HasSource,
3030
BlockId, DefWithBodyId, HasModule, Lookup,
3131
};
3232

@@ -160,8 +160,9 @@ impl Body {
160160
src.map(|it| it.body())
161161
}
162162
DefWithBodyId::VariantId(v) => {
163-
let src = v.parent.child_source(db);
164-
src.map(|it| it[v.local_id].expr())
163+
let s = v.lookup(db);
164+
let src = s.source(db);
165+
src.map(|it| it.expr())
165166
}
166167
DefWithBodyId::InTypeConstId(c) => c.lookup(db).id.map(|_| c.source(db).expr()),
167168
}

crates/hir-def/src/body/pretty.rs

+19-25
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
use std::fmt::{self, Write};
44

55
use itertools::Itertools;
6-
use syntax::ast::HasName;
76

87
use crate::{
98
hir::{
@@ -19,35 +18,30 @@ use super::*;
1918
pub(super) fn print_body_hir(db: &dyn DefDatabase, body: &Body, owner: DefWithBodyId) -> String {
2019
let header = match owner {
2120
DefWithBodyId::FunctionId(it) => {
22-
let item_tree_id = it.lookup(db).id;
23-
format!(
24-
"fn {}",
25-
item_tree_id.item_tree(db)[item_tree_id.value].name.display(db.upcast())
26-
)
21+
it.lookup(db).id.resolved(db, |it| format!("fn {}", it.name.display(db.upcast())))
2722
}
28-
DefWithBodyId::StaticId(it) => {
29-
let item_tree_id = it.lookup(db).id;
23+
DefWithBodyId::StaticId(it) => it
24+
.lookup(db)
25+
.id
26+
.resolved(db, |it| format!("static {} = ", it.name.display(db.upcast()))),
27+
DefWithBodyId::ConstId(it) => it.lookup(db).id.resolved(db, |it| {
3028
format!(
31-
"static {} = ",
32-
item_tree_id.item_tree(db)[item_tree_id.value].name.display(db.upcast())
29+
"const {} = ",
30+
match &it.name {
31+
Some(name) => name.display(db.upcast()).to_string(),
32+
None => "_".to_string(),
33+
}
3334
)
34-
}
35-
DefWithBodyId::ConstId(it) => {
36-
let item_tree_id = it.lookup(db).id;
37-
let name = match &item_tree_id.item_tree(db)[item_tree_id.value].name {
38-
Some(name) => name.display(db.upcast()).to_string(),
39-
None => "_".to_string(),
40-
};
41-
format!("const {name} = ")
42-
}
35+
}),
4336
DefWithBodyId::InTypeConstId(_) => format!("In type const = "),
4437
DefWithBodyId::VariantId(it) => {
45-
let src = it.parent.child_source(db);
46-
let variant = &src.value[it.local_id];
47-
match &variant.name() {
48-
Some(name) => name.to_string(),
49-
None => "_".to_string(),
50-
}
38+
let loc = it.lookup(db);
39+
let enum_loc = loc.parent.lookup(db);
40+
format!(
41+
"enum {}::{}",
42+
enum_loc.id.item_tree(db)[enum_loc.id.value].name.display(db.upcast()),
43+
loc.id.item_tree(db)[loc.id.value].name.display(db.upcast()),
44+
)
5145
}
5246
};
5347

crates/hir-def/src/child_by_source.rs

+17-8
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ use crate::{
1313
item_scope::ItemScope,
1414
nameres::DefMap,
1515
src::{HasChildSource, HasSource},
16-
AdtId, AssocItemId, DefWithBodyId, EnumId, EnumVariantId, ExternCrateId, FieldId, ImplId,
17-
Lookup, MacroId, ModuleDefId, ModuleId, TraitId, UseId, VariantId,
16+
AdtId, AssocItemId, DefWithBodyId, EnumId, ExternCrateId, FieldId, ImplId, Lookup, MacroId,
17+
ModuleDefId, ModuleId, TraitId, UseId, VariantId,
1818
};
1919

2020
pub trait ChildBySource {
@@ -204,13 +204,22 @@ impl ChildBySource for VariantId {
204204
}
205205

206206
impl ChildBySource for EnumId {
207-
fn child_by_source_to(&self, db: &dyn DefDatabase, res: &mut DynMap, _: HirFileId) {
208-
let arena_map = self.child_source(db);
209-
let arena_map = arena_map.as_ref();
210-
for (local_id, source) in arena_map.value.iter() {
211-
let id = EnumVariantId { parent: *self, local_id };
212-
res[keys::VARIANT].insert(source.clone(), id)
207+
fn child_by_source_to(&self, db: &dyn DefDatabase, res: &mut DynMap, file_id: HirFileId) {
208+
let loc = &self.lookup(db);
209+
if file_id != loc.id.file_id() {
210+
return;
213211
}
212+
213+
let tree = loc.id.item_tree(db);
214+
let ast_id_map = db.ast_id_map(loc.id.file_id());
215+
let root = db.parse_or_expand(loc.id.file_id());
216+
217+
db.enum_data(*self).variants.iter().for_each(|&(variant, _)| {
218+
res[keys::ENUM_VARIANT].insert(
219+
ast_id_map.get(tree[variant.lookup(db).id.value].ast_id).to_node(&root),
220+
variant,
221+
);
222+
});
214223
}
215224
}
216225

crates/hir-def/src/data.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use crate::{
1919
macro_call_as_call_id,
2020
nameres::{
2121
attr_resolution::ResolvedAttr,
22-
diagnostics::DefDiagnostic,
22+
diagnostics::{DefDiagnostic, DefDiagnostics},
2323
proc_macro::{parse_macro_name_and_helper_attrs, ProcMacroKind},
2424
DefMap, MacroSubNs,
2525
},
@@ -240,7 +240,7 @@ impl TraitData {
240240
pub(crate) fn trait_data_with_diagnostics_query(
241241
db: &dyn DefDatabase,
242242
tr: TraitId,
243-
) -> (Arc<TraitData>, Arc<[DefDiagnostic]>) {
243+
) -> (Arc<TraitData>, DefDiagnostics) {
244244
let tr_loc @ ItemLoc { container: module_id, id: tree_id } = tr.lookup(db);
245245
let item_tree = tree_id.item_tree(db);
246246
let tr_def = &item_tree[tree_id.value];
@@ -274,7 +274,7 @@ impl TraitData {
274274
rustc_has_incoherent_inherent_impls,
275275
fundamental,
276276
}),
277-
diagnostics.into(),
277+
DefDiagnostics::new(diagnostics),
278278
)
279279
}
280280

@@ -340,7 +340,7 @@ impl ImplData {
340340
pub(crate) fn impl_data_with_diagnostics_query(
341341
db: &dyn DefDatabase,
342342
id: ImplId,
343-
) -> (Arc<ImplData>, Arc<[DefDiagnostic]>) {
343+
) -> (Arc<ImplData>, DefDiagnostics) {
344344
let _p = profile::span("impl_data_with_diagnostics_query");
345345
let ItemLoc { container: module_id, id: tree_id } = id.lookup(db);
346346

@@ -367,7 +367,7 @@ impl ImplData {
367367
is_unsafe,
368368
attribute_calls,
369369
}),
370-
diagnostics.into(),
370+
DefDiagnostics::new(diagnostics),
371371
)
372372
}
373373

0 commit comments

Comments
 (0)