about summary refs log tree commit diff
diff options
context:
space:
mode:
authorEsteban Küber <esteban@kuber.com.ar>2022-12-27 18:28:02 -0800
committerEsteban Küber <esteban@kuber.com.ar>2022-12-27 18:28:02 -0800
commit50c1be1d19ccbfc5668119139d89b8163615b17c (patch)
tree05125ee26f18c79314c5ec6a9e9535b30ad082d4
parent92c1937a90e5b6f20fa6e87016d6869da363972e (diff)
downloadrust-50c1be1d19ccbfc5668119139d89b8163615b17c.tar.gz
rust-50c1be1d19ccbfc5668119139d89b8163615b17c.zip
Emit fewer errors on invalid `#[repr(transparent)]` on `enum`
Fix #68420.
-rw-r--r--compiler/rustc_hir_analysis/src/check/check.rs6
-rw-r--r--src/test/ui/repr/transparent-enum-too-many-variants.rs10
-rw-r--r--src/test/ui/repr/transparent-enum-too-many-variants.stderr11
3 files changed, 23 insertions, 4 deletions
diff --git a/compiler/rustc_hir_analysis/src/check/check.rs b/compiler/rustc_hir_analysis/src/check/check.rs
index a714663741b..87cc69757b0 100644
--- a/compiler/rustc_hir_analysis/src/check/check.rs
+++ b/compiler/rustc_hir_analysis/src/check/check.rs
@@ -1060,10 +1060,8 @@ pub(super) fn check_transparent<'tcx>(tcx: TyCtxt<'tcx>, adt: ty::AdtDef<'tcx>)
 
     if adt.variants().len() != 1 {
         bad_variant_count(tcx, adt, tcx.def_span(adt.did()), adt.did());
-        if adt.variants().is_empty() {
-            // Don't bother checking the fields. No variants (and thus no fields) exist.
-            return;
-        }
+        // Don't bother checking the fields.
+        return;
     }
 
     // For each field, figure out if it's known to be a ZST and align(1), with "known"
diff --git a/src/test/ui/repr/transparent-enum-too-many-variants.rs b/src/test/ui/repr/transparent-enum-too-many-variants.rs
new file mode 100644
index 00000000000..0dd4b4e6846
--- /dev/null
+++ b/src/test/ui/repr/transparent-enum-too-many-variants.rs
@@ -0,0 +1,10 @@
+use std::mem::size_of;
+
+#[repr(transparent)]
+enum Foo { //~ ERROR E0731
+    A(u8), B(u8),
+}
+
+fn main() {
+    println!("Foo: {}", size_of::<Foo>());
+}
diff --git a/src/test/ui/repr/transparent-enum-too-many-variants.stderr b/src/test/ui/repr/transparent-enum-too-many-variants.stderr
new file mode 100644
index 00000000000..fb44757efaf
--- /dev/null
+++ b/src/test/ui/repr/transparent-enum-too-many-variants.stderr
@@ -0,0 +1,11 @@
+error[E0731]: transparent enum needs exactly one variant, but has 2
+  --> $DIR/transparent-enum-too-many-variants.rs:4:1
+   |
+LL | enum Foo {
+   | ^^^^^^^^ needs exactly one variant, but has 2
+LL |     A(u8), B(u8),
+   |     -      - too many variants in `Foo`
+
+error: aborting due to previous error
+
+For more information about this error, try `rustc --explain E0731`.