Skip to content

Commit e5962c4

Browse files
Merge pull request #2306 from sophiapoirier/globals_strict_concurrency_opt_out_nonisolated_unsafe
nonisolated(unsafe) to opt out of strict concurrency static checking for global variables
2 parents 18c5bce + 8fb1309 commit e5962c4

File tree

3 files changed

+40
-8
lines changed

3 files changed

+40
-8
lines changed

Sources/SwiftParser/Declarations.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ extension DeclarationModifier {
1717
switch self {
1818
case .__consuming, .__setter_access, ._const, ._local, .async,
1919
.borrowing, .class, .consuming, .convenience, .distributed, .dynamic,
20-
.final, .indirect, .infix, .isolated, .lazy, .mutating, .nonisolated,
21-
.nonmutating, .optional, .override, .postfix, .prefix, .reasync,
22-
.required, .rethrows, .static, .weak:
20+
.final, .indirect, .infix, .isolated, .lazy, .mutating, .nonmutating,
21+
.optional, .override, .postfix, .prefix, .reasync, .required,
22+
.rethrows, .static, .weak:
2323
return false
24-
case .fileprivate, .internal, .package, .open, .private,
24+
case .fileprivate, .internal, .nonisolated, .package, .open, .private,
2525
.public, .unowned:
2626
return true
2727
}

Sources/SwiftParser/Modifiers.swift

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ extension Parser {
6060
}
6161
case (.declarationModifier(.unowned), let handle)?:
6262
elements.append(self.parseUnownedModifier(handle))
63+
case (.declarationModifier(.nonisolated), let handle)?:
64+
elements.append(parseNonisolatedModifier(handle))
6365
case (.declarationModifier(.final), let handle)?,
6466
(.declarationModifier(.required), let handle)?,
6567
(.declarationModifier(.optional), let handle)?,
@@ -79,7 +81,6 @@ extension Parser {
7981
(.declarationModifier(.indirect), let handle)?,
8082
(.declarationModifier(.isolated), let handle)?,
8183
(.declarationModifier(.async), let handle)?,
82-
(.declarationModifier(.nonisolated), let handle)?,
8384
(.declarationModifier(.distributed), let handle)?,
8485
(.declarationModifier(._const), let handle)?,
8586
(.declarationModifier(._local), let handle)?,
@@ -98,9 +99,9 @@ extension Parser {
9899
}
99100

100101
extension Parser {
101-
mutating func parseModifierDetail() -> RawDeclModifierDetailSyntax {
102+
mutating func parseModifierDetail(_ keyword: Keyword) -> RawDeclModifierDetailSyntax {
102103
let (unexpectedBeforeLeftParen, leftParen) = self.expect(.leftParen)
103-
let (unexpectedBeforeDetailToken, detailToken) = self.expect(.identifier, TokenSpec(.set, remapping: .identifier), default: .identifier)
104+
let (unexpectedBeforeDetailToken, detailToken) = self.expect(.identifier, TokenSpec(keyword, remapping: .identifier), default: .identifier)
104105
let (unexpectedBeforeRightParen, rightParen) = self.expect(.rightParen)
105106
return RawDeclModifierDetailSyntax(
106107
unexpectedBeforeLeftParen,
@@ -118,7 +119,7 @@ extension Parser {
118119

119120
let detail: RawDeclModifierDetailSyntax?
120121
if self.at(.leftParen) {
121-
detail = self.parseModifierDetail()
122+
detail = self.parseModifierDetail(.set)
122123
} else {
123124
detail = nil
124125
}
@@ -217,4 +218,22 @@ extension Parser {
217218
arena: self.arena
218219
)
219220
}
221+
222+
mutating func parseNonisolatedModifier(_ handle: RecoveryConsumptionHandle) -> RawDeclModifierSyntax {
223+
let (unexpectedBeforeKeyword, keyword) = self.eat(handle)
224+
225+
let detail: RawDeclModifierDetailSyntax?
226+
if self.at(.leftParen) {
227+
detail = self.parseModifierDetail(.unsafe)
228+
} else {
229+
detail = nil
230+
}
231+
232+
return RawDeclModifierSyntax(
233+
unexpectedBeforeKeyword,
234+
name: keyword,
235+
detail: detail,
236+
arena: self.arena
237+
)
238+
}
220239
}

Tests/SwiftParserTest/DeclarationTests.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,19 @@ final class DeclarationTests: ParserTestCase {
240240
)
241241
}
242242

243+
func testNonisolatedUnsafeParsing() {
244+
assertParse(
245+
"""
246+
nonisolated(unsafe) let a = 0
247+
248+
struct A {
249+
nonisolated(unsafe) let b = 0
250+
nonisolated(unsafe) var c: Int { 0 }
251+
}
252+
"""
253+
)
254+
}
255+
243256
func testProtocolParsing() {
244257
assertParse("protocol Foo {}")
245258

0 commit comments

Comments
 (0)