-
Notifications
You must be signed in to change notification settings - Fork 13.2k
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
struct Foo { | ||
let: i32, | ||
//~^ ERROR expected identifier, found keyword | ||
} | ||
|
||
fn main() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
error: expected identifier, found keyword `let` | ||
--> $DIR/bad-let-as-field.rs:2:5 | ||
| | ||
LL | struct Foo { | ||
| --- while parsing this struct | ||
LL | let: i32, | ||
| ^^^ expected identifier, found keyword | ||
| | ||
help: escape `let` to use it as an identifier | ||
| | ||
LL | r#let: i32, | ||
| ++ | ||
|
||
error: aborting due to previous error | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
struct Foo { | ||
let x: i32, | ||
//~^ ERROR expected identifier, found keyword | ||
let y: i32, | ||
//~^ ERROR expected identifier, found keyword | ||
} | ||
|
||
fn main() { | ||
let _ = Foo { | ||
//~^ ERROR missing fields `x` and `y` in initializer of `Foo` | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
error: expected identifier, found keyword `let` | ||
--> $DIR/removed-syntax-field-let-2.rs:2:5 | ||
| | ||
LL | let x: i32, | ||
| ^^^- | ||
| | | ||
| expected identifier, found keyword | ||
| help: remove this `let` keyword | ||
| | ||
= note: the `let` keyword is not allowed in `struct` fields | ||
= note: see <https://doc.rust-lang.org/book/ch05-01-defining-structs.html> for more information | ||
|
||
error: expected identifier, found keyword `let` | ||
--> $DIR/removed-syntax-field-let-2.rs:4:5 | ||
| | ||
LL | let y: i32, | ||
| ^^^- | ||
| | | ||
| expected identifier, found keyword | ||
| help: remove this `let` keyword | ||
| | ||
= note: the `let` keyword is not allowed in `struct` fields | ||
= note: see <https://doc.rust-lang.org/book/ch05-01-defining-structs.html> for more information | ||
|
||
error[E0063]: missing fields `x` and `y` in initializer of `Foo` | ||
--> $DIR/removed-syntax-field-let-2.rs:9:13 | ||
| | ||
LL | let _ = Foo { | ||
| ^^^ missing `x` and `y` | ||
|
||
error: aborting due to 3 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0063`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 thelet
keyword, and the user can runrustfmt
to fix the leftover spaces 😝 thoughts?There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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*file that hasstruct
let ident: ty
inside of it, I think formatting is a step that will need to be done regardless 😅There was a problem hiding this comment.
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.