Fix crash when a switch case's last statement is formatter-ignored#1222
Open
nenadvulic wants to merge 1 commit into
Open
Fix crash when a switch case's last statement is formatter-ignored#1222nenadvulic wants to merge 1 commit into
nenadvulic wants to merge 1 commit into
Conversation
The SwitchCaseSyntax visitor emits an open group after the case label and attaches the matching close as an 'after' token on the case's last token. When the last statement is formatter-ignored it is emitted verbatim and its tokens are never visited, so the close was dropped, leaving the group open and tripping the 'Too many unresolved delimiter token lengths' assertion in PrettyPrint. Attach the closing tokens before the next token in that case, mirroring the empty-case handling. Fixes swiftlang#309.
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.
Summary
Fixes #309.
Linting or formatting a file with
// swift-format-ignoreon the last statement of aswitchcase crashes:Root cause
visit(SwitchCaseSyntax)emits an.opengroup after the case label and attaches the matching.closeas anaftertoken on the case's last token. When the last statement is formatter-ignored it is emitted verbatim and its tokens are never visited (the node is skipped), so theafterclose tokens are dropped and the group is never closed.This is the same hazard that
visit(CodeBlockItemListSyntax)already documents and avoids ("the tokens afteritem.lastTokenwould be ignored and leave unclosed open tokens").Fix
When the case's last statement is formatter-ignored, attach the closing tokens before the next token (the next case or the closing brace) instead of after the case's last token — mirroring the existing handling for empty cases.
Tests
Added
testIgnoreStatementInSwitchCaseandtestIgnoreLastStatementInSwitchCasetoIgnoreNodeTests. Both crash without the fix and pass with it. Full test suite passes (934 tests, 0 failures).