Skip to content

Commit

Permalink
Rollup merge of #106201 - estebank:verbose-transparent, r=compiler-er…
Browse files Browse the repository at this point in the history
…rors

Emit fewer errors on invalid `#[repr(transparent)]` on `enum`

Fix #68420.
  • Loading branch information
fee1-dead committed Dec 28, 2022
2 parents 45d6f02 + 50c1be1 commit f837da7
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
6 changes: 2 additions & 4 deletions compiler/rustc_hir_analysis/src/check/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
10 changes: 10 additions & 0 deletions src/test/ui/repr/transparent-enum-too-many-variants.rs
Original file line number Diff line number Diff line change
@@ -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>());
}
11 changes: 11 additions & 0 deletions src/test/ui/repr/transparent-enum-too-many-variants.stderr
Original file line number Diff line number Diff line change
@@ -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`.

0 comments on commit f837da7

Please sign in to comment.