-
Notifications
You must be signed in to change notification settings - Fork 10.6k
[SE-0491] Parse and ASTGen module selectors #84362
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
Conversation
|
@swift-ci smoke test |
| @inline(__always) | ||
| func generateSourceLoc(_ node: some SyntaxProtocol) -> SourceLoc { | ||
| SourceLoc(at: node.positionAfterSkippingLeadingTrivia, in: self.base) | ||
| precondition(node.positionAfterSkippingLeadingTrivia.utf8Offset < 0x0000000100000000) |
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.
What is this for?
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 think this was something I added to chase down a bug caused by missing tokens having very large, invalid offsets that turned into out-of-bounds SourceLocs. It probably doesn't belong in this PR; the constant here is a fairly arbitrary "big enough" value.
| arguments: ( | ||
| leftParen: TokenSyntax, | ||
| labels: some Collection<TokenSyntax>, | ||
| rightParen: TokenSyntax | ||
| )? = Optional<(leftParen: TokenSyntax, labels: [TokenSyntax], rightParen: TokenSyntax)>.none |
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.
Can't this be just DeclNameArgumentsSyntax ?
| arguments: ( | |
| leftParen: TokenSyntax, | |
| labels: some Collection<TokenSyntax>, | |
| rightParen: TokenSyntax | |
| )? = Optional<(leftParen: TokenSyntax, labels: [TokenSyntax], rightParen: TokenSyntax)>.none | |
| arguments: DeclNameArgumentsSyntax? = nil |
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.
Looks like it can be. Good catch!
lib/Parse/ParseDecl.cpp
Outdated
| diagnose(Tok, diag::replace_module_selector_with_member_lookup) | ||
| .fixItReplace(Tok.getLoc(), "."); | ||
| } | ||
| HasNext = consumeIf(tok::period) || consumeIf(tok::colon_colon); |
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.
Could you align the implementation with SwiftParser?
lib/Parse/ParseExpr.cpp
Outdated
| DeclNameRef nameRef = | ||
| parseDeclNameRef(nameLoc, diag::impossible_parse, | ||
| DeclNameFlag::AllowAnonymousParamNames | | ||
| DeclNameFlag::ModuleSelectorUnsupported); |
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.
Is this needed? I believe this is only called when Tok is tok::dollarident so there's no chance to have a module selector here.
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.
Yeah, I could roll this back. It was more important back when parseDeclNameRef() always parsed a module selector and just diagnosed it when you passed ModuleSelectorUnsupported.
lib/Parse/ParseExpr.cpp
Outdated
| if (P.Tok.is(tok::identifier) && P.peekToken().is(tok::colon_colon)) { | ||
| return P.lookahead(2, [&](auto &scope) { |
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.
How about making isAtModuleSelector() returning number of module selector tokens and
| if (P.Tok.is(tok::identifier) && P.peekToken().is(tok::colon_colon)) { | |
| return P.lookahead(2, [&](auto &scope) { | |
| if (auto numTokens = P.isAtModuleSelector()) { | |
| return P.lookahead(*numTokens, [&](auto &scope) { |
lib/Parse/ParseType.cpp
Outdated
| break; | ||
| case tok::kw_protocol: | ||
| if (startsWithLess(peekToken())) { | ||
| if (Tok.is(tok::kw_protocol) && startsWithLess(peekToken())) { |
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 don't think this is needed.
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.
You're right; I think it was briefly needed until I factored out diagnoseAndRecover() into a helper lambda.
fe22d37 to
f7df829
Compare
|
I've also updated the ASTGen work to reflect the fact that SwiftSyntax no longer considers this to be experimental syntax. |
|
@swift-ci smoke test |
65464e8 to
6f72d14
Compare
|
@swift-ci smoke test |
rintaro
left a comment
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.
Other than these small comments, this looks good to me!
| ERROR(experimental_using_decl_disabled,PointsToFirstBadToken, | ||
| "'using' is an experimental feature that is currently disabled", ()) | ||
|
|
||
| ERROR(impossible_parse,none, |
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.
Not used? (I'm fine with having this, but just to make sure I don't miss anything)
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.
Removing it.
lib/AST/ASTDumper.cpp
Outdated
| printFlag(!ABIRoleInfo(D).providesAPI(), "abi_only"); | ||
|
|
||
| printSourceRange(D->getSourceRange(), &D->getASTContext()); | ||
| if (D->getStartLoc().isValid() && D->getEndLoc().isValid()) |
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.
| if (D->getStartLoc().isValid() && D->getEndLoc().isValid()) | |
| if (D->getSourceRange().isValid()) |
If you did this because there's a case D->getStartLoc().isValid() != D->getEndLoc().isValid(), we should fix that.
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 already did (we talked about it in the first review round), but if something like that ever happens again, it'll be helpful to have ASTDumper not crash trying to dump the invalid range.
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.
Decl::getStartLoc()/Decl::getEndLoc() are implemented as:
swift/include/swift/AST/Decl.h
Lines 1139 to 1143 in d030c8f
| /// Returns the starting location of the entire declaration. | |
| SourceLoc getStartLoc() const { return getSourceRange().Start; } | |
| /// Returns the end location of the entire declaration. | |
| SourceLoc getEndLoc() const { return getSourceRange().End; } |
If there was a invalid mismatch, it'd already crashed at this point.
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.
Okay, I can drop that change.
When closing paren is missing, point at the location where it should be found, not the opening paren.
A PatternBindingEntry formed from a MissingPatternSyntax has no valid SourceLocs in it, and a PatternBindingDecl containing only PatternBindingEntries with no valid SourceLocs trips an assertion during availability checking. (Generating dummy SourceLocs can cause invalid overlaps between AvailabilityScopes, so that’s not a workable solution.) The fix is to: • Refuse to generate a PatternBindingEntry from a PatternBindingSyntax with a MissingPatternSyntax (MissingPatternSyntaxes at nested positions don’t have this problem) • Refuse to generate a PatternBindingDecl with no PatternBindingEntries This ensures that the invalid AST nodes are never formed. No current test cases hit this problem, but certain invalid module selector tests can run into this situation when run with ASTGen.
6f72d14 to
683ce9d
Compare
|
@swift-ci please test |
|
@swift-ci test macOS platform |
1 similar comment
|
@swift-ci test macOS platform |
This PR adds support for module selectors to the legacy parser and also updates ASTGen to lower the module selector-related syntax nodes introduced by swiftlang/swift-syntax#3091. Although the compiler parses module selectors with this PR, they don't have any actual effect on name lookup or other compiler behavior.
Makes progress on rdar://problem/19481048. Extracted from #34556. Currently under review as SE-0491.