Skip to content

Commit 5a069e8

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 5a069e8

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

Sources/SwiftParser/Nominals.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,12 @@ extension Parser {
258258
arena: self.arena))
259259
} while keepGoing != nil && loopProgress.evaluate(currentToken)
260260
}
261-
let rangle = self.consumeAnyToken(remapping: .rightAngle)
261+
let rangle: RawTokenSyntax
262+
if self.currentToken.starts(with: ">") {
263+
rangle = self.consumePrefix(">", as: .rightAngle)
264+
} else {
265+
rangle = RawTokenSyntax(missing: .rightAngle, arena: self.arena)
266+
}
262267
return RawPrimaryAssociatedTypeClauseSyntax(
263268
leftAngleBracket: langle,
264269
primaryAssociatedTypeList: RawPrimaryAssociatedTypeListSyntax(elements: associatedTypes, arena: self.arena),

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 type"),
1345+
DiagnosticSpec(locationMarker: "3️⃣", message: "expected member block in protocol"),
1346+
]
1347+
)
1348+
}
13361349
}
13371350

13381351
extension Parser.DeclAttributes {

0 commit comments

Comments
 (0)