Skip to content

"cannot construct with struct literal syntax due to inaccessible fields" could use an explain #95872

Closed
@QuineDot

Description

@QuineDot

Given the following code:

pub mod m {
    pub struct S {
        pub visible: bool,
        private: (),
        also_private: String,
    }
}

fn main() {
    let _ = m::S {
        visible: true,
        private: (),
    };
}

The current output is:

error: cannot construct `S` with struct literal syntax due to inaccessible fields
  [--> src/main.rs:10:13
](https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=9f3e3c29a1dd0db9008d9683c108b330#)   |
10 |     let _ = m::S {
   |             ^^^^

Ideally, the output should

  • Explain more fully that there are private fields, perhaps via --explain
  • Not occlude E0451 when some private fields are supplied (as in the above example)

In my opinion, the errors when you supply all the private fields are more understandable than those when you supply none or only some of the private fields. See also this URLO thread.

I believe the current output is a result of #87872.

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsE-easyCall for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.E-mentorCall for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion.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