-
Notifications
You must be signed in to change notification settings - Fork 900
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
Invalid syntax produced if "in" is a subset of a variable name between for ... in
.
#5009
Comments
Thank you for the report! These types of cases are almost always related to some bad span derivations on rustfmt's part when it needs to figure out where to look for comments in between those elements explicitly represented in the AST. I wouldn't be surprised if we can't yet handle some of those comments, but the absolute worst case scenario should be emitting the original contents associated with the span of the range expression, and not emitting invalid code and carrying on happily, so this definitely a bug. For anyone interested/willing in working on this, note that we use an internal The "connector" here would be the As a starting point I'd suggest taking a look in https://github.com/rust-lang/rustfmt/blob/master/src/expr.rs for controlflow rewriting that's working with the |
I've tracked down the issue to Lines 823 to 826 in f0f449d
Case 1)We write a for loop where the variable contains the trimmed connector Calculated Comment Span
-----------------------
| |
V V
for variable_in_here /* ... */ in 0..1 {} Case 2)we write a for loop where the variable name doesn't contain the word Span is the whitespace after "in"
--------------------------------
|
V
for variable_here /* ... */ in 0..1 {} which actually leads to rustfmt bailing since it would have removed the comment error[internal]: not formatted because a comment would be lost
--> \\?\C:\Users\ytmimi\Product\RUST_PROJECTS\rustfmt\issue_5009_simple.rs:3
|
3 | for variable_here /* ... */ in 0..1 {}
|
= note: set `error_on_unformatted = false` to suppress the warning against comments or string literals
warning: rustfmt has failed to format. See previous 1 errors. Proposed solutionUpdated the let comments_lo = context
.snippet_provider
.span_after_last(self.span, self.connector.trim()); Maybe we can do better than this? but then we'd probably have to consider comments in various different spots: for /* ... */ x in 0..1 {}
for x /* ... */ in 0..1 {}
for x in /* ... */ 0..1 {} // currently only comments in here are considered |
If the subset of a variable name is "in" inside of the for-loop's
for ... in
section and requires formatting with a comment around the in-keyword, an invalid output is produced.For this input:
The output becomes invalid syntax:
If the characters "in" are removed from the input, the output will remain valid syntax:
The text was updated successfully, but these errors were encountered: