make rustc_layout also work for type definitions

This commit is contained in:
Ralf Jung 2020-03-20 17:48:03 +01:00
parent d9f60bcf67
commit c62e36bf4c
3 changed files with 238 additions and 10 deletions

View File

@ -29,12 +29,18 @@ impl ItemLikeVisitor<'tcx> for LayoutTest<'tcx> {
fn visit_item(&mut self, item: &'tcx hir::Item<'tcx>) {
let item_def_id = self.tcx.hir().local_def_id(item.hir_id);
if let ItemKind::TyAlias(..) = item.kind {
for attr in self.tcx.get_attrs(item_def_id).iter() {
if attr.check_name(sym::rustc_layout) {
self.dump_layout_of(item_def_id, item, attr);
match item.kind {
ItemKind::TyAlias(..) |
ItemKind::Enum(..) |
ItemKind::Struct(..) |
ItemKind::Union(..) => {
for attr in self.tcx.get_attrs(item_def_id).iter() {
if attr.check_name(sym::rustc_layout) {
self.dump_layout_of(item_def_id, item, attr);
}
}
}
_ => {}
}
}

View File

@ -1,7 +1,14 @@
#![feature(never_type, rustc_attrs)]
#![crate_type = "lib"]
enum E { Foo, Bar(!, i32, i32) }
#[rustc_layout(debug)]
enum E { Foo, Bar(!, i32, i32) } //~ ERROR: layout debugging
#[rustc_layout(debug)]
type Test = E; //~ ERROR: layout debugging
struct S { f1: i32, f2: (), f3: i32 } //~ ERROR: layout debugging
#[rustc_layout(debug)]
union U { f1: (i32, i32), f3: i32 } //~ ERROR: layout debugging
#[rustc_layout(debug)]
type Test = Result<i32, i32>; //~ ERROR: layout debugging

View File

@ -111,10 +111,225 @@ error: layout debugging: LayoutDetails {
raw: 12,
},
}
--> $DIR/debug.rs:7:1
--> $DIR/debug.rs:5:1
|
LL | type Test = E;
| ^^^^^^^^^^^^^^
LL | enum E { Foo, Bar(!, i32, i32) }
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: aborting due to previous error
error: layout debugging: LayoutDetails {
fields: Arbitrary {
offsets: [
Size {
raw: 0,
},
Size {
raw: 0,
},
Size {
raw: 4,
},
],
memory_index: [
1,
0,
2,
],
},
variants: Single {
index: 0,
},
abi: ScalarPair(
Scalar {
value: Int(
I32,
true,
),
valid_range: 0..=4294967295,
},
Scalar {
value: Int(
I32,
true,
),
valid_range: 0..=4294967295,
},
),
largest_niche: None,
align: AbiAndPrefAlign {
abi: Align {
pow2: 2,
},
pref: Align {
pow2: 3,
},
},
size: Size {
raw: 8,
},
}
--> $DIR/debug.rs:8:1
|
LL | struct S { f1: i32, f2: (), f3: i32 }
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: layout debugging: LayoutDetails {
fields: Union(
2,
),
variants: Single {
index: 0,
},
abi: Aggregate {
sized: true,
},
largest_niche: None,
align: AbiAndPrefAlign {
abi: Align {
pow2: 2,
},
pref: Align {
pow2: 3,
},
},
size: Size {
raw: 8,
},
}
--> $DIR/debug.rs:11:1
|
LL | union U { f1: (i32, i32), f3: i32 }
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: layout debugging: LayoutDetails {
fields: Arbitrary {
offsets: [
Size {
raw: 0,
},
],
memory_index: [
0,
],
},
variants: Multiple {
discr: Scalar {
value: Int(
I32,
false,
),
valid_range: 0..=1,
},
discr_kind: Tag,
discr_index: 0,
variants: [
LayoutDetails {
fields: Arbitrary {
offsets: [
Size {
raw: 4,
},
],
memory_index: [
0,
],
},
variants: Single {
index: 0,
},
abi: Aggregate {
sized: true,
},
largest_niche: None,
align: AbiAndPrefAlign {
abi: Align {
pow2: 2,
},
pref: Align {
pow2: 3,
},
},
size: Size {
raw: 8,
},
},
LayoutDetails {
fields: Arbitrary {
offsets: [
Size {
raw: 4,
},
],
memory_index: [
0,
],
},
variants: Single {
index: 1,
},
abi: Aggregate {
sized: true,
},
largest_niche: None,
align: AbiAndPrefAlign {
abi: Align {
pow2: 2,
},
pref: Align {
pow2: 3,
},
},
size: Size {
raw: 8,
},
},
],
},
abi: ScalarPair(
Scalar {
value: Int(
I32,
false,
),
valid_range: 0..=1,
},
Scalar {
value: Int(
I32,
true,
),
valid_range: 0..=4294967295,
},
),
largest_niche: Some(
Niche {
offset: Size {
raw: 0,
},
scalar: Scalar {
value: Int(
I32,
false,
),
valid_range: 0..=1,
},
},
),
align: AbiAndPrefAlign {
abi: Align {
pow2: 2,
},
pref: Align {
pow2: 3,
},
},
size: Size {
raw: 8,
},
}
--> $DIR/debug.rs:14:1
|
LL | type Test = Result<i32, i32>;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: aborting due to 4 previous errors