Skip to content

Commit db0aad7

Browse files
Merge pull request #2335 from sophiapoirier/nonisolated-unsafe-invalid-argument
reject invalid arguments to nonisolated contextual keyword
2 parents ece80d5 + 05de293 commit db0aad7

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
@@ -248,8 +248,24 @@ final class DeclarationTests: ParserTestCase {
248248
struct A {
249249
nonisolated(unsafe) let b = 0
250250
nonisolated(unsafe) var c: Int { 0 }
251+
nonisolated(1️⃣safe) let d = 0
251252
}
252-
"""
253+
""",
254+
diagnostics: [
255+
DiagnosticSpec(
256+
message: "expected 'unsafe' in modifier",
257+
fixIts: ["replace 'safe' with 'unsafe'"]
258+
)
259+
],
260+
fixedSource: """
261+
nonisolated(unsafe) let a = 0
262+
263+
struct A {
264+
nonisolated(unsafe) let b = 0
265+
nonisolated(unsafe) var c: Int { 0 }
266+
nonisolated(unsafe) let d = 0
267+
}
268+
"""
253269
)
254270
}
255271

0 commit comments

Comments
 (0)