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

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

Closed
QuineDot opened this issue Apr 10, 2022 · 3 comments · Fixed by #98283
Closed
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. E-mentor Call for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@QuineDot
Copy link

QuineDot commented Apr 10, 2022

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.

@QuineDot QuineDot added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Apr 10, 2022
@compiler-errors
Copy link
Member

compiler-errors commented Apr 11, 2022

I also agree that this seems like a regression.

  1. Firstly, I think that the word "inaccessible" is vague. I think "private" is pretty standard Rust terminology.
  2. In both cases (where all fields are provided, and when some are missing) we sould probably continue to mention that the struct can't be constructed due to having private fields -- this is a property that holds even when we provide all the fields.
  3. In the case that not all private fields are provided, but some are, we should at least point at which ones are private.

@compiler-errors compiler-errors added E-mentor Call for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion. E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. labels Apr 11, 2022
@hadrizi
Copy link

hadrizi commented Apr 12, 2022

@rustbot claim

@lionellloh
Copy link
Contributor

Hi @hadrizi are you still working on this?

Dylan-DPC added a commit to Dylan-DPC/rust that referenced this issue Jun 22, 2022
…struct-literal, r=compiler-errors

Point at private fields in struct literal

closes rust-lang#95872
compiler-errors added a commit to compiler-errors/rust that referenced this issue Jun 23, 2022
…struct-literal, r=compiler-errors

Point at private fields in struct literal

closes rust-lang#95872
@bors bors closed this as completed in 41cb5e9 Jun 24, 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 E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. E-mentor Call for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion. 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