Skip to content

Commit abe2148

Browse files
committed
add rustc_abi debugging attribute
1 parent b60f7b5 commit abe2148

File tree

12 files changed

+422
-29
lines changed

12 files changed

+422
-29
lines changed

compiler/rustc_feature/src/builtin_attrs.rs

+1
Original file line numberDiff line numberDiff line change
@@ -814,6 +814,7 @@ pub const BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
814814
rustc_attr!(TEST, rustc_strict_coherence, Normal, template!(Word), WarnFollowing),
815815
rustc_attr!(TEST, rustc_variance, Normal, template!(Word), WarnFollowing),
816816
rustc_attr!(TEST, rustc_layout, Normal, template!(List: "field1, field2, ..."), WarnFollowing),
817+
rustc_attr!(TEST, rustc_abi, Normal, template!(List: "field1, field2, ..."), WarnFollowing),
817818
rustc_attr!(TEST, rustc_regions, Normal, template!(Word), WarnFollowing),
818819
rustc_attr!(
819820
TEST, rustc_error, Normal,

compiler/rustc_interface/src/passes.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use rustc_middle::query::{ExternProviders, Providers};
2222
use rustc_middle::ty::{self, GlobalCtxt, RegisteredTools, TyCtxt};
2323
use rustc_mir_build as mir_build;
2424
use rustc_parse::{parse_crate_from_file, parse_crate_from_source_str, validate_attr};
25-
use rustc_passes::{self, hir_stats, layout_test};
25+
use rustc_passes::{self, abi_test, hir_stats, layout_test};
2626
use rustc_plugin_impl as plugin;
2727
use rustc_resolve::Resolver;
2828
use rustc_session::code_stats::VTableSizeInfo;
@@ -818,6 +818,7 @@ fn analysis(tcx: TyCtxt<'_>, (): ()) -> Result<()> {
818818
}
819819

820820
sess.time("layout_testing", || layout_test::test_layout(tcx));
821+
sess.time("abi_testing", || abi_test::test_abi(tcx));
821822

822823
// Avoid overwhelming user with errors if borrow checking failed.
823824
// I'm not sure how helpful this is, to be honest, but it avoids a

compiler/rustc_passes/messages.ftl

+3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
passes_abi =
88
abi: {$abi}
99
10+
passes_abi_of =
11+
fn_abi_of_instance({$fn_name}) = {$fn_abi}
12+
1013
passes_align =
1114
align: {$align}
1215

compiler/rustc_passes/src/abi_test.rs

+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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+
}

compiler/rustc_passes/src/errors.rs

+9
Original file line numberDiff line numberDiff line change
@@ -913,6 +913,15 @@ pub struct LayoutOf {
913913
pub ty_layout: String,
914914
}
915915

916+
#[derive(Diagnostic)]
917+
#[diag(passes_abi_of)]
918+
pub struct AbiOf {
919+
#[primary_span]
920+
pub span: Span,
921+
pub fn_name: Symbol,
922+
pub fn_abi: String,
923+
}
924+
916925
#[derive(Diagnostic)]
917926
#[diag(passes_unrecognized_field)]
918927
pub struct UnrecognizedField {

compiler/rustc_passes/src/layout_test.rs

+10-9
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,17 @@ use rustc_target::abi::{HasDataLayout, TargetDataLayout};
1111
use crate::errors::{Abi, Align, HomogeneousAggregate, LayoutOf, Size, UnrecognizedField};
1212

1313
pub fn test_layout(tcx: TyCtxt<'_>) {
14-
if tcx.features().rustc_attrs {
14+
if !tcx.features().rustc_attrs {
1515
// if the `rustc_attrs` feature is not enabled, don't bother testing layout
16-
for id in tcx.hir().items() {
17-
if matches!(
18-
tcx.def_kind(id.owner_id),
19-
DefKind::TyAlias { .. } | DefKind::Enum | DefKind::Struct | DefKind::Union
20-
) {
21-
for attr in tcx.get_attrs(id.owner_id, sym::rustc_layout) {
22-
dump_layout_of(tcx, id.owner_id.def_id, attr);
23-
}
16+
return;
17+
}
18+
for id in tcx.hir().items() {
19+
if matches!(
20+
tcx.def_kind(id.owner_id),
21+
DefKind::TyAlias { .. } | DefKind::Enum | DefKind::Struct | DefKind::Union
22+
) {
23+
for attr in tcx.get_attrs(id.owner_id, sym::rustc_layout) {
24+
dump_layout_of(tcx, id.owner_id.def_id, attr);
2425
}
2526
}
2627
}

compiler/rustc_passes/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ use rustc_errors::{DiagnosticMessage, SubdiagnosticMessage};
2424
use rustc_fluent_macro::fluent_messages;
2525
use rustc_middle::query::Providers;
2626

27+
pub mod abi_test;
2728
mod check_attr;
2829
mod check_const;
2930
pub mod dead;

compiler/rustc_span/src/symbol.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1281,6 +1281,7 @@ symbols! {
12811281
rust_eh_catch_typeinfo,
12821282
rust_eh_personality,
12831283
rustc,
1284+
rustc_abi,
12841285
rustc_allocator,
12851286
rustc_allocator_zeroed,
12861287
rustc_allow_const_fn_unstable,

tests/ui/abi/debug.rs

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// normalize-stderr-test "(abi|pref|unadjusted_abi_align): Align\([1-8] bytes\)" -> "$1: $$SOME_ALIGN"
2+
// normalize-stderr-test "(size): Size\([48] bytes\)" -> "$1: $$SOME_SIZE"
3+
// normalize-stderr-test "(can_unwind): (true|false)" -> "$1: $$SOME_BOOL"
4+
// normalize-stderr-test "(valid_range): 0\.\.=(4294967295|18446744073709551615)" -> "$1: $$FULL"
5+
// This pattern is prepared for when we account for alignment in the niche.
6+
// normalize-stderr-test "(valid_range): [1-9]\.\.=(429496729[0-9]|1844674407370955161[0-9])" -> "$1: $$NON_NULL"
7+
// Some attributes are only computed for release builds:
8+
// compile-flags: -O
9+
#![feature(rustc_attrs)]
10+
#![crate_type = "lib"]
11+
12+
#[rustc_abi(debug)]
13+
fn test(_x: u8) -> bool { true } //~ ERROR: fn_abi
14+
15+
16+
#[rustc_abi(debug)]
17+
fn test_generic<T>(_x: *const T) { } //~ ERROR: fn_abi
18+
19+
struct S(u16);
20+
impl S {
21+
#[rustc_abi(debug)]
22+
fn assoc_test(&self) { } //~ ERROR: fn_abi
23+
}

0 commit comments

Comments
 (0)