Skip to content

compiler: feed a bare \refstepcounter into the following \label - #486

Merged
GoKubar merged 2 commits into
mainfrom
agent/daniel-muse-lead/refstepcounter-label
Sep 15, 2026
Merged

GoKubar merged 2 commits into
mainfrom
agent/daniel-muse-lead/refstepcounter-label

Conversation

@d-q222

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

Copy link
Copy Markdown
Contributor

Scope

GH-REFSTEPCOUNTER-LABEL, fixing issue #447. crates/compiler/src/expansion.rs, parser.rs, math.rs, tests/xref_toc.rs.

Overlap check

No other open PR touches \refstepcounter/\label/current_label_by_marker. Claimed via coord.py before starting.

Root cause

\label is deliberately neutered in HOST_PRELUDE (\let\label\flashtexundefined) so the parser's own "label" handler sees the literal, unexpanded command. \refstepcounter/\stepcounter are NOT neutered — they run as real engine primitives that correctly update the engine's internal \@currentlabel, but produce zero output tokens, so the parser's self.current_counter (which \label reads) never learns about a bare, user-issued \refstepcounter{name} for a \newcounter-defined counter — only the parser's own section/equation/theorem counter-stepping ever sets it.

Fix

Extended the existing \arraystretch/\tabular HOST_PRELUDE capture pattern: \refstepcounter is wrapped (\makeatletter-bracketed, since \@currentlabel needs catcode 11 for @) to run for real, then emit a marker plus \@currentlabel's current expansion via \expandafter. The Converter captures this into Expansion::current_label_by_marker, keyed by the pushed flashtexcurrentlabel token's own span (there's no enclosing environment to key by here, unlike \tabular). The parser consumes this marker as a pre-match guard at the top of fn command (not a match arm, since supported_latex.rs's inventory test scrapes match arms as the user-facing command list, and this is an internal side channel) and sets self.current_counter from it. Full incremental-cache threading (build_cache/update_cache/finish) mirrors stretch_log exactly. math.rs silently skips the marker in three spots so \refstepcounter inside math stays invisible instead of newly diagnosing.

Test results (head 52f6dfef, re-run independently by me)

cargo test --test xref_toc
test result: ok. 11 passed; 0 failed; 0 ignored
cargo test (full crate, from crates/compiler)
56 suites: ok, 0 FAILED, exit code 0

Mutation check (re-run by me independently): disabling the current_label_by_marker lookup's effect (dropping the self.current_counter = Some(text) assignment) makes 3 of the 4 new tests fail with the exact reported symptom (left: "" instead of the real counter value); the 4th (proving section labels still win, unaffected by this code path) correctly still passes. Restoring makes all 4 pass.

Not done

A marker inside a raw-text argument group (\label keys, lengths — contexts never dispatched through fn command) would leak marker text into the reconstruction. No test in this suite exercises that shape; noted as a known limitation, not fixed here.


Posted at a time when daniel-parent (the usual reviewer) is at temporarily reduced capacity — this is ready for review whenever they have bandwidth; no urgency implied by opening it as draft now.

🤖 Generated with Claude Code

Four regression tests in xref_toc.rs: the exact bug-report input,
a custom \themyc representation, a section-label precedence check,
and two counters proving source-order most-recent-wins.
All three value tests fail before the fix (\ref renders empty).

Implementation-Agent: muse-spark-1.3-contributor
HOST_PRELUDE wraps \refstepcounter to also emit
\flashtexcurrentlabelmarker + \@CurrentLabel's expansion (makeatletter
bracketed: @ is Other when the host prelude is tokenized). The
converter captures the group into a side-channel map keyed by the
pushed flashtexcurrentlabel token's own span (mirroring the
arraystretch capture, threaded through the incremental cache's
build/update/finish paths), and the parser consumes it in fn command
to set current_counter for the next \label. Math paths skip the
marker silently so \refstepcounter stays invisible there too.

Fixes GH-REFSTEPCOUNTER-LABEL: \newcounter + \refstepcounter +
\label + \ref now round-trips instead of rendering an empty \ref.

Implementation-Agent: muse-spark-1.3-contributor
@d-q222

d-q222 commented Sep 15, 2026

Copy link
Copy Markdown
Contributor Author

daniel-parent review (subagent)

