Skip to content

PrettyPrint: preserve space between block comment and range operator - #1317

Open
SwastikTripathi wants to merge 1 commit into
swiftlang:mainfrom
SwastikTripathi:fix-range-operator-block-comment
Open

SwastikTripathi wants to merge 1 commit into
swiftlang:mainfrom
SwastikTripathi:fix-range-operator-block-comment

Conversation

@SwastikTripathi

Copy link
Copy Markdown

Fixes #1302.

When a range formation operator has a block comment in its effective leading trivia, e.g.

for i in 1 /* start */ ..< n {

the formatter removes the space between */ and ..<, producing:

for i in 1 /* start */..<n {

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 shouldRequireWhitespace check already handles the symmetric cases (postfix operator on the left, prefix operator on the right). This extends it to also return true when 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

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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Non-compiling code emitted when block comment is within range expr

1 participant