Skip to content

Commit 75e044d

Browse files
committed
Add initial support for resultDependsOn and resultDependsOnSelf
Add them under a new NonEscapableTypes expermental feature
1 parent 83e12ff commit 75e044d

File tree

16 files changed

+106
-7
lines changed

16 files changed

+106
-7
lines changed

CodeGeneration/Sources/SyntaxSupport/DeclNodes.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,7 @@ public let DECL_NODES: [Node] = [
461461
.keyword(.private),
462462
.keyword(.public),
463463
.keyword(.reasync),
464+
.keyword(.resultDependsOnSelf),
464465
.keyword(.required),
465466
.keyword(.static),
466467
.keyword(.unowned),

CodeGeneration/Sources/SyntaxSupport/ExperimentalFeatures.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public enum ExperimentalFeature: String, CaseIterable {
1717
case thenStatements
1818
case typedThrows
1919
case doExpressions
20+
case nonEscapableTypes
2021

2122
/// The name of the feature, which is used in the doc comment.
2223
public var featureName: String {
@@ -29,6 +30,8 @@ public enum ExperimentalFeature: String, CaseIterable {
2930
return "typed throws"
3031
case .doExpressions:
3132
return "'do' expressions"
33+
case .nonEscapableTypes:
34+
return "NonEscableTypes"
3235
}
3336
}
3437

CodeGeneration/Sources/SyntaxSupport/KeywordSpec.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,8 @@ public enum Keyword: CaseIterable {
239239
case renamed
240240
case `repeat`
241241
case required
242+
case resultDependsOn
243+
case resultDependsOnSelf
242244
case `rethrows`
243245
case retroactive
244246
case `return`
@@ -610,6 +612,10 @@ public enum Keyword: CaseIterable {
610612
return KeywordSpec("repeat", isLexerClassified: true)
611613
case .required:
612614
return KeywordSpec("required")
615+
case .resultDependsOn:
616+
return KeywordSpec("resultDependsOn", experimentalFeature: .nonEscapableTypes)
617+
case .resultDependsOnSelf:
618+
return KeywordSpec("resultDependsOnSelf", experimentalFeature: .nonEscapableTypes)
613619
case .rethrows:
614620
return KeywordSpec("rethrows", isLexerClassified: true)
615621
case .retroactive:

CodeGeneration/Sources/SyntaxSupport/TypeNodes.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ public let TYPE_NODES: [Node] = [
5252
.keyword(._const),
5353
.keyword(.borrowing),
5454
.keyword(.consuming),
55+
.keyword(.resultDependsOn),
5556
]),
5657
isOptional: true
5758
),

Sources/SwiftParser/Declarations.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ extension DeclarationModifier {
1818
case .__consuming, .__setter_access, ._const, ._local, .async,
1919
.borrowing, .class, .consuming, .convenience, .distributed, .dynamic,
2020
.final, .indirect, .infix, .isolated, .lazy, .mutating, .nonisolated,
21-
.nonmutating, .optional, .override, .postfix, .prefix, .reasync,
21+
.nonmutating, .optional, .override, .postfix, .prefix, .reasync, .resultDependsOnSelf,
2222
.required, .rethrows, .static, .weak:
2323
return false
2424
case .fileprivate, .internal, .package, .open, .private,

Sources/SwiftParser/Modifiers.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,8 @@ extension Parser {
8484
(.declarationModifier(._const), let handle)?,
8585
(.declarationModifier(._local), let handle)?,
8686
(.declarationModifier(.__setter_access), let handle)?,
87-
(.declarationModifier(.reasync), let handle)?:
87+
(.declarationModifier(.reasync), let handle)?,
88+
(.declarationModifier(.resultDependsOnSelf), let handle)? where experimentalFeatures.contains(.nonEscapableTypes):
8889
let (unexpectedBeforeKeyword, keyword) = self.eat(handle)
8990
elements.append(RawDeclModifierSyntax(unexpectedBeforeKeyword, name: keyword, detail: nil, arena: self.arena))
9091
case (.declarationModifier(.rethrows), _)?:

Sources/SwiftParser/Patterns.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13-
@_spi(RawSyntax) import SwiftSyntax
13+
@_spi(RawSyntax) @_spi(ExperimentalLanguageFeatures) import SwiftSyntax
1414

1515
extension Parser {
1616
/// Parse a pattern.
@@ -354,6 +354,7 @@ extension Parser.Lookahead {
354354
&& !self.at(.keyword(.__owned))
355355
&& !self.at(.keyword(.borrowing))
356356
&& !self.at(.keyword(.consuming))
357+
&& (!experimentalFeatures.contains(.nonEscapableTypes) || !self.at(.keyword(.resultDependsOn)))
357358
{
358359
return true
359360
}

Sources/SwiftParser/TokenPrecedence.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ enum TokenPrecedence: Comparable {
230230
// Declaration Modifiers
231231
.__consuming, .final, .required, .optional, .lazy, .dynamic, .infix, .postfix, .prefix, .mutating, .nonmutating, .convenience, .override, .package, .open,
232232
.__setter_access, .indirect, .isolated, .nonisolated, .distributed, ._local,
233-
.inout, ._mutating, ._borrow, ._borrowing, .borrowing, ._consuming, .consuming, .consume,
233+
.inout, ._mutating, ._borrow, ._borrowing, .borrowing, ._consuming, .consuming, .consume, .resultDependsOnSelf, .resultDependsOn,
234234
// Accessors
235235
.get, .set, .didSet, .willSet, .unsafeAddress, .addressWithOwner, .addressWithNativeOwner, .unsafeMutableAddress,
236236
.mutableAddressWithOwner, .mutableAddressWithNativeOwner, ._read, ._modify,

Sources/SwiftParser/TokenSpecSet.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,7 @@ enum DeclarationModifier: TokenSpecSet {
376376
case `static`
377377
case unowned
378378
case weak
379+
case resultDependsOnSelf
379380

380381
init?(lexeme: Lexer.Lexeme, experimentalFeatures: Parser.ExperimentalFeatures) {
381382
switch PrepareForKeywordMatch(lexeme) {
@@ -414,6 +415,7 @@ enum DeclarationModifier: TokenSpecSet {
414415
case TokenSpec(.static): self = .static
415416
case TokenSpec(.unowned): self = .unowned
416417
case TokenSpec(.weak): self = .weak
418+
case TokenSpec(.resultDependsOnSelf) where experimentalFeatures.contains(.nonEscapableTypes): self = .resultDependsOnSelf
417419
default: return nil
418420
}
419421
}
@@ -455,6 +457,7 @@ enum DeclarationModifier: TokenSpecSet {
455457
case .static: return .keyword(.static)
456458
case .unowned: return TokenSpec(.unowned, recoveryPrecedence: .declKeyword)
457459
case .weak: return TokenSpec(.weak, recoveryPrecedence: .declKeyword)
460+
case .resultDependsOnSelf: return TokenSpec(.resultDependsOnSelf, recoveryPrecedence: .declKeyword)
458461
}
459462
}
460463
}
@@ -679,6 +682,7 @@ public enum TypeSpecifier: TokenSpecSet {
679682
case shared
680683
case borrowing
681684
case consuming
685+
case resultDependsOn
682686

683687
init?(lexeme: Lexer.Lexeme, experimentalFeatures: Parser.ExperimentalFeatures) {
684688
switch PrepareForKeywordMatch(lexeme) {
@@ -687,6 +691,7 @@ public enum TypeSpecifier: TokenSpecSet {
687691
case TokenSpec(.__shared): self = .shared
688692
case TokenSpec(.consuming): self = .consuming
689693
case TokenSpec(.borrowing): self = .borrowing
694+
case TokenSpec(.resultDependsOn) where experimentalFeatures.contains(.nonEscapableTypes): self = .resultDependsOn
690695
default: return nil
691696
}
692697
}
@@ -699,6 +704,7 @@ public enum TypeSpecifier: TokenSpecSet {
699704
case TokenSpec(.__shared): self = .shared
700705
case TokenSpec(.consuming): self = .shared
701706
case TokenSpec(.borrowing): self = .shared
707+
case TokenSpec(.resultDependsOn): self = .resultDependsOn
702708
default: return nil
703709
}
704710
}
@@ -710,6 +716,7 @@ public enum TypeSpecifier: TokenSpecSet {
710716
case .shared: return .keyword(.__shared)
711717
case .borrowing: return .keyword(.borrowing)
712718
case .consuming: return .keyword(.consuming)
719+
case .resultDependsOn: return .keyword(.resultDependsOn)
713720
}
714721
}
715722
}

Sources/SwiftParser/generated/ExperimentalFeatures.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,7 @@ extension Parser.ExperimentalFeatures {
3535

3636
/// Whether to enable the parsing of 'do' expressions.
3737
public static let doExpressions = Self(rawValue: 1 << 3)
38+
39+
/// Whether to enable the parsing of NonEscableTypes.
40+
public static let nonEscapableTypes = Self(rawValue: 1 << 4)
3841
}

Sources/SwiftParser/generated/Parser+TokenSpecSet.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,8 @@ extension AttributedTypeSyntax {
183183
case _const
184184
case borrowing
185185
case consuming
186+
@_spi(ExperimentalLanguageFeatures)
187+
case resultDependsOn
186188

187189
init?(lexeme: Lexer.Lexeme, experimentalFeatures: Parser.ExperimentalFeatures) {
188190
switch PrepareForKeywordMatch(lexeme) {
@@ -200,6 +202,8 @@ extension AttributedTypeSyntax {
200202
self = .borrowing
201203
case TokenSpec(.consuming):
202204
self = .consuming
205+
case TokenSpec(.resultDependsOn) where experimentalFeatures.contains(.nonEscapableTypes):
206+
self = .resultDependsOn
203207
default:
204208
return nil
205209
}
@@ -221,6 +225,8 @@ extension AttributedTypeSyntax {
221225
return .keyword(.borrowing)
222226
case .consuming:
223227
return .keyword(.consuming)
228+
case .resultDependsOn:
229+
return .keyword(.resultDependsOn)
224230
}
225231
}
226232

@@ -244,6 +250,8 @@ extension AttributedTypeSyntax {
244250
return .keyword(.borrowing)
245251
case .consuming:
246252
return .keyword(.consuming)
253+
case .resultDependsOn:
254+
return .keyword(.resultDependsOn)
247255
}
248256
}
249257
}
@@ -714,6 +722,8 @@ extension DeclModifierSyntax {
714722
case `private`
715723
case `public`
716724
case reasync
725+
@_spi(ExperimentalLanguageFeatures)
726+
case resultDependsOnSelf
717727
case required
718728
case `static`
719729
case unowned
@@ -783,6 +793,8 @@ extension DeclModifierSyntax {
783793
self = .public
784794
case TokenSpec(.reasync):
785795
self = .reasync
796+
case TokenSpec(.resultDependsOnSelf) where experimentalFeatures.contains(.nonEscapableTypes):
797+
self = .resultDependsOnSelf
786798
case TokenSpec(.required):
787799
self = .required
788800
case TokenSpec(.static):
@@ -860,6 +872,8 @@ extension DeclModifierSyntax {
860872
return .keyword(.public)
861873
case .reasync:
862874
return .keyword(.reasync)
875+
case .resultDependsOnSelf:
876+
return .keyword(.resultDependsOnSelf)
863877
case .required:
864878
return .keyword(.required)
865879
case .static:
@@ -939,6 +953,8 @@ extension DeclModifierSyntax {
939953
return .keyword(.public)
940954
case .reasync:
941955
return .keyword(.reasync)
956+
case .resultDependsOnSelf:
957+
return .keyword(.resultDependsOnSelf)
942958
case .required:
943959
return .keyword(.required)
944960
case .static:

Sources/SwiftSyntax/generated/Keyword.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,10 @@ public enum Keyword: UInt8, Hashable {
181181
case renamed
182182
case `repeat`
183183
case required
184+
@_spi(ExperimentalLanguageFeatures)
185+
case resultDependsOn
186+
@_spi(ExperimentalLanguageFeatures)
187+
case resultDependsOnSelf
184188
case `rethrows`
185189
case retroactive
186190
case `return`
@@ -677,6 +681,8 @@ public enum Keyword: UInt8, Hashable {
677681
self = .__setter_access
678682
case "precedencegroup":
679683
self = .precedencegroup
684+
case "resultDependsOn":
685+
self = .resultDependsOn
680686
default:
681687
return nil
682688
}
@@ -713,6 +719,8 @@ public enum Keyword: UInt8, Hashable {
713719
self = ._opaqueReturnTypeOf
714720
case "_PackageDescription":
715721
self = ._PackageDescription
722+
case "resultDependsOnSelf":
723+
self = .resultDependsOnSelf
716724
default:
717725
return nil
718726
}
@@ -942,6 +950,8 @@ public enum Keyword: UInt8, Hashable {
942950
"renamed",
943951
"repeat",
944952
"required",
953+
"resultDependsOn",
954+
"resultDependsOnSelf",
945955
"rethrows",
946956
"retroactive",
947957
"return",

Sources/SwiftSyntax/generated/raw/RawSyntaxValidation.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,8 @@ func validateLayout(layout: RawSyntaxBuffer, as kind: SyntaxKind) {
388388
.keyword("isolated"),
389389
.keyword("_const"),
390390
.keyword("borrowing"),
391-
.keyword("consuming")
391+
.keyword("consuming"),
392+
.keyword("resultDependsOn")
392393
]))
393394
assertNoError(kind, 2, verify(layout[2], as: RawUnexpectedNodesSyntax?.self))
394395
assertNoError(kind, 3, verify(layout[3], as: RawAttributeListSyntax.self))
@@ -804,6 +805,7 @@ func validateLayout(layout: RawSyntaxBuffer, as kind: SyntaxKind) {
804805
.keyword("private"),
805806
.keyword("public"),
806807
.keyword("reasync"),
808+
.keyword("resultDependsOnSelf"),
807809
.keyword("required"),
808810
.keyword("static"),
809811
.keyword("unowned"),

Sources/SwiftSyntax/generated/syntaxNodes/SyntaxNodesAB.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3291,7 +3291,7 @@ public struct AttributeSyntax: SyntaxProtocol, SyntaxHashable, _LeafSyntaxNodePr
32913291

32923292
/// ### Children
32933293
///
3294-
/// - `specifier`: (`inout` | `__shared` | `__owned` | `isolated` | `_const` | `borrowing` | `consuming`)?
3294+
/// - `specifier`: (`inout` | `__shared` | `__owned` | `isolated` | `_const` | `borrowing` | `consuming` | `resultDependsOn`)?
32953295
/// - `attributes`: ``AttributeListSyntax``
32963296
/// - `baseType`: ``TypeSyntax``
32973297
public struct AttributedTypeSyntax: TypeSyntaxProtocol, SyntaxHashable, _LeafTypeSyntaxNodeProtocol {
@@ -3370,6 +3370,7 @@ public struct AttributedTypeSyntax: TypeSyntaxProtocol, SyntaxHashable, _LeafTyp
33703370
/// - `_const`
33713371
/// - `borrowing`
33723372
/// - `consuming`
3373+
/// - `resultDependsOn`
33733374
public var specifier: TokenSyntax? {
33743375
get {
33753376
return Syntax(self).child(at: 1)?.cast(TokenSyntax.self)

Sources/SwiftSyntax/generated/syntaxNodes/SyntaxNodesD.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ public struct DeclModifierDetailSyntax: SyntaxProtocol, SyntaxHashable, _LeafSyn
169169

170170
/// ### Children
171171
///
172-
/// - `name`: (`__consuming` | `__setter_access` | `_const` | `_local` | `actor` | `async` | `borrowing` | `class` | `consuming` | `convenience` | `distributed` | `dynamic` | `fileprivate` | `final` | `indirect` | `infix` | `internal` | `isolated` | `lazy` | `mutating` | `nonisolated` | `nonmutating` | `open` | `optional` | `override` | `package` | `postfix` | `prefix` | `private` | `public` | `reasync` | `required` | `static` | `unowned` | `weak`)
172+
/// - `name`: (`__consuming` | `__setter_access` | `_const` | `_local` | `actor` | `async` | `borrowing` | `class` | `consuming` | `convenience` | `distributed` | `dynamic` | `fileprivate` | `final` | `indirect` | `infix` | `internal` | `isolated` | `lazy` | `mutating` | `nonisolated` | `nonmutating` | `open` | `optional` | `override` | `package` | `postfix` | `prefix` | `private` | `public` | `reasync` | `resultDependsOnSelf` | `required` | `static` | `unowned` | `weak`)
173173
/// - `detail`: ``DeclModifierDetailSyntax``?
174174
///
175175
/// ### Contained in
@@ -270,6 +270,7 @@ public struct DeclModifierSyntax: SyntaxProtocol, SyntaxHashable, _LeafSyntaxNod
270270
/// - `private`
271271
/// - `public`
272272
/// - `reasync`
273+
/// - `resultDependsOnSelf`
273274
/// - `required`
274275
/// - `static`
275276
/// - `unowned`

Tests/SwiftParserTest/DeclarationTests.swift

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3059,4 +3059,50 @@ final class DeclarationTests: ParserTestCase {
30593059
"""
30603060
)
30613061
}
3062+
3063+
func testResultDependsOnSelf() {
3064+
assertParse(
3065+
"""
3066+
class MethodModifiers {
3067+
resultDependsOnSelf func getDependentResult() -> Builtin.NativeObject {
3068+
return Builtin.unsafeCastToNativeObject(self)
3069+
}
3070+
}
3071+
""",
3072+
experimentalFeatures: .nonEscapableTypes
3073+
)
3074+
3075+
assertParse(
3076+
"""
3077+
class MethodModifiers {
3078+
resultDependsOnSelf func resultDependsOnSelf() -> Builtin.NativeObject {
3079+
return Builtin.unsafeCastToNativeObject(self)
3080+
}
3081+
}
3082+
""",
3083+
experimentalFeatures: .nonEscapableTypes
3084+
)
3085+
}
3086+
3087+
func testResultDependsOn() {
3088+
assertParse(
3089+
"""
3090+
class Klass {}
3091+
func testTypeSpecifier(x : resultDependsOn Klass) -> Builtin.NativeObject {
3092+
return Builtin.unsafeCastToNativeObject(x)
3093+
}
3094+
""",
3095+
experimentalFeatures: .nonEscapableTypes
3096+
)
3097+
3098+
assertParse(
3099+
"""
3100+
class Klass {}
3101+
func testMultipleTypeSpecifier(x : resultDependsOn Klass, y : resultDependsOn Klass) -> (Builtin.NativeObject, Builtin.NativeObject) {
3102+
return (Builtin.unsafeCastToNativeObject(x), Builtin.unsafeCastToNativeObject(x))
3103+
}
3104+
""",
3105+
experimentalFeatures: .nonEscapableTypes
3106+
)
3107+
}
30623108
}

0 commit comments

Comments
 (0)