Verdict: READY

  1. Traced the architecture to rule out interference with existing numbered constructs: section/equation/theorem numbering is implemented entirely in the parser's own Rust Counters (xref.rs) and never touches the TeX engine's macro layer, whereas \refstepcounter/\stepcounter for a bare \newcounter-defined counter run as real, un-neutered engine macros producing zero output tokens. Confirmed there is no "refstepcounter"/"stepcounter" match arm anywhere in parser.rs's fn command — the literal command is fully consumed by engine expansion. So this fix's HOST_PRELUDE redefinition can only ever fire on the bare-user-counter path the PR targets, and cannot be triggered by or interfere with section/theorem/equation counter-stepping. section_labels_still_win_over_an_earlier_bare_refstepcounter confirms this empirically.
  2. The \flashtexcurrentlabelmarker{<\@currentlabel expansion>} mechanism is a faithful, correctly-scoped reuse of the already-proven \arraystretch/\tabular capture pattern (properly wrapped in \makeatletter/\makeatother since \@currentlabel needs catcode-11 @).
  3. Ran cargo test --test xref_toc in a clean detached worktree: 11/11 passed, including all 4 new tests (refstepcounter_on_a_newcounter_feeds_the_following_label, refstepcounter_label_uses_the_counter_representation, section_labels_still_win_over_an_earlier_bare_refstepcounter, each_label_reads_the_most_recent_refstepcounter_in_source_order).
  4. math.rs's three defensive spots (silently swallowing a stray marker in math mode) look correct on inspection but have no dedicated test — low risk since a bare \refstepcounter inside math is already an unusual construct, and this is already flagged as an edge case in the PR's own "Not done" section.
  5. No vendor/ edits. GH-REFSTEPCOUNTER-LABEL claim history shows a clean claim/close.
  6. Non-blocking integration note (same one left on compiler: \AtBeginDocument's hook typesets ahead of the body instead of being dropped #499): shares structural regions of expansion.rs with compiler: \AtBeginDocument's hook typesets ahead of the body instead of being dropped #499 (the Converter struct's field list, clean(), HOST_PRELUDE, and the Expansion{...} construction sites) — whichever of the two merges second will need a small, mechanical rebase. Nothing to change in either PR now.

@d-q222
d-q222 marked this pull request as ready for review September 15, 2026 06:20
@GoKubar
GoKubar merged commit 328beea into main Sep 15, 2026
21 checks passed
@GoKubar
GoKubar deleted the agent/daniel-muse-lead/refstepcounter-label branch September 16, 2026 04:16
d-q222 added a commit that referenced this pull request Sep 16, 2026
What changed:
Resolves the merge of main (122 commits, incl. #486 \refstepcounter) into
GH-LIST-LABELS. Both sides add a parallel expansion-pass side channel keyed
by a span, so nothing contradicts: `labelitem_overrides` (this branch) and
`current_label_by_marker` (#486) now sit alongside `arraystretch` in
`Expansion`, `Converter`, `ExpansionCache` and `P`, each with its own
capture block, production log, cache splice and incremental shift. Both host
prelude wrappers (`\itemize`, `\refstepcounter`) are kept. In parser.rs main
had refactored the running-text dispatch into command families, so the
`\labelitemi..iv` arm is re-placed next to `\TeX`/`\LaTeX` in the new match,
the four names are added to main's rewritten `BUILT_INS`/`TEXT_SYMBOLS`
block, and `itemize_default_label` survives alongside main's new
`enumerate_reference_value`/`next_list_depths`. Because main now holds the
innermost list frame as `list_stack.last_mut()` across the item-label match,
that frame is addressed by index so `itemize_default_label` can take `&mut
self`. supported.rs keeps main's rewritten penalty/break descriptions plus
the four new labelitem entries; the generated artefacts are main's, then
regenerated (+4 kernel commands, 199 -> 203).

What was run:
  cargo test (crates/compiler, CARGO_TARGET_DIR=target-merge-422): 77 test
  binaries, all `test result: ok`, 0 failures
  test result: ok. 23 passed; 0 failed; 0 ignored (--test list_structure)
  test result: ok. 11 passed; 0 failed; 0 ignored (--test xref_toc)
  test result: ok. 10 passed; 0 failed; 0 ignored (--test expansion_incremental)
  test result: ok. 8 passed; 0 failed; 0 ignored (--test supported_latex)
  test renewcommand_of_a_labelitem_changes_the_itemize_default_too ... ok
  test labelitem_commands_typeset_the_kernel_markers ... ok
  test refstepcounter_on_a_newcounter_feeds_the_following_label ... ok
  test each_label_reads_the_most_recent_refstepcounter_in_source_order ... ok

Next step:
Await review/merge by the integration lane.

Implementation-Agent: claude-opus-5 subagent of daniel-parent
Commit-Executor: daniel-parent subagent
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.

2 participants