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

No mention of "field init shorthand" #98917

Closed
andrzejtp opened this issue Jul 5, 2022 · 0 comments · Fixed by #99030
Closed

No mention of "field init shorthand" #98917

andrzejtp opened this issue Jul 5, 2022 · 0 comments · Fixed by #99030
Labels
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.

Comments

@andrzejtp
Copy link

Given the following code:
https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=6fd5b067e894e9be655bd28918386f7e

struct Struct {
    id: usize,
    option: Option<usize>,
}

fn create(id: usize) -> Struct {
    Struct {
        id,
        Some(1),
    }
}

fn main() {
    let _ = create(1);
}

The current output is:

error: expected one of `,` or `}`, found `(`
 --> src/bin/lib.rs:9:13
  |
7 |     Struct {
  |     ------ while parsing this struct
8 |         id,
9 |         Some(1),
  |             ^ expected one of `,` or `}`

error[E0063]: missing field `option` in initializer of `Struct`
 --> src/bin/lib.rs:7:5
  |
7 |     Struct {
  |     ^^^^^^ missing `option`

The first message is about an expected comma or closing curly brace, which is not terribly helpful - we do want to initialize more than one member, so why would we be required to end the instantiation after the id,?

The "missing field option" message will perhaps prompt the reader to explicitly name the option member, like this:

fn create(id: usize) -> Struct {
    Struct {
        id,
        option: Some(1),
    }
}

and this will solve the problem, which may trick the reader into believing that only "simple" initializers can be used when instantiating the Struct (and Some(1) would not be considered "simple").

Reading the output of rustc --explain E0063 does not help, because it says "A struct's or struct-like enum variant's field was not provided". The actual problem is that while the id member can use the "field init shorthand" (because it is called the same as the create's argument), the option member cannot, but there's no such hint neither in the compiler messages nor in the rustc --explain E0063.

Ideally some mention of the "field init shorthand" should be provided either in the messages, or in the output of rustc --explain.

@andrzejtp andrzejtp 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 Jul 5, 2022
@bors bors closed this as completed in 980579a Jul 13, 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 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.

1 participant