Skip to content

Commit

Permalink
Merge pull request #2596 from jckarter/enable-borrowing-switch-frontend
Browse files Browse the repository at this point in the history
Enable `borrowingSwitch` syntax.
  • Loading branch information
jckarter committed May 15, 2024
2 parents 770e0e3 + f69234e commit 3f80354
Show file tree
Hide file tree
Showing 8 changed files with 15 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ public enum ExperimentalFeature: String, CaseIterable {
case doExpressions
case nonescapableTypes
case transferringArgsAndResults
case borrowingSwitch
case trailingComma

/// The name of the feature, which is used in the doc comment.
Expand All @@ -34,8 +33,6 @@ public enum ExperimentalFeature: String, CaseIterable {
return "NonEscableTypes"
case .transferringArgsAndResults:
return "TransferringArgsAndResults"
case .borrowingSwitch:
return "borrowing pattern matching"
case .trailingComma:
return "trailing comma"
}
Expand Down
23 changes: 1 addition & 22 deletions CodeGeneration/Sources/SyntaxSupport/KeywordSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ public struct KeywordSpec {
/// The experimental feature the keyword is part of, or `nil` if this isn't
/// for an experimental feature.
public let experimentalFeature: ExperimentalFeature?
public let experimentalFeature2: ExperimentalFeature?

/// Indicates if the token kind is switched from being an identifier to a keyword in the lexer.
public let isLexerClassified: Bool
Expand Down Expand Up @@ -69,26 +68,6 @@ public struct KeywordSpec {
) {
self.name = name
self.experimentalFeature = experimentalFeature
self.experimentalFeature2 = nil
self.isLexerClassified = isLexerClassified
}

/// Initializes a new `KeywordSpec` instance.
///
/// - Parameters:
/// - name: A name of the keyword.
/// - experimentalFeature: The experimental feature the keyword is part of, or `nil` if this isn't for an experimental feature.
/// - or: A second experimental feature the keyword is also part of, or `nil` if this isn't for an experimental feature.
/// - isLexerClassified: Indicates if the token kind is switched from being an identifier to a keyword in the lexer.
init(
_ name: String,
experimentalFeature: ExperimentalFeature,
or experimentalFeature2: ExperimentalFeature,
isLexerClassified: Bool = false
) {
self.name = name
self.experimentalFeature = experimentalFeature
self.experimentalFeature2 = experimentalFeature2
self.isLexerClassified = isLexerClassified
}
}
Expand Down Expand Up @@ -340,7 +319,7 @@ public enum Keyword: CaseIterable {
case ._borrow:
return KeywordSpec("_borrow")
case ._borrowing:
return KeywordSpec("_borrowing", experimentalFeature: .referenceBindings, or: .borrowingSwitch)
return KeywordSpec("_borrowing")
case ._BridgeObject:
return KeywordSpec("_BridgeObject")
case ._cdecl:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,11 @@ import Utils

func tokenCaseMatch(
_ caseName: TokenSyntax,
experimentalFeature: ExperimentalFeature?,
experimentalFeature2: ExperimentalFeature?
experimentalFeature: ExperimentalFeature?
) -> SwitchCaseSyntax {
var whereClause = ""
if let feature = experimentalFeature {
whereClause += "where experimentalFeatures.contains(.\(feature.token))"
if let feature2 = experimentalFeature2 {
whereClause += " || experimentalFeatures.contains(.\(feature2.token))"
}
}
return "case TokenSpec(.\(caseName))\(raw: whereClause): self = .\(caseName)"
}
Expand Down Expand Up @@ -74,14 +70,12 @@ let parserTokenSpecSetFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
case .keyword(let keyword):
tokenCaseMatch(
keyword.spec.varOrCaseName,
experimentalFeature: keyword.spec.experimentalFeature,
experimentalFeature2: keyword.spec.experimentalFeature2
experimentalFeature: keyword.spec.experimentalFeature
)
case .token(let token):
tokenCaseMatch(
token.spec.varOrCaseName,
experimentalFeature: token.spec.experimentalFeature,
experimentalFeature2: nil
experimentalFeature: token.spec.experimentalFeature
)
}
}
Expand Down
3 changes: 1 addition & 2 deletions Sources/SwiftParser/Patterns.swift
Original file line number Diff line number Diff line change
Expand Up @@ -272,8 +272,7 @@ extension Parser.Lookahead {
// TODO: the other ownership modifiers (borrowing/consuming/mutating) more
// than likely need to be made contextual as well before finalizing their
// grammar.
case ._borrowing where experimentalFeatures.contains(.borrowingSwitch),
.borrowing where experimentalFeatures.contains(.borrowingSwitch):
case ._borrowing, .borrowing:
return peek(isAt: TokenSpec(.identifier, allowAtStartOfLine: false))
default:
// Other keywords can be parsed unconditionally.
Expand Down
5 changes: 1 addition & 4 deletions Sources/SwiftParser/generated/ExperimentalFeatures.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,6 @@ extension Parser.ExperimentalFeatures {
/// Whether to enable the parsing of TransferringArgsAndResults.
public static let transferringArgsAndResults = Self (rawValue: 1 << 4)

/// Whether to enable the parsing of borrowing pattern matching.
public static let borrowingSwitch = Self (rawValue: 1 << 5)

/// Whether to enable the parsing of trailing comma.
public static let trailingComma = Self (rawValue: 1 << 6)
public static let trailingComma = Self (rawValue: 1 << 5)
}
15 changes: 3 additions & 12 deletions Sources/SwiftParser/generated/Parser+TokenSpecSet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2915,9 +2915,6 @@ extension OptionalBindingConditionSyntax {
@_spi(ExperimentalLanguageFeatures)
#endif
case _mutating
#if compiler(>=5.8)
@_spi(ExperimentalLanguageFeatures)
#endif
case _borrowing
#if compiler(>=5.8)
@_spi(ExperimentalLanguageFeatures)
Expand All @@ -2934,7 +2931,7 @@ extension OptionalBindingConditionSyntax {
self = .inout
case TokenSpec(._mutating) where experimentalFeatures.contains(.referenceBindings):
self = ._mutating
case TokenSpec(._borrowing) where experimentalFeatures.contains(.referenceBindings) || experimentalFeatures.contains(.borrowingSwitch):
case TokenSpec(._borrowing):
self = ._borrowing
case TokenSpec(._consuming) where experimentalFeatures.contains(.referenceBindings):
self = ._consuming
Expand Down Expand Up @@ -3897,9 +3894,6 @@ extension ValueBindingPatternSyntax {
@_spi(ExperimentalLanguageFeatures)
#endif
case _mutating
#if compiler(>=5.8)
@_spi(ExperimentalLanguageFeatures)
#endif
case _borrowing
#if compiler(>=5.8)
@_spi(ExperimentalLanguageFeatures)
Expand All @@ -3917,7 +3911,7 @@ extension ValueBindingPatternSyntax {
self = .inout
case TokenSpec(._mutating) where experimentalFeatures.contains(.referenceBindings):
self = ._mutating
case TokenSpec(._borrowing) where experimentalFeatures.contains(.referenceBindings) || experimentalFeatures.contains(.borrowingSwitch):
case TokenSpec(._borrowing):
self = ._borrowing
case TokenSpec(._consuming) where experimentalFeatures.contains(.referenceBindings):
self = ._consuming
Expand Down Expand Up @@ -4003,9 +3997,6 @@ extension VariableDeclSyntax {
@_spi(ExperimentalLanguageFeatures)
#endif
case _mutating
#if compiler(>=5.8)
@_spi(ExperimentalLanguageFeatures)
#endif
case _borrowing
#if compiler(>=5.8)
@_spi(ExperimentalLanguageFeatures)
Expand All @@ -4022,7 +4013,7 @@ extension VariableDeclSyntax {
self = .inout
case TokenSpec(._mutating) where experimentalFeatures.contains(.referenceBindings):
self = ._mutating
case TokenSpec(._borrowing) where experimentalFeatures.contains(.referenceBindings) || experimentalFeatures.contains(.borrowingSwitch):
case TokenSpec(._borrowing):
self = ._borrowing
case TokenSpec(._consuming) where experimentalFeatures.contains(.referenceBindings):
self = ._consuming
Expand Down
3 changes: 0 additions & 3 deletions Sources/SwiftSyntax/generated/Keyword.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@ public enum Keyword: UInt8, Hashable, Sendable {
case _alignment
case _backDeploy
case _borrow
#if compiler(>=5.8)
@_spi(ExperimentalLanguageFeatures)
#endif
case _borrowing
case _BridgeObject
case _cdecl
Expand Down
18 changes: 6 additions & 12 deletions Tests/SwiftParserTest/translated/MatchingPatternsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -616,8 +616,7 @@ final class MatchingPatternsTests: ParserTestCase {
break
}
""",
substructure: ExprSyntax(DeclReferenceExprSyntax(baseName: .identifier("borrowing"))),
experimentalFeatures: .borrowingSwitch
substructure: ExprSyntax(DeclReferenceExprSyntax(baseName: .identifier("borrowing")))
)

assertParse(
Expand All @@ -627,8 +626,7 @@ final class MatchingPatternsTests: ParserTestCase {
break
}
""",
substructure: ExprSyntax(DeclReferenceExprSyntax(baseName: .identifier("borrowing"))),
experimentalFeatures: .borrowingSwitch
substructure: ExprSyntax(DeclReferenceExprSyntax(baseName: .identifier("borrowing")))
)

assertParse(
Expand Down Expand Up @@ -692,8 +690,7 @@ final class MatchingPatternsTests: ParserTestCase {
bindingSpecifier: .keyword(.let),
pattern: PatternSyntax(IdentifierPatternSyntax(identifier: .identifier("borrowing")))
)
),
experimentalFeatures: .borrowingSwitch
)
)
assertParse(
"""
Expand All @@ -702,8 +699,7 @@ final class MatchingPatternsTests: ParserTestCase {
break
}
""",
substructure: ExprSyntax(DeclReferenceExprSyntax(baseName: .identifier("borrowing"))),
experimentalFeatures: .borrowingSwitch
substructure: ExprSyntax(DeclReferenceExprSyntax(baseName: .identifier("borrowing")))
)
assertParse(
"""
Expand All @@ -717,8 +713,7 @@ final class MatchingPatternsTests: ParserTestCase {
bindingSpecifier: .keyword(.let),
pattern: PatternSyntax(IdentifierPatternSyntax(identifier: .identifier("borrowing")))
)
),
experimentalFeatures: .borrowingSwitch
)
)
assertParse(
"""
Expand All @@ -727,8 +722,7 @@ final class MatchingPatternsTests: ParserTestCase {
break
}
""",
substructure: ExprSyntax(DeclReferenceExprSyntax(baseName: .identifier("borrowing"))),
experimentalFeatures: .borrowingSwitch
substructure: ExprSyntax(DeclReferenceExprSyntax(baseName: .identifier("borrowing")))
)
}
}

0 comments on commit 3f80354

Please sign in to comment.