fix: replace panic with fallback in colored printer token handling #9449
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
Replaces
panic!inset_colorfunction with a graceful fallback that returns uncolored text for unhandled token kinds. This prevents crashes when encountering tokens likeTokenAt,TokenDollar,TokenPub, or newly added tokens that aren't explicitly handled in the match expression.Type of change
Please check one:
Why is this change needed?
The
set_colorfunction incolored_printer.rsusespanic!for unhandledSyntaxKindvariants. Several existing tokens (e.g.,TokenAt,TokenDollar,TokenPub,TokenMacro,TokenContinue,TokenBreak, assignment operators likeTokenDivEq,TokenMinusEq, etc.) are not explicitly handled, which causes the program to crash when these tokens are encountered during colored printing. Additionally, any future token additions to the language would trigger the same panic.This is particularly problematic because
print_coloredis used in test utilities, and crashes prevent proper debugging output.What was the behavior or documentation before?
When
set_colorencountered an unhandled token kind (either existing unhandled tokens or newly added ones), it would callpanic!("Unexpected syntax kind: {kind:?}"), causing the entire program to crash. The TODO comment on line 126 acknowledged this limitation but didn't provide a solution.What is the behavior or documentation after?
Unhandled token kinds now return
text.clear()(uncolored text) instead of panicking. This allows the colored printer to continue functioning even when encountering tokens that aren't explicitly colorized, making the code resilient to new token additions without requiring immediate updates to the color mapping.Related issue or discussion (if any)
Addresses the TODO comment on line 126:
// TODO(yuval): Can this be made exhaustive?Additional context
The change is minimal and maintains backward compatibility. All existing handled tokens continue to work as before. The fallback behavior (uncolored text) is consistent with how other "neutral" tokens like
TokenEndOfFile,TokenMissing, andTokenEmptyare handled in the same function.All tests pass (69 tests), and the code compiles without warnings.