Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
pnkfelix opened this issue Jan 21, 2020 · 1 comment · Fixed by #106201
Closed

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

pnkfelix opened this issue Jan 21, 2020 · 1 comment · Fixed by #106201
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. D-verbose Diagnostics: Too much output caused by a single piece of incorrect code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@pnkfelix
Copy link
Member

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).

@pnkfelix pnkfelix added T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. A-diagnostics Area: Messages for errors, warnings, and lints labels Jan 21, 2020
@estebank estebank added the D-verbose Diagnostics: Too much output caused by a single piece of incorrect code. label Jan 21, 2020
@JohnTitor JohnTitor added the C-enhancement Category: An issue proposing an enhancement or a PR with one. label Jan 26, 2020
@eddyb
Copy link
Member

eddyb commented Mar 22, 2020

If this is using ty::AdtDef::all_fields, we might want to audit its other uses, just came across a call to it in struct-only code, it should probably not exist, as it seems easy to misuse.

fee1-dead added a commit to fee1-dead-contrib/rust that referenced this issue Dec 28, 2022
…mpiler-errors

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

Fix rust-lang#68420.
@bors bors closed this as completed in 50c1be1 Dec 28, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. D-verbose Diagnostics: Too much output caused by a single piece of incorrect code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants