Skip to content

Commit c2a7e68

Browse files
committed
use hir_crate_items(()).definitions() instead of hir().items()
1 parent a5b0311 commit c2a7e68

File tree

4 files changed

+118
-141
lines changed

4 files changed

+118
-141
lines changed

compiler/rustc_passes/src/abi_test.rs

+14-29
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use rustc_ast::Attribute;
22
use rustc_hir::def::DefKind;
3-
use rustc_hir::def_id::DefId;
3+
use rustc_hir::def_id::LocalDefId;
44
use rustc_middle::ty::layout::{FnAbiError, LayoutError};
55
use rustc_middle::ty::{self, GenericArgs, Instance, Ty, TyCtxt};
66
use rustc_span::source_map::Spanned;
@@ -15,32 +15,17 @@ pub fn test_abi(tcx: TyCtxt<'_>) {
1515
// if the `rustc_attrs` feature is not enabled, don't bother testing ABI
1616
return;
1717
}
18-
for id in tcx.hir().items() {
19-
for attr in tcx.get_attrs(id.owner_id, sym::rustc_abi) {
20-
match tcx.def_kind(id.owner_id) {
21-
DefKind::Fn => {
22-
dump_abi_of_fn_item(tcx, id.owner_id.def_id.into(), attr);
18+
for id in tcx.hir_crate_items(()).definitions() {
19+
for attr in tcx.get_attrs(id, sym::rustc_abi) {
20+
match tcx.def_kind(id) {
21+
DefKind::Fn | DefKind::AssocFn => {
22+
dump_abi_of_fn_item(tcx, id, attr);
2323
}
2424
DefKind::TyAlias { .. } => {
25-
dump_abi_of_fn_type(tcx, id.owner_id.def_id.into(), attr);
25+
dump_abi_of_fn_type(tcx, id, attr);
2626
}
2727
_ => {
28-
tcx.sess.emit_err(AbiInvalidAttribute { span: tcx.def_span(id.owner_id) });
29-
}
30-
}
31-
}
32-
if matches!(tcx.def_kind(id.owner_id), DefKind::Impl { .. }) {
33-
// To find associated functions we need to go into the child items here.
34-
for &id in tcx.associated_item_def_ids(id.owner_id) {
35-
for attr in tcx.get_attrs(id, sym::rustc_abi) {
36-
match tcx.def_kind(id) {
37-
DefKind::AssocFn => {
38-
dump_abi_of_fn_item(tcx, id, attr);
39-
}
40-
_ => {
41-
tcx.sess.emit_err(AbiInvalidAttribute { span: tcx.def_span(id) });
42-
}
43-
}
28+
tcx.sess.emit_err(AbiInvalidAttribute { span: tcx.def_span(id) });
4429
}
4530
}
4631
}
@@ -50,7 +35,7 @@ pub fn test_abi(tcx: TyCtxt<'_>) {
5035
fn unwrap_fn_abi<'tcx>(
5136
abi: Result<&'tcx FnAbi<'tcx, Ty<'tcx>>, &'tcx FnAbiError<'tcx>>,
5237
tcx: TyCtxt<'tcx>,
53-
item_def_id: DefId,
38+
item_def_id: LocalDefId,
5439
) -> &'tcx FnAbi<'tcx, Ty<'tcx>> {
5540
match abi {
5641
Ok(abi) => abi,
@@ -72,10 +57,10 @@ fn unwrap_fn_abi<'tcx>(
7257
}
7358
}
7459

75-
fn dump_abi_of_fn_item(tcx: TyCtxt<'_>, item_def_id: DefId, attr: &Attribute) {
60+
fn dump_abi_of_fn_item(tcx: TyCtxt<'_>, item_def_id: LocalDefId, attr: &Attribute) {
7661
let param_env = tcx.param_env(item_def_id);
7762
let args = GenericArgs::identity_for_item(tcx, item_def_id);
78-
let instance = match Instance::resolve(tcx, param_env, item_def_id, args) {
63+
let instance = match Instance::resolve(tcx, param_env, item_def_id.into(), args) {
7964
Ok(Some(instance)) => instance,
8065
Ok(None) => {
8166
// 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) {
10085
for meta_item in meta_items {
10186
match meta_item.name_or_empty() {
10287
sym::debug => {
103-
let fn_name = tcx.item_name(item_def_id);
88+
let fn_name = tcx.item_name(item_def_id.into());
10489
tcx.sess.emit_err(AbiOf {
10590
span: tcx.def_span(item_def_id),
10691
fn_name,
@@ -129,7 +114,7 @@ fn test_abi_eq<'tcx>(abi1: &'tcx FnAbi<'tcx, Ty<'tcx>>, abi2: &'tcx FnAbi<'tcx,
129114
&& abi1.args.iter().zip(abi2.args.iter()).all(|(arg1, arg2)| arg1.eq_abi(arg2))
130115
}
131116

132-
fn dump_abi_of_fn_type(tcx: TyCtxt<'_>, item_def_id: DefId, attr: &Attribute) {
117+
fn dump_abi_of_fn_type(tcx: TyCtxt<'_>, item_def_id: LocalDefId, attr: &Attribute) {
133118
let param_env = tcx.param_env(item_def_id);
134119
let ty = tcx.type_of(item_def_id).instantiate_identity();
135120
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) {
152137
item_def_id,
153138
);
154139

155-
let fn_name = tcx.item_name(item_def_id);
140+
let fn_name = tcx.item_name(item_def_id.into());
156141
tcx.sess.emit_err(AbiOf { span, fn_name, fn_abi: format!("{:#?}", abi) });
157142
}
158143
sym::assert_eq => {

compiler/rustc_passes/src/layout_test.rs

+4-12
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,13 @@ pub fn test_layout(tcx: TyCtxt<'_>) {
2020
// if the `rustc_attrs` feature is not enabled, don't bother testing layout
2121
return;
2222
}
23-
for id in tcx.hir().items() {
24-
for attr in tcx.get_attrs(id.owner_id, sym::rustc_layout) {
25-
match tcx.def_kind(id.owner_id) {
23+
for id in tcx.hir_crate_items(()).definitions() {
24+
for attr in tcx.get_attrs(id, sym::rustc_layout) {
25+
match tcx.def_kind(id) {
2626
DefKind::TyAlias { .. } | DefKind::Enum | DefKind::Struct | DefKind::Union => {
27-
dump_layout_of(tcx, id.owner_id.def_id, attr);
27+
dump_layout_of(tcx, id, attr);
2828
}
2929
_ => {
30-
tcx.sess.emit_err(LayoutInvalidAttribute { span: tcx.def_span(id.owner_id) });
31-
}
32-
}
33-
}
34-
if matches!(tcx.def_kind(id.owner_id), DefKind::Impl { .. }) {
35-
// To find associated functions we need to go into the child items here.
36-
for &id in tcx.associated_item_def_ids(id.owner_id) {
37-
for _attr in tcx.get_attrs(id, sym::rustc_layout) {
3830
tcx.sess.emit_err(LayoutInvalidAttribute { span: tcx.def_span(id) });
3931
}
4032
}

tests/ui/abi/debug.stderr

+94-94
Original file line numberDiff line numberDiff line change
@@ -268,100 +268,6 @@ error: `#[rustc_abi]` can only be applied to function items, type aliases, and a
268268
LL | const C: () = ();
269269
| ^^^^^^^^^^^
270270

271-
error: `#[rustc_abi]` can only be applied to function items, type aliases, and associated functions
272-
--> $DIR/debug.rs:28:5
273-
|
274-
LL | const C: () = ();
275-
| ^^^^^^^^^^^
276-
277-
error: fn_abi_of(assoc_test) = FnAbi {
278-
args: [
279-
ArgAbi {
280-
layout: TyAndLayout {
281-
ty: &S,
282-
layout: Layout {
283-
size: $SOME_SIZE,
284-
align: AbiAndPrefAlign {
285-
abi: $SOME_ALIGN,
286-
pref: $SOME_ALIGN,
287-
},
288-
abi: Scalar(
289-
Initialized {
290-
value: Pointer(
291-
AddressSpace(
292-
0,
293-
),
294-
),
295-
valid_range: $NON_NULL,
296-
},
297-
),
298-
fields: Primitive,
299-
largest_niche: Some(
300-
Niche {
301-
offset: Size(0 bytes),
302-
value: Pointer(
303-
AddressSpace(
304-
0,
305-
),
306-
),
307-
valid_range: $NON_NULL,
308-
},
309-
),
310-
variants: Single {
311-
index: 0,
312-
},
313-
max_repr_align: None,
314-
unadjusted_abi_align: $SOME_ALIGN,
315-
},
316-
},
317-
mode: Direct(
318-
ArgAttributes {
319-
regular: NoAlias | NonNull | ReadOnly | NoUndef,
320-
arg_ext: None,
321-
pointee_size: Size(2 bytes),
322-
pointee_align: Some(
323-
Align(2 bytes),
324-
),
325-
},
326-
),
327-
},
328-
],
329-
ret: ArgAbi {
330-
layout: TyAndLayout {
331-
ty: (),
332-
layout: Layout {
333-
size: Size(0 bytes),
334-
align: AbiAndPrefAlign {
335-
abi: $SOME_ALIGN,
336-
pref: $SOME_ALIGN,
337-
},
338-
abi: Aggregate {
339-
sized: true,
340-
},
341-
fields: Arbitrary {
342-
offsets: [],
343-
memory_index: [],
344-
},
345-
largest_niche: None,
346-
variants: Single {
347-
index: 0,
348-
},
349-
max_repr_align: None,
350-
unadjusted_abi_align: $SOME_ALIGN,
351-
},
352-
},
353-
mode: Ignore,
354-
},
355-
c_variadic: false,
356-
fixed_count: 1,
357-
conv: Rust,
358-
can_unwind: $SOME_BOOL,
359-
}
360-
--> $DIR/debug.rs:33:5
361-
|
362-
LL | fn assoc_test(&self) { }
363-
| ^^^^^^^^^^^^^^^^^^^^
364-
365271
error: ABIs are not compatible
366272
left ABI = FnAbi {
367273
args: [
@@ -954,6 +860,100 @@ LL | type TestAbiEqNonsense = (fn((str, str)), fn((str, str)));
954860
= help: the trait `Sized` is not implemented for `str`
955861
= note: only the last element of a tuple may have a dynamically sized type
956862

863+
error: `#[rustc_abi]` can only be applied to function items, type aliases, and associated functions
864+
--> $DIR/debug.rs:28:5
865+
|
866+
LL | const C: () = ();
867+
| ^^^^^^^^^^^
868+
869+
error: fn_abi_of(assoc_test) = FnAbi {
870+
args: [
871+
ArgAbi {
872+
layout: TyAndLayout {
873+
ty: &S,
874+
layout: Layout {
875+
size: $SOME_SIZE,
876+
align: AbiAndPrefAlign {
877+
abi: $SOME_ALIGN,
878+
pref: $SOME_ALIGN,
879+
},
880+
abi: Scalar(
881+
Initialized {
882+
value: Pointer(
883+
AddressSpace(
884+
0,
885+
),
886+
),
887+
valid_range: $NON_NULL,
888+
},
889+
),
890+
fields: Primitive,
891+
largest_niche: Some(
892+
Niche {
893+
offset: Size(0 bytes),
894+
value: Pointer(
895+
AddressSpace(
896+
0,
897+
),
898+
),
899+
valid_range: $NON_NULL,
900+
},
901+
),
902+
variants: Single {
903+
index: 0,
904+
},
905+
max_repr_align: None,
906+
unadjusted_abi_align: $SOME_ALIGN,
907+
},
908+
},
909+
mode: Direct(
910+
ArgAttributes {
911+
regular: NoAlias | NonNull | ReadOnly | NoUndef,
912+
arg_ext: None,
913+
pointee_size: Size(2 bytes),
914+
pointee_align: Some(
915+
Align(2 bytes),
916+
),
917+
},
918+
),
919+
},
920+
],
921+
ret: ArgAbi {
922+
layout: TyAndLayout {
923+
ty: (),
924+
layout: Layout {
925+
size: Size(0 bytes),
926+
align: AbiAndPrefAlign {
927+
abi: $SOME_ALIGN,
928+
pref: $SOME_ALIGN,
929+
},
930+
abi: Aggregate {
931+
sized: true,
932+
},
933+
fields: Arbitrary {
934+
offsets: [],
935+
memory_index: [],
936+
},
937+
largest_niche: None,
938+
variants: Single {
939+
index: 0,
940+
},
941+
max_repr_align: None,
942+
unadjusted_abi_align: $SOME_ALIGN,
943+
},
944+
},
945+
mode: Ignore,
946+
},
947+
c_variadic: false,
948+
fixed_count: 1,
949+
conv: Rust,
950+
can_unwind: $SOME_BOOL,
951+
}
952+
--> $DIR/debug.rs:33:5
953+
|
954+
LL | fn assoc_test(&self) { }
955+
| ^^^^^^^^^^^^^^^^^^^^
956+
957957
error: aborting due to 11 previous errors
958958

959959
For more information about this error, try `rustc --explain E0277`.

tests/ui/layout/debug.stderr

+6-6
Original file line numberDiff line numberDiff line change
@@ -557,12 +557,6 @@ error: `#[rustc_layout]` can only be applied to `struct`/`enum`/`union` declarat
557557
LL | const C: () = ();
558558
| ^^^^^^^^^^^
559559

560-
error: `#[rustc_layout]` can only be applied to `struct`/`enum`/`union` declarations and type aliases
561-
--> $DIR/debug.rs:74:5
562-
|
563-
LL | const C: () = ();
564-
| ^^^^^^^^^^^
565-
566560
error[E0277]: the size for values of type `str` cannot be known at compilation time
567561
--> $DIR/debug.rs:78:1
568562
|
@@ -572,6 +566,12 @@ LL | type Impossible = (str, str);
572566
= help: the trait `Sized` is not implemented for `str`
573567
= note: only the last element of a tuple may have a dynamically sized type
574568

569+
error: `#[rustc_layout]` can only be applied to `struct`/`enum`/`union` declarations and type aliases
570+
--> $DIR/debug.rs:74:5
571+
|
572+
LL | const C: () = ();
573+
| ^^^^^^^^^^^
574+
575575
error: aborting due to 17 previous errors
576576

577577
For more information about this error, try `rustc --explain E0277`.

0 commit comments

Comments
 (0)