rustc_hir_analysis/collect/
dump.rs1use rustc_hir as hir;
2use rustc_hir::def::DefKind;
3use rustc_hir::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.hir_krate_attrs() {
#[allow(unused_imports)]
use rustc_hir::attrs::AttributeKind::*;
let i: &rustc_hir::Attribute = i;
match i {
rustc_hir::Attribute::Parsed(RustcHiddenTypeOfOpaques) => {
break 'done Some(());
}
rustc_hir::Attribute::Unparsed(..) =>
{}
#[deny(unreachable_patterns)]
_ => {}
}
}
None
}
}.is_some()find_attr!(tcx, crate, 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 #[expect(deprecated)] let attrs = tcx.get_all_attrs(id);
33
34 if {
{
'done:
{
for i in attrs {
#[allow(unused_imports)]
use rustc_hir::attrs::AttributeKind::*;
let i: &rustc_hir::Attribute = i;
match i {
rustc_hir::Attribute::Parsed(RustcDumpPredicates) => {
break 'done Some(());
}
rustc_hir::Attribute::Unparsed(..) =>
{}
#[deny(unreachable_patterns)]
_ => {}
}
}
None
}
}.is_some()
}find_attr!(attrs, RustcDumpPredicates) {
35 let preds = tcx.predicates_of(id).instantiate_identity(tcx).predicates;
36 let span = tcx.def_span(id);
37
38 let mut diag = tcx.dcx().struct_span_err(span, sym::rustc_dump_predicates.as_str());
39 for pred in preds {
40 diag.note(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0:?}", pred))
})format!("{pred:?}"));
41 }
42 diag.emit();
43 }
44
45 if {
{
'done:
{
for i in attrs {
#[allow(unused_imports)]
use rustc_hir::attrs::AttributeKind::*;
let i: &rustc_hir::Attribute = i;
match i {
rustc_hir::Attribute::Parsed(RustcDumpItemBounds) => {
break 'done Some(());
}
rustc_hir::Attribute::Unparsed(..) =>
{}
#[deny(unreachable_patterns)]
_ => {}
}
}
None
}
}.is_some()
}find_attr!(attrs, RustcDumpItemBounds) {
46 let name = sym::rustc_dump_item_bounds.as_str();
47
48 match tcx.def_kind(id) {
49 DefKind::AssocTy => {
50 let bounds = tcx.item_bounds(id).instantiate_identity();
51 let span = tcx.def_span(id);
52
53 let mut diag = tcx.dcx().struct_span_err(span, name);
54 for bound in bounds {
55 diag.note(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0:?}", bound))
})format!("{bound:?}"));
56 }
57 diag.emit()
58 }
59 kind => tcx.dcx().span_delayed_bug(
60 tcx.def_span(id),
61 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("attr parsing didn\'t report an error for `#[{0}]` on {1:?}",
name, kind))
})format!("attr parsing didn't report an error for `#[{name}]` on {kind:?}"),
62 ),
63 };
64 }
65 }
66}
67
68pub(crate) fn def_parents(tcx: TyCtxt<'_>) {
69 for iid in tcx.hir_free_items() {
70 let did = iid.owner_id.def_id;
71 if {
#[allow(deprecated)]
{
{
'done:
{
for i in tcx.get_all_attrs(did) {
#[allow(unused_imports)]
use rustc_hir::attrs::AttributeKind::*;
let i: &rustc_hir::Attribute = i;
match i {
rustc_hir::Attribute::Parsed(RustcDumpDefParents) => {
break 'done Some(());
}
rustc_hir::Attribute::Unparsed(..) =>
{}
#[deny(unreachable_patterns)]
_ => {}
}
}
None
}
}
}
}.is_some()find_attr!(tcx, did, RustcDumpDefParents) {
72 struct AnonConstFinder<'tcx> {
73 tcx: TyCtxt<'tcx>,
74 anon_consts: Vec<LocalDefId>,
75 }
76
77 impl<'tcx> intravisit::Visitor<'tcx> for AnonConstFinder<'tcx> {
78 type NestedFilter = nested_filter::All;
79
80 fn maybe_tcx(&mut self) -> Self::MaybeTyCtxt {
81 self.tcx
82 }
83
84 fn visit_anon_const(&mut self, c: &'tcx rustc_hir::AnonConst) {
85 self.anon_consts.push(c.def_id);
86 intravisit::walk_anon_const(self, c)
87 }
88 }
89
90 let mut anon_ct_finder = AnonConstFinder { tcx, anon_consts: ::alloc::vec::Vec::new()vec![] };
94 intravisit::walk_item(&mut anon_ct_finder, tcx.hir_item(iid));
95
96 for did in [did].into_iter().chain(anon_ct_finder.anon_consts) {
97 let span = tcx.def_span(did);
98
99 let mut diag = tcx.dcx().struct_span_err(
100 span,
101 ::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()),
102 );
103
104 let mut current_did = did.to_def_id();
105 while let Some(parent_did) = tcx.opt_parent(current_did) {
106 current_did = parent_did;
107 diag.span_note(tcx.def_span(parent_did), ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0:?}", parent_did))
})format!("{parent_did:?}"));
108 }
109 diag.emit();
110 }
111 }
112 }
113}
114
115pub(crate) fn vtables<'tcx>(tcx: TyCtxt<'tcx>) {
116 for id in tcx.hir_free_items() {
117 let def_id = id.owner_id.def_id;
118
119 let Some(&attr_span) = {
#[allow(deprecated)]
{
{
'done:
{
for i in tcx.get_all_attrs(def_id) {
#[allow(unused_imports)]
use rustc_hir::attrs::AttributeKind::*;
let i: &rustc_hir::Attribute = i;
match i {
rustc_hir::Attribute::Parsed(RustcDumpVtable(span)) => {
break 'done Some(span);
}
rustc_hir::Attribute::Unparsed(..) =>
{}
#[deny(unreachable_patterns)]
_ => {}
}
}
None
}
}
}
}find_attr!(tcx, def_id, RustcDumpVtable(span) => span) else {
120 continue;
121 };
122
123 let vtable_entries = match tcx.hir_item(id).kind {
124 hir::ItemKind::Impl(hir::Impl { of_trait: Some(_), .. }) => {
125 let trait_ref = tcx.impl_trait_ref(def_id).instantiate_identity();
126 if trait_ref.has_non_region_param() {
127 tcx.dcx().span_err(
128 attr_span,
129 "`rustc_dump_vtable` must be applied to non-generic impl",
130 );
131 continue;
132 }
133 if !tcx.is_dyn_compatible(trait_ref.def_id) {
134 tcx.dcx().span_err(
135 attr_span,
136 "`rustc_dump_vtable` must be applied to dyn-compatible trait",
137 );
138 continue;
139 }
140 let Ok(trait_ref) = tcx
141 .try_normalize_erasing_regions(ty::TypingEnv::fully_monomorphized(), trait_ref)
142 else {
143 tcx.dcx().span_err(
144 attr_span,
145 "`rustc_dump_vtable` applied to impl header that cannot be normalized",
146 );
147 continue;
148 };
149 tcx.vtable_entries(trait_ref)
150 }
151 hir::ItemKind::TyAlias(..) => {
152 let ty = tcx.type_of(def_id).instantiate_identity();
153 if ty.has_non_region_param() {
154 tcx.dcx().span_err(
155 attr_span,
156 "`rustc_dump_vtable` must be applied to non-generic type",
157 );
158 continue;
159 }
160 let Ok(ty) =
161 tcx.try_normalize_erasing_regions(ty::TypingEnv::fully_monomorphized(), ty)
162 else {
163 tcx.dcx().span_err(
164 attr_span,
165 "`rustc_dump_vtable` applied to type alias that cannot be normalized",
166 );
167 continue;
168 };
169 let ty::Dynamic(data, _) = *ty.kind() else {
170 tcx.dcx().span_err(attr_span, "`rustc_dump_vtable` to type alias of dyn type");
171 continue;
172 };
173 if let Some(principal) = data.principal() {
174 tcx.vtable_entries(
175 tcx.instantiate_bound_regions_with_erased(principal).with_self_ty(tcx, ty),
176 )
177 } else {
178 TyCtxt::COMMON_VTABLE_ENTRIES
179 }
180 }
181 _ => {
182 tcx.dcx().span_err(
183 attr_span,
184 "`rustc_dump_vtable` only applies to impl, or type alias of dyn type",
185 );
186 continue;
187 }
188 };
189
190 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:#?}"));
191 }
192}