1use rustc_data_structures::fx::FxIndexMap;
2use rustc_hir::attrs::{AttributeKind, EiiDecl, EiiImpl, EiiImplResolution};
3use rustc_hir::def_id::DefId;
4use rustc_hir::find_attr;
5use rustc_middle::query::LocalCrate;
6use rustc_middle::ty::TyCtxt;
78// basically the map below but flattened out
9pub(crate) type EiiMapEncodedKeyValue = (DefId, (EiiDecl, Vec<(DefId, EiiImpl)>));
1011pub(crate) type EiiMap = FxIndexMap<
12DefId, // the defid of the foreign item associated with the eii
13(
14// the corresponding declaration
15EiiDecl,
16// all the given implementations, indexed by defid.
17 // We expect there to be only one, but collect them all to give errors if there are more
18 // (or if there are none) in the final crate we build.
19FxIndexMap<DefId, EiiImpl>,
20 ),
21>;
2223pub(crate) fn collect<'tcx>(tcx: TyCtxt<'tcx>, LocalCrate: LocalCrate) -> EiiMap {
24let mut eiis = EiiMap::default();
2526// iterate over all items in the current crate
27for id in tcx.hir_crate_items(()).eiis() {
28for i in
29{
'done:
{
for i in tcx.get_all_attrs(id) {
let i: &rustc_hir::Attribute = i;
match i {
rustc_hir::Attribute::Parsed(AttributeKind::EiiImpls(e)) => {
break 'done Some(e);
}
_ => {}
}
}
None
}
}find_attr!(tcx.get_all_attrs(id), AttributeKind::EiiImpls(e) => e).into_iter().flatten()
30 {
31let decl = match i.resolution {
32 EiiImplResolution::Macro(macro_defid) => {
33// find the decl for this one if it wasn't in yet (maybe it's from the local crate? not very useful but not illegal)
34let Some(decl) = {
'done:
{
for i in tcx.get_all_attrs(macro_defid) {
let i: &rustc_hir::Attribute = i;
match i {
rustc_hir::Attribute::Parsed(AttributeKind::EiiDeclaration(d))
=> {
break 'done Some(*d);
}
_ => {}
}
}
None
}
}find_attr!(tcx.get_all_attrs(macro_defid), AttributeKind::EiiDeclaration(d) => *d)35else {
36// skip if it doesn't have eii_declaration (if we resolved to another macro that's not an EII)
37tcx.dcx()
38 .span_delayed_bug(i.span, "resolved to something that's not an EII");
39continue;
40 };
41 decl
42 }
43 EiiImplResolution::Known(decl) => decl,
44 EiiImplResolution::Error(_eg) => continue,
45 };
4647// FIXME(eii) remove extern target from encoded decl
48eiis.entry(decl.foreign_item)
49 .or_insert_with(|| (decl, Default::default()))
50 .1
51.insert(id.into(), *i);
52 }
5354// if we find a new declaration, add it to the list without a known implementation
55if let Some(decl) =
56{
'done:
{
for i in tcx.get_all_attrs(id) {
let i: &rustc_hir::Attribute = i;
match i {
rustc_hir::Attribute::Parsed(AttributeKind::EiiDeclaration(d))
=> {
break 'done Some(*d);
}
_ => {}
}
}
None
}
}find_attr!(tcx.get_all_attrs(id), AttributeKind::EiiDeclaration(d) => *d)57 {
58 eiis.entry(decl.foreign_item).or_insert((decl, Default::default()));
59 }
60 }
6162eiis63}