Repository navigation
fix(format): read {#! and {#+ as the lexer does in the line-ending pass - #2892
Merged
Merged
Conversation
…g pass
The lexer reads `{#` as one `LBraceHash` token, but the formatter's
line-ending state machine (`advance_source_state`) saw the `#!` / `#+`
after the `{` and opened a comment, so a `"` later on the line was taken
for comment text and a string's own line endings could be rewritten on a
CRLF round trip. A `#` right after a `{` that starts a token now opens
nothing. The lexer pairs `{{` greedily from the left, so that is an odd
run of `{` in code; `next_lbrace_odd` tracks it, one definition shared by
the four walkers.
The drift guard (`source_state_classification_agrees_with_lexer`) is now
a helper run on the formatter fixtures, on edge inputs that are not
fixtures (`SOURCE_STATE_EXTRA`: the issue's two inputs, `{#+`, `{{#!`,
`{{{#!`, a `%` comment with a `"`), and on every string of up to five
characters over the twelve characters the two classifiers treat
specially (271,452 inputs, 4 s in a debug build). Both new checks fail
without the fix. The `%` half of the issue was already done: the
classifier maps `PercentComment`, and the `percent_flag_and_comment`
fixture makes it trip.
Closes #2780
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Contributor
📊 Benchmark Results
vs base, measured in this run
A difference counts only when it clears the combined spread of the two measurements, per shape (2σ: What do these numbers mean?
|
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
Contributor
✅ Compatibility Test Results
What's tested
Full results available in workflow artifacts |
…ce-hash # Conflicts: # CHANGELOG.md
…puts too Review round 1 on #2780: three em-dashes in comment text re-indented into `assert_source_state_agrees_with_lexer` are replaced with plain punctuation. The generated-input check now includes `\r` in its alphabet and skips only an input with a mid-line `\r` (more of its line after it), the documented difference; a `\r` before its line ending is checked. Co-Authored-By: Claude Opus 5.5 <[email protected]>
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.
What changed
Issue #2780 has two parts. One was already done on main; this PR fixes the other and widens the drift guard.
%gap in the drift guard was already closed on main. The lexer-side classifier insource_state_classification_agrees_with_lexermapsToken::PercentCommenttoInComment.IDEMPOTENCE_MATRIX'spercent_flag_and_commentfixture has a column-0% c "qline, and I checked that the guard can trip: withPercentCommentremoved from the classifier, it fails on that fixture.{#!and{#+are fixed here. The lexer reads{#as oneLBraceHashtoken. The formatter's line-ending state machine (advance_source_state) instead saw the#!/#+after the{and opened a comment. A"later on the line was then taken for comment text, so a string's own line endings could be rewritten on a CRLF round trip.#right after a{that starts a token opens nothing.{{greedily from the left ({{is one token, and{{{#is{{then{#), so the condition is an odd run of{in code.next_lbrace_oddtracks that run. It is one definition, used by all four walkers:crlf_to_lf_outside_strings,lf_to_crlf_outside_strings,cr_outside_strings_present, and the test classifier.assert_source_state_agrees_with_lexer, which runs on:IDEMPOTENCE_MATRIXfixture, as before;SOURCE_STATE_EXTRA, edge inputs that are not formatter fixtures: the issue's"\n{#! \n"and"{#!#\"2\u{feff}\n%\"", plus{#+,{{#!,{{{#!, and a%comment with a";\r { # ! + % ; " \n space a * \), about 2 s in a debug build.\r(more of its line after it) are skipped. Such a\rcloses a comment in the state machine but stays in the comment for the lexer, which is a documented difference. A\rbefore its line ending is checked.Why
Issue #2780.
How to test
Verification
{#rule disabled, both the extra-inputs check and the generated-input check fail (the first disagreements are atlbrace_hash_bangbyte 4 and generated{#!byte 3).rledger format --no-ledgeroutput is byte-identical between origin/main and this branch on all 1,113 in-tree and compat ledgers, and so are the exit codes. No corpus file contains{#!.cargo test --workspace --all-features --no-fail-fast,STRICT_BASELINE=1corpus and CST baselines,cargo clippy --all-features --all-targets -- -D warnings,RUSTDOCFLAGS=-Dwarnings cargo doc --workspace --no-depsandcargo fmt --checkall pass.Review round 1
assert_source_state_agrees_with_lexerare replaced with plain punctuation.\rin its alphabet and skips only inputs with a mid-line\r.-D warningsandcargo fmt --checkpass.Closes #2780
🤖 Generated with Claude Code