diff options
| author | Ralf Jung <post@ralfj.de> | 2023-08-25 23:01:06 +0200 |
|---|---|---|
| committer | Ralf Jung <post@ralfj.de> | 2023-08-27 11:55:49 +0200 |
| commit | abe2148aeef3a1ee4622ae8df43370aa749c7b4e (patch) | |
| tree | 9cb6b60774af1263f69faf31bd2d736e7dd49c3d /compiler/rustc_passes | |
| parent | b60f7b51a27dfd9cbc5ecaa56ee679cd1b67f4c4 (diff) | |
| download | rust-abe2148aeef3a1ee4622ae8df43370aa749c7b4e.tar.gz rust-abe2148aeef3a1ee4622ae8df43370aa749c7b4e.zip | |
add rustc_abi debugging attribute
Diffstat (limited to 'compiler/rustc_passes')
| -rw-r--r-- | compiler/rustc_passes/messages.ftl | 3 | ||||
| -rw-r--r-- | compiler/rustc_passes/src/abi_test.rs | 93 | ||||
| -rw-r--r-- | compiler/rustc_passes/src/errors.rs | 9 | ||||
| -rw-r--r-- | compiler/rustc_passes/src/layout_test.rs | 19 | ||||
| -rw-r--r-- | compiler/rustc_passes/src/lib.rs | 1 |
5 files changed, 116 insertions, 9 deletions
diff --git a/compiler/rustc_passes/messages.ftl b/compiler/rustc_passes/messages.ftl index b2a4da885aa..57598cf8bcf 100644 --- a/compiler/rustc_passes/messages.ftl +++ b/compiler/rustc_passes/messages.ftl @@ -7,6 +7,9 @@ passes_abi = abi: {$abi} +passes_abi_of = + fn_abi_of_instance({$fn_name}) = {$fn_abi} + passes_align = align: {$align} diff --git a/compiler/rustc_passes/src/abi_test.rs b/compiler/rustc_passes/src/abi_test.rs new file mode 100644 index 00000000000..5c0438e78ae --- /dev/null +++ b/compiler/rustc_passes/src/abi_test.rs @@ -0,0 +1,93 @@ +use rustc_ast::Attribute; +use rustc_hir::def::DefKind; +use rustc_hir::def_id::DefId; +use rustc_middle::ty::layout::{FnAbiError, LayoutError}; +use rustc_middle::ty::{self, GenericArgs, Instance, TyCtxt}; +use rustc_span::source_map::Spanned; +use rustc_span::symbol::sym; + +use crate::errors::{AbiOf, UnrecognizedField}; + +pub fn test_abi(tcx: TyCtxt<'_>) { + if !tcx.features().rustc_attrs { + // if the `rustc_attrs` feature is not enabled, don't bother testing ABI + return; + } + for id in tcx.hir().items() { + match tcx.def_kind(id.owner_id) { + DefKind::Fn => { + for attr in tcx.get_attrs(id.owner_id, sym::rustc_abi) { + dump_abi_of(tcx, id.owner_id.def_id.into(), attr); + } + } + DefKind::Impl { .. } => { + // To find associated functions we need to go into the child items here. + for &id in tcx.associated_item_def_ids(id.owner_id) { + if matches!(tcx.def_kind(id), DefKind::AssocFn) { + for attr in tcx.get_attrs(id, sym::rustc_abi) { + dump_abi_of(tcx, id, attr); + } + } + } + } + _ => {} + } + } +} + +fn dump_abi_of(tcx: TyCtxt<'_>, item_def_id: DefId, attr: &Attribute) { + let param_env = tcx.param_env(item_def_id); + let args = GenericArgs::identity_for_item(tcx, item_def_id); + let instance = match Instance::resolve(tcx, param_env, item_def_id, args) { + Ok(Some(instance)) => instance, + Ok(None) => { + // Not sure what to do here, but `LayoutError::Unknown` seems reasonable? + let ty = tcx.type_of(item_def_id).instantiate_identity(); + tcx.sess.emit_fatal(Spanned { + node: LayoutError::Unknown(ty).into_diagnostic(), + + span: tcx.def_span(item_def_id), + }); + } + Err(_guaranteed) => return, + }; + match tcx.fn_abi_of_instance(param_env.and((instance, /* extra_args */ ty::List::empty()))) { + Ok(abi) => { + // Check out the `#[rustc_abi(..)]` attribute to tell what to dump. + // The `..` are the names of fields to dump. + let meta_items = attr.meta_item_list().unwrap_or_default(); + for meta_item in meta_items { + match meta_item.name_or_empty() { + sym::debug => { + let fn_name = tcx.item_name(item_def_id); + tcx.sess.emit_err(AbiOf { + span: tcx.def_span(item_def_id), + fn_name, + fn_abi: format!("{:#?}", abi), + }); + } + + name => { + tcx.sess.emit_err(UnrecognizedField { span: meta_item.span(), name }); + } + } + } + } + + Err(FnAbiError::Layout(layout_error)) => { + tcx.sess.emit_fatal(Spanned { + node: layout_error.into_diagnostic(), + span: tcx.def_span(item_def_id), + }); + } + Err(FnAbiError::AdjustForForeignAbi(e)) => { + // Sadly there seems to be no `into_diagnostic` for this case... and I am not sure if + // this can even be reached. Anyway this is a perma-unstable debug attribute, an ICE + // isn't the worst thing. Also this matches what codegen does. + span_bug!( + tcx.def_span(item_def_id), + "error computing fn_abi_of_instance, cannot adjust for foreign ABI: {e:?}", + ) + } + } +} diff --git a/compiler/rustc_passes/src/errors.rs b/compiler/rustc_passes/src/errors.rs index 683717344ce..32dd02a4aa9 100644 --- a/compiler/rustc_passes/src/errors.rs +++ b/compiler/rustc_passes/src/errors.rs @@ -914,6 +914,15 @@ pub struct LayoutOf { } #[derive(Diagnostic)] +#[diag(passes_abi_of)] +pub struct AbiOf { + #[primary_span] + pub span: Span, + pub fn_name: Symbol, + pub fn_abi: String, +} + +#[derive(Diagnostic)] #[diag(passes_unrecognized_field)] pub struct UnrecognizedField { #[primary_span] diff --git a/compiler/rustc_passes/src/layout_test.rs b/compiler/rustc_passes/src/layout_test.rs index a7a8af864ac..d839fee07a6 100644 --- a/compiler/rustc_passes/src/layout_test.rs +++ b/compiler/rustc_passes/src/layout_test.rs @@ -11,16 +11,17 @@ use rustc_target::abi::{HasDataLayout, TargetDataLayout}; use crate::errors::{Abi, Align, HomogeneousAggregate, LayoutOf, Size, UnrecognizedField}; pub fn test_layout(tcx: TyCtxt<'_>) { - if tcx.features().rustc_attrs { + if !tcx.features().rustc_attrs { // if the `rustc_attrs` feature is not enabled, don't bother testing layout - for id in tcx.hir().items() { - if matches!( - tcx.def_kind(id.owner_id), - DefKind::TyAlias { .. } | DefKind::Enum | DefKind::Struct | DefKind::Union - ) { - for attr in tcx.get_attrs(id.owner_id, sym::rustc_layout) { - dump_layout_of(tcx, id.owner_id.def_id, attr); - } + return; + } + for id in tcx.hir().items() { + if matches!( + tcx.def_kind(id.owner_id), + DefKind::TyAlias { .. } | DefKind::Enum | DefKind::Struct | DefKind::Union + ) { + for attr in tcx.get_attrs(id.owner_id, sym::rustc_layout) { + dump_layout_of(tcx, id.owner_id.def_id, attr); } } } diff --git a/compiler/rustc_passes/src/lib.rs b/compiler/rustc_passes/src/lib.rs index 0da4b294648..51f3c9ad76f 100644 --- a/compiler/rustc_passes/src/lib.rs +++ b/compiler/rustc_passes/src/lib.rs @@ -24,6 +24,7 @@ use rustc_errors::{DiagnosticMessage, SubdiagnosticMessage}; use rustc_fluent_macro::fluent_messages; use rustc_middle::query::Providers; +pub mod abi_test; mod check_attr; mod check_const; pub mod dead; |
