Skip to content

Commit

Permalink
make rustc_layout also work for type definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
RalfJung committed Mar 20, 2020
1 parent d9f60bc commit c62e36b
Show file tree
Hide file tree
Showing 3 changed files with 238 additions and 10 deletions.
14 changes: 10 additions & 4 deletions src/librustc_passes/layout_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
_ => {}
}
}

Expand Down
11 changes: 9 additions & 2 deletions src/test/ui/layout/debug.rs
Original file line number Diff line number Diff line change
@@ -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)]
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 = E; //~ ERROR: layout debugging
type Test = Result<i32, i32>; //~ ERROR: layout debugging
223 changes: 219 additions & 4 deletions src/test/ui/layout/debug.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit c62e36b

Please sign in to comment.