Skip to main content

rustc_passes/
abi_test.rs

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