Skip to content

Commit f4acb89

Browse files
Merge pull request #2855 from nate-chandler/general-coro/20240917/1
[CoroutineAccessors] Add read and modify.
2 parents 1e8e227 + 8fb85ad commit f4acb89

File tree

10 files changed

+127
-2
lines changed

10 files changed

+127
-2
lines changed

Diff for: CodeGeneration/Sources/SyntaxSupport/DeclNodes.swift

+2
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,9 @@ public let DECL_NODES: [Node] = [
113113
.keyword(.mutableAddressWithOwner),
114114
.keyword(.mutableAddressWithNativeOwner),
115115
.keyword(._read),
116+
.keyword(.read),
116117
.keyword(._modify),
118+
.keyword(.modify),
117119
.keyword(.`init`),
118120
])
119121
),

Diff for: CodeGeneration/Sources/SyntaxSupport/ExperimentalFeatures.swift

+3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public enum ExperimentalFeature: String, CaseIterable {
1818
case doExpressions
1919
case nonescapableTypes
2020
case trailingComma
21+
case coroutineAccessors
2122

2223
/// The name of the feature, which is used in the doc comment.
2324
public var featureName: String {
@@ -32,6 +33,8 @@ public enum ExperimentalFeature: String, CaseIterable {
3233
return "NonEscableTypes"
3334
case .trailingComma:
3435
return "trailing comma"
36+
case .coroutineAccessors:
37+
return "CoroutineAccessors"
3538
}
3639
}
3740

Diff for: CodeGeneration/Sources/SyntaxSupport/KeywordSpec.swift

+6
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,7 @@ public enum Keyword: CaseIterable {
218218
case macro
219219
case message
220220
case metadata
221+
case modify
221222
case module
222223
case mutableAddressWithNativeOwner
223224
case mutableAddressWithOwner
@@ -245,6 +246,7 @@ public enum Keyword: CaseIterable {
245246
case `Protocol`
246247
case `protocol`
247248
case `public`
249+
case read
248250
case reasync
249251
case renamed
250252
case `repeat`
@@ -576,6 +578,8 @@ public enum Keyword: CaseIterable {
576578
return KeywordSpec("message")
577579
case .metadata:
578580
return KeywordSpec("metadata")
581+
case .modify:
582+
return KeywordSpec("modify", experimentalFeature: .coroutineAccessors)
579583
case .module:
580584
return KeywordSpec("module")
581585
case .mutableAddressWithNativeOwner:
@@ -628,6 +632,8 @@ public enum Keyword: CaseIterable {
628632
return KeywordSpec("protocol", isLexerClassified: true)
629633
case .public:
630634
return KeywordSpec("public", isLexerClassified: true)
635+
case .read:
636+
return KeywordSpec("read", experimentalFeature: .coroutineAccessors)
631637
case .reasync:
632638
return KeywordSpec("reasync")
633639
case .renamed:

Diff for: Sources/SwiftParser/TokenPrecedence.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ enum TokenPrecedence: Comparable {
239239
.dependsOn, .scoped, .sending,
240240
// Accessors
241241
.get, .set, .didSet, .willSet, .unsafeAddress, .addressWithOwner, .addressWithNativeOwner, .unsafeMutableAddress,
242-
.mutableAddressWithOwner, .mutableAddressWithNativeOwner, ._read, ._modify,
242+
.mutableAddressWithOwner, .mutableAddressWithNativeOwner, ._read, .read, ._modify, .modify,
243243
// Misc
244244
.import:
245245
self = .declKeyword

Diff for: Sources/SwiftParser/generated/ExperimentalFeatures.swift

+3
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,7 @@ extension Parser.ExperimentalFeatures {
3838

3939
/// Whether to enable the parsing of trailing comma.
4040
public static let trailingComma = Self (rawValue: 1 << 4)
41+
42+
/// Whether to enable the parsing of CoroutineAccessors.
43+
public static let coroutineAccessors = Self (rawValue: 1 << 5)
4144
}

Diff for: Sources/SwiftParser/generated/Parser+TokenSpecSet.swift

+24
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,15 @@ extension AccessorDeclSyntax {
3232
case mutableAddressWithOwner
3333
case mutableAddressWithNativeOwner
3434
case _read
35+
#if compiler(>=5.8)
36+
@_spi(ExperimentalLanguageFeatures)
37+
#endif
38+
case read
3539
case _modify
40+
#if compiler(>=5.8)
41+
@_spi(ExperimentalLanguageFeatures)
42+
#endif
43+
case modify
3644
case `init`
3745

3846
init?(lexeme: Lexer.Lexeme, experimentalFeatures: Parser.ExperimentalFeatures) {
@@ -59,8 +67,12 @@ extension AccessorDeclSyntax {
5967
self = .mutableAddressWithNativeOwner
6068
case TokenSpec(._read):
6169
self = ._read
70+
case TokenSpec(.read) where experimentalFeatures.contains(.coroutineAccessors):
71+
self = .read
6272
case TokenSpec(._modify):
6373
self = ._modify
74+
case TokenSpec(.modify) where experimentalFeatures.contains(.coroutineAccessors):
75+
self = .modify
6476
case TokenSpec(.`init`):
6577
self = .`init`
6678
default:
@@ -92,8 +104,12 @@ extension AccessorDeclSyntax {
92104
self = .mutableAddressWithNativeOwner
93105
case TokenSpec(._read):
94106
self = ._read
107+
case TokenSpec(.read):
108+
self = .read
95109
case TokenSpec(._modify):
96110
self = ._modify
111+
case TokenSpec(.modify):
112+
self = .modify
97113
case TokenSpec(.`init`):
98114
self = .`init`
99115
default:
@@ -125,8 +141,12 @@ extension AccessorDeclSyntax {
125141
return .keyword(.mutableAddressWithNativeOwner)
126142
case ._read:
127143
return .keyword(._read)
144+
case .read:
145+
return .keyword(.read)
128146
case ._modify:
129147
return .keyword(._modify)
148+
case .modify:
149+
return .keyword(.modify)
130150
case .`init`:
131151
return .keyword(.`init`)
132152
}
@@ -160,8 +180,12 @@ extension AccessorDeclSyntax {
160180
return .keyword(.mutableAddressWithNativeOwner)
161181
case ._read:
162182
return .keyword(._read)
183+
case .read:
184+
return .keyword(.read)
163185
case ._modify:
164186
return .keyword(._modify)
187+
case .modify:
188+
return .keyword(.modify)
165189
case .`init`:
166190
return .keyword(.`init`)
167191
}

Diff for: Sources/SwiftSyntax/generated/Keyword.swift

+14
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,10 @@ public enum Keyword: UInt8, Hashable, Sendable {
161161
case macro
162162
case message
163163
case metadata
164+
#if compiler(>=5.8)
165+
@_spi(ExperimentalLanguageFeatures)
166+
#endif
167+
case modify
164168
case module
165169
case mutableAddressWithNativeOwner
166170
case mutableAddressWithOwner
@@ -188,6 +192,10 @@ public enum Keyword: UInt8, Hashable, Sendable {
188192
case `Protocol`
189193
case `protocol`
190194
case `public`
195+
#if compiler(>=5.8)
196+
@_spi(ExperimentalLanguageFeatures)
197+
#endif
198+
case read
191199
case reasync
192200
case renamed
193201
case `repeat`
@@ -322,6 +330,8 @@ public enum Keyword: UInt8, Hashable, Sendable {
322330
self = .objc
323331
case "open":
324332
self = .open
333+
case "read":
334+
self = .read
325335
case "safe":
326336
self = .safe
327337
case "self":
@@ -416,6 +426,8 @@ public enum Keyword: UInt8, Hashable, Sendable {
416426
self = .inline
417427
case "linear":
418428
self = .linear
429+
case "modify":
430+
self = .modify
419431
case "module":
420432
self = .module
421433
case "prefix":
@@ -946,6 +958,7 @@ public enum Keyword: UInt8, Hashable, Sendable {
946958
"macro",
947959
"message",
948960
"metadata",
961+
"modify",
949962
"module",
950963
"mutableAddressWithNativeOwner",
951964
"mutableAddressWithOwner",
@@ -973,6 +986,7 @@ public enum Keyword: UInt8, Hashable, Sendable {
973986
"Protocol",
974987
"protocol",
975988
"public",
989+
"read",
976990
"reasync",
977991
"renamed",
978992
"repeat",

Diff for: Sources/SwiftSyntax/generated/raw/RawSyntaxValidation.swift

+2
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,9 @@ func validateLayout(layout: RawSyntaxBuffer, as kind: SyntaxKind) {
242242
.keyword("mutableAddressWithOwner"),
243243
.keyword("mutableAddressWithNativeOwner"),
244244
.keyword("_read"),
245+
.keyword("read"),
245246
.keyword("_modify"),
247+
.keyword("modify"),
246248
.keyword("init")
247249
]))
248250
assertNoError(kind, 6, verify(layout[6], as: RawUnexpectedNodesSyntax?.self))

Diff for: Sources/SwiftSyntax/generated/syntaxNodes/SyntaxNodesAB.swift

+3-1
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ public struct AccessorBlockSyntax: SyntaxProtocol, SyntaxHashable, _LeafSyntaxNo
251251
///
252252
/// - `attributes`: ``AttributeListSyntax``
253253
/// - `modifier`: ``DeclModifierSyntax``?
254-
/// - `accessorSpecifier`: (`get` | `set` | `didSet` | `willSet` | `unsafeAddress` | `addressWithOwner` | `addressWithNativeOwner` | `unsafeMutableAddress` | `mutableAddressWithOwner` | `mutableAddressWithNativeOwner` | `_read` | `_modify` | `init`)
254+
/// - `accessorSpecifier`: (`get` | `set` | `didSet` | `willSet` | `unsafeAddress` | `addressWithOwner` | `addressWithNativeOwner` | `unsafeMutableAddress` | `mutableAddressWithOwner` | `mutableAddressWithNativeOwner` | `_read` | `read` | `_modify` | `modify` | `init`)
255255
/// - `parameters`: ``AccessorParametersSyntax``?
256256
/// - `effectSpecifiers`: ``AccessorEffectSpecifiersSyntax``?
257257
/// - `body`: ``CodeBlockSyntax``?
@@ -418,7 +418,9 @@ public struct AccessorDeclSyntax: DeclSyntaxProtocol, SyntaxHashable, _LeafDeclS
418418
/// - `mutableAddressWithOwner`
419419
/// - `mutableAddressWithNativeOwner`
420420
/// - `_read`
421+
/// - `read`
421422
/// - `_modify`
423+
/// - `modify`
422424
/// - `init`
423425
public var accessorSpecifier: TokenSyntax {
424426
get {

Diff for: Tests/SwiftParserTest/DeclarationTests.swift

+69
Original file line numberDiff line numberDiff line change
@@ -3334,4 +3334,73 @@ final class DeclarationTests: ParserTestCase {
33343334
)
33353335
)
33363336
}
3337+
3338+
func testCoroutineAccessors() {
3339+
assertParse(
3340+
"""
3341+
var irm: Int {
3342+
read {
3343+
yield _i
3344+
}
3345+
modify {
3346+
yield &_i
3347+
}
3348+
}
3349+
""",
3350+
experimentalFeatures: .coroutineAccessors
3351+
)
3352+
assertParse(
3353+
"""
3354+
public var i: Int {
3355+
_read {
3356+
yield _i
3357+
}
3358+
read {
3359+
yield _i
3360+
}
3361+
_modify {
3362+
yield &_i
3363+
}
3364+
modify {
3365+
yield &_i
3366+
}
3367+
}
3368+
""",
3369+
experimentalFeatures: .coroutineAccessors
3370+
)
3371+
assertParse(
3372+
"""
3373+
var i_rm: Int {
3374+
_read {
3375+
yield _i
3376+
}
3377+
1️⃣modify {
3378+
yield &_i
3379+
}
3380+
}
3381+
""",
3382+
diagnostics: [
3383+
DiagnosticSpec(
3384+
message: "unexpected code in variable"
3385+
)
3386+
]
3387+
)
3388+
assertParse(
3389+
"""
3390+
var ir_m: Int {
3391+
_modify {
3392+
yield &_i
3393+
}
3394+
1️⃣read {
3395+
yield _i
3396+
}
3397+
}
3398+
""",
3399+
diagnostics: [
3400+
DiagnosticSpec(
3401+
message: "unexpected code in variable"
3402+
)
3403+
]
3404+
)
3405+
}
33373406
}

0 commit comments

Comments
 (0)