1use rustc_hir::Attribute;
2use rustc_hir::def::DefKind;
3use rustc_hir::def_id::LocalDefId;
4use rustc_middle::span_bug;
5use rustc_middle::ty::layout::{FnAbiError, LayoutError};
6use rustc_middle::ty::{self, GenericArgs, Instance, Ty, TyCtxt};
7use rustc_span::source_map::Spanned;
8use rustc_span::sym;
9use rustc_target::callconv::FnAbi;
10
11use super::layout_test::ensure_wf;
12use crate::errors::{AbiInvalidAttribute, AbiNe, AbiOf, UnrecognizedField};
13
14pub fn test_abi(tcx: TyCtxt<'_>) {
15 if !tcx.features().rustc_attrs() {
16 return;
18 }
19 for id in tcx.hir_crate_items(()).definitions() {
20 for attr in tcx.get_attrs(id, sym::rustc_abi) {
21 match tcx.def_kind(id) {
22 DefKind::Fn | DefKind::AssocFn => {
23 dump_abi_of_fn_item(tcx, id, attr);
24 }
25 DefKind::TyAlias => {
26 dump_abi_of_fn_type(tcx, id, attr);
27 }
28 _ => {
29 tcx.dcx().emit_err(AbiInvalidAttribute { span: tcx.def_span(id) });
30 }
31 }
32 }
33 }
34}
35
36fn unwrap_fn_abi<'tcx>(
37 abi: Result<&'tcx FnAbi<'tcx, Ty<'tcx>>, &'tcx FnAbiError<'tcx>>,
38 tcx: TyCtxt<'tcx>,
39 item_def_id: LocalDefId,
40) -> &'tcx FnAbi<'tcx, Ty<'tcx>> {
41 match abi {
42 Ok(abi) => abi,
43 Err(FnAbiError::Layout(layout_error)) => {
44 tcx.dcx().emit_fatal(Spanned {
45 node: layout_error.into_diagnostic(),
46 span: tcx.def_span(item_def_id),
47 });
48 }
49 }
50}
51
52fn dump_abi_of_fn_item(tcx: TyCtxt<'_>, item_def_id: LocalDefId, attr: &Attribute) {
53 let typing_env = ty::TypingEnv::post_analysis(tcx, item_def_id);
54 let args = GenericArgs::identity_for_item(tcx, item_def_id);
55 let instance = match Instance::try_resolve(tcx, typing_env, item_def_id.into(), args) {
56 Ok(Some(instance)) => instance,
57 Ok(None) => {
58 let ty = tcx.type_of(item_def_id).instantiate_identity();
60 tcx.dcx().emit_fatal(Spanned {
61 node: LayoutError::Unknown(ty).into_diagnostic(),
62
63 span: tcx.def_span(item_def_id),
64 });
65 }
66 Err(_guaranteed) => return,
67 };
68 let abi = unwrap_fn_abi(
69 tcx.fn_abi_of_instance(
70 typing_env.as_query_input((instance, ty::List::empty())),
71 ),
72 tcx,
73 item_def_id,
74 );
75
76 let meta_items = attr.meta_item_list().unwrap_or_default();
79 for meta_item in meta_items {
80 match meta_item.name_or_empty() {
81 sym::debug => {
82 let fn_name = tcx.item_name(item_def_id.into());
83 tcx.dcx().emit_err(AbiOf {
84 span: tcx.def_span(item_def_id),
85 fn_name,
86 fn_abi: format!("{:#?}", abi),
88 });
89 }
90
91 name => {
92 tcx.dcx().emit_err(UnrecognizedField { span: meta_item.span(), name });
93 }
94 }
95 }
96}
97
98fn test_abi_eq<'tcx>(abi1: &'tcx FnAbi<'tcx, Ty<'tcx>>, abi2: &'tcx FnAbi<'tcx, Ty<'tcx>>) -> bool {
99 if abi1.conv != abi2.conv
100 || abi1.args.len() != abi2.args.len()
101 || abi1.c_variadic != abi2.c_variadic
102 || abi1.fixed_count != abi2.fixed_count
103 || abi1.can_unwind != abi2.can_unwind
104 {
105 return false;
106 }
107
108 abi1.ret.eq_abi(&abi2.ret)
109 && abi1.args.iter().zip(abi2.args.iter()).all(|(arg1, arg2)| arg1.eq_abi(arg2))
110}
111
112fn dump_abi_of_fn_type(tcx: TyCtxt<'_>, item_def_id: LocalDefId, attr: &Attribute) {
113 let typing_env = ty::TypingEnv::post_analysis(tcx, item_def_id);
114 let ty = tcx.type_of(item_def_id).instantiate_identity();
115 let span = tcx.def_span(item_def_id);
116 if !ensure_wf(tcx, typing_env, ty, item_def_id, span) {
117 return;
118 }
119 let meta_items = attr.meta_item_list().unwrap_or_default();
120 for meta_item in meta_items {
121 match meta_item.name_or_empty() {
122 sym::debug => {
123 let ty::FnPtr(sig_tys, hdr) = ty.kind() else {
124 span_bug!(
125 meta_item.span(),
126 "`#[rustc_abi(debug)]` on a type alias requires function pointer type"
127 );
128 };
129 let abi = unwrap_fn_abi(
130 tcx.fn_abi_of_fn_ptr(typing_env.as_query_input((
131 sig_tys.with(*hdr),
132 ty::List::empty(),
133 ))),
134 tcx,
135 item_def_id,
136 );
137
138 let fn_name = tcx.item_name(item_def_id.into());
139 tcx.dcx().emit_err(AbiOf { span, fn_name, fn_abi: format!("{:#?}", abi) });
140 }
141 sym::assert_eq => {
142 let ty::Tuple(fields) = ty.kind() else {
143 span_bug!(
144 meta_item.span(),
145 "`#[rustc_abi(assert_eq)]` on a type alias requires pair type"
146 );
147 };
148 let [field1, field2] = ***fields else {
149 span_bug!(
150 meta_item.span(),
151 "`#[rustc_abi(assert_eq)]` on a type alias requires pair type"
152 );
153 };
154 let ty::FnPtr(sig_tys1, hdr1) = field1.kind() else {
155 span_bug!(
156 meta_item.span(),
157 "`#[rustc_abi(assert_eq)]` on a type alias requires pair of function pointer types"
158 );
159 };
160 let abi1 = unwrap_fn_abi(
161 tcx.fn_abi_of_fn_ptr(typing_env.as_query_input((
162 sig_tys1.with(*hdr1),
163 ty::List::empty(),
164 ))),
165 tcx,
166 item_def_id,
167 );
168 let ty::FnPtr(sig_tys2, hdr2) = field2.kind() else {
169 span_bug!(
170 meta_item.span(),
171 "`#[rustc_abi(assert_eq)]` on a type alias requires pair of function pointer types"
172 );
173 };
174 let abi2 = unwrap_fn_abi(
175 tcx.fn_abi_of_fn_ptr(typing_env.as_query_input((
176 sig_tys2.with(*hdr2),
177 ty::List::empty(),
178 ))),
179 tcx,
180 item_def_id,
181 );
182
183 if !test_abi_eq(abi1, abi2) {
184 tcx.dcx().emit_err(AbiNe {
185 span,
186 left: format!("{:#?}", abi1),
187 right: format!("{:#?}", abi2),
188 });
189 }
190 }
191 name => {
192 tcx.dcx().emit_err(UnrecognizedField { span: meta_item.span(), name });
193 }
194 }
195 }
196}