Skip to content

Commit acd5bbd

Browse files
committed
Add diagnostic for wrong inheritance
1 parent 92178c0 commit acd5bbd

File tree

3 files changed

+35
-3
lines changed

3 files changed

+35
-3
lines changed

Sources/SwiftParser/Nominals.swift

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ extension Parser {
180180
}
181181

182182
let inheritance: RawTypeInheritanceClauseSyntax?
183-
if self.at(.colon) {
183+
if self.at(.colon) || self.at(.leftParen) {
184184
inheritance = self.parseInheritance()
185185
} else {
186186
inheritance = nil
@@ -213,7 +213,16 @@ extension Parser {
213213
/// Parse an inheritance clause.
214214
@_spi(RawSyntax)
215215
public mutating func parseInheritance() -> RawTypeInheritanceClauseSyntax {
216-
let (unexpectedBeforeColon, colon) = self.expect(.colon)
216+
let unexpectedBeforeColon: RawUnexpectedNodesSyntax?
217+
let colon: RawTokenSyntax
218+
if self.at(.colon) {
219+
unexpectedBeforeColon = nil
220+
colon = consumeAnyToken(remapping: .colon)
221+
} else {
222+
unexpectedBeforeColon = RawUnexpectedNodesSyntax([self.consumeAnyToken()], arena: self.arena)
223+
colon = missingToken(.colon)
224+
}
225+
217226
var elements = [RawInheritedTypeSyntax]()
218227
do {
219228
var keepGoing: RawTokenSyntax? = nil
@@ -241,6 +250,7 @@ extension Parser {
241250
)
242251
} while keepGoing != nil && loopProgress.evaluate(currentToken)
243252
}
253+
244254
return RawTypeInheritanceClauseSyntax(
245255
unexpectedBeforeColon,
246256
colon: colon,

Sources/SwiftParserDiagnostics/ParseDiagnosticsGenerator.swift

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1171,7 +1171,26 @@ public class ParseDiagnosticsGenerator: SyntaxAnyVisitor {
11711171
return handleEffectSpecifiers(node)
11721172
}
11731173

1174-
public override func visit(_ node: TypeInitializerClauseSyntax) -> SyntaxVisitorContinueKind {
1174+
1175+
1176+
public override func visit(_ node: TypeInheritanceClauseSyntax) -> SyntaxVisitorContinueKind {
1177+
if shouldSkip(node) {
1178+
return .skipChildren
1179+
}
1180+
1181+
if let unexpected = node.unexpectedBeforeColon, !unexpected.tokens(satisfying: { $0.tokenKind == .leftParen }).isEmpty {
1182+
addDiagnostic(unexpected,
1183+
.expectedColonClass,
1184+
fixIts: [FixIt(message: <#T##FixItMessage#>,
1185+
changes: [FixIt.Changes.makePresent(node.colon), .makeMissing(unexpected)])],
1186+
handledNodes: [unexpected.id, node.colon.id]
1187+
)
1188+
}
1189+
1190+
return .visitChildren
1191+
}
1192+
1193+
public override func visit(_ node: TypeInitializerClauseSyntax) -> SyntaxVisitorContinueKind {
11751194
if shouldSkip(node) {
11761195
return .skipChildren
11771196
}

Sources/SwiftParserDiagnostics/ParserDiagnosticMessages.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,9 @@ extension DiagnosticMessage where Self == StaticParserError {
125125
public static var escapedNewlineAtLatlineOfMultiLineStringLiteralNotAllowed: Self {
126126
.init("escaped newline at the last line of a multi-line string literal is not allowed")
127127
}
128+
public static var expectedColonClass: Self {
129+
.init("expected ':' to begin inheritance clause")
130+
}
128131
public static var expectedExpressionAfterTry: Self {
129132
.init("expected expression after 'try'")
130133
}

0 commit comments

Comments
 (0)