Repository navigation
PrettyPrint: preserve space between block comment and range operator - #1317
Open
SwastikTripathi wants to merge 1 commit into
Open
SwastikTripathi wants to merge 1 commit into
SwastikTripathi wants to merge 1 commit into
Conversation
When a range formation operator has a block comment in its effective
leading trivia (e.g. `1 /* start */ ..<n`), removing the space between
the comment end and the operator produces non-compilable output.
The Swift parser counts block comments as whitespace for operator-fixity
classification. With whitespace on the left (the comment) but not on the
right, the parser treats the operator as a prefix operator rather than
an infix operator, emitting an "expected '{'" or similar parse error.
Fix: extend `shouldRequireWhitespace` to return `true` when the range
formation operator's effective leading trivia (previous token's trailing
trivia plus the operator's own leading trivia) contains a block comment.
Fixes swiftlang#1302.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
SwastikTripathi
requested review from
allevato,
dylansturg,
hamishknight,
hborla and
rintaro
as code owners
October 26, 2026 06:16
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Fixes #1302.
When a range formation operator has a block comment in its effective leading trivia, e.g.
the formatter removes the space between
*/and..<, producing:This does not compile. The Swift parser counts block comments as whitespace for operator-fixity classification. With whitespace on the left (the comment) but none on the right, the parser treats
..<as a prefix operator.The existing
shouldRequireWhitespacecheck already handles the symmetric cases (postfix operator on the left, prefix operator on the right). This extends it to also returntruewhen the range operator's effective leading trivia contains a block comment or doc-block comment.Verified by running
swift test --filter BinaryOperatorExprTests(8 tests, 0 failures), including the new regression test added for this case.🤖 Generated with Claude Code