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

Better error message for .. in struct patterns not in last position #49257

Closed
Badel2 opened this issue Mar 22, 2018 · 3 comments
Closed

Better error message for .. in struct patterns not in last position #49257

Badel2 opened this issue Mar 22, 2018 · 3 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one.

Comments

@Badel2
Copy link
Contributor

Badel2 commented Mar 22, 2018

Since the let P(.., y) = p; syntax is allowed in unnamed patterns, people will try the same in named patterns, let P { .., y } = p;,and it won't work. Right now the error is

error: expected `}`, found `,`
   |
13 |     let Point { .., y } = a;
   |                   ^ expected `}`

followed by other errors about "pattern does not mention field x". Test code playground link

struct Point { x: u8, y: u8 }
struct P(u8, u8);

fn main() {
    // Unnamed patterns allow `.., y`
    let p = P(0, 0);
    let P(x, ..) = p;
    let P(.., y) = p;
    
    // But named patterns don't
    let a = Point { x: 0, y: 0 };
    let Point { x, .. } = a; // works
    let Point { .., y } = a; // lots of errors
}

Adding a clear error message should be pretty easy, just check if there is a comma after DotDot and display a better error message ".. must always be the last field, and it cannot have a trailing comma". Actually, I think this is the code that checks if .. is followed by a }, so adding the , check should be pretty straight-forward:

if self.token != token::CloseDelim(token::Brace) {
let token_str = self.this_token_to_string();
let mut err = self.fatal(&format!("expected `{}`, found `{}`", "}", token_str));
err.span_label(self.span, "expected `}`");
return Err(err);
}

@Badel2
Copy link
Contributor Author

Badel2 commented Mar 22, 2018

An alternative would be allowing .. anywhere in struct patterns, to keep it similar to tuple patterns, but that would probably need an RFC, right?

Anyway, if someone wants to work on this issue, go ahead, otherwise I will fix it when I have some free time.

@estebank estebank added A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. labels Mar 22, 2018
@scottmcm
Copy link
Member

I think that it should remain accepted only at the end, the same as in struct literals. Tuple patterns need the flexibility, but struct patterns are named, so there's no reason to every have it anywhere but the end.

@ordovicia
Copy link
Contributor

I'll take this.

frewsxcv added a commit to frewsxcv/rust that referenced this issue Mar 23, 2018
…trochenkov

Better diagnostics for '..' pattern fragment not in the last position

Fixes rust-lang#49257.
kennytm added a commit to kennytm/rust that referenced this issue Mar 24, 2018
…trochenkov

Better diagnostics for '..' pattern fragment not in the last position

Fixes rust-lang#49257.
kennytm added a commit to kennytm/rust that referenced this issue Mar 24, 2018
…trochenkov

Better diagnostics for '..' pattern fragment not in the last position

Fixes rust-lang#49257.
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 C-enhancement Category: An issue proposing an enhancement or a PR with one.
Projects
None yet
Development

No branches or pull requests

4 participants