fix(beam): make Emit $N substitution a single left-to-right pass#4631
Merged
Conversation
The Erlang printer substituted Emit placeholders with naive sequential
String.Replace($"$%d{i}", ...), which had two correctness bugs:
- `$1` corrupted `$10` (textual prefix match): `[$0,$1,$10]` produced
`[0,99,990]` instead of `[0,99,1010]`.
- A `$N` sequence inside an already-substituted argument was re-substituted:
`pair "$1" 7` produced `{<<"7">>, 7}` instead of `{<<"$1">>, 7}`.
Replace it with a single left-to-right scan that parses the full integer
index and never re-scans substituted text, matching the JS printer's
single-pass behaviour. Beam-only, printer-only.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
86a2da1 to
825a83e
Compare
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.
Problem
The Erlang/BEAM printer substituted
[<Emit>]placeholders with naive sequentialString.Replace($"$%d{i}", ...). Iterating one placeholder at a time over the mutated string has two correctness bugs:$1corrupts$10(textual prefix match). Replacing$1also matches the$1prefix of$10:$Nsequence inside an already-substituted argument gets re-substituted:Fix
Replace the loop with a single
Regex.Replaceover\$\d+— the same approach the JS printer (BabelPrinter.fs) already uses:This fixes both bugs by construction:
\d+matches the full integer index (so$10isn't clipped by$1), andRegex.Replaceevaluates matches against the original template only, so a$Nappearing inside a substituted argument is never re-scanned.Scope is limited to the Erlang printer (
ErlangPrinter.fs); no other backend is touched.Tests
Added two regression tests in
tests/Beam/InteropTests.fscovering both bugs. Full BEAM suite passes (2444 passed, 0 failed).🤖 Generated with Claude Code