1use rustc_attr_ir::{EiiDecl, EiiImpl, EiiImplResolution, find_attr};
2use rustc_data_structures::fx::FxIndexMap;
3use rustc_hir::def_id::DefId;
4use rustc_middle::query::LocalCrate;
5use rustc_middle::ty::TyCtxt;
6use rustc_span::bug;
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// Needed because Known only stores a DefId (not the full EiiDecl),
27 // and we can't call the externally_implementable_items query (cycle).
28let decls_by_foreign_item: FxIndexMap<DefId, EiiDecl> = tcx29 .hir_crate_items(())
30 .eiis()
31 .filter_map(|id| {
{
'done:
{
for i in ::rustc_attr_ir::HasAttrs::get_attrs(id, &tcx) {
#[allow(unused_imports)]
use ::rustc_attr_ir::AttributeKind::*;
let i: &::rustc_attr_ir::Attribute = i;
match i {
::rustc_attr_ir::Attribute::Parsed(EiiDeclaration(d)) => {
break 'done Some(*d);
}
::rustc_attr_ir::Attribute::Unparsed(..) =>
{}
#[deny(unreachable_patterns)]
_ => {}
}
}
None
}
}
}find_attr!(tcx, id, EiiDeclaration(d) => *d))
32 .map(|decl| (decl.foreign_item, decl))
33 .collect();
3435// iterate over all items in the current crate
36for id in tcx.hir_crate_items(()).eiis() {
37// if we find a new declaration, add it to the list without a known implementation
38if let Some(decl) = {
{
'done:
{
for i in ::rustc_attr_ir::HasAttrs::get_attrs(id, &tcx) {
#[allow(unused_imports)]
use ::rustc_attr_ir::AttributeKind::*;
let i: &::rustc_attr_ir::Attribute = i;
match i {
::rustc_attr_ir::Attribute::Parsed(EiiDeclaration(d)) => {
break 'done Some(*d);
}
::rustc_attr_ir::Attribute::Unparsed(..) =>
{}
#[deny(unreachable_patterns)]
_ => {}
}
}
None
}
}
}find_attr!(tcx, id, EiiDeclaration(d) => *d) {
39 eiis.entry(decl.foreign_item).or_insert((decl, Default::default()));
40 }
4142if let Some(i) = {
{
'done:
{
for i in ::rustc_attr_ir::HasAttrs::get_attrs(id, &tcx) {
#[allow(unused_imports)]
use ::rustc_attr_ir::AttributeKind::*;
let i: &::rustc_attr_ir::Attribute = i;
match i {
::rustc_attr_ir::Attribute::Parsed(EiiImpl(i)) => {
break 'done Some(i);
}
::rustc_attr_ir::Attribute::Unparsed(..) =>
{}
#[deny(unreachable_patterns)]
_ => {}
}
}
None
}
}
}find_attr!(tcx, id, EiiImpl(i) => i) {
43let (foreign_item, decl) = match i.resolution {
44 EiiImplResolution::Macro(macro_defid) => {
45// 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)
46let Some(decl) = {
{
'done:
{
for i in ::rustc_attr_ir::HasAttrs::get_attrs(macro_defid, &tcx) {
#[allow(unused_imports)]
use ::rustc_attr_ir::AttributeKind::*;
let i: &::rustc_attr_ir::Attribute = i;
match i {
::rustc_attr_ir::Attribute::Parsed(EiiDeclaration(d)) => {
break 'done Some(*d);
}
::rustc_attr_ir::Attribute::Unparsed(..) =>
{}
#[deny(unreachable_patterns)]
_ => {}
}
}
None
}
}
}find_attr!(tcx, macro_defid, EiiDeclaration(d) => *d)else {
47// skip if it doesn't have eii_declaration (if we resolved to another macro that's not an EII)
48tcx.dcx()
49 .span_delayed_bug(i.span, "resolved to something that's not an EII");
50continue;
51 };
52 (decl.foreign_item, decl)
53 }
54// Recover the EiiDecl from the local lookup map.
55EiiImplResolution::Known(foreign_item_did) => {
56let decl = decls_by_foreign_item.get(&foreign_item_did).unwrap_or_else(|| {
57::rustc_span::macros::bug_impl(None,
format_args!("EII impl has Known resolution but can\'t find EiiDeclaration for {0:?}",
foreign_item_did), Location::caller())bug!(
58"EII impl has Known resolution but can't find EiiDeclaration for {:?}",
59 foreign_item_did
60 )61 });
62 (foreign_item_did, *decl)
63 }
64 EiiImplResolution::Error(_eg) => continue,
65 };
6667 eiis.entry(foreign_item)
68 .or_insert_with(|| (decl, Default::default()))
69 .1
70.insert(id.into(), **i);
71 }
72 }
7374eiis75}