Skip to content

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

Merged
merged 6 commits into from
Oct 8, 2020

Conversation

slavapestov
Copy link
Contributor

@slavapestov slavapestov commented Oct 6, 2020

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:

var x = "\(hello)"

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 expression hello has a source range consisting of the single token hello. Notice that hello 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.

@slavapestov slavapestov requested a review from davidungar October 6, 2020 20:48
@slavapestov
Copy link
Contributor Author

@swift-ci Please smoke test

@slavapestov
Copy link
Contributor Author

@swift-ci Please test source compatibility

Copy link
Contributor

@davidungar davidungar left a 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.

@slavapestov
Copy link
Contributor Author

@swift-ci Please test source compatibility

@slavapestov
Copy link
Contributor Author

@swift-ci Please smoke test macOS

@slavapestov slavapestov force-pushed the astscope-char-source-ranges branch from a3bdf0f to e59069f Compare October 7, 2020 19:25
@slavapestov
Copy link
Contributor Author

@swift-ci Please smoke test

@slavapestov
Copy link
Contributor Author

@swift-ci Please test source compatibility

@slavapestov
Copy link
Contributor Author

swiftlang/llvm-project#1916
@swift-ci Please smoke test

@slavapestov
Copy link
Contributor Author

@swift-ci Please test source compatibility

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.

2 participants