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

Detect blocks that could be struct expr bodies #75470

Merged
merged 1 commit into from
Oct 8, 2020

Conversation

estebank
Copy link
Contributor

@estebank estebank commented Aug 12, 2020

This approach lives exclusively in the parser, so struct expr bodies
that are syntactically correct on their own but are otherwise incorrect
will still emit confusing errors, like in the following case:

fn foo() -> Foo {
    bar: Vec::new()
}
error[E0425]: cannot find value `bar` in this scope
 --> src/file.rs:5:5
  |
5 |     bar: Vec::new()
  |     ^^^ expecting a type here because of type ascription

error[E0214]: parenthesized type parameters may only be used with a `Fn` trait
 --> src/file.rs:5:15
  |
5 |     bar: Vec::new()
  |               ^^^^^ only `Fn` traits may use parentheses

error[E0107]: wrong number of type arguments: expected 1, found 0
 --> src/file.rs:5:10
  |
5 |     bar: Vec::new()
  |          ^^^^^^^^^^ expected 1 type argument

If that field had a trailing comma, that would be a parse error and it
would trigger the new, more targetted, error:

error: struct literal body without path
 --> file.rs:4:17
  |
4 |   fn foo() -> Foo {
  |  _________________^
5 | |     bar: Vec::new(),
6 | | }
  | |_^
  |
help: you might have forgotten to add the struct literal inside the block
  |
4 | fn foo() -> Foo { Path {
5 |     bar: Vec::new(),
6 | } }
  |

Partially address last remaining part of #34255.

@rust-highfive
Copy link
Collaborator

r? @davidtwco

(rust_highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Aug 12, 2020
Copy link
Member

@davidtwco davidtwco left a comment

Choose a reason for hiding this comment

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

Looks good, a couple comments.

src/librustc_parse/parser/stmt.rs Outdated Show resolved Hide resolved
src/librustc_parse/parser/stmt.rs Outdated Show resolved Hide resolved
src/librustc_parse/parser/stmt.rs Outdated Show resolved Hide resolved
src/librustc_parse/parser/stmt.rs Outdated Show resolved Hide resolved
src/librustc_parse/parser/stmt.rs Outdated Show resolved Hide resolved
@bors
Copy link
Contributor

bors commented Aug 18, 2020

☔ The latest upstream changes (presumably #75120) made this pull request unmergeable. Please resolve the merge conflicts.

@davidtwco
Copy link
Member

davidtwco commented Sep 15, 2020

Oops, looks like I missed this after you addressed the comments - could you rebase @estebank?

Nevermind, looks like you already did that.

@davidtwco
Copy link
Member

@bors r+

@bors
Copy link
Contributor

bors commented Sep 15, 2020

📌 Commit 8460f0dbe73123036813fbe036c9d5b2e2ac3fcd has been approved by davidtwco

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Sep 15, 2020
@bors
Copy link
Contributor

bors commented Sep 15, 2020

⌛ Testing commit 8460f0dbe73123036813fbe036c9d5b2e2ac3fcd with merge 5d754460711212fc385e4c257697dc8d85354356...

@bors
Copy link
Contributor

bors commented Sep 15, 2020

💔 Test failed - checks-azure

@bors bors added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Sep 15, 2020
@bors
Copy link
Contributor

bors commented Sep 15, 2020

☔ The latest upstream changes (presumably #76171) made this pull request unmergeable. Please resolve the merge conflicts.

Note that reviewers usually do not review pull requests until merge conflicts are resolved! Once you resolve the conflicts, you should change the labels applied by bors to indicate that your PR is ready for review. Post this as a comment to change the labels:

@rustbot modify labels: +S-waiting-on-review -S-waiting-on-author

@camelid camelid added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Oct 7, 2020
@camelid
Copy link
Member

camelid commented Oct 7, 2020

@estebank Can you fix the merge conflicts?

This approach lives exclusively in the parser, so struct expr bodies
that are syntactically correct on their own but are otherwise incorrect
will still emit confusing errors, like in the following case:

```rust
fn foo() -> Foo {
    bar: Vec::new()
}
```

```
error[E0425]: cannot find value `bar` in this scope
 --> src/file.rs:5:5
  |
5 |     bar: Vec::new()
  |     ^^^ expecting a type here because of type ascription

error[E0214]: parenthesized type parameters may only be used with a `Fn` trait
 --> src/file.rs:5:15
  |
5 |     bar: Vec::new()
  |               ^^^^^ only `Fn` traits may use parentheses

error[E0107]: wrong number of type arguments: expected 1, found 0
 --> src/file.rs:5:10
  |
5 |     bar: Vec::new()
  |          ^^^^^^^^^^ expected 1 type argument
  ```

If that field had a trailing comma, that would be a parse error and it
would trigger the new, more targetted, error:

```
error: struct literal body without path
 --> file.rs:4:17
  |
4 |   fn foo() -> Foo {
  |  _________________^
5 | |     bar: Vec::new(),
6 | | }
  | |_^
  |
help: you might have forgotten to add the struct literal inside the block
  |
4 | fn foo() -> Foo { Path {
5 |     bar: Vec::new(),
6 | } }
  |
```

Partially address last part of rust-lang#34255.
@estebank
Copy link
Contributor Author

estebank commented Oct 7, 2020

@bors r=davidtwco

@bors
Copy link
Contributor

bors commented Oct 7, 2020

📌 Commit e5f83bc has been approved by davidtwco

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Oct 7, 2020
@bors
Copy link
Contributor

bors commented Oct 8, 2020

⌛ Testing commit e5f83bc with merge d9985fc...

@camelid camelid added A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` labels Oct 8, 2020
@bors
Copy link
Contributor

bors commented Oct 8, 2020

☀️ Test successful - checks-actions, checks-azure
Approved by: davidtwco
Pushing d9985fc to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Oct 8, 2020
@bors bors merged commit d9985fc into rust-lang:master Oct 8, 2020
@rustbot rustbot added this to the 1.49.0 milestone Oct 8, 2020
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 A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` merged-by-bors This PR was explicitly merged by bors. 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.

6 participants