Skip to content

Commit 1924ab8

Browse files
committed
Split assoc items out of trait_data/impl_data queries
1 parent 2f5e8d7 commit 1924ab8

33 files changed

+439
-391
lines changed

crates/hir-def/src/data.rs

Lines changed: 15 additions & 301 deletions
Original file line numberDiff line numberDiff line change
@@ -3,33 +3,21 @@
33
pub mod adt;
44

55
use base_db::CrateId;
6-
use hir_expand::{
7-
name::Name, AstId, ExpandResult, HirFileId, InFile, MacroCallId, MacroCallKind, MacroDefKind,
8-
};
6+
use hir_expand::name::Name;
97
use intern::{sym, Symbol};
108
use la_arena::{Idx, RawIdx};
11-
use smallvec::SmallVec;
12-
use syntax::{ast, Parse};
139
use triomphe::Arc;
1410
use tt::iter::TtElement;
1511

1612
use crate::{
1713
db::DefDatabase,
18-
expander::{Expander, Mark},
19-
item_tree::{self, AssocItem, FnFlags, ItemTree, ItemTreeId, MacroCall, ModItem, TreeId},
20-
macro_call_as_call_id,
21-
nameres::{
22-
attr_resolution::ResolvedAttr,
23-
diagnostics::{DefDiagnostic, DefDiagnostics},
24-
proc_macro::{parse_macro_name_and_helper_attrs, ProcMacroKind},
25-
DefMap, MacroSubNs,
26-
},
14+
item_tree::{self, FnFlags, ModItem},
15+
nameres::proc_macro::{parse_macro_name_and_helper_attrs, ProcMacroKind},
2716
path::ImportAlias,
2817
type_ref::{TraitRef, TypeBound, TypeRefId, TypesMap},
2918
visibility::RawVisibility,
30-
AssocItemId, AstIdWithPath, ConstId, ConstLoc, ExternCrateId, FunctionId, FunctionLoc,
31-
HasModule, ImplId, Intern, ItemContainerId, ItemLoc, Lookup, Macro2Id, MacroRulesId, ModuleId,
32-
ProcMacroId, StaticId, TraitAliasId, TraitId, TypeAliasId, TypeAliasLoc,
19+
ConstId, ExternCrateId, FunctionId, HasModule, ImplId, ItemContainerId, ItemLoc, Lookup,
20+
Macro2Id, MacroRulesId, ProcMacroId, StaticId, TraitAliasId, TraitId, TypeAliasId,
3321
};
3422

3523
#[derive(Debug, Clone, PartialEq, Eq)]
@@ -257,23 +245,13 @@ bitflags::bitflags! {
257245
#[derive(Debug, Clone, PartialEq, Eq)]
258246
pub struct TraitData {
259247
pub name: Name,
260-
pub items: Box<[(Name, AssocItemId)]>,
261248
pub flags: TraitFlags,
262249
pub visibility: RawVisibility,
263-
// box it as the vec is usually empty anyways
264-
pub macro_calls: Option<Box<Vec<(AstId<ast::Item>, MacroCallId)>>>,
265250
}
266251

267252
impl TraitData {
268253
#[inline]
269254
pub(crate) fn trait_data_query(db: &dyn DefDatabase, tr: TraitId) -> Arc<TraitData> {
270-
db.trait_data_with_diagnostics(tr).0
271-
}
272-
273-
pub(crate) fn trait_data_with_diagnostics_query(
274-
db: &dyn DefDatabase,
275-
tr: TraitId,
276-
) -> (Arc<TraitData>, DefDiagnostics) {
277255
let ItemLoc { container: module_id, id: tree_id } = tr.lookup(db);
278256
let item_tree = tree_id.item_tree(db);
279257
let tr_def = &item_tree[tree_id.value];
@@ -318,40 +296,7 @@ impl TraitData {
318296
flags |= TraitFlags::SKIP_BOXED_SLICE_DURING_METHOD_DISPATCH;
319297
}
320298

321-
let mut collector =
322-
AssocItemCollector::new(db, module_id, tree_id.file_id(), ItemContainerId::TraitId(tr));
323-
collector.collect(&item_tree, tree_id.tree_id(), &tr_def.items);
324-
let (items, macro_calls, diagnostics) = collector.finish();
325-
326-
(
327-
Arc::new(TraitData { name, macro_calls, items, visibility, flags }),
328-
DefDiagnostics::new(diagnostics),
329-
)
330-
}
331-
332-
pub fn associated_types(&self) -> impl Iterator<Item = TypeAliasId> + '_ {
333-
self.items.iter().filter_map(|(_name, item)| match item {
334-
AssocItemId::TypeAliasId(t) => Some(*t),
335-
_ => None,
336-
})
337-
}
338-
339-
pub fn associated_type_by_name(&self, name: &Name) -> Option<TypeAliasId> {
340-
self.items.iter().find_map(|(item_name, item)| match item {
341-
AssocItemId::TypeAliasId(t) if item_name == name => Some(*t),
342-
_ => None,
343-
})
344-
}
345-
346-
pub fn method_by_name(&self, name: &Name) -> Option<FunctionId> {
347-
self.items.iter().find_map(|(item_name, item)| match item {
348-
AssocItemId::FunctionId(t) if item_name == name => Some(*t),
349-
_ => None,
350-
})
351-
}
352-
353-
pub fn attribute_calls(&self) -> impl Iterator<Item = (AstId<ast::Item>, MacroCallId)> + '_ {
354-
self.macro_calls.iter().flat_map(|it| it.iter()).copied()
299+
Arc::new(TraitData { name, visibility, flags })
355300
}
356301
}
357302

