Skip to content

Commit 8f04c49

Browse files
committed
[SE-0474] Support yielding borrow and yielding mutate accessors
`yielding borrow` and `yielding mutate` are the final approved names for the accessors previously known as `read` and `modify` (which are themselves improvements over the earlier non-standard `_read` and `_modify` accessors). Since some people are already using these under the `read` and `modify` names, we want to support the old and new names as synonyms for a transition period until everyone has had a chance to migrate.
1 parent 44ece85 commit 8f04c49

File tree

5 files changed

+38
-3
lines changed

5 files changed

+38
-3
lines changed

CodeGeneration/Sources/SyntaxSupport/KeywordSpec.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,7 @@ public enum Keyword: CaseIterable {
279279
case willSet
280280
case wrt
281281
case yield
282+
case yielding
282283

283284
public var spec: KeywordSpec {
284285
switch self {
@@ -691,7 +692,9 @@ public enum Keyword: CaseIterable {
691692
case .wrt:
692693
return KeywordSpec("wrt")
693694
case .yield:
694-
return KeywordSpec("yield")
695+
return KeywordSpec("yield")
696+
case .yielding:
697+
return KeywordSpec("yielding")
695698
}
696699
}
697700
}

Sources/SwiftParser/Declarations.swift

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1671,9 +1671,14 @@ extension Parser {
16711671
var look = self.lookahead()
16721672
let _ = look.consumeAttributeList()
16731673
let hasModifier = look.consume(ifAnyIn: AccessorModifier.self) != nil
1674+
let yielding = look.consume(if: TokenSpec(.yielding)) != nil
16741675
guard let (kind, _) = look.at(anyIn: AccessorDeclSyntax.AccessorSpecifierOptions.self) ?? forcedKind else {
16751676
return nil
16761677
}
1678+
guard !yielding || [AccessorDeclSyntax.AccessorSpecifierOptions.borrow, .mutate].contains(kind) else {
1679+
// `yielding` can only be followed by `borrow` or `mutate`
1680+
return nil
1681+
}
16771682

16781683
let attrs = self.parseAttributeList()
16791684

@@ -1692,11 +1697,28 @@ extension Parser {
16921697
modifier = nil
16931698
}
16941699

1700+
if yielding {
1701+
_ = self.expect(TokenSpec(.yielding))
1702+
}
1703+
16951704
let (unexpectedBeforeIntroducer, introducer) = self.expect(kind.spec)
1705+
1706+
// Map `yielding borrow` => `read`, etc.
1707+
let resolvedKind : AccessorDeclSyntax.AccessorSpecifierOptions
1708+
if yielding {
1709+
switch kind {
1710+
case .borrow: resolvedKind = .read // `yielding borrow` == `read`
1711+
case .mutate: resolvedKind = .modify // `yielding mutate` == `modify`
1712+
default: resolvedKind = kind
1713+
}
1714+
} else {
1715+
resolvedKind = kind
1716+
}
1717+
16961718
return AccessorIntroducer(
16971719
attributes: attrs,
16981720
modifier: modifier,
1699-
kind: kind,
1721+
kind: resolvedKind,
17001722
unexpectedBeforeToken: unexpectedBeforeIntroducer,
17011723
token: introducer
17021724
)

Sources/SwiftParser/TokenPrecedence.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ enum TokenPrecedence: Comparable {
237237
.dependsOn, .scoped, .sending,
238238
// Accessors
239239
.get, .set, .didSet, .willSet, .unsafeAddress, .addressWithOwner, .addressWithNativeOwner, .unsafeMutableAddress,
240-
.mutableAddressWithOwner, .mutableAddressWithNativeOwner, ._read, .read, ._modify, .modify, .mutate,
240+
.mutableAddressWithOwner, .mutableAddressWithNativeOwner, ._read, .read, ._modify, .modify, .mutate, .yielding,
241241
// Misc
242242
.import, .using:
243243
self = .declKeyword

Sources/SwiftSyntax/generated/Keyword.swift

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Tests/SwiftParserTest/DeclarationTests.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3467,12 +3467,18 @@ final class DeclarationTests: ParserTestCase {
34673467
read {
34683468
yield _i
34693469
}
3470+
yielding borrow {
3471+
yield _i
3472+
}
34703473
_modify {
34713474
yield &_i
34723475
}
34733476
modify {
34743477
yield &_i
34753478
}
3479+
yielding mutate {
3480+
yield &_i
3481+
}
34763482
}
34773483
""",
34783484
experimentalFeatures: .coroutineAccessors

0 commit comments

Comments
 (0)