-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Migrate ASTScope to CharSourceRanges #34207
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
Migrate ASTScope to CharSourceRanges #34207
Conversation
@swift-ci Please smoke test |
@swift-ci Please test source compatibility |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can't go through this in detail right now, but it's nice to simplify things, and if it works, it's fine with me! Nothing jumped out at me to suggest.
@swift-ci Please test source compatibility |
@swift-ci Please smoke test macOS |
a3bdf0f
to
e59069f
Compare
@swift-ci Please smoke test |
@swift-ci Please test source compatibility |
swiftlang/llvm-project#1916 |
@swift-ci Please test source compatibility |
SourceRanges behave funny with string interpolation. A SourceRange always spans at least one token, and the end location points at the beginning of the last token that is part of the source range.
Since string literals and string interpolations are always a single token, a SourceRange that ends on a string interpolation will point at the beginning of the string interpolation. However, any source locations of expressions inside the interpolation itself now point into the middle of the string interpolation token. Eg, consider this code:
The source range of the initializer expression for
var x
consists of a single token, so it's start and end location both point at the beginning of the token"\(hello)"
. However, the expressionhello
has a source range consisting of the single tokenhello
. Notice thathello
comes after"\(hello)" in the source file, despite being logically contained within the source range of
"(hello)"`.Since ASTScope lookup relies on SourceRanges of parent nodes containing the SourceRanges of their parents, adding a child scope would sometimes have to "widen" the parent's source range. This was error-prone and complex.
Instead, we can use CharSourceRanges. Since the end location of a CharSourceRange is a byte offset of the end of the last token of the CharSourceRange, we the required invariant around nested scopes for free, without having to "widen" source ranges.
This PR adds stronger assertions for detecting children which are not contained in their parents, or overlapping sibling children.
Builds upon #34205, which fixes all the non-string-interpolation cases where source range "widening" was required.
Fixes https://bugs.swift.org/browse/SR-12997, https://bugs.swift.org/browse/SR-12999, rdar://problem/64314753, rdar://problem/64316115.