Skip to content

compiler: key math spacing on control-symbol identity, not span length (#756) - #759

Merged
d-q222 merged 2 commits into
mainfrom
agent/daniel-muse-lead-2/thinspace-macro-span
Sep 17, 2026
Merged

d-q222 merged 2 commits into
mainfrom
agent/daniel-muse-lead-2/thinspace-macro-span

Conversation

@d-q222

@d-q222 d-q222 commented Sep 16, 2026

Copy link
Copy Markdown
Contributor

Fixes #756. `,`/`:`/`;`/`!` inside a macro body typeset a literal punctuation glyph instead of math spacing, unless the macro name happened to be 2 bytes long (`\e` worked by coincidence, `\dd`/`\ee` did not).

`math.rs` detected these spacing commands by testing `token.span.end - token.span.start == 2` — a proxy for "this token is a literal 2-byte `,` written directly in the source". Macro expansion rebinds a token's span to the invocation, so `\dd` (3 bytes) failed the check and fell through to being typeset as an ordinary comma.

Root cause, one level deeper than the byte-length symptom: the lexer's control-symbol arm (`\` + one non-letter character, covering `,` `%` `&` `$` etc.) produces the exact same `TokenKind::Word` variant as an ordinary literal character typed in math mode — so span length was the only signal distinguishing "backslash-escaped" from "literal", and that signal isn't stable across macro expansion.

Fix: a `control_symbol: bool` mark on `Token`, set only at the two places a backslash control symbol is actually lexed — the compiler lexer's control-symbol arm, and the expansion converter's matching single-character control-sequence arm (the parser consumes the converter's output, not the lexer's directly, so both needed the mark). Every derived-token site (word splitting, tabular cell splits, `&`-alignment splits, parameter substitution, incremental span shifting) preserves the mark from its source token; `math.rs` now keys spacing off the mark instead of span length.

A bool flag rather than a new `TokenKind` variant, deliberately: a new variant would silently change behavior at ~100 existing `TokenKind::Word` match sites (most use `matches!`/`if let` with catch-alls, so no compile error would flag them — e.g. every text-accumulation arm would silently drop `%`). The flag keeps every existing kind-match working identically; only `math.rs`'s spacing arm reads it, so the behavior change is confined to exactly the bug.

Tests: new `thinspace_macro_span.rs` (5 tests) — all three falsification cases from the issue (`\dd`, `\e`, `\ee`) lay out byte-identically to a direct `,`; a literal comma stays literal directly and via both 2- and 3-byte macro names (the mirror-image case that would have masked a regression); `%`/`&`/`$` still render correctly in both text and math, directly and via macros. `crates/compiler` full suite green, 0 failures — independently re-verified by the reviewer with a fresh `cargo test` run, plus a line-by-line review of every derived-token construction site given the wider blast radius (5 files touched).

Does not touch `vendor/`: the same bug exists in the frozen render-pipeline compiler mirror, but that copy is re-pinned only by the compiler-lead role. The render-pipeline route will not reflect this fix until a future re-pin (#711).

Implementation: Muse Code (`muse-spark-1.3-contributor`), lane `thinspace-macro-span`. Reviewed and published by daniel-muse-lead-2 (Claude Sonnet supervisor).

🤖 Generated with Claude Code

#756)

`\,`/`\:`/`\;`/`\!` inside a macro body typeset a literal punctuation
glyph instead of math spacing, unless the macro name happened to be
2 bytes long (`\e` worked by coincidence, `\dd`/`\ee` did not).

math.rs detected these spacing commands by testing
`token.span.end - token.span.start == 2` -- a proxy for "this token is
a literal 2-byte `\,` written directly in the source". Macro expansion
rebinds a token's span to the invocation, so `\dd` (3 bytes) failed
the check and fell through to being typeset as an ordinary comma.

Root cause traced one level deeper than the byte-length symptom: the
lexer's control-symbol arm (`\` + one non-letter character, covering
`\,` `\%` `\&` `\$` etc.) produces the exact same `TokenKind::Word`
variant as an ordinary literal character typed in math mode, so span
length was the ONLY signal distinguishing "backslash-escaped" from
"literal" -- and that signal is not stable across macro expansion.

Fix: a `control_symbol: bool` mark on `Token`, set only at the two
places a backslash control symbol is actually lexed (the compiler
lexer's control-symbol arm, and the expansion converter's matching
single-character control-sequence arm -- the parser consumes the
converter's output, not the lexer's, so both needed the mark). Every
derived-token site (word splitting, tabular cell splits, `&`-alignment
splits, parameter substitution, incremental span shifting) preserves
the mark from its source token; math.rs now keys spacing off the mark
instead of span length.

A bool flag rather than a new TokenKind variant, deliberately: a new
variant would silently change behavior at ~100 existing
TokenKind::Word match sites (most use matches!/if-let with
catch-alls, so no compile error would flag them -- e.g. every
text-accumulation arm would silently drop `\%`). The flag keeps every
existing kind-match working identically; only math.rs's spacing arm
reads it, so the behavior change is confined to exactly the bug.

Tests: new thinspace_macro_span.rs (5 tests) -- all three
falsification cases from the issue (`\dd`, `\e`, `\ee`) lay out
byte-identically to a direct `\,`; a literal comma stays literal
directly and via both 2- and 3-byte macro names (the mirror-image
case that would have masked a regression); `\%`/`\&`/`\$` still
render correctly in both text and math, directly and via macros.
crates/compiler full suite green, 0 failures. Independently
re-verified by the reviewer with a fresh cargo test run, plus a
line-by-line review of every derived-token construction site given
the wider blast radius (5 files touched).

Does not touch vendor/: the same bug exists in the frozen
render-pipeline compiler mirror, but that copy is re-pinned only by
the compiler-lead role. The render-pipeline route will not reflect
this fix until a future re-pin (#711).

Implementation-Agent: muse-spark-1.3-contributor (Muse Code, lane thinspace-macro-span, slice 1)
Commit-Executor: daniel-muse-lead-2 (Claude Sonnet)
Reviewed-by: daniel-muse-lead-2 (Claude Sonnet)
Muse-Lane-Head: 310139300864888c32fb2e8227985c5ba243470a
Co-authored-by: d-q222 <[email protected]>
…length (#756 follow-up)

take_delimiter decided \left\| (double bar) vs \left| (single bar)
by span byte length, the same proxy issue #756 fixed for math
spacing: macro expansion rebinds spans to the invocation, so a macro
wrapping \left\| lost the double bar and a coincidentally-short
macro wrapping \left| spuriously gained it. Key off
token.control_symbol instead, the identity mark already preserved
through expansion and split_word_tokens.

Flagged by daniel-parent's review of #756 as the same bug class in
the same function family.

Tests: unit-level regression (parse_tests) covering literal
delimiters plus macro-shaped spans in both directions, plus an
independent end-to-end verification by the reviewer with real
\newcommand compiles (\dblbar 7-byte name -> still double bar;
\bb 3-byte name, the exact coincidence-length case from the
original bug -> still single bar; zero diagnostics in all cases).
crates/compiler full suite green, 0 failures.

Implementation-Agent: muse-spark-1.3-contributor (Muse Code, lane leftvert-macro-span, slice 1)
Commit-Executor: daniel-muse-lead-2 (Claude Sonnet)
Reviewed-by: daniel-muse-lead-2 (Claude Sonnet)
Muse-Lane-Head: 9c4a912ce790e65dfe3501c350e72bbc74459b3d
Co-authored-by: d-q222 <[email protected]>
@d-q222
d-q222 merged commit 2f19385 into main Sep 17, 2026
21 of 22 checks passed
d-q222 added a commit that referenced this pull request Sep 17, 2026
 #784

All eight independently reviewed APPROVE. Batch verification: compiler
935/0, render-pipeline 487/0, perf-bench 14/0, perf gate OK (270 metrics,
no regression), swift build --build-tests OK, CompletionTests 29/0.

Commit-Executor: daniel-parent
Claude-Session: https://claude.ai/code/session_012c9XLkHjePPGBuarrmE2mz
ItsAkilesh pushed a commit to ItsAkilesh/flashtex that referenced this pull request Sep 18, 2026
…ain 8a8a22b, vendor bibliography

compiler c95977d -> 8a8a22b (51 compiler commits, incl. flash-tex#784 run-in
heading flush, flash-tex#756/flash-tex#759 math spacing in macro bodies); tex-expansion
87a513d -> 8a8a22b in lockstep; new vendor/bibliography (the compiler
now depends on it). Copied with git archive; no local patches existed
(old vendor/compiler differed from c95977d only in PIN).

Implementation-Agent: claude-opus-5 subagent of daniel-parent
Commit-Executor: daniel-parent
Co-authored-by: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_012c9XLkHjePPGBuarrmE2mz
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.

compiler: \, inside a macro body emits a literal comma — math spacing commands are detected by source-span byte length

1 participant