Skip to content

add #[rustc_layout(debug)] #69901

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 6 commits into from
Mar 21, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 21 additions & 8 deletions src/librustc_passes/layout_test.rs
Original file line number Diff line number Diff line change
@@ -17,32 +17,38 @@ use rustc_span::symbol::sym;
pub fn test_layout(tcx: TyCtxt<'_>) {
if tcx.features().rustc_attrs {
// if the `rustc_attrs` feature is not enabled, don't bother testing layout
tcx.hir().krate().visit_all_item_likes(&mut VarianceTest { tcx });
tcx.hir().krate().visit_all_item_likes(&mut LayoutTest { tcx });
}
}

struct VarianceTest<'tcx> {
struct LayoutTest<'tcx> {
tcx: TyCtxt<'tcx>,
}

impl ItemLikeVisitor<'tcx> for VarianceTest<'tcx> {
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);
}
}
}
_ => {}
}
}

fn visit_trait_item(&mut self, _: &'tcx hir::TraitItem<'tcx>) {}
fn visit_impl_item(&mut self, _: &'tcx hir::ImplItem<'tcx>) {}
}

impl VarianceTest<'tcx> {
impl LayoutTest<'tcx> {
fn dump_layout_of(&self, item_def_id: DefId, item: &hir::Item<'tcx>, attr: &Attribute) {
let tcx = self.tcx;
let param_env = self.tcx.param_env(item_def_id);
@@ -81,6 +87,13 @@ impl VarianceTest<'tcx> {
);
}

sym::debug => {
self.tcx.sess.span_err(
item.span,
&format!("layout debugging: {:#?}", *ty_layout),
);
}

name => {
self.tcx.sess.span_err(
meta_item.span(),
1 change: 1 addition & 0 deletions src/librustc_span/symbol.rs
Original file line number Diff line number Diff line change
@@ -253,6 +253,7 @@ symbols! {
debug_trait,
declare_lint_pass,
decl_macro,
debug,
Debug,
Decodable,
Default,
15 changes: 15 additions & 0 deletions src/test/ui/layout/debug.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// normalize-stderr-test "pref: Align \{\n *pow2: [1-3],\n *\}" -> "pref: $$PREF_ALIGN"
#![feature(never_type, rustc_attrs)]
#![crate_type = "lib"]

#[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 = Result<i32, i32>; //~ ERROR: layout debugging
319 changes: 319 additions & 0 deletions src/test/ui/layout/debug.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,319 @@
error: layout debugging: LayoutDetails {
fields: Arbitrary {
offsets: [
Size {
raw: 0,
},
],
memory_index: [
0,
],
},
variants: Multiple {
discr: Scalar {
value: Int(
I32,
false,
),
valid_range: 0..=0,
},
discr_kind: Tag,
discr_index: 0,
variants: [
LayoutDetails {
fields: Arbitrary {
offsets: [],
memory_index: [],
},
variants: Single {
index: 0,
},
abi: Aggregate {
sized: true,
},
largest_niche: None,
align: AbiAndPrefAlign {
abi: Align {
pow2: 0,
},
pref: $PREF_ALIGN,
},
size: Size {
raw: 4,
},
},
LayoutDetails {
fields: Arbitrary {
offsets: [
Size {
raw: 4,
},
Size {
raw: 4,
},
Size {
raw: 8,
},
],
memory_index: [
0,
1,
2,
],
},
variants: Single {
index: 1,
},
abi: Uninhabited,
largest_niche: None,
align: AbiAndPrefAlign {
abi: Align {
pow2: 2,
},
pref: $PREF_ALIGN,
},
size: Size {
raw: 12,
},
},
],
},
abi: Aggregate {
sized: true,
},
largest_niche: Some(
Niche {
offset: Size {
raw: 0,
},
scalar: Scalar {
value: Int(
I32,
false,
),
valid_range: 0..=0,
},
},
),
align: AbiAndPrefAlign {
abi: Align {
pow2: 2,
},
pref: $PREF_ALIGN,
},
size: Size {
raw: 12,
},
}
--> $DIR/debug.rs:6:1
|
LL | enum E { Foo, Bar(!, i32, i32) }
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

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: $PREF_ALIGN,
},
size: Size {
raw: 8,
},
}
--> $DIR/debug.rs:9: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: $PREF_ALIGN,
},
size: Size {
raw: 8,
},
}
--> $DIR/debug.rs:12: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: $PREF_ALIGN,
},
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: $PREF_ALIGN,
},
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: $PREF_ALIGN,
},
size: Size {
raw: 8,
},
}
--> $DIR/debug.rs:15:1
|
LL | type Test = Result<i32, i32>;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to 4 previous errors