1use rustc_attr_ir::{RustcAbiAttrKind, find_attr};
2use rustc_hir::def::DefKind;
3use rustc_hir::def_id::LocalDefId;
4use rustc_middle::ty::layout::{FnAbiError, LayoutError};
5use rustc_middle::ty::{self, GenericArgs, Instance, Ty, TyCtxt};
6use rustc_span::{Span, span_bug};
7use rustc_target::callconv::FnAbi;
8
9use super::layout_test::ensure_wf;
10use crate::diagnostics::{AbiInvalidAttribute, AbiNe, AbiOf, UnrecognizedArgument};
11
12pub fn test_abi(tcx: TyCtxt<'_>) {
13 if !tcx.features().rustc_attrs() {
14 return;
16 }
17 for id in tcx.hir_crate_items(()).definitions() {
18 let Some((attr_span, attr_kind)) =
19 {
{
'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(RustcAbi {
attr_span, kind }) => {
break 'done Some((*attr_span, *kind));
}
::rustc_attr_ir::Attribute::Unparsed(..) =>
{}
#[deny(unreachable_patterns)]
_ => {}
}
}
None
}
}
}find_attr!(tcx, id, RustcAbi{ attr_span, kind } => (*attr_span, *kind))
20 else {
21 continue;
22 };
23 match tcx.def_kind(id) {
24 DefKind::Fn | DefKind::AssocFn => {
25 dump_abi_of_fn_item(tcx, id, attr_span, attr_kind);
26 }
27 DefKind::TyAlias => {
28 dump_abi_of_fn_type(tcx, id, attr_span, attr_kind);
29 }
30 _ => {
31 tcx.dcx().emit_err(AbiInvalidAttribute { span: tcx.def_span(id) });
32 }
33 }
34 }
35}
36
37fn unwrap_fn_abi<'tcx>(
38 abi: Result<&'tcx FnAbi<'tcx, Ty<'tcx>>, &'tcx FnAbiError<'tcx>>,
39 tcx: TyCtxt<'tcx>,
40 item_def_id: LocalDefId,
41) -> &'tcx FnAbi<'tcx, Ty<'tcx>> {
42 match abi {
43 Ok(abi) => abi,
44 Err(FnAbiError::Layout(layout_error)) => {
45 tcx.dcx().span_fatal(tcx.def_span(item_def_id), layout_error.to_string());
46 }
47 }
48}
49
50fn dump_abi_of_fn_item(
51 tcx: TyCtxt<'_>,
52 item_def_id: LocalDefId,
53 attr_span: Span,
54 attr_kind: RustcAbiAttrKind,
55) {
56 let typing_env = ty::TypingEnv::post_analysis(tcx, item_def_id);
57 let args = GenericArgs::identity_for_item(tcx, item_def_id);
58 let instance = match Instance::try_resolve(tcx, typing_env, item_def_id.into(), args) {
59 Ok(Some(instance)) => instance,
60 Ok(None) => {
61 let ty = tcx.type_of(item_def_id).instantiate_identity().skip_norm_wip();
63 tcx.dcx().span_fatal(tcx.def_span(item_def_id), LayoutError::Unknown(ty).to_string());
64 }
65 Err(_guaranteed) => return,
66 };
67 let abi = unwrap_fn_abi(
68 tcx.fn_abi_of_instance(
69 typing_env.as_query_input((instance, ty::List::empty())),
70 ),
71 tcx,
72 item_def_id,
73 );
74
75 match attr_kind {
78 RustcAbiAttrKind::Debug => {
79 let fn_name = tcx.item_name(item_def_id);
80 tcx.dcx().emit_err(AbiOf {
81 span: tcx.def_span(item_def_id),
82 fn_name,
83 fn_abi: ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0:#?}", abi))
})format!("{:#?}", abi),
85 });
86 }
87 _ => {
88 tcx.dcx().emit_err(UnrecognizedArgument { span: attr_span });
89 }
90 }
91}
92
93fn test_abi_eq<'tcx>(abi1: &'tcx FnAbi<'tcx, Ty<'tcx>>, abi2: &'tcx FnAbi<'tcx, Ty<'tcx>>) -> bool {
94 if abi1.conv != abi2.conv
95 || abi1.args.len() != abi2.args.len()
96 || abi1.c_variadic != abi2.c_variadic
97 || abi1.fixed_count != abi2.fixed_count
98 || abi1.can_unwind != abi2.can_unwind
99 {
100 return false;
101 }
102
103 abi1.ret.eq_abi(&abi2.ret)
104 && abi1.args.iter().zip(abi2.args.iter()).all(|(arg1, arg2)| arg1.eq_abi(arg2))
105}
106
107fn dump_abi_of_fn_type(
108 tcx: TyCtxt<'_>,
109 item_def_id: LocalDefId,
110 attr_span: Span,
111 attr_kind: RustcAbiAttrKind,
112) {
113 let typing_env = ty::TypingEnv::post_analysis(tcx, item_def_id);
114 let ty = tcx.type_of(item_def_id).instantiate_identity().skip_norm_wip();
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
120 match attr_kind {
121 RustcAbiAttrKind::Debug => {
122 let ty::FnPtr(sig_tys, hdr) = ty.kind() else {
123 ::rustc_span::macros::bug_impl(Some(attr_span),
format_args!("`#[rustc_abi(debug)]` on a type alias requires function pointer type"),
Location::caller());span_bug!(
124 attr_span,
125 "`#[rustc_abi(debug)]` on a type alias requires function pointer type"
126 );
127 };
128 let abi =
129 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);
139 tcx.dcx().emit_err(AbiOf { span, fn_name, fn_abi: ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0:#?}", abi))
})format!("{:#?}", abi) });
140 }
141 RustcAbiAttrKind::AssertEq => {
142 let ty::Tuple(fields) = ty.kind() else {
143 ::rustc_span::macros::bug_impl(Some(attr_span),
format_args!("`#[rustc_abi(assert_eq)]` on a type alias requires pair type"),
Location::caller());span_bug!(
144 attr_span,
145 "`#[rustc_abi(assert_eq)]` on a type alias requires pair type"
146 );
147 };
148 let [field1, field2] = ***fields else {
149 ::rustc_span::macros::bug_impl(Some(attr_span),
format_args!("`#[rustc_abi(assert_eq)]` on a type alias requires pair type"),
Location::caller());span_bug!(
150 attr_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 ::rustc_span::macros::bug_impl(Some(attr_span),
format_args!("`#[rustc_abi(assert_eq)]` on a type alias requires pair of function pointer types"),
Location::caller());span_bug!(
156 attr_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 ::rustc_span::macros::bug_impl(Some(attr_span),
format_args!("`#[rustc_abi(assert_eq)]` on a type alias requires pair of function pointer types"),
Location::caller());span_bug!(
170 attr_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: ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0:#?}", abi1))
})format!("{:#?}", abi1),
187 right: ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0:#?}", abi2))
})format!("{:#?}", abi2),
188 });
189 }
190 }
191 }
192}