Skip to content

The "cannot create non-exhaustive struct using struct expression" warning should silence the "cannot construct Foo with struct literal syntax due to private fields" warning #147513

@Darksonn

Description

@Darksonn

Code

// src/lib.rs
#[non_exhaustive]
pub struct Foo {
    pub my_field: u32,
    private_field: i32,
}
// src/main.rs
use exhaustive_test::Foo;

fn main() {
    let foo = Foo {
        my_field: 10,
    };
}

Current output

error[E0639]: cannot create non-exhaustive struct using struct expression
 --> src/main.rs:4:15
  |
4 |       let foo = Foo {
  |  _______________^
5 | |         my_field: 10,
6 | |     };
  | |_____^

error: cannot construct `Foo` with struct literal syntax due to private fields
 --> src/main.rs:4:15
  |
4 |     let foo = Foo {
  |               ^^^
  |
  = note: ...and other private field `private_field` that was not provided

Desired output

error[E0639]: cannot create non-exhaustive struct using struct expression
 --> src/main.rs:4:15
  |
4 |       let foo = Foo {
  |  _______________^
5 | |         my_field: 10,
6 | |     };
  | |_____^

Rationale and extra context

There is no reason to provide two errors for the same issue, and the #[non_exhaustive] issue is the one that should be highlighted since it's explicitly chosen by the struct author.

Other cases

If there is no private field, then the correct output is created.

Rust Version

$ rustc +nightly --version --verbose
rustc 1.92.0-nightly (b6f0945e4 2025-10-08)
binary: rustc
commit-hash: b6f0945e4681bc4d2faa7c22c5f61dc36abf7dd2
commit-date: 2025-10-08
host: x86_64-unknown-linux-gnu
release: 1.92.0-nightly
LLVM version: 21.1.2

Anything else?

No response

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsD-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