Skip to content

Commit c04e83d

Browse files
committed
reject invalid arguments to nonisolated contextual keyword
1 parent 114c422 commit c04e83d

File tree

2 files changed

+32
-5
lines changed

2 files changed

+32
-5
lines changed

Sources/SwiftParser/Modifiers.swift

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,9 @@ extension Parser {
9999
}
100100

101101
extension Parser {
102-
mutating func parseModifierDetail(_ keyword: Keyword) -> RawDeclModifierDetailSyntax {
102+
mutating func parseModifierDetail() -> RawDeclModifierDetailSyntax {
103103
let (unexpectedBeforeLeftParen, leftParen) = self.expect(.leftParen)
104-
let (unexpectedBeforeDetailToken, detailToken) = self.expect(.identifier, TokenSpec(keyword, remapping: .identifier), default: .identifier)
104+
let (unexpectedBeforeDetailToken, detailToken) = self.expect(.identifier, TokenSpec(.set, remapping: .identifier), default: .identifier)
105105
let (unexpectedBeforeRightParen, rightParen) = self.expect(.rightParen)
106106
return RawDeclModifierDetailSyntax(
107107
unexpectedBeforeLeftParen,
@@ -119,7 +119,7 @@ extension Parser {
119119

120120
let detail: RawDeclModifierDetailSyntax?
121121
if self.at(.leftParen) {
122-
detail = self.parseModifierDetail(.set)
122+
detail = self.parseModifierDetail()
123123
} else {
124124
detail = nil
125125
}
@@ -224,7 +224,18 @@ extension Parser {
224224

225225
let detail: RawDeclModifierDetailSyntax?
226226
if self.at(.leftParen) {
227-
detail = self.parseModifierDetail(.unsafe)
227+
let (unexpectedBeforeLeftParen, leftParen) = self.expect(.leftParen)
228+
let (unexpectedBeforeDetailToken, detailToken) = self.expect(TokenSpec(.unsafe, remapping: .identifier))
229+
let (unexpectedBeforeRightParen, rightParen) = self.expect(.rightParen)
230+
detail = RawDeclModifierDetailSyntax(
231+
unexpectedBeforeLeftParen,
232+
leftParen: leftParen,
233+
unexpectedBeforeDetailToken,
234+
detail: detailToken,
235+
unexpectedBeforeRightParen,
236+
rightParen: rightParen,
237+
arena: self.arena
238+
)
228239
} else {
229240
detail = nil
230241
}

Tests/SwiftParserTest/DeclarationTests.swift

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,8 +193,24 @@ final class DeclarationTests: ParserTestCase {
193193
struct A {
194194
nonisolated(unsafe) let b = 0
195195
nonisolated(unsafe) var c: Int { 0 }
196+
nonisolated(1️⃣safe) let d = 0
196197
}
197-
"""
198+
""",
199+
diagnostics: [
200+
DiagnosticSpec(
201+
message: "expected 'unsafe' in modifier",
202+
fixIts: ["replace 'safe' with 'unsafe'"]
203+
)
204+
],
205+
fixedSource: """
206+
nonisolated(unsafe) let a = 0
207+
208+
struct A {
209+
nonisolated(unsafe) let b = 0
210+
nonisolated(unsafe) var c: Int { 0 }
211+
nonisolated(unsafe) let d = 0
212+
}
213+
"""
198214
)
199215
}
200216

0 commit comments

Comments
 (0)