use hir_crate_items(()).definitions() instead of hir().items()

This commit is contained in:
Ralf Jung 2023-09-09 17:39:53 +02:00
parent a5b0311367
commit c2a7e684cd
4 changed files with 118 additions and 141 deletions

View File

@ -1,6 +1,6 @@
use rustc_ast::Attribute;
use rustc_hir::def::DefKind;
use rustc_hir::def_id::DefId;
use rustc_hir::def_id::LocalDefId;
use rustc_middle::ty::layout::{FnAbiError, LayoutError};
use rustc_middle::ty::{self, GenericArgs, Instance, Ty, TyCtxt};
use rustc_span::source_map::Spanned;
@ -15,28 +15,15 @@ pub fn test_abi(tcx: TyCtxt<'_>) {
// if the `rustc_attrs` feature is not enabled, don't bother testing ABI
return;
}
for id in tcx.hir().items() {
for attr in tcx.get_attrs(id.owner_id, sym::rustc_abi) {
match tcx.def_kind(id.owner_id) {
DefKind::Fn => {
dump_abi_of_fn_item(tcx, id.owner_id.def_id.into(), attr);
}
DefKind::TyAlias { .. } => {
dump_abi_of_fn_type(tcx, id.owner_id.def_id.into(), attr);
}
_ => {
tcx.sess.emit_err(AbiInvalidAttribute { span: tcx.def_span(id.owner_id) });
}
}
}
if matches!(tcx.def_kind(id.owner_id), 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) {
for id in tcx.hir_crate_items(()).definitions() {
for attr in tcx.get_attrs(id, sym::rustc_abi) {
match tcx.def_kind(id) {
DefKind::AssocFn => {
DefKind::Fn | DefKind::AssocFn => {
dump_abi_of_fn_item(tcx, id, attr);
}
DefKind::TyAlias { .. } => {
dump_abi_of_fn_type(tcx, id, attr);
}
_ => {
tcx.sess.emit_err(AbiInvalidAttribute { span: tcx.def_span(id) });
}
@ -44,13 +31,11 @@ pub fn test_abi(tcx: TyCtxt<'_>) {
}
}
}
}
}
fn unwrap_fn_abi<'tcx>(
abi: Result<&'tcx FnAbi<'tcx, Ty<'tcx>>, &'tcx FnAbiError<'tcx>>,
tcx: TyCtxt<'tcx>,
item_def_id: DefId,
item_def_id: LocalDefId,
) -> &'tcx FnAbi<'tcx, Ty<'tcx>> {
match abi {
Ok(abi) => abi,
@ -72,10 +57,10 @@ fn unwrap_fn_abi<'tcx>(
}
}
fn dump_abi_of_fn_item(tcx: TyCtxt<'_>, item_def_id: DefId, attr: &Attribute) {
fn dump_abi_of_fn_item(tcx: TyCtxt<'_>, item_def_id: LocalDefId, 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) {
let instance = match Instance::resolve(tcx, param_env, item_def_id.into(), args) {
Ok(Some(instance)) => instance,
Ok(None) => {
// Not sure what to do here, but `LayoutError::Unknown` seems reasonable?
@ -100,7 +85,7 @@ fn dump_abi_of_fn_item(tcx: TyCtxt<'_>, item_def_id: DefId, attr: &Attribute) {
for meta_item in meta_items {
match meta_item.name_or_empty() {
sym::debug => {
let fn_name = tcx.item_name(item_def_id);
let fn_name = tcx.item_name(item_def_id.into());
tcx.sess.emit_err(AbiOf {
span: tcx.def_span(item_def_id),
fn_name,
@ -129,7 +114,7 @@ fn test_abi_eq<'tcx>(abi1: &'tcx FnAbi<'tcx, Ty<'tcx>>, abi2: &'tcx FnAbi<'tcx,
&& abi1.args.iter().zip(abi2.args.iter()).all(|(arg1, arg2)| arg1.eq_abi(arg2))
}
fn dump_abi_of_fn_type(tcx: TyCtxt<'_>, item_def_id: DefId, attr: &Attribute) {
fn dump_abi_of_fn_type(tcx: TyCtxt<'_>, item_def_id: LocalDefId, attr: &Attribute) {
let param_env = tcx.param_env(item_def_id);
let ty = tcx.type_of(item_def_id).instantiate_identity();
let span = tcx.def_span(item_def_id);
@ -152,7 +137,7 @@ fn dump_abi_of_fn_type(tcx: TyCtxt<'_>, item_def_id: DefId, attr: &Attribute) {
item_def_id,
);
let fn_name = tcx.item_name(item_def_id);
let fn_name = tcx.item_name(item_def_id.into());
tcx.sess.emit_err(AbiOf { span, fn_name, fn_abi: format!("{:#?}", abi) });
}
sym::assert_eq => {

View File

@ -20,21 +20,13 @@ pub fn test_layout(tcx: TyCtxt<'_>) {
// if the `rustc_attrs` feature is not enabled, don't bother testing layout
return;
}
for id in tcx.hir().items() {
for attr in tcx.get_attrs(id.owner_id, sym::rustc_layout) {
match tcx.def_kind(id.owner_id) {
for id in tcx.hir_crate_items(()).definitions() {
for attr in tcx.get_attrs(id, sym::rustc_layout) {
match tcx.def_kind(id) {
DefKind::TyAlias { .. } | DefKind::Enum | DefKind::Struct | DefKind::Union => {
dump_layout_of(tcx, id.owner_id.def_id, attr);
dump_layout_of(tcx, id, attr);
}
_ => {
tcx.sess.emit_err(LayoutInvalidAttribute { span: tcx.def_span(id.owner_id) });
}
}
}
if matches!(tcx.def_kind(id.owner_id), 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) {
for _attr in tcx.get_attrs(id, sym::rustc_layout) {
tcx.sess.emit_err(LayoutInvalidAttribute { span: tcx.def_span(id) });
}
}

View File

@ -268,100 +268,6 @@ error: `#[rustc_abi]` can only be applied to function items, type aliases, and a
LL | const C: () = ();
| ^^^^^^^^^^^
error: `#[rustc_abi]` can only be applied to function items, type aliases, and associated functions
--> $DIR/debug.rs:28:5
|
LL | const C: () = ();
| ^^^^^^^^^^^
error: fn_abi_of(assoc_test) = FnAbi {
args: [
ArgAbi {
layout: TyAndLayout {
ty: &S,
layout: Layout {
size: $SOME_SIZE,
align: AbiAndPrefAlign {
abi: $SOME_ALIGN,
pref: $SOME_ALIGN,
},
abi: Scalar(
Initialized {
value: Pointer(
AddressSpace(
0,
),
),
valid_range: $NON_NULL,
},
),
fields: Primitive,
largest_niche: Some(
Niche {
offset: Size(0 bytes),
value: Pointer(
AddressSpace(
0,
),
),
valid_range: $NON_NULL,
},
),
variants: Single {
index: 0,
},
max_repr_align: None,
unadjusted_abi_align: $SOME_ALIGN,
},
},
mode: Direct(
ArgAttributes {
regular: NoAlias | NonNull | ReadOnly | NoUndef,
arg_ext: None,
pointee_size: Size(2 bytes),
pointee_align: Some(
Align(2 bytes),
),
},
),
},
],
ret: ArgAbi {
layout: TyAndLayout {
ty: (),
layout: Layout {
size: Size(0 bytes),
align: AbiAndPrefAlign {
abi: $SOME_ALIGN,
pref: $SOME_ALIGN,
},
abi: Aggregate {
sized: true,
},
fields: Arbitrary {
offsets: [],
memory_index: [],
},
largest_niche: None,
variants: Single {
index: 0,
},
max_repr_align: None,
unadjusted_abi_align: $SOME_ALIGN,
},
},
mode: Ignore,
},
c_variadic: false,
fixed_count: 1,
conv: Rust,
can_unwind: $SOME_BOOL,
}
--> $DIR/debug.rs:33:5
|
LL | fn assoc_test(&self) { }
| ^^^^^^^^^^^^^^^^^^^^
error: ABIs are not compatible
left ABI = FnAbi {
args: [
@ -954,6 +860,100 @@ LL | type TestAbiEqNonsense = (fn((str, str)), fn((str, str)));
= help: the trait `Sized` is not implemented for `str`
= note: only the last element of a tuple may have a dynamically sized type
error: `#[rustc_abi]` can only be applied to function items, type aliases, and associated functions
--> $DIR/debug.rs:28:5
|
LL | const C: () = ();
| ^^^^^^^^^^^
error: fn_abi_of(assoc_test) = FnAbi {
args: [
ArgAbi {
layout: TyAndLayout {
ty: &S,
layout: Layout {
size: $SOME_SIZE,
align: AbiAndPrefAlign {
abi: $SOME_ALIGN,
pref: $SOME_ALIGN,
},
abi: Scalar(
Initialized {
value: Pointer(
AddressSpace(
0,
),
),
valid_range: $NON_NULL,
},
),
fields: Primitive,
largest_niche: Some(
Niche {
offset: Size(0 bytes),
value: Pointer(
AddressSpace(
0,
),
),
valid_range: $NON_NULL,
},
),
variants: Single {
index: 0,
},
max_repr_align: None,
unadjusted_abi_align: $SOME_ALIGN,
},
},
mode: Direct(
ArgAttributes {
regular: NoAlias | NonNull | ReadOnly | NoUndef,
arg_ext: None,
pointee_size: Size(2 bytes),
pointee_align: Some(
Align(2 bytes),
),
},
),
},
],
ret: ArgAbi {
layout: TyAndLayout {
ty: (),
layout: Layout {
size: Size(0 bytes),
align: AbiAndPrefAlign {
abi: $SOME_ALIGN,
pref: $SOME_ALIGN,
},
abi: Aggregate {
sized: true,
},
fields: Arbitrary {
offsets: [],
memory_index: [],
},
largest_niche: None,
variants: Single {
index: 0,
},
max_repr_align: None,
unadjusted_abi_align: $SOME_ALIGN,
},
},
mode: Ignore,
},
c_variadic: false,
fixed_count: 1,
conv: Rust,
can_unwind: $SOME_BOOL,
}
--> $DIR/debug.rs:33:5
|
LL | fn assoc_test(&self) { }
| ^^^^^^^^^^^^^^^^^^^^
error: aborting due to 11 previous errors
For more information about this error, try `rustc --explain E0277`.

View File

@ -557,12 +557,6 @@ error: `#[rustc_layout]` can only be applied to `struct`/`enum`/`union` declarat
LL | const C: () = ();
| ^^^^^^^^^^^
error: `#[rustc_layout]` can only be applied to `struct`/`enum`/`union` declarations and type aliases
--> $DIR/debug.rs:74:5
|
LL | const C: () = ();
| ^^^^^^^^^^^
error[E0277]: the size for values of type `str` cannot be known at compilation time
--> $DIR/debug.rs:78:1
|
@ -572,6 +566,12 @@ LL | type Impossible = (str, str);
= help: the trait `Sized` is not implemented for `str`
= note: only the last element of a tuple may have a dynamically sized type
error: `#[rustc_layout]` can only be applied to `struct`/`enum`/`union` declarations and type aliases
--> $DIR/debug.rs:74:5
|
LL | const C: () = ();
| ^^^^^^^^^^^
error: aborting due to 17 previous errors
For more information about this error, try `rustc --explain E0277`.