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 comma after base struct #45178

Merged
merged 1 commit into from
Oct 13, 2017

Conversation

Badel2
Copy link
Contributor

@Badel2 Badel2 commented Oct 10, 2017

#41834

This adds a better error for commas after the base struct:

let foo = Foo {
    one: 111,
    ..Foo::default(), // This comma is a syntax error
};

The current error is a generic expected one of ... which isn't beginner-friendly. My error looks like this:

error: cannot use a comma after the base struct
  --> tmp/example.rs:26:9
   |
26 |         ..Foo::default(),
   |         ^^^^^^^^^^^^^^^^- help: remove this comma
   |
   = note: the base struct expansion must always be the last field

I even added a note for people who don't know why this isn't allowed.

@rust-highfive
Copy link
Collaborator

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @nikomatsakis (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

Please see the contribution instructions for more information.

Copy link
Member

@scottmcm scottmcm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like a great improvement!

I think it should have a test, though? I guess it'd be a parse-fail test?

"cannot use a comma after struct expansion",
);
err.span_suggestion_short(self.span, "remove this comma", "".to_owned());
err.note("the struct expansion should always be the last field");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick: It must be the last field, not just should, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright, thanks, I will add a test. Should I call it issue-41834.rs or struct-expansion.rs? (not sure what's the correct term for the ..default())

@Badel2 Badel2 force-pushed the comma-after-struct branch 2 times, most recently from 4d5e257 to 1a0c551 Compare October 10, 2017 23:23
@@ -0,0 +1,27 @@
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you make this test an UI test?

@@ -2323,6 +2324,16 @@ impl<'a> Parser<'a> {
self.recover_stmt();
}
}
if self.check(&token::Comma) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: check adds the token into the "expected tokens" set, self.token == &token::Comma should be enough here.

"cannot use a comma after struct expansion",
);
err.span_suggestion_short(self.span, "remove this comma", "".to_owned());
err.note("the struct expansion must always be the last field");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"the" shouldn't be here if we talk about struct expansions in general?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, this is the first time I see the name "struct expansion".
Looks like it was a @Xion's invention in #41834.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not even sure how this thing is called (..expr specifically, the whole Struct { field1, field2, ..expr } is usually called a "functional record update").
What the book/reference/etc say?

Copy link
Contributor Author

@Badel2 Badel2 Oct 11, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The book refers to this as "field init shorthand", or "field init syntax", however still not sure how we call the item. The first edition of the book refers to all this as "update syntax", so I'm not even sure if that is the official name.

Anyway I suggest we just call it the "base struct":
"cannot use a comma after the base struct"
"the base struct must always be the last field"
(I use "the" because there is only one base struct)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, sounds good.

`let x = { ..default(), } // This comma is an error`
@petrochenkov
Copy link
Contributor

@bors r+

@bors
Copy link
Contributor

bors commented Oct 11, 2017

📌 Commit 72cfd20 has been approved by petrochenkov

@bors
Copy link
Contributor

bors commented Oct 11, 2017

🌲 The tree is currently closed for pull requests below priority 2, this pull request will be tested once the tree is reopened

@Badel2 Badel2 changed the title Better error message for comma after struct expansion Better error message for comma after base struct Oct 11, 2017
@aidanhs aidanhs added the S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. label Oct 12, 2017
@Mark-Simulacrum
Copy link
Member

@bors rollup

kennytm added a commit to kennytm/rust that referenced this pull request Oct 13, 2017
…henkov

Better error message for comma after base struct

rust-lang#41834

This adds a better error for commas after the base struct:
```
let foo = Foo {
    one: 111,
    ..Foo::default(), // This comma is a syntax error
};
```

The current error is a generic `expected one of ...` which isn't beginner-friendly. My error looks like this:

```
error: cannot use a comma after the base struct
  --> tmp/example.rs:26:9
   |
26 |         ..Foo::default(),
   |         ^^^^^^^^^^^^^^^^- help: remove this comma
   |
   = note: the base struct expansion must always be the last field
```

I even added a note for people who don't know why this isn't allowed.
bors added a commit that referenced this pull request Oct 13, 2017
Rollup of 14 pull requests

- Successful merges: #44855, #45110, #45122, #45133, #45173, #45178, #45189, #45203, #45209, #45221, #45236, #45240, #45245, #45253
- Failed merges:
@bors bors merged commit 72cfd20 into rust-lang:master Oct 13, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants