[rust-compiler] Fix off-by-one in SWC source-location conversion#36502
Merged
Conversation
SWC's BytePos is 1-based (BytePos(0) is the DUMMY/synthetic sentinel)
while the Babel AST that the rest of the compiler consumes uses
0-based offsets in loc.{start,end}.{line,column,index}. `position()`
in the SWC convert_ast was binary-searching the 1-based offset
against the 0-based line_offsets table, producing line/column/index
values that were off by 1, and that flipped to "next line, column 0"
when the referenced byte happened to be the byte right before a `\n`.
Shift the offset down by 1 before the line lookup. BaseNode.start/end
keeps the SWC-native 1-based value because the SWC scope collector
keys its node_to_scope map on span.lo.0 and the HIR builder looks it
up via base.start.
Also drop the e2e harness's `oneBasedColumns = variant === 'swc'`
workaround that was masking part of this divergence.
Test plan:
- bash compiler/scripts/test-e2e.sh --variant swc:
Before: Total 1682/1795 (113 failures)
After: Total 1742/1795 (53 failures, 60 fixed)
- bash compiler/scripts/test-e2e.sh --variant babel: 1788/1795 (unchanged)
- bash compiler/scripts/test-e2e.sh --variant oxc: 1702/1795 (unchanged)
- cargo test --workspace: 56 passed, 0 failed (unchanged)
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.
SWC's BytePos is 1-based (BytePos(0) is the DUMMY/synthetic sentinel)
while the Babel AST that the rest of the compiler consumes uses
0-based offsets in loc.{start,end}.{line,column,index}.
position()in the SWC convert_ast was binary-searching the 1-based offset
against the 0-based line_offsets table, producing line/column/index
values that were off by 1, and that flipped to "next line, column 0"
when the referenced byte happened to be the byte right before a
\n.Shift the offset down by 1 before the line lookup. BaseNode.start/end
keeps the SWC-native 1-based value because the SWC scope collector
keys its node_to_scope map on span.lo.0 and the HIR builder looks it
up via base.start.
Also drop the e2e harness's
oneBasedColumns = variant === 'swc'workaround that was masking part of this divergence.
Test plan:
Before: Total 1682/1795 (113 failures)
After: Total 1742/1795 (53 failures, 60 fixed)