Skip to content

[CoroutineAccessors] Add read and modify. #2855

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CodeGeneration/Sources/SyntaxSupport/DeclNodes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,9 @@ public let DECL_NODES: [Node] = [
.keyword(.mutableAddressWithOwner),
.keyword(.mutableAddressWithNativeOwner),
.keyword(._read),
.keyword(.read),
.keyword(._modify),
.keyword(.modify),
.keyword(.`init`),
])
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public enum ExperimentalFeature: String, CaseIterable {
case doExpressions
case nonescapableTypes
case trailingComma
case coroutineAccessors

/// The name of the feature, which is used in the doc comment.
public var featureName: String {
Expand All @@ -32,6 +33,8 @@ public enum ExperimentalFeature: String, CaseIterable {
return "NonEscableTypes"
case .trailingComma:
return "trailing comma"
case .coroutineAccessors:
return "CoroutineAccessors"
}
}

Expand Down
6 changes: 6 additions & 0 deletions CodeGeneration/Sources/SyntaxSupport/KeywordSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ public enum Keyword: CaseIterable {
case macro
case message
case metadata
case modify
case module
case mutableAddressWithNativeOwner
case mutableAddressWithOwner
Expand Down Expand Up @@ -245,6 +246,7 @@ public enum Keyword: CaseIterable {
case `Protocol`
case `protocol`
case `public`
case read
case reasync
case renamed
case `repeat`
Expand Down Expand Up @@ -576,6 +578,8 @@ public enum Keyword: CaseIterable {
return KeywordSpec("message")
case .metadata:
return KeywordSpec("metadata")
case .modify:
return KeywordSpec("modify", experimentalFeature: .coroutineAccessors)
case .module:
return KeywordSpec("module")
case .mutableAddressWithNativeOwner:
Expand Down Expand Up @@ -628,6 +632,8 @@ public enum Keyword: CaseIterable {
return KeywordSpec("protocol", isLexerClassified: true)
case .public:
return KeywordSpec("public", isLexerClassified: true)
case .read:
return KeywordSpec("read", experimentalFeature: .coroutineAccessors)
case .reasync:
return KeywordSpec("reasync")
case .renamed:
Expand Down
2 changes: 1 addition & 1 deletion Sources/SwiftParser/TokenPrecedence.swift
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ enum TokenPrecedence: Comparable {
.dependsOn, .scoped, .sending,
// Accessors
.get, .set, .didSet, .willSet, .unsafeAddress, .addressWithOwner, .addressWithNativeOwner, .unsafeMutableAddress,
.mutableAddressWithOwner, .mutableAddressWithNativeOwner, ._read, ._modify,
.mutableAddressWithOwner, .mutableAddressWithNativeOwner, ._read, .read, ._modify, .modify,
// Misc
.import:
self = .declKeyword
Expand Down
3 changes: 3 additions & 0 deletions Sources/SwiftParser/generated/ExperimentalFeatures.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,7 @@ extension Parser.ExperimentalFeatures {

/// Whether to enable the parsing of trailing comma.
public static let trailingComma = Self (rawValue: 1 << 4)

/// Whether to enable the parsing of CoroutineAccessors.
public static let coroutineAccessors = Self (rawValue: 1 << 5)
}
24 changes: 24 additions & 0 deletions Sources/SwiftParser/generated/Parser+TokenSpecSet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,15 @@ extension AccessorDeclSyntax {
case mutableAddressWithOwner
case mutableAddressWithNativeOwner
case _read
#if compiler(>=5.8)
@_spi(ExperimentalLanguageFeatures)
#endif
case read
case _modify
#if compiler(>=5.8)
@_spi(ExperimentalLanguageFeatures)
#endif
case modify
case `init`

init?(lexeme: Lexer.Lexeme, experimentalFeatures: Parser.ExperimentalFeatures) {
Expand All @@ -59,8 +67,12 @@ extension AccessorDeclSyntax {
self = .mutableAddressWithNativeOwner
case TokenSpec(._read):
self = ._read
case TokenSpec(.read) where experimentalFeatures.contains(.coroutineAccessors):
self = .read
case TokenSpec(._modify):
self = ._modify
case TokenSpec(.modify) where experimentalFeatures.contains(.coroutineAccessors):
self = .modify
case TokenSpec(.`init`):
self = .`init`
default:
Expand Down Expand Up @@ -92,8 +104,12 @@ extension AccessorDeclSyntax {
self = .mutableAddressWithNativeOwner
case TokenSpec(._read):
self = ._read
case TokenSpec(.read):
self = .read
case TokenSpec(._modify):
self = ._modify
case TokenSpec(.modify):
self = .modify
case TokenSpec(.`init`):
self = .`init`
default:
Expand Down Expand Up @@ -125,8 +141,12 @@ extension AccessorDeclSyntax {
return .keyword(.mutableAddressWithNativeOwner)
case ._read:
return .keyword(._read)
case .read:
return .keyword(.read)
case ._modify:
return .keyword(._modify)
case .modify:
return .keyword(.modify)
case .`init`:
return .keyword(.`init`)
}
Expand Down Expand Up @@ -160,8 +180,12 @@ extension AccessorDeclSyntax {
return .keyword(.mutableAddressWithNativeOwner)
case ._read:
return .keyword(._read)
case .read:
return .keyword(.read)
case ._modify:
return .keyword(._modify)
case .modify:
return .keyword(.modify)
case .`init`:
return .keyword(.`init`)
}
Expand Down
14 changes: 14 additions & 0 deletions Sources/SwiftSyntax/generated/Keyword.swift
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,10 @@ public enum Keyword: UInt8, Hashable, Sendable {
case macro
case message
case metadata
#if compiler(>=5.8)
@_spi(ExperimentalLanguageFeatures)
#endif
case modify
case module
case mutableAddressWithNativeOwner
case mutableAddressWithOwner
Expand Down Expand Up @@ -188,6 +192,10 @@ public enum Keyword: UInt8, Hashable, Sendable {
case `Protocol`
case `protocol`
case `public`
#if compiler(>=5.8)
@_spi(ExperimentalLanguageFeatures)
#endif
case read
case reasync
case renamed
case `repeat`
Expand Down Expand Up @@ -322,6 +330,8 @@ public enum Keyword: UInt8, Hashable, Sendable {
self = .objc
case "open":
self = .open
case "read":
self = .read
case "safe":
self = .safe
case "self":
Expand Down Expand Up @@ -416,6 +426,8 @@ public enum Keyword: UInt8, Hashable, Sendable {
self = .inline
case "linear":
self = .linear
case "modify":
self = .modify
case "module":
self = .module
case "prefix":
Expand Down Expand Up @@ -946,6 +958,7 @@ public enum Keyword: UInt8, Hashable, Sendable {
"macro",
"message",
"metadata",
"modify",
"module",
"mutableAddressWithNativeOwner",
"mutableAddressWithOwner",
Expand Down Expand Up @@ -973,6 +986,7 @@ public enum Keyword: UInt8, Hashable, Sendable {
"Protocol",
"protocol",
"public",
"read",
"reasync",
"renamed",
"repeat",
Expand Down
2 changes: 2 additions & 0 deletions Sources/SwiftSyntax/generated/raw/RawSyntaxValidation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,9 @@ func validateLayout(layout: RawSyntaxBuffer, as kind: SyntaxKind) {
.keyword("mutableAddressWithOwner"),
.keyword("mutableAddressWithNativeOwner"),
.keyword("_read"),
.keyword("read"),
.keyword("_modify"),
.keyword("modify"),
.keyword("init")
]))
assertNoError(kind, 6, verify(layout[6], as: RawUnexpectedNodesSyntax?.self))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ public struct AccessorBlockSyntax: SyntaxProtocol, SyntaxHashable, _LeafSyntaxNo
///
/// - `attributes`: ``AttributeListSyntax``
/// - `modifier`: ``DeclModifierSyntax``?
/// - `accessorSpecifier`: (`get` | `set` | `didSet` | `willSet` | `unsafeAddress` | `addressWithOwner` | `addressWithNativeOwner` | `unsafeMutableAddress` | `mutableAddressWithOwner` | `mutableAddressWithNativeOwner` | `_read` | `_modify` | `init`)
/// - `accessorSpecifier`: (`get` | `set` | `didSet` | `willSet` | `unsafeAddress` | `addressWithOwner` | `addressWithNativeOwner` | `unsafeMutableAddress` | `mutableAddressWithOwner` | `mutableAddressWithNativeOwner` | `_read` | `read` | `_modify` | `modify` | `init`)
/// - `parameters`: ``AccessorParametersSyntax``?
/// - `effectSpecifiers`: ``AccessorEffectSpecifiersSyntax``?
/// - `body`: ``CodeBlockSyntax``?
Expand Down Expand Up @@ -418,7 +418,9 @@ public struct AccessorDeclSyntax: DeclSyntaxProtocol, SyntaxHashable, _LeafDeclS
/// - `mutableAddressWithOwner`
/// - `mutableAddressWithNativeOwner`
/// - `_read`
/// - `read`
/// - `_modify`
/// - `modify`
/// - `init`
public var accessorSpecifier: TokenSyntax {
get {
Expand Down
69 changes: 69 additions & 0 deletions Tests/SwiftParserTest/DeclarationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3334,4 +3334,73 @@ final class DeclarationTests: ParserTestCase {
)
)
}

func testCoroutineAccessors() {
assertParse(
"""
var irm: Int {
read {
yield _i
}
modify {
yield &_i
}
}
""",
experimentalFeatures: .coroutineAccessors
)
assertParse(
"""
public var i: Int {
_read {
yield _i
}
read {
yield _i
}
_modify {
yield &_i
}
modify {
yield &_i
}
}
""",
experimentalFeatures: .coroutineAccessors
)
assertParse(
"""
var i_rm: Int {
_read {
yield _i
}
1️⃣modify {
yield &_i
}
}
""",
diagnostics: [
DiagnosticSpec(
message: "unexpected code in variable"
)
]
)
assertParse(
"""
var ir_m: Int {
_modify {
yield &_i
}
1️⃣read {
yield _i
}
}
""",
diagnostics: [
DiagnosticSpec(
message: "unexpected code in variable"
)
]
)
}
}