@@ -376,26 +321,16 @@ impl TraitAliasData {
376321
pub struct ImplData {
377322
pub target_trait: Option<TraitRef>,
378323
pub self_ty: TypeRefId,
379-
pub items: Box<[(Name, AssocItemId)]>,
380324
pub is_negative: bool,
381325
pub is_unsafe: bool,
382-
// box it as the vec is usually empty anyways
383-
pub macro_calls: Option<Box<Vec<(AstId<ast::Item>, MacroCallId)>>>,
384326
pub types_map: Arc<TypesMap>,
385327
}
386328

387329
impl ImplData {
388330
#[inline]
389331
pub(crate) fn impl_data_query(db: &dyn DefDatabase, id: ImplId) -> Arc<ImplData> {
390-
db.impl_data_with_diagnostics(id).0
391-
}
392-
393-
pub(crate) fn impl_data_with_diagnostics_query(
394-
db: &dyn DefDatabase,
395-
id: ImplId,
396-
) -> (Arc<ImplData>, DefDiagnostics) {
397-
let _p = tracing::info_span!("impl_data_with_diagnostics_query").entered();
398-
let ItemLoc { container: module_id, id: tree_id } = id.lookup(db);
332+
let _p = tracing::info_span!("impl_data_query").entered();
333+
let ItemLoc { id: tree_id, .. } = id.lookup(db);
399334

400335
let item_tree = tree_id.item_tree(db);
401336
let impl_def = &item_tree[tree_id.value];
@@ -404,28 +339,13 @@ impl ImplData {
404339
let is_negative = impl_def.is_negative;
405340
let is_unsafe = impl_def.is_unsafe;
406341

407-
let mut collector =
408-
AssocItemCollector::new(db, module_id, tree_id.file_id(), ItemContainerId::ImplId(id));
409-
collector.collect(&item_tree, tree_id.tree_id(), &impl_def.items);
410-
411-
let (items, macro_calls, diagnostics) = collector.finish();
412-
413-
(
414-
Arc::new(ImplData {
415-
target_trait,
416-
self_ty,
417-
items,
418-
is_negative,
419-
is_unsafe,
420-
macro_calls,
421-
types_map: impl_def.types_map.clone(),
422-
}),
423-
DefDiagnostics::new(diagnostics),
424-
)
425-
}
426-
427-
pub fn attribute_calls(&self) -> impl Iterator<Item = (AstId<ast::Item>, MacroCallId)> + '_ {
428-
self.macro_calls.iter().flat_map(|it| it.iter()).copied()
342+
Arc::new(ImplData {
343+
target_trait,
344+
self_ty,
345+
is_negative,
346+
is_unsafe,
347+
types_map: impl_def.types_map.clone(),
348+
})
429349
}
430350
}
431351

@@ -629,212 +549,6 @@ impl StaticData {
629549
}
630550
}
631551

