Skip to content

Improve recovery of misplaced attributes on statements #1282

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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Sources/SwiftParser/Statements.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ extension TokenConsumer {
/// - Seealso: ``Parser/parseStatement()``
func atStartOfStatement(allowRecovery: Bool = false) -> Bool {
var lookahead = self.lookahead()
if allowRecovery {
// Attributes are not allowed on statements. But for recovery, skip over
// misplaced attributes.
_ = lookahead.consumeAttributeList()
}
return lookahead.isStartOfStatement(allowRecovery: allowRecovery)
}
}
Expand Down
4 changes: 1 addition & 3 deletions Sources/SwiftParser/TokenPrecedence.swift
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public enum TokenPrecedence: Comparable {
case // Chaining punctuators
.infixQuestionMark, .period, .postfixQuestionMark, .exclamationMark,
// Misc
.backslash, .backtick, .colon, .comma, .ellipsis, .equal, .prefixAmpersand:
.atSign, .backslash, .backtick, .colon, .comma, .ellipsis, .equal, .prefixAmpersand:
self = .weakPunctuator

// MARK: Weak bracket close
Expand All @@ -159,8 +159,6 @@ public enum TokenPrecedence: Comparable {
.semicolon,
// Arrow is a strong indicator in a function type that we are now in the return type
.arrow,
// '@' typically occurs at the start of declarations
.atSign,
// EOF is here because it is a very stong marker and doesn't belong anywhere else
.eof:
self = .strongPunctuator
Expand Down
4 changes: 2 additions & 2 deletions Tests/SwiftParserTest/StatementTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -265,8 +265,8 @@ final class StatementTests: XCTestCase {
}
""",
diagnostics: [
DiagnosticSpec(locationMarker: "1️⃣", message: "unexpected code '@s return' in function"),
DiagnosticSpec(locationMarker: "2️⃣", message: "unexpected code '@unknown return' in function"),
DiagnosticSpec(locationMarker: "1️⃣", message: "unexpected code '@s' before 'return' statement"),
DiagnosticSpec(locationMarker: "2️⃣", message: "unexpected code '@unknown' before 'return' statement"),
]
)
}
Expand Down
11 changes: 5 additions & 6 deletions Tests/SwiftParserTest/translated/SwitchTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -950,7 +950,7 @@ final class SwitchTests: XCTestCase {
}
""",
diagnostics: [
DiagnosticSpec(message: "expected label in switch case")
DiagnosticSpec(message: "unexpected code '@moreGarbage' in switch case")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we parse any single attribute rather than just @unknown?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, we do. That’s what the syntax tree currently allows.

]
)
}
Expand Down Expand Up @@ -986,22 +986,21 @@ final class SwitchTests: XCTestCase {
switch Whatever.Thing {
case .Thing:
break
@unknown2️⃣
@unknown
2️⃣@unknown
case _:
break
}
switch Whatever.Thing {
@unknown 3️⃣@garbage4️⃣(foobar)
@unknown 3️⃣@garbage(foobar)
case _:
break
}
""",
diagnostics: [
DiagnosticSpec(locationMarker: "1️⃣", message: "unexpected code '(garbage)' in switch case"),
DiagnosticSpec(locationMarker: "2️⃣", message: "expected label in switch case"),
DiagnosticSpec(locationMarker: "3️⃣", message: "expected label in switch case"),
DiagnosticSpec(locationMarker: "4️⃣", message: "unexpected code '(foobar)' in switch case"),
DiagnosticSpec(locationMarker: "2️⃣", message: "unexpected code '@unknown' in switch case"),
DiagnosticSpec(locationMarker: "3️⃣", message: "unexpected code '@garbage(foobar)' in switch case"),
]
)
}
Expand Down