Skip to main content

rustc_hir_analysis/collect/
dump.rs

1use rustc_hir as hir;
2use rustc_hir::attrs::AttributeKind;
3use rustc_hir::def_id::{CRATE_DEF_ID, LocalDefId};
4use rustc_hir::{find_attr, intravisit};
5use rustc_middle::hir::nested_filter;
6use rustc_middle::ty::{self, TyCtxt, TypeVisitableExt};
7use rustc_span::sym;
8
9pub(crate) fn opaque_hidden_types(tcx: TyCtxt<'_>) {
10    if !{
    {
            'done:
                {
                for i in tcx.get_all_attrs(CRATE_DEF_ID) {
                    let i: &rustc_hir::Attribute = i;
                    match i {
                        rustc_hir::Attribute::Parsed(AttributeKind::RustcHiddenTypeOfOpaques)
                            => {
                            break 'done Some(());
                        }
                        _ => {}
                    }
                }
                None
            }
        }.is_some()
}find_attr!(tcx.get_all_attrs(CRATE_DEF_ID), AttributeKind::RustcHiddenTypeOfOpaques) {
11        return;
12    }
13    for id in tcx.hir_crate_items(()).opaques() {
14        if let hir::OpaqueTyOrigin::FnReturn { parent: fn_def_id, .. }
15        | hir::OpaqueTyOrigin::AsyncFn { parent: fn_def_id, .. } =
16            tcx.hir_expect_opaque_ty(id).origin
17            && let hir::Node::TraitItem(trait_item) = tcx.hir_node_by_def_id(fn_def_id)
18            && let (_, hir::TraitFn::Required(..)) = trait_item.expect_fn()
19        {
20            continue;
21        }
22
23        let ty = tcx.type_of(id).instantiate_identity();
24        let span = tcx.def_span(id);
25        tcx.dcx().emit_err(crate::errors::TypeOf { span, ty });
26    }
27}
28
29pub(crate) fn predicates_and_item_bounds(tcx: TyCtxt<'_>) {
30    for id in tcx.hir_crate_items(()).owners() {
31        if {
    {
            'done:
                {
                for i in tcx.get_all_attrs(id) {
                    let i: &rustc_hir::Attribute = i;
                    match i {
                        rustc_hir::Attribute::Parsed(AttributeKind::RustcDumpPredicates)
                            => {
                            break 'done Some(());
                        }
                        _ => {}
                    }
                }
                None
            }
        }.is_some()
}find_attr!(tcx.get_all_attrs(id), AttributeKind::RustcDumpPredicates) {
32            let preds = tcx.predicates_of(id).instantiate_identity(tcx).predicates;
33            let span = tcx.def_span(id);
34
35            let mut diag = tcx.dcx().struct_span_err(span, sym::rustc_dump_predicates.as_str());
36            for pred in preds {
37                diag.note(::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{0:?}", pred))
    })format!("{pred:?}"));
38            }
39            diag.emit();
40        }
41        if {
    {
            'done:
                {
                for i in tcx.get_all_attrs(id) {
                    let i: &rustc_hir::Attribute = i;
                    match i {
                        rustc_hir::Attribute::Parsed(AttributeKind::RustcDumpItemBounds)
                            => {
                            break 'done Some(());
                        }
                        _ => {}
                    }
                }
                None
            }
        }.is_some()
}find_attr!(tcx.get_all_attrs(id), AttributeKind::RustcDumpItemBounds) {
42            let bounds = tcx.item_bounds(id).instantiate_identity();
43            let span = tcx.def_span(id);
44
45            let mut diag = tcx.dcx().struct_span_err(span, sym::rustc_dump_item_bounds.as_str());
46            for bound in bounds {
47                diag.note(::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{0:?}", bound))
    })format!("{bound:?}"));
48            }
49            diag.emit();
50        }
51    }
52}
53
54pub(crate) fn def_parents(tcx: TyCtxt<'_>) {
55    for iid in tcx.hir_free_items() {
56        let did = iid.owner_id.def_id;
57        if {
    {
            'done:
                {
                for i in tcx.get_all_attrs(did) {
                    let i: &rustc_hir::Attribute = i;
                    match i {
                        rustc_hir::Attribute::Parsed(AttributeKind::RustcDumpDefParents)
                            => {
                            break 'done Some(());
                        }
                        _ => {}
                    }
                }
                None
            }
        }.is_some()
}find_attr!(tcx.get_all_attrs(did), AttributeKind::RustcDumpDefParents) {
58            struct AnonConstFinder<'tcx> {
59                tcx: TyCtxt<'tcx>,
60                anon_consts: Vec<LocalDefId>,
61            }
62
63            impl<'tcx> intravisit::Visitor<'tcx> for AnonConstFinder<'tcx> {
64                type NestedFilter = nested_filter::All;
65
66                fn maybe_tcx(&mut self) -> Self::MaybeTyCtxt {
67                    self.tcx
68                }
69
70                fn visit_anon_const(&mut self, c: &'tcx rustc_hir::AnonConst) {
71                    self.anon_consts.push(c.def_id);
72                    intravisit::walk_anon_const(self, c)
73                }
74            }
75
76            // Look for any anon consts inside of this item as there is no way to apply
77            // the `rustc_dump_def_parents` attribute to the anon const so it would not be possible
78            // to see what its def parent is.
79            let mut anon_ct_finder = AnonConstFinder { tcx, anon_consts: ::alloc::vec::Vec::new()vec![] };
80            intravisit::walk_item(&mut anon_ct_finder, tcx.hir_item(iid));
81
82            for did in [did].into_iter().chain(anon_ct_finder.anon_consts) {
83                let span = tcx.def_span(did);
84
85                let mut diag = tcx.dcx().struct_span_err(
86                    span,
87                    ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{0}: {1:?}",
                sym::rustc_dump_def_parents.as_str(), did))
    })format!("{}: {did:?}", sym::rustc_dump_def_parents.as_str()),
