Skip to content

Commit aee7a84

Browse files
committed
Enable borrowingSwitch syntax.
1 parent d722cfc commit aee7a84

File tree

10 files changed

+45
-75
lines changed

10 files changed

+45
-75
lines changed

CodeGeneration/Sources/SyntaxSupport/ExperimentalFeatures.swift

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ public enum ExperimentalFeature: String, CaseIterable {
1818
case doExpressions
1919
case nonescapableTypes
2020
case transferringArgsAndResults
21-
case borrowingSwitch
2221

2322
/// The name of the feature, which is used in the doc comment.
2423
public var featureName: String {
@@ -33,8 +32,6 @@ public enum ExperimentalFeature: String, CaseIterable {
3332
return "NonEscableTypes"
3433
case .transferringArgsAndResults:
3534
return "TransferringArgsAndResults"
36-
case .borrowingSwitch:
37-
return "borrowing pattern matching"
3835
}
3936
}
4037

CodeGeneration/Sources/SyntaxSupport/KeywordSpec.swift

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ public struct KeywordSpec {
1919
/// The experimental feature the keyword is part of, or `nil` if this isn't
2020
/// for an experimental feature.
2121
public let experimentalFeature: ExperimentalFeature?
22-
public let experimentalFeature2: ExperimentalFeature?
2322

2423
/// Indicates if the token kind is switched from being an identifier to a keyword in the lexer.
2524
public let isLexerClassified: Bool
@@ -69,26 +68,6 @@ public struct KeywordSpec {
6968
) {
7069
self.name = name
7170
self.experimentalFeature = experimentalFeature
72-
self.experimentalFeature2 = nil
73-
self.isLexerClassified = isLexerClassified
74-
}
75-
76-
/// Initializes a new `KeywordSpec` instance.
77-
///
78-
/// - Parameters:
79-
/// - name: A name of the keyword.
80-
/// - experimentalFeature: The experimental feature the keyword is part of, or `nil` if this isn't for an experimental feature.
81-
/// - or: A second experimental feature the keyword is also part of, or `nil` if this isn't for an experimental feature.
82-
/// - isLexerClassified: Indicates if the token kind is switched from being an identifier to a keyword in the lexer.
83-
init(
84-
_ name: String,
85-
experimentalFeature: ExperimentalFeature,
86-
or experimentalFeature2: ExperimentalFeature,
87-
isLexerClassified: Bool = false
88-
) {
89-
self.name = name
90-
self.experimentalFeature = experimentalFeature
91-
self.experimentalFeature2 = experimentalFeature2
9271
self.isLexerClassified = isLexerClassified
9372
}
9473
}
@@ -340,7 +319,7 @@ public enum Keyword: CaseIterable {
340319
case ._borrow:
341320
return KeywordSpec("_borrow")
342321
case ._borrowing:
343-
return KeywordSpec("_borrowing", experimentalFeature: .referenceBindings, or: .borrowingSwitch)
322+
return KeywordSpec("_borrowing", experimentalFeature: .referenceBindings)
344323
case ._BridgeObject:
345324
return KeywordSpec("_BridgeObject")
346325
case ._cdecl:

CodeGeneration/Sources/SyntaxSupport/PatternNodes.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ public let PATTERN_NODES: [Node] = [
201201
kind: .token(choices: [
202202
.keyword(.let), .keyword(.var), .keyword(.inout),
203203
.keyword(._mutating), .keyword(._borrowing), .keyword(._consuming),
204+
.keyword(.borrowing),
204205
])
205206
),
206207
Child(

CodeGeneration/Sources/generate-swift-syntax/templates/swiftparser/ParserTokenSpecSetFile.swift

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,11 @@ import Utils
1717

1818
func tokenCaseMatch(
1919
_ caseName: TokenSyntax,
20-
experimentalFeature: ExperimentalFeature?,
21-
experimentalFeature2: ExperimentalFeature?
20+
experimentalFeature: ExperimentalFeature?
2221
) -> SwitchCaseSyntax {
2322
var whereClause = ""
2423
if let feature = experimentalFeature {
2524
whereClause += "where experimentalFeatures.contains(.\(feature.token))"
26-
if let feature2 = experimentalFeature2 {
27-
whereClause += " || experimentalFeatures.contains(.\(feature2.token))"
28-
}
2925
}
3026
return "case TokenSpec(.\(caseName))\(raw: whereClause): self = .\(caseName)"
3127
}
@@ -74,14 +70,12 @@ let parserTokenSpecSetFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
7470
case .keyword(let keyword):
7571
tokenCaseMatch(
7672
keyword.spec.varOrCaseName,
77-
experimentalFeature: keyword.spec.experimentalFeature,
78-
experimentalFeature2: keyword.spec.experimentalFeature2
73+
experimentalFeature: keyword.spec.experimentalFeature
7974
)
8075
case .token(let token):
8176
tokenCaseMatch(
8277
token.spec.varOrCaseName,
83-
experimentalFeature: token.spec.experimentalFeature,
84-
experimentalFeature2: nil
78+
experimentalFeature: token.spec.experimentalFeature
8579
)
8680
}
8781
}

Sources/SwiftParser/Patterns.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ extension Parser.Lookahead {
268268
// TODO: the other ownership modifiers (borrowing/consuming/mutating) more
269269
// than likely need to be made contextual as well before finalizing their
270270
// grammar.
271-
case ._borrowing where experimentalFeatures.contains(.borrowingSwitch):
271+
case ._borrowing, .borrowing:
272272
return peek(isAt: TokenSpec(.identifier, allowAtStartOfLine: false))
273273
default:
274274
// Other keywords can be parsed unconditionally.

Sources/SwiftParser/generated/ExperimentalFeatures.swift

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

3939
/// Whether to enable the parsing of TransferringArgsAndResults.
4040
public static let transferringArgsAndResults = Self (rawValue: 1 << 4)
41-
42-
/// Whether to enable the parsing of borrowing pattern matching.
43-
public static let borrowingSwitch = Self (rawValue: 1 << 5)
4441
}

Sources/SwiftParser/generated/Parser+TokenSpecSet.swift

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2934,7 +2934,7 @@ extension OptionalBindingConditionSyntax {
29342934
self = .inout
29352935
case TokenSpec(._mutating) where experimentalFeatures.contains(.referenceBindings):
29362936
self = ._mutating
2937-
case TokenSpec(._borrowing) where experimentalFeatures.contains(.referenceBindings) || experimentalFeatures.contains(.borrowingSwitch):
2937+
case TokenSpec(._borrowing) where experimentalFeatures.contains(.referenceBindings):
29382938
self = ._borrowing
29392939
case TokenSpec(._consuming) where experimentalFeatures.contains(.referenceBindings):
29402940
self = ._consuming
@@ -3905,6 +3905,7 @@ extension ValueBindingPatternSyntax {
39053905
@_spi(ExperimentalLanguageFeatures)
39063906
#endif
39073907
case _consuming
3908+
case borrowing
39083909

39093910
init?(lexeme: Lexer.Lexeme, experimentalFeatures: Parser.ExperimentalFeatures) {
39103911
switch PrepareForKeywordMatch(lexeme) {
@@ -3916,10 +3917,12 @@ extension ValueBindingPatternSyntax {
39163917
self = .inout
39173918
case TokenSpec(._mutating) where experimentalFeatures.contains(.referenceBindings):
39183919
self = ._mutating
3919-
case TokenSpec(._borrowing) where experimentalFeatures.contains(.referenceBindings) || experimentalFeatures.contains(.borrowingSwitch):
3920+
case TokenSpec(._borrowing) where experimentalFeatures.contains(.referenceBindings):
39203921
self = ._borrowing
39213922
case TokenSpec(._consuming) where experimentalFeatures.contains(.referenceBindings):
39223923
self = ._consuming
3924+
case TokenSpec(.borrowing):
3925+
self = .borrowing
39233926
default:
39243927
return nil
39253928
}
@@ -3939,6 +3942,8 @@ extension ValueBindingPatternSyntax {
39393942
self = ._borrowing
39403943
case TokenSpec(._consuming):
39413944
self = ._consuming
3945+
case TokenSpec(.borrowing):
3946+
self = .borrowing
39423947
default:
39433948
return nil
39443949
}
@@ -3958,6 +3963,8 @@ extension ValueBindingPatternSyntax {
39583963
return .keyword(._borrowing)
39593964
case ._consuming:
39603965
return .keyword(._consuming)
3966+
case .borrowing:
3967+
return .keyword(.borrowing)
39613968
}
39623969
}
39633970

@@ -3979,6 +3986,8 @@ extension ValueBindingPatternSyntax {
39793986
return .keyword(._borrowing)
39803987
case ._consuming:
39813988
return .keyword(._consuming)
3989+
case .borrowing:
3990+
return .keyword(.borrowing)
39823991
}
39833992
}
39843993
}
@@ -4013,7 +4022,7 @@ extension VariableDeclSyntax {
40134022
self = .inout
40144023
case TokenSpec(._mutating) where experimentalFeatures.contains(.referenceBindings):
40154024
self = ._mutating
4016-
case TokenSpec(._borrowing) where experimentalFeatures.contains(.referenceBindings) || experimentalFeatures.contains(.borrowingSwitch):
4025+
case TokenSpec(._borrowing) where experimentalFeatures.contains(.referenceBindings):
40174026
self = ._borrowing
40184027
case TokenSpec(._consuming) where experimentalFeatures.contains(.referenceBindings):
40194028
self = ._consuming

Sources/SwiftSyntax/generated/raw/RawSyntaxValidation.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2706,7 +2706,8 @@ func validateLayout(layout: RawSyntaxBuffer, as kind: SyntaxKind) {
27062706
.keyword("inout"),
27072707
.keyword("_mutating"),
27082708
.keyword("_borrowing"),
2709-
.keyword("_consuming")
2709+
.keyword("_consuming"),
2710+
.keyword("borrowing")
27102711
]))
27112712
assertNoError(kind, 2, verify(layout[2], as: RawUnexpectedNodesSyntax?.self))
27122713
assertNoError(kind, 3, verify(layout[3], as: RawPatternSyntax.self))

Sources/SwiftSyntax/generated/syntaxNodes/SyntaxNodesTUVWXYZ.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3256,7 +3256,7 @@ public struct UnresolvedTernaryExprSyntax: ExprSyntaxProtocol, SyntaxHashable, _
32563256

32573257
/// ### Children
32583258
///
3259-
/// - `bindingSpecifier`: (`let` | `var` | `inout` | `_mutating` | `_borrowing` | `_consuming`)
3259+
/// - `bindingSpecifier`: (`let` | `var` | `inout` | `_mutating` | `_borrowing` | `_consuming` | `borrowing`)
32603260
/// - `pattern`: ``PatternSyntax``
32613261
public struct ValueBindingPatternSyntax: PatternSyntaxProtocol, SyntaxHashable, _LeafPatternSyntaxNodeProtocol {
32623262
public let _syntaxNode: Syntax
@@ -3327,6 +3327,7 @@ public struct ValueBindingPatternSyntax: PatternSyntaxProtocol, SyntaxHashable,
33273327
/// - `_mutating`
33283328
/// - `_borrowing`
33293329
/// - `_consuming`
3330+
/// - `borrowing`
33303331
public var bindingSpecifier: TokenSyntax {
33313332
get {
33323333
return Syntax(self).child(at: 1)!.cast(TokenSyntax.self)

Tests/SwiftParserTest/translated/MatchingPatternsTests.swift

Lines changed: 23 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -612,126 +612,117 @@ final class MatchingPatternsTests: ParserTestCase {
612612
assertParse(
613613
"""
614614
switch 42 {
615-
case _borrowing .foo(): // parses as `_borrowing.foo()` as before
615+
case borrowing .foo(): // parses as `borrowing.foo()` as before
616616
break
617617
}
618618
""",
619-
substructure: ExprSyntax(DeclReferenceExprSyntax(baseName: .identifier("_borrowing"))),
620-
experimentalFeatures: .borrowingSwitch
619+
substructure: ExprSyntax(DeclReferenceExprSyntax(baseName: .identifier("borrowing")))
621620
)
622621

623622
assertParse(
624623
"""
625624
switch 42 {
626-
case _borrowing (): // parses as `_borrowing()` as before
625+
case borrowing (): // parses as `borrowing()` as before
627626
break
628627
}
629628
""",
630-
substructure: ExprSyntax(DeclReferenceExprSyntax(baseName: .identifier("_borrowing"))),
631-
experimentalFeatures: .borrowingSwitch
629+
substructure: ExprSyntax(DeclReferenceExprSyntax(baseName: .identifier("borrowing")))
632630
)
633631

634632
assertParse(
635633
"""
636634
switch 42 {
637-
case _borrowing x: // parses as binding
635+
case borrowing x: // parses as binding
638636
break
639637
}
640638
""",
641639
substructure: PatternSyntax(
642640
ValueBindingPatternSyntax(
643-
bindingSpecifier: .keyword(._borrowing),
641+
bindingSpecifier: .keyword(.borrowing),
644642
pattern: PatternSyntax(IdentifierPatternSyntax(identifier: .identifier("x")))
645643
)
646-
),
647-
experimentalFeatures: .borrowingSwitch
644+
)
648645
)
649646

650647
assertParse(
651648
"""
652649
switch bar {
653-
case .payload(_borrowing x): // parses as binding
650+
case .payload(borrowing x): // parses as binding
654651
break
655652
}
656653
""",
657654
substructure: PatternSyntax(
658655
ValueBindingPatternSyntax(
659-
bindingSpecifier: .keyword(._borrowing),
656+
bindingSpecifier: .keyword(.borrowing),
660657
pattern: PatternSyntax(IdentifierPatternSyntax(identifier: .identifier("x")))
661658
)
662-
),
663-
experimentalFeatures: .borrowingSwitch
659+
)
664660
)
665661

666662
assertParse(
667663
"""
668664
switch bar {
669-
case _borrowing x.member: // parses as var introducer surrounding postfix expression (which never is valid)
665+
case borrowing x.member: // parses as var introducer surrounding postfix expression (which never is valid)
670666
break
671667
}
672668
""",
673669
substructure: PatternSyntax(
674670
ValueBindingPatternSyntax(
675-
bindingSpecifier: .keyword(._borrowing),
671+
bindingSpecifier: .keyword(.borrowing),
676672
pattern: ExpressionPatternSyntax(
677673
expression: MemberAccessExprSyntax(
678674
base: DeclReferenceExprSyntax(baseName: .identifier("x")),
679675
declName: DeclReferenceExprSyntax(baseName: .identifier("member"))
680676
)
681677
)
682678
)
683-
),
684-
experimentalFeatures: .borrowingSwitch
679+
)
685680
)
686681
assertParse(
687682
"""
688683
switch 42 {
689-
case let _borrowing: // parses as let binding named '_borrowing'
684+
case let borrowing: // parses as let binding named 'borrowing'
690685
break
691686
}
692687
""",
693688
substructure: PatternSyntax(
694689
ValueBindingPatternSyntax(
695690
bindingSpecifier: .keyword(.let),
696-
pattern: PatternSyntax(IdentifierPatternSyntax(identifier: .identifier("_borrowing")))
691+
pattern: PatternSyntax(IdentifierPatternSyntax(identifier: .identifier("borrowing")))
697692
)
698-
),
699-
experimentalFeatures: .borrowingSwitch
693+
)
700694
)
701695
assertParse(
702696
"""
703697
switch 42 {
704-
case _borrowing + _borrowing: // parses as expr pattern
698+
case borrowing + borrowing: // parses as expr pattern
705699
break
706700
}
707701
""",
708-
substructure: ExprSyntax(DeclReferenceExprSyntax(baseName: .identifier("_borrowing"))),
709-
experimentalFeatures: .borrowingSwitch
702+
substructure: ExprSyntax(DeclReferenceExprSyntax(baseName: .identifier("borrowing")))
710703
)
711704
assertParse(
712705
"""
713706
switch 42 {
714-
case _borrowing(let _borrowing): // parses as let binding named '_borrowing' inside a case pattern named 'borrowing'
707+
case borrowing(let borrowing): // parses as let binding named 'borrowing' inside a case pattern named 'borrowing'
715708
break
716709
}
717710
""",
718711
substructure: PatternSyntax(
719712
ValueBindingPatternSyntax(
720713
bindingSpecifier: .keyword(.let),
721-
pattern: PatternSyntax(IdentifierPatternSyntax(identifier: .identifier("_borrowing")))
714+
pattern: PatternSyntax(IdentifierPatternSyntax(identifier: .identifier("borrowing")))
722715
)
723-
),
724-
experimentalFeatures: .borrowingSwitch
716+
)
725717
)
726718
assertParse(
727719
"""
728720
switch 42 {
729-
case {}(_borrowing + _borrowing): // parses as expr pattern
721+
case {}(borrowing + borrowing): // parses as expr pattern
730722
break
731723
}
732724
""",
733-
substructure: ExprSyntax(DeclReferenceExprSyntax(baseName: .identifier("_borrowing"))),
734-
experimentalFeatures: .borrowingSwitch
725+
substructure: ExprSyntax(DeclReferenceExprSyntax(baseName: .identifier("borrowing")))
735726
)
736727
}
737728
}

0 commit comments

Comments
 (0)