Skip to content

transparent enum diagnostic should suppress field count when >1 variant #68420

Closed

Description

Consider this code:

#![feature(transparent_enums)]

use std::mem::size_of;

fn main() {
    #[repr(transparent)]
    enum VisibleE8 {
        A(u8), B(u8), C(u8), D(u8), E(u8), F(u8), G(u8), H(u8), I(u8), J(u8),
        K(u8), L(u8), M(u8), N(u8), O(u8), P(u8), Q(u8), R(u8), S(u8), T(u8),
    }

    println!("VisibleE8: {}", size_of::<VisibleE8>());
}

When you try to compile this, you get two error diagnostics:

error[E0731]: transparent enum needs exactly one variant, but has 20
 --> src/main.rs:7:5
  |
7 |     enum VisibleE8 {
  |     ^^^^^^^^^^^^^^ needs exactly one variant, but has 20
8 |         A(u8), B(u8), C(u8), D(u8), E(u8), F(u8), G(u8), H(u8), I(u8), J(u8),
  |         -----  -----  -----  -----  -----  -----  -----  -----  -----  -----
9 |         K(u8), L(u8), M(u8), N(u8), O(u8), P(u8), Q(u8), R(u8), S(u8), T(u8),
  |         -----  -----  -----  -----  -----  -----  -----  -----  -----  ----- too many variants in `main::VisibleE8`

error[E0690]: the variant of a transparent enum needs exactly one non-zero-sized field, but has 20
 --> src/main.rs:7:5
  |
7 |     enum VisibleE8 {
  |     ^^^^^^^^^^^^^^ needs exactly one non-zero-sized field, but has 20
8 |         A(u8), B(u8), C(u8), D(u8), E(u8), F(u8), G(u8), H(u8), I(u8), J(u8),
  |           --     --     --     --     --     --     --     --     --     -- this field is non-zero-sized
  |           |      |      |      |      |      |      |      |      |
  |           |      |      |      |      |      |      |      |      this field is non-zero-sized
  |           |      |      |      |      |      |      |      this field is non-zero-sized
  |           |      |      |      |      |      |      this field is non-zero-sized
  |           |      |      |      |      |      this field is non-zero-sized
  |           |      |      |      |      this field is non-zero-sized
  |           |      |      |      this field is non-zero-sized
  |           |      |      this field is non-zero-sized
  |           |      this field is non-zero-sized
  |           this field is non-zero-sized
9 |         K(u8), L(u8), M(u8), N(u8), O(u8), P(u8), Q(u8), R(u8), S(u8), T(u8),
  |           --     --     --     --     --     --     --     --     --     -- this field is non-zero-sized
  |           |      |      |      |      |      |      |      |      |
  |           |      |      |      |      |      |      |      |      this field is non-zero-sized
  |           |      |      |      |      |      |      |      this field is non-zero-sized
  |           |      |      |      |      |      |      this field is non-zero-sized
  |           |      |      |      |      |      this field is non-zero-sized
  |           |      |      |      |      this field is non-zero-sized
  |           |      |      |      this field is non-zero-sized
  |           |      |      this field is non-zero-sized
  |           |      this field is non-zero-sized
  |           this field is non-zero-sized

error: aborting due to 2 previous errors

The first diagnostic is exactly describing the problem.

The second diagnostic is irrelevant, and arguably confusing (at least in my mental model of enums, the field count of one variant should be considered independently of the counts for all of the enum's other variants).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsC-enhancementCategory: An issue proposing an enhancement or a PR with one.D-verboseDiagnostics: Too much output caused by a single piece of incorrect code.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions