Skip to content

Detect attempt to use '...' for struct update #102806

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
jruderman opened this issue Oct 8, 2022 · 1 comment · Fixed by #103012
Closed

Detect attempt to use '...' for struct update #102806

jruderman opened this issue Oct 8, 2022 · 1 comment · Fixed by #103012
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

@jruderman
Copy link
Contributor

jruderman commented Oct 8, 2022

If I try to use "functional record update syntax" (aka "struct update syntax") with three dots instead of two, the compiler could emit a clearer error message.

Given the following code (playground):

struct V3 {
    x: f32,
    y: f32,
    z: f32
}

fn pz(v: V3) {
    let _ = V3 {
        z: 0.0,
        ...v
    };
}

The current output is:

error: expected identifier, found `...`
  --> src/lib.rs:10:9
   |
8  |     let _ = V3 {
   |             -- while parsing this struct
9  |         z: 0.0,
10 |         ...v
   |         ^^^ expected identifier

error[E0063]: missing fields `x` and `y` in initializer of `V3`
 --> src/lib.rs:8:13
  |
8 |     let _ = V3 {
  |             ^^ missing `x` and `y`

Ideally the output should be more inclusive about what token was expected:

error: expected identifier or `..`, found `...`

and include a suggestion directing the programmer to the correct syntax:

help: to fill in the rest of the fields from `v`, write `..v` (note only two dots)

Motivation

  • Programmers who also write in JavaScript might be familiar with the similar spread syntax in object literals, which uses ...
  • In English prose, ... is more common than ..
@jruderman jruderman 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 Oct 8, 2022
@chenyukang
Copy link
Member

@rustbot claim

matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Nov 6, 2022
…,compiler-errors

Suggest use .. to fill in the rest of the fields of Struct

Fixes rust-lang#102806
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Nov 6, 2022
…,compiler-errors

Suggest use .. to fill in the rest of the fields of Struct

Fixes rust-lang#102806
@bors bors closed this as completed in 1e25882 Nov 6, 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.

2 participants