]> git.lizzy.rs Git - rust.git/commitdiff
make rustc_layout also work for type definitions
authorRalf Jung <post@ralfj.de>
Fri, 20 Mar 2020 16:48:03 +0000 (17:48 +0100)
committerRalf Jung <post@ralfj.de>
Fri, 20 Mar 2020 16:48:03 +0000 (17:48 +0100)
src/librustc_passes/layout_test.rs
src/test/ui/layout/debug.rs
src/test/ui/layout/debug.stderr

index 66297eb9727365d9ce9d5f014cf33c2f0a243f2b..32561c6bd87e79f28077579cc5f852b573226810 100644 (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);
+                    }
                 }
             }
+            _ => {}
         }
     }
 
index 64a02ee5a22c821cd5fa8da119ef16a2a6d76d3f..047002c9c99e2ae94f536d681a8179d57b3757ae 100644 (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)]
+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
index df8b70f307074d4c5c4a1cb9d5f8edf42946f529..6e704528c4130bb01a33f7bf830e51b31128a523 100644 (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