Skip to content

Ensure deep cloned AST nodes have synthetic positions #1174

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 12, 2025

Conversation

weswigham
Copy link
Member

Fixes #1092

Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR ensures that deep cloned AST nodes receive synthetic position ranges to avoid unintended propagation of source locations, fixing issue #1092. Key changes include:

  • Adding a new test case to catch declaration emit crashes when comments are copied.
  • Updating baseline outputs for various compiler submodules to reflect the new synthetic positions.
  • Modifying the deep clone implementation to explicitly set node locations as synthetic.

Reviewed Changes

Copilot reviewed 13 out of 13 changed files in this pull request and generated 1 comment.

File Description
testdata/tests/cases/compiler/declarationEmitNoCrashOnCommentCopiedFromOtherFile.ts Added test case for ensuring no crash on comment copying.
testdata/baselines/reference/** Updated baseline outputs reflecting synthetic node positions.
internal/ast/deepclone.go Modified deep clone logic to set synthetic locations on cloned nodes.

// In strada, `factory.cloneNode` was dynamic and did _not_ clone positions for any "special cases", meanwhile
// Node.Clone in corsa reliably uses `Update` calls for all nodes and so copies locations by default.
// Deep clones are done to copy a node across files, so here, we explicitly make the location range synthetic on all cloned nodes
copy.Loc = core.NewTextRange(-1, -1)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Honestly, looking at this made me realize there's probably a latent bug in strada, too. While strada's cloneNode does strip locations from Identifier nodes (so won't trigger a crash in this scenario), it does copy location information on anything that falls through to the reflective copy case.

Which seems wrong. I, at least, have always used cloneNode to copy a node to copy a node for another location/a new file, and didn't realize that non-identifier node kinds retained a real .pos and .end after a clone... (see the comment in setTextRange in the checker immortalizing my thinking!)


function f([, a, , b, , , , s, , , ] = results) {
->f : ([, a, , b, , , , s, , ,]?: string[]) => void
+>f : ([, a, , b, , , , s, , ]?: string[]) => void
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is odd, what's the deal?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, it's mainly just losing the trailing comma, then.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

trailing comma preservation is done by looking at positions

@weswigham weswigham enabled auto-merge June 12, 2025 21:30
@weswigham weswigham added this pull request to the merge queue Jun 12, 2025
Merged via the queue into microsoft:main with commit 8e7cc78 Jun 12, 2025
22 checks passed
@weswigham weswigham deleted the synthetic-clone-positions branch June 12, 2025 21:53
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.

Panic in SkipTriviaEx when printing a type predicate from another file with declaration maps enabled
2 participants