632-
struct AssocItemCollector<'a> {
633-
db: &'a dyn DefDatabase,
634-
module_id: ModuleId,
635-
def_map: Arc<DefMap>,
636-
diagnostics: Vec<DefDiagnostic>,
637-
container: ItemContainerId,
638-
expander: Expander,
639-
640-
items: Vec<(Name, AssocItemId)>,
641-
macro_calls: Vec<(AstId<ast::Item>, MacroCallId)>,
642-
}
643-
644-
impl<'a> AssocItemCollector<'a> {
645-
fn new(
646-
db: &'a dyn DefDatabase,
647-
module_id: ModuleId,
648-
file_id: HirFileId,
649-
container: ItemContainerId,
650-
) -> Self {
651-
Self {
652-
db,
653-
module_id,
654-
def_map: module_id.def_map(db),
655-
container,
656-
expander: Expander::new(db, file_id, module_id),
657-
items: Vec::new(),
658-
macro_calls: Vec::new(),
659-
diagnostics: Vec::new(),
660-
}
661-
}
662-
663-
fn finish(
664-
self,
665-
) -> (
666-
Box<[(Name, AssocItemId)]>,
667-
Option<Box<Vec<(AstId<ast::Item>, MacroCallId)>>>,
668-
Vec<DefDiagnostic>,
669-
) {
670-
(
671-
self.items.into_boxed_slice(),
672-
if self.macro_calls.is_empty() { None } else { Some(Box::new(self.macro_calls)) },
673-
self.diagnostics,
674-
)
675-
}
676-
677-
fn collect(&mut self, item_tree: &ItemTree, tree_id: TreeId, assoc_items: &[AssocItem]) {
678-
let container = self.container;
679-
self.items.reserve(assoc_items.len());
680-
681-
'items: for &item in assoc_items {
682-
let attrs = item_tree.attrs(self.db, self.module_id.krate, ModItem::from(item).into());
683-
if !attrs.is_cfg_enabled(self.expander.cfg_options()) {
684-
self.diagnostics.push(DefDiagnostic::unconfigured_code(
685-
self.module_id.local_id,
686-
tree_id,
687-
ModItem::from(item).into(),
688-
attrs.cfg().unwrap(),
689-
self.expander.cfg_options().clone(),
690-
));
691-
continue;
692-
}
693-
694-
'attrs: for attr in &*attrs {
695-
let ast_id =
696-
AstId::new(self.expander.current_file_id(), item.ast_id(item_tree).upcast());
697-
let ast_id_with_path = AstIdWithPath { path: attr.path.clone(), ast_id };
698-
699-
match self.def_map.resolve_attr_macro(
700-
self.db,
701-
self.module_id.local_id,
702-
ast_id_with_path,
703-
attr,
704-
) {
705-
Ok(ResolvedAttr::Macro(call_id)) => {
706-
let loc = self.db.lookup_intern_macro_call(call_id);
707-
if let MacroDefKind::ProcMacro(_, exp, _) = loc.def.kind {
708-
// If there's no expander for the proc macro (e.g. the
709-
// proc macro is ignored, or building the proc macro
710-
// crate failed), skip expansion like we would if it was
711-
// disabled. This is analogous to the handling in
712-
// `DefCollector::collect_macros`.
713-
if let Some(err) = exp.as_expand_error(self.module_id.krate) {
714-
self.diagnostics.push(DefDiagnostic::macro_error(
715-
self.module_id.local_id,
716-
ast_id,
717-
(*attr.path).clone(),
718-
err,
719-
));
720-
continue 'attrs;
721-
}
722-
}
723-
724-
self.macro_calls.push((ast_id, call_id));
725-
let res =
726-
self.expander.enter_expand_id::<ast::MacroItems>(self.db, call_id);
727-
self.collect_macro_items(res);
728-
continue 'items;
729-
}
730-
Ok(_) => (),
731-
Err(_) => {
732-
self.diagnostics.push(DefDiagnostic::unresolved_macro_call(
733-
self.module_id.local_id,
734-
MacroCallKind::Attr {
735-
ast_id,
736-
attr_args: None,
737-
invoc_attr_index: attr.id,
738-
},
739-
attr.path().clone(),
740-
));
741-
}
742-
}
743-
}
744-
745-
self.collect_item(item_tree, tree_id, container, item);
746-
}
747-
}
748-
749-
fn collect_item(
750-
&mut self,
751-
item_tree: &ItemTree,
752-
tree_id: TreeId,
753-
container: ItemContainerId,
754-
item: AssocItem,
755-
) {
756-
match item {
757-
AssocItem::Function(id) => {
758-
let item = &item_tree[id];
759-
let def =
760-
FunctionLoc { container, id: ItemTreeId::new(tree_id, id) }.intern(self.db);
761-
self.items.push((item.name.clone(), def.into()));
762-
}
763-
AssocItem::TypeAlias(id) => {
764-
let item = &item_tree[id];
765-
let def =
766-
TypeAliasLoc { container, id: ItemTreeId::new(tree_id, id) }.intern(self.db);
767-
self.items.push((item.name.clone(), def.into()));
768-
}
769-
AssocItem::Const(id) => {
770-
let item = &item_tree[id];
771-
let Some(name) = item.name.clone() else { return };
772-
let def = ConstLoc { container, id: ItemTreeId::new(tree_id, id) }.intern(self.db);
773-
self.items.push((name, def.into()));
774-
}
775-
AssocItem::MacroCall(call) => {
776-
let file_id = self.expander.current_file_id();
777-
let MacroCall { ast_id, expand_to, ctxt, ref path } = item_tree[call];
778-
let module = self.expander.module.local_id;
779-
780-
let resolver = |path: &_| {
781-
self.def_map
782-
.resolve_path(
783-
self.db,
784-
module,
785-
path,
786-
crate::item_scope::BuiltinShadowMode::Other,
787-
Some(MacroSubNs::Bang),
788-
)
789-
.0
790-
.take_macros()
791-
.map(|it| self.db.macro_def(it))
792-
};
793-
match macro_call_as_call_id(
794-
self.db.upcast(),
795-
&AstIdWithPath::new(file_id, ast_id, Clone::clone(path)),
796-
ctxt,
797-
expand_to,
798-
self.expander.krate(),
799-
resolver,
800-
) {
801-
Ok(Some(call_id)) => {
802-
let res =
803-
self.expander.enter_expand_id::<ast::MacroItems>(self.db, call_id);
804-
self.macro_calls.push((InFile::new(file_id, ast_id.upcast()), call_id));
805-
self.collect_macro_items(res);
806-
}
807-
Ok(None) => (),
808-
Err(_) => {
809-
self.diagnostics.push(DefDiagnostic::unresolved_macro_call(
810-
self.module_id.local_id,
811-
MacroCallKind::FnLike {
812-
ast_id: InFile::new(file_id, ast_id),
813-
expand_to,
814-
eager: None,
815-
},
816-
Clone::clone(path),
817-
));
818-
}
819-
}
820-
}
821-
}
822-
}
823-
824-
fn collect_macro_items(&mut self, res: ExpandResult<Option<(Mark, Parse<ast::MacroItems>)>>) {
825-
let Some((mark, _parse)) = res.value else { return };
826-
827-
let tree_id = item_tree::TreeId::new(self.expander.current_file_id(), None);
828-
let item_tree = tree_id.item_tree(self.db);
829-
let iter: SmallVec<[_; 2]> =
830-
item_tree.top_level_items().iter().filter_map(ModItem::as_assoc_item).collect();
831-
832-
self.collect(&item_tree, tree_id, &iter);
833-
834-
self.expander.exit(mark);
835-
}
836-
}
837-
838552
fn trait_vis(db: &dyn DefDatabase, trait_id: TraitId) -> RawVisibility {
839553
let ItemLoc { id: tree_id, .. } = trait_id.lookup(db);
840554
let item_tree = tree_id.item_tree(db);

0 commit comments

Comments
 (0)