Skip to content

Commit 12f54ee

Browse files
committed
Split assoc items out of trait_data/impl_data queries
1 parent 185f9de commit 12f54ee

34 files changed

+446
-397
lines changed

crates/hir-def/src/data.rs

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

55
use base_db::Crate;
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, LocalDefMap, 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)]
@@ -256,23 +244,13 @@ bitflags::bitflags! {
256244
#[derive(Debug, Clone, PartialEq, Eq)]
257245
pub struct TraitData {
258246
pub name: Name,
259-
pub items: Box<[(Name, AssocItemId)]>,
260247
pub flags: TraitFlags,
261248
pub visibility: RawVisibility,
262-
// box it as the vec is usually empty anyways
263-
pub macro_calls: Option<Box<Vec<(AstId<ast::Item>, MacroCallId)>>>,
264249
}
265250

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

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

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

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

399334
let item_tree = tree_id.item_tree(db);
400335
let impl_def = &item_tree[tree_id.value];
@@ -403,28 +338,13 @@ impl ImplData {
403338
let is_negative = impl_def.is_negative;
404339
let is_unsafe = impl_def.is_unsafe;
405340

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

@@ -628,217 +548,6 @@ impl StaticData {
628548
}
629549
}
630550

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

0 commit comments

Comments
 (0)