88                );
89
90                let mut current_did = did.to_def_id();
91                while let Some(parent_did) = tcx.opt_parent(current_did) {
92                    current_did = parent_did;
93                    diag.span_note(tcx.def_span(parent_did), ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{0:?}", parent_did))
    })format!("{parent_did:?}"));
94                }
95                diag.emit();
96            }
97        }
98    }
99}
100
101pub(crate) fn vtables<'tcx>(tcx: TyCtxt<'tcx>) {
102    for id in tcx.hir_free_items() {
103        let def_id = id.owner_id.def_id;
104
105        let Some(&attr_span) =
106            {
    'done:
        {
        for i in tcx.get_all_attrs(def_id) {
            let i: &rustc_hir::Attribute = i;
            match i {
                rustc_hir::Attribute::Parsed(AttributeKind::RustcDumpVtable(span))
                    => {
                    break 'done Some(span);
                }
                _ => {}
            }
        }
        None
    }
}find_attr!(tcx.get_all_attrs(def_id), AttributeKind::RustcDumpVtable(span) => span)
107        else {
108            continue;
109        };
110
111        let vtable_entries = match tcx.hir_item(id).kind {
112            hir::ItemKind::Impl(hir::Impl { of_trait: Some(_), .. }) => {
113                let trait_ref = tcx.impl_trait_ref(def_id).instantiate_identity();
114                if trait_ref.has_non_region_param() {
115                    tcx.dcx().span_err(
116                        attr_span,
117                        "`rustc_dump_vtable` must be applied to non-generic impl",
118                    );
119                    continue;
120                }
121                if !tcx.is_dyn_compatible(trait_ref.def_id) {
122                    tcx.dcx().span_err(
123                        attr_span,
124                        "`rustc_dump_vtable` must be applied to dyn-compatible trait",
125                    );
126                    continue;
127                }
128                let Ok(trait_ref) = tcx
129                    .try_normalize_erasing_regions(ty::TypingEnv::fully_monomorphized(), trait_ref)
130                else {
131                    tcx.dcx().span_err(
132                        attr_span,
133                        "`rustc_dump_vtable` applied to impl header that cannot be normalized",
134                    );
135                    continue;
136                };
137                tcx.vtable_entries(trait_ref)
138            }
139            hir::ItemKind::TyAlias(..) => {
140                let ty = tcx.type_of(def_id).instantiate_identity();
141                if ty.has_non_region_param() {
142                    tcx.dcx().span_err(
143                        attr_span,
144                        "`rustc_dump_vtable` must be applied to non-generic type",
145                    );
146                    continue;
147                }
148                let Ok(ty) =
149                    tcx.try_normalize_erasing_regions(ty::TypingEnv::fully_monomorphized(), ty)
150                else {
151                    tcx.dcx().span_err(
152                        attr_span,
153                        "`rustc_dump_vtable` applied to type alias that cannot be normalized",
154                    );
155                    continue;
156                };
157                let ty::Dynamic(data, _) = *ty.kind() else {
158                    tcx.dcx().span_err(attr_span, "`rustc_dump_vtable` to type alias of dyn type");
159                    continue;
160                };
161                if let Some(principal) = data.principal() {
162                    tcx.vtable_entries(
163                        tcx.instantiate_bound_regions_with_erased(principal).with_self_ty(tcx, ty),
164                    )
165                } else {
166                    TyCtxt::COMMON_VTABLE_ENTRIES
167                }
168            }
169            _ => {
170                tcx.dcx().span_err(
171                    attr_span,
172                    "`rustc_dump_vtable` only applies to impl, or type alias of dyn type",
173                );
174                continue;
175            }
176        };
177
178        tcx.dcx().span_err(tcx.def_span(def_id), ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("vtable entries: {0:#?}",
                vtable_entries))
    })format!("vtable entries: {vtable_entries:#?}"));
179    }
180}