Skip to content

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

Closed
@jruderman

Description

@jruderman

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 ..

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsT-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