8000 Merge pull request #2356 from sophiapoirier/nonisolated-unsafe-global… · swiftlang/swift-syntax@7f41827 · GitHub
[go: up one dir, main page]

Skip to content

Commit 7f41827

Browse files
Merge pull request #2356 from sophiapoirier/nonisolated-unsafe-globals-guard
🍒 guard nonisolated(unsafe) by experimental feature
2 parents 114c422 + b57568f commit 7f41827

File tree

3 files changed

+42
-6
lines changed

3 files changed

+42
-6
lines changed

Sources/SwiftParser/ExperimentalFeatures.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,7 @@ extension Parser.ExperimentalFeatures {
2626

2727
/// Whether to enable the parsing of 'then' statements.
2828
public static let thenStatements = Self(rawValue: 1 << 1)
29+
30+
/// Whether to enable the parsing of strict concurrency for globals.
31+
public static let globalConcurrency = Self(rawValue: 1 << 2)
2932
}

Sources/SwiftParser/Modifiers.swift

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,12 @@ extension Parser {
6161
case (.declarationModifier(.unowned), let handle)?:
6262
elements.append(self.parseUnownedModifier(handle))
6363
case (.declarationModifier(.nonisolated), let handle)?:
64-
elements.append(parseNonisolatedModifier(handle))
64+
if experimentalFeatures.contains(.globalConcurrency) {
65+
elements.append(parseNonisolatedModifier(handle))
66+
} else {
67+
let (unexpectedBeforeKeyword, keyword) = self.eat(handle)
68+
elements.append(RawDeclModifierSyntax(unexpectedBeforeKeyword, name: keyword, detail: nil, arena: self.arena))
69+
}
6570
case (.declarationModifier(.final), let handle)?,
6671
(.declarationModifier(.required), let handle)?,
6772
(.declarationModifier(.optional), let handle)?,
@@ -99,9 +104,9 @@ extension Parser {
99104
}
100105

101106
extension Parser {
102-
mutating func parseModifierDetail(_ keyword: Keyword) -> RawDeclModifierDetailSyntax {
107+
mutating func parseModifierDetail() -> RawDeclModifierDetailSyntax {
103108
let (unexpectedBeforeLeftParen, leftParen) = self.expect(.leftParen)
104-
let (unexpectedBeforeDetailToken, detailToken) = self.expect(.identifier, TokenSpec(keyword, remapping: .identifier), default: .identifier)
109+
let (unexpectedBeforeDetailToken, detailToken) = self.expect(.identifier, TokenSpec(.set, remapping: .identifier), default: .identifier)
105110
let (unexpectedBeforeRightParen, rightParen) = self.expect(.rightParen)
106111
return RawDeclModifierDetailSyntax(
107112
unexpectedBeforeLeftParen,
@@ -119,7 +124,7 @@ extension Parser {
119124

120125
let detail: RawDeclModifierDetailSyntax?
121126
if self.at(.leftParen) {
122-
detail = self.parseModifierDetail(.set)
127+
detail = self.parseModifierDetail()
123128
} else {
124129
detail = nil
125130
}
@@ -224,7 +229,18 @@ extension Parser {
224229

225230
let detail: RawDeclModifierDetailSyntax?
226231
if self.at(.leftParen) {
227-
detail = self.parseModifierDetail(.unsafe)
232+
let (unexpectedBeforeLeftParen, leftParen) = self.expect(.leftParen)
233+
let (unexpectedBeforeDetailToken, detailToken) = self.expect(TokenSpec(.unsafe, remapping: .identifier))
234+
let (unexpectedBeforeRightParen, rightParen) = self.expect(.rightParen)
235+
detail = RawDeclModifierDetailSyntax(
236+
unexpectedBeforeLeftParen,
237+
leftParen: leftParen,
238+
unexpectedBeforeDetailToken,
239+
detail: detailToken,
240+
unexpectedBeforeRightParen,
241+
rightParen: rightParen,
242+
arena: self.arena
243+
)
228244
} else {
229245
detail = nil
230246
}

Tests/SwiftParserTest/DeclarationTests.swift

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,8 +193,25 @@ 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+
""",
214+
experimentalFeatures: [.globalConcurrency]
198215
)
199216
}
200217

0 commit comments

Comments
 (0)
0