-
Notifications
You must be signed in to change notification settings - Fork 13.5k
add rustc_abi debugging attribute #115226
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 { | ||
compiler-errors marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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:?}", | ||
) | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// normalize-stderr-test "(abi|pref|unadjusted_abi_align): Align\([1-8] bytes\)" -> "$1: $$SOME_ALIGN" | ||
// normalize-stderr-test "(size): Size\([48] bytes\)" -> "$1: $$SOME_SIZE" | ||
// normalize-stderr-test "(can_unwind): (true|false)" -> "$1: $$SOME_BOOL" | ||
// normalize-stderr-test "(valid_range): 0\.\.=(4294967295|18446744073709551615)" -> "$1: $$FULL" | ||
// This pattern is prepared for when we account for alignment in the niche. | ||
// normalize-stderr-test "(valid_range): [1-9]\.\.=(429496729[0-9]|1844674407370955161[0-9])" -> "$1: $$NON_NULL" | ||
// Some attributes are only computed for release builds: | ||
// compile-flags: -O | ||
#![feature(rustc_attrs)] | ||
#![crate_type = "lib"] | ||
|
||
#[rustc_abi(debug)] | ||
fn test(_x: u8) -> bool { true } //~ ERROR: fn_abi | ||
|
||
|
||
#[rustc_abi(debug)] | ||
fn test_generic<T>(_x: *const T) { } //~ ERROR: fn_abi | ||
|
||
struct S(u16); | ||
impl S { | ||
#[rustc_abi(debug)] | ||
fn assoc_test(&self) { } //~ ERROR: fn_abi | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.