Skip to content

Conversation

@slavapestov
Copy link
Contributor

@slavapestov slavapestov commented Oct 6, 2020

Today, if the source range of a child scope extends beyond the bounds of the parent, we automatically extend the source range of the parent. This PR fixes the handful of scope implementations which relied on this behavior to compute their complete source range up-front.

Also, in a few places in the parser, we recover from a missing BraceStmt by creating a new, empty BraceStmt whose source location is where the brace was expected to be seen. This can overlap with a scope created for the following statement, though. To avoid violating invariants, we now skip creating a BraceStmtScope entirely if the corresponding BraceStmt is empty. Not only is this a small optimization, it also avoids creating the scope in the invalid case.

@slavapestov slavapestov force-pushed the astscope-source-range-fixes branch from 0397f93 to 9cf355e Compare October 6, 2020 20:38
@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.

Can't do a detailed review today, but based on our offline conversation, the idea is sound. Let me know if you want me to go back and closely scrutinize it tomorrow.

@slavapestov
Copy link
Contributor Author

@swift-ci Please test source compatibility

@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

@swift-ci Please smoke test Linux

The source range needs to contain the argument expression.
The source range needs to contain any attributes on the subscript
declaration, since those might create child scopes (@_specialize,
property wrappers, etc).
The top-level scope for a conditional clause with a pattern is now
ConditionalClausePatternUseScope, which introduces the pattern's
bindings.

Since the patterns are not visible in their own initializer, a new
ConditionalClauseInitializerScope is used for the initializer.
While it is nested inside of the ConditionalClausePatternUseScope,
it's lookup parent skips one level, giving us the desired behavior.
Top-level code can contain guard statements which introduce
bindings until the end of the parent scope, so plumb that
through.
A SourceFile might contain TopLevelCodeDecls with guard statements,
which introduce names until the end of the file, so plumb that
through.
@slavapestov slavapestov force-pushed the astscope-source-range-fixes branch from 9cf355e to b8cccb1 Compare October 7, 2020 16:35
@slavapestov
Copy link
Contributor Author

@swift-ci Please test source compatibility

@slavapestov
Copy link
Contributor Author

@swift-ci Please smoke test

@slavapestov slavapestov merged commit 112e45d into swiftlang:main Oct 7, 2020
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