Skip to content

Commit 9dc574b

Browse files
committed
Fix a crash in diagnostic generation if a primary associated type is not ended with a >
In the new test case, we were parsing `:` as a right angle, which caused a crash when we were retrieving the token’s kind in diagnostic generation.
1 parent b41b87b commit 9dc574b

File tree

5 files changed

+25
-4
lines changed

5 files changed

+25
-4
lines changed

CodeGeneration/Sources/SyntaxSupport/gyb_generated/DeclNodes.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@ public let DECL_NODES: [Node] = [
413413
]),
414414

415415
Node(name: "InheritedType",
416-
nameForDiagnostics: "type",
416+
nameForDiagnostics: "inherited type",
417417
kind: "Syntax",
418418
traits: [
419419
"WithTrailingComma"

Sources/SwiftParser/Nominals.swift

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,10 +258,18 @@ extension Parser {
258258
arena: self.arena))
259259
} while keepGoing != nil && loopProgress.evaluate(currentToken)
260260
}
261-
let rangle = self.consumeAnyToken(remapping: .rightAngle)
261+
let unexpectedBeforeRangle: RawUnexpectedNodesSyntax?
262+
let rangle: RawTokenSyntax
263+
if self.currentToken.starts(with: ">") {
264+
unexpectedBeforeRangle = nil
265+
rangle = self.consumePrefix(">", as: .rightAngle)
266+
} else {
267+
(unexpectedBeforeRangle, rangle) = self.expect(.rightAngle)
268+
}
262269
return RawPrimaryAssociatedTypeClauseSyntax(
263270
leftAngleBracket: langle,
264271
primaryAssociatedTypeList: RawPrimaryAssociatedTypeListSyntax(elements: associatedTypes, arena: self.arena),
272+
unexpectedBeforeRangle,
265273
rightAngleBracket: rangle,
266274
arena: self.arena)
267275
}

Sources/SwiftSyntax/gyb_generated/Misc.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -828,7 +828,7 @@ extension SyntaxKind {
828828
case .declModifier:
829829
return "modifier"
830830
case .inheritedType:
831-
return "type"
831+
return "inherited type"
832832
case .inheritedTypeList:
833833
return nil
834834
case .typeInheritanceClause:

Tests/SwiftParserTest/DeclarationTests.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1333,6 +1333,19 @@ final class DeclarationTests: XCTestCase {
13331333
]
13341334
)
13351335
}
1336+
1337+
func testPrimaryAssociatedTypeNotTerminatedWithAngleBracket() {
1338+
AssertParse(
1339+
"protocol1️⃣<2️⃣:3️⃣",
1340+
diagnostics: [
1341+
DiagnosticSpec(locationMarker: "1️⃣", message: "expected identifier in protocol"),
1342+
DiagnosticSpec(locationMarker: "2️⃣", message: "expected name in primary associated type clause"),
1343+
DiagnosticSpec(locationMarker: "2️⃣", message: "expected '>' to end primary associated type clause"),
1344+
DiagnosticSpec(locationMarker: "3️⃣", message: "expected type in inherited type"),
1345+
DiagnosticSpec(locationMarker: "3️⃣", message: "expected member block in protocol"),
1346+
]
1347+
)
1348+
}
13361349
}
13371350

13381351
extension Parser.DeclAttributes {

gyb_syntax_support/DeclNodes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@
190190
Child('Detail', kind='DeclModifierDetail', is_optional=True),
191191
]),
192192

193-
Node('InheritedType', name_for_diagnostics='type', kind='Syntax',
193+
Node('InheritedType', name_for_diagnostics='inherited type', kind='Syntax',
194194
traits=['WithTrailingComma'],
195195
children=[
196196
Child('TypeName', kind='Type'),

0 commit comments

Comments
 (0)