Skip to content

Private fields are displayed in the error message for using struct or tuple struct literal syntax #151408

Description

@T-Dark0

Code

use std::num::NonZero;
fn main() {
    let owo = NonZero();
    let uwu = Vec {};

Current output

error[E0423]: cannot initialize a tuple struct which contains private fields
   --> src/main.rs:4:15
    |
  4 |     let owo = NonZero();
    |               ^^^^^^^
    |
note: constructor is not visible here due to private fields
   --> /playground/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/num/nonzero.rs:127:42
    |
127 | pub struct NonZero<T: ZeroablePrimitive>(T::NonZeroInner);
    |                                          ^^^^^^^^^^^^^^^ private field
help: you might have meant to use an associated function to build this type
    |
  4 -     let owo = NonZero();
  4 +     let owo = NonZero::new_unchecked(_);
    |
  4 -     let owo = NonZero();
  4 +     let owo = NonZero::from_be(_);
    |
  4 -     let owo = NonZero();
  4 +     let owo = NonZero::from_le(_);
    |
  4 -     let owo = NonZero();
  4 +     let owo = NonZero::from_be(_);
    |
    = and 21 other candidates

error: cannot construct `Vec<_, _>` with struct literal syntax due to private fields
 --> src/main.rs:5:15
  |
5 |     let uwu = Vec {};
  |               ^^^
  |
  = note: private fields `buf` and `len` that were not provided
help: you might have meant to use an associated function to build this type
  |
5 -     let uwu = Vec {};
5 +     let uwu = Vec::new();
  |
5 -     let uwu = Vec {};
5 +     let uwu = Vec::new_in(_);
  |
5 -     let uwu = Vec {};
5 +     let uwu = Vec::with_capacity(_);
  |
5 -     let uwu = Vec {};
5 +     let uwu = Vec::from_raw_parts(_, _, _);
  |
  = and 4 other candidates
help: consider using the `Default` trait
  |
5 -     let uwu = Vec {};
5 +     let uwu = <Vec as std::default::Default>::default();
  |

For more information about this error, try `rustc --explain E0423`.

Desired output

// help messages omitted for brevity. This issue is not about them

error[E0423]: cannot initialize a tuple struct which contains private fields
   --> src/main.rs:4:15
    |
  4 |     let owo = NonZero();
    |               ^^^^^^^
    |

error: cannot construct `Vec<_, _>` with struct literal syntax due to private fields
 --> src/main.rs:5:15
  |
5 |     let uwu = Vec {};
  |               ^^^
  |

Rationale and extra context

(A reproduction of the issue is available on the playground)

The existing error message reports the precise names or types of private fields. This is completely useless to any end user: all they could possibly care about knowing is that there are private fields and as such the type cannot be constructed using the literal syntax. Telling them the precise type, or the names, of these fields is unnecessary verbosity and runs the risk of confusing users, making them think they need to produce a T::NonZeroInner or figure out what type buf is inside Vec.

Other cases

Rust Version

$ rustc +stable --version --verbose
rustc 1.92.0 (ded5c06cf 2025-12-08)
binary: rustc
commit-hash: ded5c06cf21d2b93bffd5d884aa6e96934ee4234
commit-date: 2025-12-08
host: x86_64-pc-windows-msvc
release: 1.92.0
LLVM version: 21.1.3

Anything else?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions