Skip to main content

rustc_metadata/
eii.rs

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;
7
8// basically the map below but flattened out
9pub(crate) type EiiMapEncodedKeyValue = (DefId, (EiiDecl, Vec<(DefId, EiiImpl)>));
10
11pub(crate) type EiiMap = FxIndexMap<
12    DefId, // the defid of the foreign item associated with the eii
13    (
14        // the corresponding declaration
15        EiiDecl,
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.
19        FxIndexMap<DefId, EiiImpl>,
20    ),
21>;
22
23pub(crate) fn collect<'tcx>(tcx: TyCtxt<'tcx>, LocalCrate: LocalCrate) -> EiiMap {
24    let mut eiis = EiiMap::default();
25
26    // Needed because Known only stores a DefId (not the full EiiDecl),
27    // and we can't call the externally_implementable_items query (cycle).
28    let decls_by_foreign_item: FxIndexMap<DefId, EiiDecl> = tcx
29        .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();
34
35    // iterate over all items in the current crate
36    for id in tcx.hir_crate_items(()).eiis() {
37        // if we find a new declaration, add it to the list without a known implementation
38        if 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        }
41
42        if 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) {
43            let (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)
46                    let 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)
48                        tcx.dcx()
49                            .span_delayed_bug(i.span, "resolved to something that's not an EII");
50                        continue;
51                    };
52                    (decl.foreign_item, decl)
53                }
54                // Recover the EiiDecl from the local lookup map.
55                EiiImplResolution::Known(foreign_item_did) => {
56                    let 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            };
66
67            eiis.entry(foreign_item)
68                .or_insert_with(|| (decl, Default::default()))
69                .1
70                .insert(id.into(), **i);
71        }
72    }
73
74    eiis
75}