Skip to content

Missing comma when defaulting fields on a struct #96871

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

Closed
gilescope opened this issue May 9, 2022 · 9 comments
Closed

Missing comma when defaulting fields on a struct #96871

gilescope opened this issue May 9, 2022 · 9 comments
Assignees
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

@gilescope
Copy link
Contributor

gilescope commented May 9, 2022

Context: You have an existing struct which has some fields defaulted. If you add another field but forget the comma like this:

struct A {
    field1: String,
    field2: String
    ..default()
}

Then currently rustc complains about a range. Here's a snapshot of an actual example:
image

It could be a legitimate range problem but in that situation it might very well be a missing comma.

@gilescope gilescope 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 May 9, 2022
@pierwill
Copy link
Member

@rustbot claim

@pierwill
Copy link
Member

@pierwill
Copy link
Member

Looks like maybe a fix could be implemented in parse_pat_fields, which has logic for handling commas and dots (..,...).

@pierwill
Copy link
Member

pierwill commented May 10, 2022

There's also this:

// If the last field is a range literal, but it isn't supposed to be, then they probably
// meant to use functional update syntax.
//
// I don't use 'is_range_literal' because only double-sided, half-open ranges count.

@pierwill
Copy link
Member

pierwill commented May 10, 2022

Using nightly in the playground example above, the slightly confusing error that mentions Range is followed by

error[E0063]: missing fields `n1` and `n2` in initializer of `A`
  --> src/main.rs:11:13
   |
11 |     let x = A {
   |             ^ missing `n1` and `n2`
   |
help: to set the remaining fields from `x`, separate the last named field with a comma
   |
13 |         field2: String::from("yo"),..x,
   |                                   +

This is spot on. See #94939 and #51103.

There is arguably room for improvement here, but if this change from stable to nightly is improvement enough (and I think it is), I'm inclined to have this closed. @gilescope @estebank

@gilescope
Copy link
Contributor Author

gilescope commented May 11, 2022 via email

@estebank
Copy link
Contributor

There is arguably room for improvement here, but if this change from stable to nightly is improvement enough (and I think it is), I'm inclined to have this closed.

I'm ok with closing as is, but would love it if you're still interested in looking into finding a way to improve this if we could silence the type mismatch error when encountering this case and only emit the later one that has the suggestion.

@pierwill
Copy link
Member

finding a way to... silence the type mismatch error

I'll give this a look. 👍

pierwill added a commit to pierwill/rust that referenced this issue Nov 1, 2022
@estebank
Copy link
Contributor

Current output:

error[E0308]: mismatched types
  --> src/main.rs:14:11
   |
14 |         ..x
   |           ^ expected `String`, found `A`

error[E0063]: missing fields `n1` and `n2` in initializer of `A`
  --> src/main.rs:11:13
   |
11 |     let x = A {
   |             ^ missing `n1` and `n2`
   |
note: this expression may have been misinterpreted as a `..` range expression
  --> src/main.rs:13:17
   |
13 |         field2: String::from("yo")
   |                 ^^^^^^^^^^^^^^^^^^ this expression does not end in a comma...
14 |         ..x
   |         ^^^ ... so this is interpreted as a `..` range expression, instead of functional record update syntax
help: to set the remaining fields from `x`, separate the last named field with a comma
   |
13 |         field2: String::from("yo"), 
   |                                   +

I'm gonna close as is.

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

No branches or pull requests

3 participants