about summary refs log tree commit diff
diff options
context:
space:
mode:
authorRalf Jung <post@ralfj.de>2020-03-20 17:48:03 +0100
committerRalf Jung <post@ralfj.de>2020-03-20 17:48:03 +0100
commitc62e36bf4c6f7a128f1af4dadd2abd5ecce397f0 (patch)
tree194641ff8cff76197837d95c5269a7b9048d318d
parentd9f60bcf67ea175ab3298608d8a94563e1ac0f6d (diff)
downloadrust-c62e36bf4c6f7a128f1af4dadd2abd5ecce397f0.tar.gz
rust-c62e36bf4c6f7a128f1af4dadd2abd5ecce397f0.zip
make rustc_layout also work for type definitions
-rw-r--r--src/librustc_passes/layout_test.rs14
-rw-r--r--src/test/ui/layout/debug.rs11
-rw-r--r--src/test/ui/layout/debug.stderr223
3 files changed, 238 insertions, 10 deletions
diff --git a/src/librustc_passes/layout_test.rs b/src/librustc_passes/layout_test.rs
index 66297eb9727..32561c6bd87 100644
--- a/src/librustc_passes/layout_test.rs
+++ b/src/librustc_passes/layout_test.rs
@@ -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);
+                    }
                 }
             }
+            _ => {}
         }
     }
 
diff --git a/src/test/ui/layout/debug.rs b/src/test/ui/layout/debug.rs
index 64a02ee5a22..047002c9c99 100644
--- a/src/test/ui/layout/debug.rs
+++ b/src/test/ui/layout/debug.rs
@@ -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
diff --git a/src/test/ui/layout/debug.stderr b/src/test/ui/layout/debug.stderr
index df8b70f3070..6e704528c41 100644
--- a/src/test/ui/layout/debug.stderr
+++ b/src/test/ui/layout/debug.stderr
@@ -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