[rust-compiler] Fix off-by-one in SWC source-location conversion#36501
Closed
poteto wants to merge 1 commit into
Closed
[rust-compiler] Fix off-by-one in SWC source-location conversion#36501poteto wants to merge 1 commit into
poteto wants to merge 1 commit into
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)
Collaborator
Author
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
position()in the SWCconvert_astwas binary-searching SWC's 1-basedBytePosagainst the 0-basedline_offsetstable, producingloc.{start,end}.{line,column,index}values that were off by 1 in events. When the referenced byte happened to fall just before a\n, the result flipped to "next line, column 0", e.g. TS reportsline: 3, column: 18and SWC reportedline: 4, column: 0for the same source position.Fix
Shift the offset down by 1 (saturating, to keep DUMMY spans at 0) before the line lookup.
BaseNode.start/endkeeps the SWC-native 1-based value becauseconvert_scopekeys itsnode_to_scopemap onspan.lo.0and the HIR builder looks it up viabase.start; changing those would require updating bothconvert_scopeand the reverse converter together. Doing the focused fix onlocclears the bulk of the divergence with minimal blast radius.Also drop the e2e harness's
oneBasedColumns = variant === 'swc'workaround that was attempting to compensate post-hoc by subtracting 1 fromcolumn/indexbut couldn't handle the cases where the off-by-one had pushed the position onto a different line.Test plan
swcbabeloxccargo test --workspace: 56 passed, 0 failed (unchanged)