Skip to content

Commit e6e2dac

Browse files
committed
Handle missing func keyword
1 parent ad237ff commit e6e2dac

File tree

2 files changed

+20
-22
lines changed

2 files changed

+20
-22
lines changed

Sources/SwiftParser/Declarations.swift

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ extension Parser {
232232
case (.lhs(.extension), let handle)?:
233233
return RawDeclSyntax(self.parseExtensionDeclaration(attrs, handle))
234234
case (.lhs(.func), let handle)?:
235-
return RawDeclSyntax(self.parseFuncDeclaration(attrs, handle))
235+
return RawDeclSyntax(self.parseFuncDeclaration(attrs, isMissingFuncKeyword() ? .missing(.keyword(.func)) : handle))
236236
case (.lhs(.subscript), let handle)?:
237237
return RawDeclSyntax(self.parseSubscriptDeclaration(attrs, handle))
238238
case (.lhs(.`init`), let handle)?:
@@ -283,10 +283,7 @@ extension Parser {
283283
)
284284
}
285285

286-
let isPossibleFuncIdentifier = self.at(.identifier, .wildcard)
287-
let isPossibleFuncParen = self.peek(isAt: .leftParen, .binaryOperator)
288-
// Treat operators specially because they're likely to be functions.
289-
if (isPossibleFuncIdentifier && isPossibleFuncParen) || self.at(anyIn: Operator.self) != nil {
286+
if isMissingFuncKeyword() {
290287
return RawDeclSyntax(self.parseFuncDeclaration(attrs, .missing(.keyword(.func))))
291288
}
292289
}
@@ -298,6 +295,13 @@ extension Parser {
298295
)
299296
)
300297
}
298+
299+
fileprivate mutating func isMissingFuncKeyword() -> Bool {
300+
// Treat operators specially because they're likely to be functions.
301+
let isPossibleFuncIdentifier = self.at(.identifier, .wildcard)
302+
let isPossibleFuncParen = self.peek(isAt: .leftParen, .binaryOperator)
303+
return (isPossibleFuncIdentifier && isPossibleFuncParen) || self.at(anyIn: Operator.self) != nil
304+
}
301305
}
302306

303307
extension Parser {

Tests/SwiftParserTest/translated/RecoveryTests.swift

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2107,13 +2107,7 @@ final class RecoveryTests: ParserTestCase {
21072107
)
21082108
}
21092109

2110-
func testRecovery120() {
2111-
assertParse(
2112-
"""
2113-
//===--- Recovery for wrong decl introducer keyword.
2114-
"""
2115-
)
2116-
}
2110+
// MARK: - Recovery for wrong decl introducer keyword.
21172111

21182112
func testRecovery121() {
21192113
assertParse(
@@ -2125,19 +2119,19 @@ final class RecoveryTests: ParserTestCase {
21252119
}
21262120
""",
21272121
diagnostics: [
2128-
// TODO: Old parser expected error on line 2: expected 'func' keyword in instance method declaration
2129-
DiagnosticSpec(message: "unexpected code 'notAKeyword() {}' before function")
2130-
]
2122+
DiagnosticSpec(message: "expected 'func' in function", fixIts: ["insert 'func'"])
2123+
],
2124+
fixedSource: """
2125+
class WrongDeclIntroducerKeyword1 {
2126+
func notAKeyword() {}
2127+
func foo() {}
2128+
class func bar() {}
2129+
}
2130+
"""
21312131
)
21322132
}
21332133

2134-
func testRecovery122() {
2135-
assertParse(
2136-
"""
2137-
//===--- Recovery for wrong inheritance clause.
2138-
"""
2139-
)
2140-
}
2134+
// MARK: - Recovery for wrong inheritance clause.
21412135

21422136
func testRecovery123() {
21432137
assertParse(

0 commit comments

Comments
 (0)