Skip to content

Commit 4a1ba3b

Browse files
committed
Support parsing of lifetime dependence specifiers
1 parent a598b64 commit 4a1ba3b

36 files changed

+1938
-382
lines changed

CodeGeneration/Sources/SyntaxSupport/KeywordSpec.swift

+15-6
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,9 @@ public enum Keyword: CaseIterable {
108108
case _Class
109109
case _compilerInitialized
110110
case _const
111+
case _consume
111112
case _consuming
113+
case _copy
112114
case _documentation
113115
case _dynamicReplacement
114116
case _effects
@@ -119,6 +121,7 @@ public enum Keyword: CaseIterable {
119121
case _local
120122
case _modify
121123
case _move
124+
case _mutate
122125
case _mutating
123126
case _NativeClass
124127
case _NativeRefCountedObject
@@ -328,6 +331,8 @@ public enum Keyword: CaseIterable {
328331
return KeywordSpec("_backDeploy")
329332
case ._borrow:
330333
return KeywordSpec("_borrow")
334+
case ._borrowing:
335+
return KeywordSpec("_borrowing", experimentalFeature: .referenceBindings, or: .borrowingSwitch)
331336
case ._BridgeObject:
332337
return KeywordSpec("_BridgeObject")
333338
case ._cdecl:
@@ -338,6 +343,12 @@ public enum Keyword: CaseIterable {
338343
return KeywordSpec("_compilerInitialized")
339344
case ._const:
340345
return KeywordSpec("_const")
346+
case ._consume:
347+
return KeywordSpec("_consume", experimentalFeature: .nonescapableTypes)
348+
case ._consuming:
349+
return KeywordSpec("_consuming", experimentalFeature: .referenceBindings)
350+
case ._copy:
351+
return KeywordSpec("_copy", experimentalFeature: .nonescapableTypes)
341352
case ._documentation:
342353
return KeywordSpec("_documentation")
343354
case ._dynamicReplacement:
@@ -358,6 +369,10 @@ public enum Keyword: CaseIterable {
358369
return KeywordSpec("_modify")
359370
case ._move:
360371
return KeywordSpec("_move")
372+
case ._mutate:
373+
return KeywordSpec("_mutate", experimentalFeature: .nonescapableTypes)
374+
case ._mutating:
375+
return KeywordSpec("_mutating", experimentalFeature: .referenceBindings)
361376
case ._NativeClass:
362377
return KeywordSpec("_NativeClass")
363378
case ._NativeRefCountedObject:
@@ -743,12 +758,6 @@ public enum Keyword: CaseIterable {
743758
return KeywordSpec("wrt")
744759
case .yield:
745760
return KeywordSpec("yield")
746-
case ._borrowing:
747-
return KeywordSpec("_borrowing", experimentalFeature: .referenceBindings, or: .borrowingSwitch)
748-
case ._consuming:
749-
return KeywordSpec("_consuming", experimentalFeature: .referenceBindings)
750-
case ._mutating:
751-
return KeywordSpec("_mutating", experimentalFeature: .referenceBindings)
752761
}
753762
}
754763
}

CodeGeneration/Sources/SyntaxSupport/SyntaxNodeKind.swift

+13-7
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,8 @@ public enum SyntaxNodeKind: String, CaseIterable {
9999
case dictionaryExpr
100100
case dictionaryType
101101
case differentiabilityArgument
102-
case differentiabilityArguments
103102
case differentiabilityArgumentList
103+
case differentiabilityArguments
104104
case differentiabilityWithRespectToArgument
105105
case differentiableAttributeArguments
106106
case discardAssignmentExpr
@@ -182,6 +182,9 @@ public enum SyntaxNodeKind: String, CaseIterable {
182182
case labeledSpecializeArgument
183183
case labeledStmt
184184
case layoutRequirement
185+
case lifetimeSpecifierArgument
186+
case lifetimeSpecifierArgumentList
187+
case lifetimeTypeSpecifier
185188
case macroDecl
186189
case macroExpansionDecl
187190
case macroExpansionExpr
@@ -244,14 +247,15 @@ public enum SyntaxNodeKind: String, CaseIterable {
244247
case returnStmt
245248
case sameTypeRequirement
246249
case sequenceExpr
250+
case simpleTypeSpecifier
251+
case simpleStringLiteralExpr
252+
case simpleStringLiteralSegmentList
247253
case someOrAnyType
248254
case sourceFile
249255
case specializeAttributeArgumentList
250256
case specializeAvailabilityArgument
251257
case specializeTargetFunctionArgument
252258
case stmt
253-
case simpleStringLiteralExpr
254-
case simpleStringLiteralSegmentList
255259
case stringLiteralExpr
256260
case stringLiteralSegmentList
257261
case stringSegment
@@ -286,6 +290,7 @@ public enum SyntaxNodeKind: String, CaseIterable {
286290
case typeExpr
287291
case typeInitializerClause
288292
case typeSpecifier
293+
case lifetimeSpecifierArguments
289294
case typeSpecifierList
290295
case unavailableFromAsyncAttributeArguments
291296
case underscorePrivateAttributeArguments
@@ -301,10 +306,10 @@ public enum SyntaxNodeKind: String, CaseIterable {
301306
case whereClause
302307
case whileStmt
303308
case wildcardPattern
304-
case yieldStmt
305309
case yieldedExpression
306-
case yieldedExpressionsClause
307310
case yieldedExpressionList
311+
case yieldedExpressionsClause
312+
case yieldStmt
308313

309314
// Nodes that have special handling throughout the codebase
310315

@@ -407,8 +412,8 @@ public enum SyntaxNodeKind: String, CaseIterable {
407412
case .derivativeAttributeArguments: return "derivativeRegistrationAttributeArguments"
408413
case .designatedType: return "designatedTypeElement"
409414
case .differentiabilityArgument: return "differentiabilityParam"
410-
case .differentiabilityArguments: return "differentiabilityParams"
411415
case .differentiabilityArgumentList: return "differentiabilityParamList"
416+
case .differentiabilityArguments: return "differentiabilityParams"
412417
case .differentiabilityWithRespectToArgument: return "differentiabilityParamsClause"
413418
case .documentationAttributeArgumentList: return "documentationAttributeArguments"
414419
case .dynamicReplacementAttributeArguments: return "dynamicReplacementArguments"
@@ -442,6 +447,7 @@ public enum SyntaxNodeKind: String, CaseIterable {
442447
case .precedenceGroupName: return "precedenceGroupNameElement"
443448
case .repeatStmt: return "repeatWhileStmt"
444449
case .someOrAnyType: return "constrainedSugarType"
450+
case .simpleTypeSpecifier: return "typeSpecifier"
445451
case .specializeAttributeArgumentList: return "specializeAttributeSpecList"
446452
case .specializeAvailabilityArgument: return "availabilityEntry"
447453
case .specializeTargetFunctionArgument: return "targetFunctionEntry"
@@ -453,8 +459,8 @@ public enum SyntaxNodeKind: String, CaseIterable {
453459
case .typeAliasDecl: return "typealiasDecl"
454460
case .unavailableFromAsyncAttributeArguments: return "unavailableFromAsyncArguments"
455461
case .yieldedExpression: return "yieldExprListElement"
456-
case .yieldedExpressionsClause: return "yieldList"
457462
case .yieldedExpressionList: return "yieldExprList"
463+
case .yieldedExpressionsClause: return "yieldList"
458464
default: return nil
459465
}
460466
}

CodeGeneration/Sources/SyntaxSupport/TypeNodes.swift

+96-2
Original file line numberDiff line numberDiff line change
@@ -496,9 +496,103 @@ public let TYPE_NODES: [Node] = [
496496
),
497497

498498
Node(
499-
kind: .typeSpecifier,
499+
kind: .lifetimeSpecifierArgument,
500500
base: .syntax,
501501
nameForDiagnostics: nil,
502+
documentation: """
503+
A single argument that can be added to a lifetime specifier like `borrow`, `mutate`, `consume` or `copy`.
504+
505+
### Example
506+
`data` in `func foo(data: Array<Item>) -> borrow(data) ComplexReferenceType`
507+
""",
508+
traits: [
509+
"WithTrailingComma"
510+
],
511+
children: [
512+
Child(
513+
name: "parameter",
514+
kind: .token(choices: [.token(.identifier), .keyword(.self), .token(.integerLiteral)]),
515+
documentation: """
516+
The parameter on which the lifetime of this type depends.
517+
518+
This can be an identifier referring to an external parameter name, an integer literal to refer to an unnamed
519+
parameter or `self` if the type's lifetime depends on the object the method is called on.
520+
"""
521+
),
522+
Child(
523+
name: "trailingComma",
524+
kind: .token(choices: [.token(.comma)]),
525+
isOptional: true
526+
),
527+
]
528+
),
529+
530+
Node(
531+
kind: .lifetimeSpecifierArgumentList,
532+
base: .syntaxCollection,
533+
nameForDiagnostics: nil,
534+
elementChoices: [.lifetimeSpecifierArgument]
535+
),
536+
537+
Node(
538+
kind: .lifetimeSpecifierArguments,
539+
base: .syntax,
540+
nameForDiagnostics: nil,
541+
documentation: """
542+
An optional argument passed to a type parameter.
543+
544+
### Example
545+
`borrow(data)` in `func foo(data: Array<Item>) -> borrow(data) ComplexReferenceType`
546+
""",
547+
traits: [
548+
"Parenthesized"
549+
],
550+
children: [
551+
Child(
552+
name: "leftParen",
553+
kind: .token(choices: [.token(.leftParen)])
554+
),
555+
Child(
556+
name: "arguments",
557+
kind: .collection(kind: .lifetimeSpecifierArgumentList, collectionElementName: "Arguments"),
558+
documentation: """
559+
The function parameters that the lifetime of the annotated type depends on.
560+
"""
561+
),
562+
Child(
563+
name: "rightParen",
564+
kind: .token(choices: [.token(.rightParen)])
565+
),
566+
]
567+
),
568+
569+
Node(
570+
kind: .lifetimeTypeSpecifier,
571+
base: .syntax,
572+
nameForDiagnostics: "lifetime specifier",
573+
documentation: "A specifier that specifies function parameter on whose lifetime a type depends",
574+
children: [
575+
Child(
576+
name: "specifier",
577+
kind: .token(choices: [
578+
.keyword(._copy),
579+
.keyword(._consume),
580+
.keyword(._borrow),
581+
.keyword(._mutate),
582+
]),
583+
documentation: "The specifier token that's attached to the type."
584+
),
585+
Child(
586+
name: "arguments",
587+
kind: .node(kind: .lifetimeSpecifierArguments)
588+
),
589+
]
590+
),
591+
592+
Node(
593+
kind: .simpleTypeSpecifier,
594+
base: .syntax,
595+
nameForDiagnostics: "type specifier",
502596
documentation: "A specifier that can be attached to a type to eg. mark a parameter as `inout` or `consuming`",
503597
children: [
504598
Child(
@@ -523,6 +617,6 @@ public let TYPE_NODES: [Node] = [
523617
kind: .typeSpecifierList,
524618
base: .syntaxCollection,
525619
nameForDiagnostics: nil,
526-
elementChoices: [.typeSpecifier]
620+
elementChoices: [.simpleTypeSpecifier, .lifetimeTypeSpecifier]
527621
),
528622
]

Release Notes/600.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@
5858
- Description: Uses heuristics to infer the indentation width used in a syntax tree.
5959
- Pull Request: https://github.com/apple/swift-syntax/pull/2514
6060

61-
- `TypeSpecifierListSyntax` and `TypeSpecifierSyntax`
62-
- Description: `AttributedTypeSyntax` can now contain multiple specifiers and these types are used to model the list of specifiers.
61+
- Type specifiers
62+
- Description: `AttributedTypeSyntax` can now contain multiple specifiers and these types are used to model the list of specifiers. Additionally, type specifiers can now contain arguments, like `borrow(data)`. To facilitate this, the following new types were introduces: `LifetimeSpecifierArgumentListSyntax`, `LifetimeSpecifierArgumentSyntax`, `LifetimeSpecifierArgumentsSyntax`, `LifetimeTypeSpecifierSyntax`, `SimpleTypeSpecifierSyntax`, `TypeSpecifierListSyntax`
6363
- Pull request: https://github.com/apple/swift-syntax/pull/2433
6464

6565
## API Behavior Changes
@@ -87,7 +87,7 @@
8787
- Description: `EditorPlaceholderDeclSyntax` and `EditorPlaceholderExprSyntax` are now deprecated and placeholders are instead parsed as identifiers within a `MissingDeclSyntax` or `DeclReferenceExprSyntax`.
8888
- Pull request: https://github.com/apple/swift-syntax/pull/2237
8989

90-
- `AttributedTypeSyntax.specifier` has renamed and changed to be a collection
90+
- `AttributedTypeSyntax.specifier` has renamed to `specifiers` and changed to be a collection
9191
- Description: Types can have multiple specifiers now and the syntax tree has been modified to reflect that.
9292
- Pull request: https://github.com/apple/swift-syntax/pull/2433
9393

Sources/SwiftParser/Parameters.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ extension Parser {
261261
mutating func parseMisplacedSpecifiers() -> [RawTokenSyntax] {
262262
var misplacedSpecifiers: [RawTokenSyntax] = []
263263
if self.withLookahead({ !$0.startsParameterName(isClosure: false, allowMisplacedSpecifierRecovery: false) }) {
264-
while canHaveParameterSpecifier, let specifier = self.consume(ifAnyIn: TypeSpecifierSyntax.SpecifierOptions.self) {
264+
while canHaveParameterSpecifier, let specifier = self.consume(ifAnyIn: SimpleTypeSpecifierSyntax.SpecifierOptions.self) {
265265
misplacedSpecifiers.append(specifier)
266266
}
267267
}

Sources/SwiftParser/Patterns.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ extension Parser.Lookahead {
340340
mutating func startsParameterName(isClosure: Bool, allowMisplacedSpecifierRecovery: Bool) -> Bool {
341341
if allowMisplacedSpecifierRecovery {
342342
while canHaveParameterSpecifier,
343-
self.consume(ifAnyIn: TypeSpecifierSyntax.SpecifierOptions.self) != nil
343+
self.consume(ifAnyIn: SimpleTypeSpecifierSyntax.SpecifierOptions.self) != nil
344344
{}
345345
}
346346

Sources/SwiftParser/SwiftParserCompatibility.swift

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13+
import SwiftSyntax
14+
1315
// This file provides compatibility aliases to keep dependents of SwiftSyntax building.
1416
// All users of the declarations in this file should transition away from them ASAP.
1517

Sources/SwiftParser/TokenPrecedence.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ enum TokenPrecedence: Comparable {
231231
.__consuming, .final, .required, .optional, .lazy, .dynamic, .infix, .postfix, .prefix, .mutating, .nonmutating, .convenience, .override, .package, .open,
232232
.__setter_access, .indirect, .isolated, .nonisolated, .distributed, ._local,
233233
.inout, ._mutating, ._borrow, ._borrowing, .borrowing, ._consuming, .consuming, .consume, ._resultDependsOnSelf, ._resultDependsOn,
234-
.transferring,
234+
.transferring, ._consume, ._copy, ._mutate,
235235
// Accessors
236236
.get, .set, .didSet, .willSet, .unsafeAddress, .addressWithOwner, .addressWithNativeOwner, .unsafeMutableAddress,
237237
.mutableAddressWithOwner, .mutableAddressWithNativeOwner, ._read, ._modify,

0 commit comments

Comments
 (0)