-
Notifications
You must be signed in to change notification settings - Fork 439
Produce more useful diagnostics for bogus specifier before arrow and leftbrace #1381
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
Produce more useful diagnostics for bogus specifier before arrow and leftbrace #1381
Conversation
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.
Thank you for your contribution @StevenWong12. Your changes look good to me, I've got a few small comments to make sure we don't accidentally parse valid code as invalid with the new recovery.
Thanks for your detailed suggestions! @ahoppen Although the code snippet above was correctly parsed, I think it is indeed a little risky to recover to a left brace on an arbitrary line so I will make changes as your suggestions about checking As for the modification about
Since the marco signature parsing method reuses the logic of function signature parsing, the tokens from About this issue, I came up with two solutions:
Solution 1 is pretty straightforward but, from my perspective, not pretty fit the context, since the precedence of For now, I simply implement solution 1. But it seems solution 2 is a more generic solution, maybe I can implement it in the future if needed. Also, if there is anything I missed or misunderstood, please let me know. Thanks in advance! |
14b0bfa
to
f5675f5
Compare
I just debugged this myself and the problem seems to be that diff --git a/Sources/SwiftParser/Declarations.swift b/Sources/SwiftParser/Declarations.swift
index b3368e3c..0d56ef09 100644
--- a/Sources/SwiftParser/Declarations.swift
+++ b/Sources/SwiftParser/Declarations.swift
@@ -1376,7 +1376,7 @@ extension Parser {
let output: RawReturnClauseSyntax?
- if self.at(.arrow) || self.canRecoverTo(TokenSpec(.arrow, recoveryPrecedence: .weakPunctuator, allowAtStartOfLine: false)) != nil {
+ if self.at(.arrow) || self.canRecoverTo(TokenSpec(.arrow, allowAtStartOfLine: false)) != nil {
output = self.parseFunctionReturnClause(effectSpecifiers: &effectSpecifiers, allowNamedOpaqueResultType: true)
} else {
output = nil
diff --git a/Sources/SwiftParser/Recovery.swift b/Sources/SwiftParser/Recovery.swift
index 21b4f7f7..4e48f45d 100644
--- a/Sources/SwiftParser/Recovery.swift
+++ b/Sources/SwiftParser/Recovery.swift
@@ -70,9 +70,10 @@ extension Parser.Lookahead {
let initialTokensConsumed = self.tokensConsumed
let recoveryPrecedence = min(spec1.recoveryPrecedence, spec2.recoveryPrecedence, spec3.recoveryPrecedence)
+ let shouldSkipOverNewlines = recoveryPrecedence.shouldSkipOverNewlines && spec1.allowAtStartOfLine && spec2.allowAtStartOfLine && spec3.allowAtStartOfLine
while !self.at(.eof) {
- if !recoveryPrecedence.shouldSkipOverNewlines, self.currentToken.isAtStartOfLine {
+ if !shouldSkipOverNewlines, self.currentToken.isAtStartOfLine {
break
}
let matchedSpec: TokenSpec?
|
f5675f5
to
396ca71
Compare
Cool! Thanks for your help @ahoppen ! The changes have been committed.😀 |
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.
Thank you for your contribution @StevenWong12!
@swift-ci Please test |
Purpose
To produce more useful diagnostics for bogus specifier before arrow and leftbrace as the first step to solve issue #1262
Implementation details
Replace
at
with recovery function and a new testcase.Additionally, to pass the
testMacroDecl
testcase, I made an edit aboutTokenPrecedence
to get the correct precedence of themacro
keyword.I also modified some of the other testcases but I am not sure if they're reasonable.
If there is anything I can do for this PR, please tell me. Many thanks!