|
| 1 | +use rustc_ast::Attribute; |
| 2 | +use rustc_hir::def::DefKind; |
| 3 | +use rustc_hir::def_id::DefId; |
| 4 | +use rustc_middle::ty::layout::{FnAbiError, LayoutError}; |
| 5 | +use rustc_middle::ty::{self, GenericArgs, Instance, TyCtxt}; |
| 6 | +use rustc_span::source_map::Spanned; |
| 7 | +use rustc_span::symbol::sym; |
| 8 | + |
| 9 | +use crate::errors::{AbiOf, UnrecognizedField}; |
| 10 | + |
| 11 | +pub fn test_abi(tcx: TyCtxt<'_>) { |
| 12 | + if !tcx.features().rustc_attrs { |
| 13 | + // if the `rustc_attrs` feature is not enabled, don't bother testing ABI |
| 14 | + return; |
| 15 | + } |
| 16 | + for id in tcx.hir().items() { |
| 17 | + match tcx.def_kind(id.owner_id) { |
| 18 | + DefKind::Fn => { |
| 19 | + for attr in tcx.get_attrs(id.owner_id, sym::rustc_abi) { |
| 20 | + dump_abi_of(tcx, id.owner_id.def_id.into(), attr); |
| 21 | + } |
| 22 | + } |
| 23 | + DefKind::Impl { .. } => { |
| 24 | + // To find associated functions we need to go into the child items here. |
| 25 | + for &id in tcx.associated_item_def_ids(id.owner_id) { |
| 26 | + if matches!(tcx.def_kind(id), DefKind::AssocFn) { |
| 27 | + for attr in tcx.get_attrs(id, sym::rustc_abi) { |
| 28 | + dump_abi_of(tcx, id, attr); |
| 29 | + } |
| 30 | + } |
| 31 | + } |
| 32 | + } |
| 33 | + _ => {} |
| 34 | + } |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +fn dump_abi_of(tcx: TyCtxt<'_>, item_def_id: DefId, attr: &Attribute) { |
| 39 | + let param_env = tcx.param_env(item_def_id); |
| 40 | + let args = GenericArgs::identity_for_item(tcx, item_def_id); |
| 41 | + let instance = match Instance::resolve(tcx, param_env, item_def_id, args) { |
| 42 | + Ok(Some(instance)) => instance, |
| 43 | + Ok(None) => { |
| 44 | + // Not sure what to do here, but `LayoutError::Unknown` seems reasonable? |
| 45 | + let ty = tcx.type_of(item_def_id).instantiate_identity(); |
| 46 | + tcx.sess.emit_fatal(Spanned { |
| 47 | + node: LayoutError::Unknown(ty).into_diagnostic(), |
| 48 | + |
| 49 | + span: tcx.def_span(item_def_id), |
| 50 | + }); |
| 51 | + } |
| 52 | + Err(_guaranteed) => return, |
| 53 | + }; |
| 54 | + match tcx.fn_abi_of_instance(param_env.and((instance, /* extra_args */ ty::List::empty()))) { |
| 55 | + Ok(abi) => { |
| 56 | + // Check out the `#[rustc_abi(..)]` attribute to tell what to dump. |
| 57 | + // The `..` are the names of fields to dump. |
| 58 | + let meta_items = attr.meta_item_list().unwrap_or_default(); |
| 59 | + for meta_item in meta_items { |
| 60 | + match meta_item.name_or_empty() { |
| 61 | + sym::debug => { |
| 62 | + let fn_name = tcx.item_name(item_def_id); |
| 63 | + tcx.sess.emit_err(AbiOf { |
| 64 | + span: tcx.def_span(item_def_id), |
| 65 | + fn_name, |
| 66 | + fn_abi: format!("{:#?}", abi), |
| 67 | + }); |
| 68 | + } |
| 69 | + |
| 70 | + name => { |
| 71 | + tcx.sess.emit_err(UnrecognizedField { span: meta_item.span(), name }); |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + Err(FnAbiError::Layout(layout_error)) => { |
| 78 | + tcx.sess.emit_fatal(Spanned { |
| 79 | + node: layout_error.into_diagnostic(), |
| 80 | + span: tcx.def_span(item_def_id), |
| 81 | + }); |
| 82 | + } |
| 83 | + Err(FnAbiError::AdjustForForeignAbi(e)) => { |
| 84 | + // Sadly there seems to be no `into_diagnostic` for this case... and I am not sure if |
| 85 | + // this can even be reached. Anyway this is a perma-unstable debug attribute, an ICE |
| 86 | + // isn't the worst thing. Also this matches what codegen does. |
| 87 | + span_bug!( |
| 88 | + tcx.def_span(item_def_id), |
| 89 | + "error computing fn_abi_of_instance, cannot adjust for foreign ABI: {e:?}", |
| 90 | + ) |
| 91 | + } |
| 92 | + } |
| 93 | +} |
0 commit comments