Skip to content

Commit ca20739

Browse files
authored
Merge pull request #2564 from apple/updatelifetimesyntax
Update lifetime dependence syntax
2 parents 7233193 + a22fb3d commit ca20739

File tree

13 files changed

+299
-358
lines changed

13 files changed

+299
-358
lines changed

CodeGeneration/Sources/SyntaxSupport/KeywordSpec.swift

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,7 @@ public enum Keyword: CaseIterable {
108108
case _Class
109109
case _compilerInitialized
110110
case _const
111-
case _consume
112111
case _consuming
113-
case _copy
114112
case _documentation
115113
case _dynamicReplacement
116114
case _effects
@@ -121,7 +119,6 @@ public enum Keyword: CaseIterable {
121119
case _local
122120
case _modify
123121
case _move
124-
case _mutate
125122
case _mutating
126123
case _NativeClass
127124
case _NativeRefCountedObject
@@ -186,6 +183,7 @@ public enum Keyword: CaseIterable {
186183
case `default`
187184
case `defer`
188185
case `deinit`
186+
case dependsOn
189187
case deprecated
190188
case derivative
191189
case didSet
@@ -275,6 +273,7 @@ public enum Keyword: CaseIterable {
275273
case reverse
276274
case right
277275
case safe
276+
case scoped
278277
case `self`
279278
case `Self`
280279
case Sendable
@@ -343,12 +342,8 @@ public enum Keyword: CaseIterable {
343342
return KeywordSpec("_compilerInitialized")
344343
case ._const:
345344
return KeywordSpec("_const")
346-
case ._consume:
347-
return KeywordSpec("_consume", experimentalFeature: .nonescapableTypes)
348345
case ._consuming:
349346
return KeywordSpec("_consuming", experimentalFeature: .referenceBindings)
350-
case ._copy:
351-
return KeywordSpec("_copy", experimentalFeature: .nonescapableTypes)
352347
case ._documentation:
353348
return KeywordSpec("_documentation")
354349
case ._dynamicReplacement:
@@ -369,8 +364,6 @@ public enum Keyword: CaseIterable {
369364
return KeywordSpec("_modify")
370365
case ._move:
371366
return KeywordSpec("_move")
372-
case ._mutate:
373-
return KeywordSpec("_mutate", experimentalFeature: .nonescapableTypes)
374367
case ._mutating:
375368
return KeywordSpec("_mutating", experimentalFeature: .referenceBindings)
376369
case ._NativeClass:
@@ -499,6 +492,8 @@ public enum Keyword: CaseIterable {
499492
return KeywordSpec("defer", isLexerClassified: true)
500493
case .deinit:
501494
return KeywordSpec("deinit", isLexerClassified: true)
495+
case .dependsOn:
496+
return KeywordSpec("dependsOn", experimentalFeature: .nonescapableTypes)
502497
case .deprecated:
503498
return KeywordSpec("deprecated")
504499
case .derivative:
@@ -675,6 +670,8 @@ public enum Keyword: CaseIterable {
675670
return KeywordSpec("right")
676671
case .safe:
677672
return KeywordSpec("safe")
673+
case .scoped:
674+
return KeywordSpec("scoped", experimentalFeature: .nonescapableTypes)
678675
case .self:
679676
return KeywordSpec("self", isLexerClassified: true)
680677
case .Self:

CodeGeneration/Sources/SyntaxSupport/TypeNodes.swift

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -553,25 +553,14 @@ public let TYPE_NODES: [Node] = [
553553
### Example
554554
`borrow(data)` in `func foo(data: Array<Item>) -> borrow(data) ComplexReferenceType`
555555
""",
556-
traits: [
557-
"Parenthesized"
558-
],
559556
children: [
560-
Child(
561-
name: "leftParen",
562-
kind: .token(choices: [.token(.leftParen)])
563-
),
564557
Child(
565558
name: "arguments",
566559
kind: .collection(kind: .lifetimeSpecifierArgumentList, collectionElementName: "Arguments"),
567560
documentation: """
568561
The function parameters that the lifetime of the annotated type depends on.
569562
"""
570-
),
571-
Child(
572-
name: "rightParen",
573-
kind: .token(choices: [.token(.rightParen)])
574-
),
563+
)
575564
]
576565
),
577566

@@ -581,21 +570,32 @@ public let TYPE_NODES: [Node] = [
581570
experimentalFeature: .nonescapableTypes,
582571
nameForDiagnostics: "lifetime specifier",
583572
documentation: "A specifier that specifies function parameter on whose lifetime a type depends",
573+
traits: [
574+
"Parenthesized"
575+
],
584576
children: [
585577
Child(
586-
name: "specifier",
587-
kind: .token(choices: [
588-
.keyword(._copy),
589-
.keyword(._consume),
590-
.keyword(._borrow),
591-
.keyword(._mutate),
592-
]),
578+
name: "dependsOnKeyword",
579+
kind: .token(choices: [.keyword(.dependsOn)]),
593580
documentation: "The specifier token that's attached to the type."
594581
),
582+
Child(
583+
name: "leftParen",
584+
kind: .token(choices: [.token(.leftParen)])
585+
),
586+
Child(
587+
name: "scopedKeyword",
588+
kind: .token(choices: [.keyword(.scoped)]),
589+
isOptional: true
590+
),
595591
Child(
596592
name: "arguments",
597593
kind: .node(kind: .lifetimeSpecifierArguments)
598594
),
595+
Child(
596+
name: "rightParen",
597+
kind: .token(choices: [.token(.rightParen)])
598+
),
599599
]
600600
),
601601

Sources/SwiftParser/TokenPrecedence.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ enum TokenPrecedence: Comparable {
233233
.__setter_access, .indirect, .isolated, .nonisolated, .distributed, ._local,
234234
.inout, ._mutating, ._borrow, ._borrowing, .borrowing, ._consuming, .consuming, .consume, ._resultDependsOnSelf,
235235
._resultDependsOn,
236-
.transferring, ._consume, ._copy, ._mutate,
236+
.transferring, .dependsOn, .scoped,
237237
// Accessors
238238
.get, .set, .didSet, .willSet, .unsafeAddress, .addressWithOwner, .addressWithNativeOwner, .unsafeMutableAddress,
239239
.mutableAddressWithOwner, .mutableAddressWithNativeOwner, ._read, ._modify,

Sources/SwiftParser/Types.swift

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -900,33 +900,33 @@ extension Parser.Lookahead {
900900
}
901901

902902
extension Parser {
903-
private mutating func parseLifetimeTypeSpecifier(
904-
specifierHandle: TokenConsumptionHandle
905-
) -> RawTypeSpecifierListSyntax.Element {
906-
let specifier = self.eat(specifierHandle)
903+
private mutating func parseLifetimeTypeSpecifier() -> RawTypeSpecifierListSyntax.Element {
904+
let specifier = self.eat(TokenSpec(.dependsOn))
907905

908906
guard let leftParen = self.consume(if: .leftParen) else {
909907
// If there is no left paren, add an entirely missing detail. Otherwise, we start to consume the following type
910908
// name as a token inside the detail, which leads to confusing recovery results.
911909
let arguments = RawLifetimeSpecifierArgumentsSyntax(
912-
leftParen: missingToken(.leftParen),
913910
arguments: RawLifetimeSpecifierArgumentListSyntax(
914911
elements: [
915912
RawLifetimeSpecifierArgumentSyntax(parameter: missingToken(.identifier), trailingComma: nil, arena: arena)
916913
],
917914
arena: self.arena
918915
),
919-
rightParen: missingToken(.rightParen),
920916
arena: self.arena
921917
)
922918
let lifetimeSpecifier = RawLifetimeTypeSpecifierSyntax(
923-
specifier: specifier,
919+
dependsOnKeyword: specifier,
920+
leftParen: missingToken(.leftParen),
921+
scopedKeyword: nil,
924922
arguments: arguments,
923+
rightParen: missingToken(.rightParen),
925924
arena: self.arena
926925
)
927926
return .lifetimeTypeSpecifier(lifetimeSpecifier)
928927
}
929928

929+
let scoped = self.consume(if: .keyword(.scoped))
930930
var keepGoing: RawTokenSyntax?
931931
var arguments: [RawLifetimeSpecifierArgumentSyntax] = []
932932
var loopProgress = LoopProgressCondition()
@@ -948,15 +948,16 @@ extension Parser {
948948
let lifetimeSpecifierArgumentList = RawLifetimeSpecifierArgumentListSyntax(elements: arguments, arena: self.arena)
949949
let (unexpectedBeforeRightParen, rightParen) = self.expect(.rightParen)
950950
let argumentsSyntax = RawLifetimeSpecifierArgumentsSyntax(
951-
leftParen: leftParen,
952951
arguments: lifetimeSpecifierArgumentList,
953-
unexpectedBeforeRightParen,
954-
rightParen: rightParen,
955952
arena: self.arena
956953
)
957954
let lifetimeSpecifier = RawLifetimeTypeSpecifierSyntax(
958-
specifier: specifier,
955+
dependsOnKeyword: specifier,
956+
leftParen: leftParen,
957+
scopedKeyword: scoped,
959958
arguments: argumentsSyntax,
959+
unexpectedBeforeRightParen,
960+
rightParen: rightParen,
960961
arena: self.arena
961962
)
962963
return .lifetimeTypeSpecifier(lifetimeSpecifier)
@@ -976,20 +977,18 @@ extension Parser {
976977
specifiers: RawTypeSpecifierListSyntax,
977978
attributes: RawAttributeListSyntax
978979
)? {
979-
typealias SimpleOrLifetimeSpecifier =
980-
EitherTokenSpecSet<SimpleTypeSpecifierSyntax.SpecifierOptions, LifetimeTypeSpecifierSyntax.SpecifierOptions>
981980
var specifiers: [RawTypeSpecifierListSyntax.Element] = []
982-
SPECIFIER_PARSING: while canHaveParameterSpecifier,
983-
let (specifierSpec, specifierHandle) = self.at(anyIn: SimpleOrLifetimeSpecifier.self)
984-
{
985-
switch specifierSpec {
986-
case .lhs: specifiers.append(parseSimpleTypeSpecifier(specifierHandle: specifierHandle))
987-
case .rhs:
981+
SPECIFIER_PARSING: while canHaveParameterSpecifier {
982+
if let (_, specifierHandle) = self.at(anyIn: SimpleTypeSpecifierSyntax.SpecifierOptions.self) {
983+
specifiers.append(parseSimpleTypeSpecifier(specifierHandle: specifierHandle))
984+
} else if self.at(TokenSpec(.dependsOn)) {
988985
if self.experimentalFeatures.contains(.nonescapableTypes) {
989-
specifiers.append(parseLifetimeTypeSpecifier(specifierHandle: specifierHandle))
986+
specifiers.append(parseLifetimeTypeSpecifier())
990987
} else {
991988
break SPECIFIER_PARSING
992989
}
990+
} else {
991+
break SPECIFIER_PARSING
993992
}
994993
}
995994
specifiers += misplacedSpecifiers.map {

Sources/SwiftParser/generated/Parser+TokenSpecSet.swift

Lines changed: 0 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -2623,79 +2623,6 @@ extension LifetimeSpecifierArgumentSyntax {
26232623
}
26242624
}
26252625

2626-
extension LifetimeTypeSpecifierSyntax {
2627-
@_spi(Diagnostics)
2628-
public enum SpecifierOptions: TokenSpecSet {
2629-
@_spi(ExperimentalLanguageFeatures)
2630-
case _copy
2631-
@_spi(ExperimentalLanguageFeatures)
2632-
case _consume
2633-
case _borrow
2634-
@_spi(ExperimentalLanguageFeatures)
2635-
case _mutate
2636-
2637-
init?(lexeme: Lexer.Lexeme, experimentalFeatures: Parser.ExperimentalFeatures) {
2638-
switch PrepareForKeywordMatch(lexeme) {
2639-
case TokenSpec(._copy) where experimentalFeatures.contains(.nonescapableTypes):
2640-
self = ._copy
2641-
case TokenSpec(._consume) where experimentalFeatures.contains(.nonescapableTypes):
2642-
self = ._consume
2643-
case TokenSpec(._borrow):
2644-
self = ._borrow
2645-
case TokenSpec(._mutate) where experimentalFeatures.contains(.nonescapableTypes):
2646-
self = ._mutate
2647-
default:
2648-
return nil
2649-
}
2650-
}
2651-
2652-
public init?(token: TokenSyntax) {
2653-
switch token {
2654-
case TokenSpec(._copy):
2655-
self = ._copy
2656-
case TokenSpec(._consume):
2657-
self = ._consume
2658-
case TokenSpec(._borrow):
2659-
self = ._borrow
2660-
case TokenSpec(._mutate):
2661-
self = ._mutate
2662-
default:
2663-
return nil
2664-
}
2665-
}
2666-
2667-
var spec: TokenSpec {
2668-
switch self {
2669-
case ._copy:
2670-
return .keyword(._copy)
2671-
case ._consume:
2672-
return .keyword(._consume)
2673-
case ._borrow:
2674-
return .keyword(._borrow)
2675-
case ._mutate:
2676-
return .keyword(._mutate)
2677-
}
2678-
}
2679-
2680-
/// Returns a token that satisfies the `TokenSpec` of this case.
2681-
///
2682-
/// If the token kind of this spec has variable text, e.g. for an identifier, this returns a token with empty text.
2683-
@_spi(Diagnostics)
2684-
public var tokenSyntax: TokenSyntax {
2685-
switch self {
2686-
case ._copy:
2687-
return .keyword(._copy)
2688-
case ._consume:
2689-
return .keyword(._consume)
2690-
case ._borrow:
2691-
return .keyword(._borrow)
2692-
case ._mutate:
2693-
return .keyword(._mutate)
2694-
}
2695-
}
2696-
}
2697-
}
2698-
26992626
extension MemberTypeSyntax {
27002627
@_spi(Diagnostics)
27012628
public enum NameOptions: TokenSpecSet {

Sources/SwiftSyntax/generated/ChildNameForKeyPath.swift

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2009,30 +2009,34 @@ public func childName(_ keyPath: AnyKeyPath) -> String? {
20092009
return "trailingComma"
20102010
case \LifetimeSpecifierArgumentSyntax.unexpectedAfterTrailingComma:
20112011
return "unexpectedAfterTrailingComma"
2012-
case \LifetimeSpecifierArgumentsSyntax.unexpectedBeforeLeftParen:
2013-
return "unexpectedBeforeLeftParen"
2014-
case \LifetimeSpecifierArgumentsSyntax.leftParen:
2015-
return "leftParen"
2016-
case \LifetimeSpecifierArgumentsSyntax.unexpectedBetweenLeftParenAndArguments:
2017-
return "unexpectedBetweenLeftParenAndArguments"
2012+
case \LifetimeSpecifierArgumentsSyntax.unexpectedBeforeArguments:
2013+
return "unexpectedBeforeArguments"
20182014
case \LifetimeSpecifierArgumentsSyntax.arguments:
20192015
return "arguments"
2020-
case \LifetimeSpecifierArgumentsSyntax.unexpectedBetweenArgumentsAndRightParen:
2016+
case \LifetimeSpecifierArgumentsSyntax.unexpectedAfterArguments:
2017+
return "unexpectedAfterArguments"
2018+
case \LifetimeTypeSpecifierSyntax.unexpectedBeforeDependsOnKeyword:
2019+
return "unexpectedBeforeDependsOnKeyword"
2020+
case \LifetimeTypeSpecifierSyntax.dependsOnKeyword:
2021+
return "dependsOnKeyword"
2022+
case \LifetimeTypeSpecifierSyntax.unexpectedBetweenDependsOnKeywordAndLeftParen:
2023+
return "unexpectedBetweenDependsOnKeywordAndLeftParen"
2024+
case \LifetimeTypeSpecifierSyntax.leftParen:
2025+
return "leftParen"
2026+
case \LifetimeTypeSpecifierSyntax.unexpectedBetweenLeftParenAndScopedKeyword:
2027+
return "unexpectedBetweenLeftParenAndScopedKeyword"
2028+
case \LifetimeTypeSpecifierSyntax.scopedKeyword:
2029+
return "scopedKeyword"
2030+
case \LifetimeTypeSpecifierSyntax.unexpectedBetweenScopedKeywordAndArguments:
2031+
return "unexpectedBetweenScopedKeywordAndArguments"
2032+
case \LifetimeTypeSpecifierSyntax.arguments:
2033+
return "arguments"
2034+
case \LifetimeTypeSpecifierSyntax.unexpectedBetweenArgumentsAndRightParen:
20212035
return "unexpectedBetweenArgumentsAndRightParen"
2022-
case \LifetimeSpecifierArgumentsSyntax.rightParen:
2036+
case \LifetimeTypeSpecifierSyntax.rightParen:
20232037
return "rightParen"
2024-
case \LifetimeSpecifierArgumentsSyntax.unexpectedAfterRightParen:
2038+
case \LifetimeTypeSpecifierSyntax.unexpectedAfterRightParen:
20252039
return "unexpectedAfterRightParen"
2026-
case \LifetimeTypeSpecifierSyntax.unexpectedBeforeSpecifier:
2027-
return "unexpectedBeforeSpecifier"
2028-
case \LifetimeTypeSpecifierSyntax.specifier:
2029-
return "specifier"
2030-
case \LifetimeTypeSpecifierSyntax.unexpectedBetweenSpecifierAndArguments:
2031-
return "unexpectedBetweenSpecifierAndArguments"
2032-
case \LifetimeTypeSpecifierSyntax.arguments:
2033-
return "arguments"
2034-
case \LifetimeTypeSpecifierSyntax.unexpectedAfterArguments:
2035-
return "unexpectedAfterArguments"
20362040
case \MacroDeclSyntax.unexpectedBeforeAttributes:
20372041
return "unexpectedBeforeAttributes"
20382042
case \MacroDeclSyntax.attributes:

0 commit comments

Comments
 (0)