swiftlang / swiftlang/swift-syntax
Copying a node breaks its source location
- Dominant language
- Swift
- Stars
- 3.7k
- Forks
- 553
- Avg merge
- 5d 13h
- Merged PRs (30d)
- 16
Description
### Description
When building a macro, it is commonplace to take a syntax node from the original tree and copy it into a new position, e.g. by constructing a `TupleExprElementSyntax` around an existing `ExprSyntax` to make a function call argument. Doing so causes the copy of the `ExprSyntax` to lose its source location information. Subsequently, using `MacroExpansionContext.location(of:)` will fail, and emitting instances of `Diagnostic` with a `position` value equal to the copied `ExprSyntax`'s `position` (etc.) property will result in a source location for the diagnostic of ":0".
To be clear, I'm not saying I expect the constructed `TupleExprElementSyntax` to have valid source location information, but the copy of the `ExprSyntax` would need it. Without that information, we must actively maintain some sort of mapping back to the original `ExprSyntax` in order to correctly diagnose issues during macro expansion.
### Steps to Reproduce
_No response_
Contributor guide
Assessment
This issue has not been assessed yet.