Fix comment loss and misplacement in match arms, nested records, and raw strings#203
Merged
Merged
Conversation
- match-arm guards no longer hoist or drop neighboring comments - nested records stay multiline instead of collapsing and eating field comments - extract_comments skips r#'...'# raw strings (were eaten, non-idempotent)
Contributor
|
Thanks |
xav-ie
added a commit
to xav-ie/dots
that referenced
this pull request
Jul 8, 2026
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.
Description of changes
Fixes three formatter bugs that silently lose or misplace comments, several of which also make formatting non-idempotent (running the formatter repeatedly keeps changing the file). Each has a ground-truth + idempotency fixture.
1. Parenthesized
match-arm guards vacuum nearby comments into(A match arm with a parenthesized guard (
pat if (EXPR) => body) pulled surrounding standalone comments inside the guard's(:The guard is rendered in a
probe_formatsub-formatter whose comment cursor was reset to the start of file, so the parenthesized sub-expression re-flushed every not-yet-written comment. Fix: seed each arm's probe with the pattern's own start position, and emit arm-preceding comments at the arm boundary informat_match_block(a bare guardif $y == 'z'previously dropped the same comment entirely).2. Nested records collapse inline and drop field comments
The record layout decision only preserved source newlines at the top level and never checked for comments, so a short nested record took the inline path — which emits fields without
write_comments_before. Fix: force multiline when the record span contains comments, using the samehas_comments_in_spanguardformat_listalready applies.3.
r#'…'#raw strings derail comment extractionA raw string with hash delimiters containing
#causedextract_commentsto mis-lex: the#inr#'started a bogus comment and the closing'#opened a phantom string that swallowed real comments until the next stray apostrophe — deleting ~1 comment per pass (non-idempotent). Fix: teachextract_commentsto skipr#'…'#(and multi-hash variants) and consume each quoted string inline so an unterminated string can't bleed across the file; single-quoted strings are treated as raw (no backslash escapes).Testing
cargo test— full suite green (273 tests, incl. 7 new fixtures). All three cases verified idempotent.Relevant Issues
None found open; happy to file tracking issues if preferred.