Skip to content

Commit c19ef95

Browse files
committed
Add diagnostics for rethrows in accessor declaration
1 parent 7e099ed commit c19ef95

File tree

3 files changed

+37
-2
lines changed

3 files changed

+37
-2
lines changed

Sources/SwiftParser/Declarations.swift

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1710,8 +1710,24 @@ extension Parser {
17101710
} else {
17111711
parameter = nil
17121712
}
1713-
1714-
let effectSpecifiers = self.parseDeclEffectSpecifiers()
1713+
1714+
var effectSpecifiers = self.parseDeclEffectSpecifiers()
1715+
1716+
if introducer.kind == .get,
1717+
let throwSpecifier = effectSpecifiers?.throwsSpecifier,
1718+
throwSpecifier.tokenText == "rethrows" {
1719+
1720+
let unexpected = RawUnexpectedNodesSyntax([ effectSpecifiers?.unexpectedBetweenAsyncSpecifierAndThrowsSpecifier, RawUnexpectedNodesSyntax([throwSpecifier], arena: self.arena)], arena: self.arena)
1721+
1722+
effectSpecifiers = RawDeclEffectSpecifiersSyntax(
1723+
effectSpecifiers?.unexpectedBeforeAsyncSpecifier,
1724+
asyncSpecifier: effectSpecifiers?.asyncSpecifier,
1725+
unexpected,
1726+
throwsSpecifier: nil,
1727+
effectSpecifiers?.unexpectedAfterThrowsSpecifier,
1728+
arena: self.arena
1729+
)
1730+
}
17151731

17161732
let body = self.parseOptionalCodeBlock()
17171733
return RawAccessorDeclSyntax(

Sources/SwiftParserDiagnostics/ParseDiagnosticsGenerator.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,22 @@ public class ParseDiagnosticsGenerator: SyntaxAnyVisitor {
383383

384384
return .visitChildren
385385
}
386+
387+
388+
public override func visit(_ node: AccessorDeclSyntax) -> SyntaxVisitorContinueKind {
389+
if shouldSkip(node) {
390+
return .skipChildren
391+
}
392+
393+
if let unexpectedBetweenAsyncSpecifierAndThrowsSpecifier = node.effectSpecifiers?.unexpectedBetweenAsyncSpecifierAndThrowsSpecifier,
394+
unexpectedBetweenAsyncSpecifierAndThrowsSpecifier.tokens(viewMode: .sourceAccurate).contains(where: {$0.tokenKind == .keyword(.rethrows)}) == true {
395+
addDiagnostic(
396+
unexpectedBetweenAsyncSpecifierAndThrowsSpecifier, .misspelledThrowsInEffectfulProperties,
397+
handledNodes: [unexpectedBetweenAsyncSpecifierAndThrowsSpecifier.id])
398+
}
399+
400+
return .visitChildren
401+
}
386402

387403
public override func visit(_ node: AssociatedtypeDeclSyntax) -> SyntaxVisitorContinueKind {
388404
if shouldSkip(node) {

Sources/SwiftParserDiagnostics/ParserDiagnosticMessages.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,9 @@ extension DiagnosticMessage where Self == StaticParserError {
173173
public static var misspelledThrows: Self {
174174
.init("expected throwing specifier; did you mean 'throws'?")
175175
}
176+
public static var misspelledThrowsInEffectfulProperties: Self {
177+
.init("only function declarations may be marked 'rethrows'; did you mean 'throws'?")
178+
}
176179
public static var multiLineStringLiteralMustBeginOnNewLine: Self {
177180
.init("multi-line string literal content must begin on a new line")
178181
}

0 commit comments

Comments
 (0)