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

Fix let keyword removal suggestion in structs #102927

Merged
merged 1 commit into from
Oct 12, 2022

Conversation

compiler-errors
Copy link
Member

(1.) Fixes a bug where, given this code:

struct Foo {
  let x: i32,
}

We were parsing the field name as let instead of x, which causes issues later on in the type-checking phase.

(2.) Also, suggestions for let: i32 as a field regressed, displaying this extra help: which is removed by this PR

help: remove the let, the `let` keyword is not allowed in struct field definitions
  |
2 -     let: i32,
2 +     : i32,

(3.) Makes the suggestion text a bit more succinct, since we don't need to re-explain that let is not allowed in this position (since it's in a note that follows). This causes the suggestion to render inline as well.

cc @gimbles, this addresses a few nits I mentioned in your PR.

@rustbot rustbot added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Oct 11, 2022
@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 Oct 11, 2022
error: expected identifier, found keyword `let`
--> $DIR/removed-syntax-field-let-2.rs:2:5
|
LL | let x: i32,
Copy link
Member Author

@compiler-errors compiler-errors Oct 11, 2022

Choose a reason for hiding this comment

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

I'm actually not a fan of this 4-character wide removal span that overlaps (but isn't equal to) the let keyword span -- I feel like we don't usually care that spacing gets messed up by suggestions anyways, which is what this span is trying to account for.

I think we should just adjust the "remove this let keyword" span to remove just the let keyword, and the user can run rustfmt to fix the leftover spaces 😝 thoughts?

Copy link
Contributor

@gimbling-away gimbling-away Oct 11, 2022

Choose a reason for hiding this comment

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

The running rustfmt part might not be ideal :P

Copy link
Member Author

Choose a reason for hiding this comment

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

Suggestions are not aware of global formatting rules, nor are they typically designed to handle cases where spacing, newlines, etc. are messed up because of code suggestions. At least, as long as the suggestion renders in a way that the user can understand, I feel like this is not the responsibility of the parser's error messages.

Copy link
Contributor

Choose a reason for hiding this comment

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

Right, for just diagnostics that makes sense. I'm more worried about cargo-fix fixing this and resulting in non-formatted code.

Copy link
Member Author

@compiler-errors compiler-errors Oct 11, 2022

Choose a reason for hiding this comment

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

Given that rustfmt absolutely refuses to format a struct *file that has let ident: ty inside of it, I think formatting is a step that will need to be done regardless 😅

Copy link
Member

Choose a reason for hiding this comment

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

I don't have a strong preference here - I'd probably bias towards keeping the four-character span. Feel free to change it if you want.

@compiler-errors compiler-errors changed the title Fix let removal suggestion in struct Fix let keyword removal suggestion in structs Oct 11, 2022
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.

LGTM, r=me if you're happy with this

@compiler-errors
Copy link
Member Author

@bors r=davidtwco

@bors
Copy link
Contributor

bors commented Oct 12, 2022

📌 Commit f9d3c83 has been approved by davidtwco

It is now in the queue for this repository.

@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 Oct 12, 2022
@compiler-errors
Copy link
Member Author

@bors rollup

bors added a commit to rust-lang-ci/rust that referenced this pull request Oct 12, 2022
Rollup of 7 pull requests

Successful merges:

 - rust-lang#102623 (translation: eager translation)
 - rust-lang#102719 (Enforce alphabetical sorting with tidy)
 - rust-lang#102830 (Unify `tcx.constness` query and param env constness checks)
 - rust-lang#102883 (Fix stabilization of `feature(half_open_range_patterns)`)
 - rust-lang#102927 (Fix `let` keyword removal suggestion in structs)
 - rust-lang#102936 (rustdoc: remove unused CSS `nav.sum`)
 - rust-lang#102940 (Update books)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit a9a5529 into rust-lang:master Oct 12, 2022
@rustbot rustbot added this to the 1.66.0 milestone Oct 12, 2022
Aaron1011 pushed a commit to Aaron1011/rust that referenced this pull request Jan 6, 2023
Fix `let` keyword removal suggestion in structs

(1.) Fixes a bug where, given this code:
```rust
struct Foo {
  let x: i32,
}
```

We were parsing the field name as `let` instead of `x`, which causes issues later on in the type-checking phase.

(2.) Also, suggestions for `let: i32` as a field regressed, displaying this extra `help:` which is removed by this PR

```
help: remove the let, the `let` keyword is not allowed in struct field definitions
  |
2 -     let: i32,
2 +     : i32,
```

(3.) Makes the suggestion text a bit more succinct, since we don't need to re-explain that `let` is not allowed in this position (since it's in a note that follows). This causes the suggestion to render inline as well.

cc `@gimbles,` this addresses a few nits I mentioned in your PR.
Aaron1011 pushed a commit to Aaron1011/rust that referenced this pull request Jan 6, 2023
Rollup of 7 pull requests

Successful merges:

 - rust-lang#102623 (translation: eager translation)
 - rust-lang#102719 (Enforce alphabetical sorting with tidy)
 - rust-lang#102830 (Unify `tcx.constness` query and param env constness checks)
 - rust-lang#102883 (Fix stabilization of `feature(half_open_range_patterns)`)
 - rust-lang#102927 (Fix `let` keyword removal suggestion in structs)
 - rust-lang#102936 (rustdoc: remove unused CSS `nav.sum`)
 - rust-lang#102940 (Update books)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
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. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants