Skip to content

Commit 72553a2

Browse files
authored
Merge pull request #710 from MEnnabah/specialize-attribute-invalid-param
Fixes #679: @Specialize with invalid attribute
2 parents ec7f015 + 19c5c3f commit 72553a2

File tree

2 files changed

+38
-4
lines changed

2 files changed

+38
-4
lines changed

Sources/SwiftParser/Attributes.swift

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -491,11 +491,9 @@ extension Parser {
491491
mutating func parseSpecializeAttributeSpecList() -> RawSpecializeAttributeSpecListSyntax {
492492
var elements = [RawSyntax]()
493493
// Parse optional "exported" and "kind" labeled parameters.
494-
while !self.at(.eof) && !self.at(.whereKeyword) {
494+
while !self.at(.eof) && !self.at(.rightParen) && !self.at(.whereKeyword) {
495495
let ident = self.parseAnyIdentifier()
496-
guard let knownParameter = SpecializeParameter(rawValue: ident.tokenText) else {
497-
fatalError()
498-
}
496+
let knownParameter = SpecializeParameter(rawValue: ident.tokenText)
499497
let (unexpectedBeforeColon, colon) = self.expect(.colon)
500498

501499
switch knownParameter {
@@ -573,6 +571,17 @@ extension Parser {
573571
trailingComma: comma,
574572
arena: self.arena
575573
)))
574+
case nil:
575+
let valueLabel = self.consumeAnyToken()
576+
let comma = self.consume(if: .comma)
577+
elements.append(RawSyntax(RawLabeledSpecializeEntrySyntax(
578+
label: ident,
579+
unexpectedBeforeColon,
580+
colon: colon,
581+
value: valueLabel,
582+
trailingComma: comma,
583+
arena: self.arena
584+
)))
576585
}
577586
}
578587

Tests/SwiftParserTest/Attributes.swift

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,29 @@ final class AttributeTests: XCTestCase {
3232
]
3333
)
3434
}
35+
36+
func testMissingClosingParenToAttribute() {
37+
AssertParse(
38+
"""
39+
@_specialize(e#^DIAG_1^#
40+
""",
41+
diagnostics: [
42+
DiagnosticSpec(locationMarker: "DIAG_1", message: "Expected ':' in attribute argument"),
43+
DiagnosticSpec(locationMarker: "DIAG_1", message: "Expected ')' to end attribute"),
44+
]
45+
)
46+
}
47+
48+
func testMultipleInvalidSpecializeParams() {
49+
AssertParse(
50+
"""
51+
@_specialize(e#^DIAG_1^#, exported#^DIAG_2^#)
52+
""",
53+
diagnostics: [
54+
DiagnosticSpec(locationMarker: "DIAG_1", message: "Expected ':' in attribute argument"),
55+
DiagnosticSpec(locationMarker: "DIAG_2", message: "Expected ':' in attribute argument"),
56+
DiagnosticSpec(locationMarker: "DIAG_2", message: "Expected 'false' in attribute argument"),
57+
]
58+
)
59+
}
3560
}

0 commit comments

Comments
 (0)