Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add keypath method and initializer syntax under an experimental feature flag keypathWithMethodMembers #2950

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public enum ExperimentalFeature: String, CaseIterable {
case valueGenerics
case abiAttribute
case unsafeExpression
case keypathWithMethodMembers

/// The name of the feature as it is written in the compiler's `Features.def` file.
public var featureName: String {
Expand All @@ -44,6 +45,8 @@ public enum ExperimentalFeature: String, CaseIterable {
return "ABIAttribute"
case .unsafeExpression:
return "WarnUnsafe"
case .keypathWithMethodMembers:
return "KeypathWithMethodMembers"
}
}

Expand All @@ -68,6 +71,8 @@ public enum ExperimentalFeature: String, CaseIterable {
return "@abi attribute"
case .unsafeExpression:
return "'unsafe' expression"
case .keypathWithMethodMembers:
return "keypaths with method members"
}
}

Expand Down
32 changes: 32 additions & 0 deletions CodeGeneration/Sources/SyntaxSupport/ExprNodes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1209,6 +1209,11 @@ public let EXPR_NODES: [Node] = [
name: "property",
kind: .node(kind: .keyPathPropertyComponent)
),
Child(
name: "method",
kind: .node(kind: .keyPathMethodComponent),
experimentalFeature: .keypathWithMethodMembers
),
Child(
name: "subscript",
kind: .node(kind: .keyPathSubscriptComponent)
Expand Down Expand Up @@ -1313,6 +1318,33 @@ public let EXPR_NODES: [Node] = [
]
),

Node(
kind: .keyPathMethodComponent,
base: .syntax,
experimentalFeature: .keypathWithMethodMembers,
nameForDiagnostics: "key path method component",
documentation: "A key path component like `.method()`, `.method(10)`, or `.method(val: 10)`.",
amritpan marked this conversation as resolved.
Show resolved Hide resolved
children: [
Child(
name: "declName",
kind: .node(kind: .declReferenceExpr)
),
Child(
name: "leftParen",
kind: .token(choices: [.token(.leftParen)])
),
Child(
name: "arguments",
kind: .collection(kind: .labeledExprList, collectionElementName: "Argument"),
nameForDiagnostics: "arguments"
),
Child(
name: "rightParen",
kind: .token(choices: [.token(.rightParen)])
),
]
),

Node(
kind: .macroExpansionExpr,
base: .expr,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ public enum SyntaxNodeKind: String, CaseIterable, IdentifierConvertible, TypeCon
case keyPathOptionalComponent
case keyPathPropertyComponent
case keyPathSubscriptComponent
case keyPathMethodComponent
case labeledExpr
case labeledExprList
case labeledSpecializeArgument
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,10 @@ class ValidateSyntaxNodes: XCTestCase {
message: "could conform to trait 'Parenthesized' but does not"
),
ValidationFailure(node: .lifetimeTypeSpecifier, message: "could conform to trait 'Parenthesized' but does not"),
ValidationFailure(
node: .keyPathMethodComponent,
message: "could conform to trait 'Parenthesized' but does not"
),
]
)
}
Expand Down
54 changes: 50 additions & 4 deletions Sources/SwiftParser/Expressions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,15 @@
//
//===----------------------------------------------------------------------===//

#if compiler(>=6)
@_spi(RawSyntax) internal import SwiftSyntax
#if swift(>=6)
@_spi(RawSyntax) @_spi(ExperimentalLanguageFeatures) internal import SwiftSyntax
#else
#if swift(>=5.8)
@_spi(RawSyntax) @_spi(ExperimentalLanguageFeatures) import SwiftSyntax
#else
@_spi(RawSyntax) import SwiftSyntax
#endif
#endif

extension TokenConsumer {
mutating func atStartOfExpression() -> Bool {
Expand Down Expand Up @@ -1103,11 +1107,54 @@ extension Parser {
continue
}

// Check for a .name or .1 suffix.
// Check for a .name, .1, .name(), .name("Kiwi"), .name(fruit:),
// .name(_:), .name(fruit: "Kiwi) suffix.
if self.at(.period) {
let (unexpectedPeriod, period, declName, generics) = parseDottedExpressionSuffix(
previousNode: components.last?.raw ?? rootType?.raw ?? backslash.raw
)

// If fully applied method component, parse as a keypath method.
if self.experimentalFeatures.contains(.keypathWithMethodMembers)
&& self.at(.leftParen)
{
var (unexpectedBeforeLParen, leftParen) = self.expect(.leftParen)
if let generics = generics {
unexpectedBeforeLParen = RawUnexpectedNodesSyntax(
(unexpectedBeforeLParen?.elements ?? []) + [RawSyntax(generics)],
arena: self.arena
)
}
let args = self.parseArgumentListElements(
pattern: pattern,
allowTrailingComma: true
)
let (unexpectedBeforeRParen, rightParen) = self.expect(.rightParen)

components.append(
RawKeyPathComponentSyntax(
unexpectedPeriod,
period: period,
component: .method(
RawKeyPathMethodComponentSyntax(
declName: declName,
unexpectedBeforeLParen,
leftParen: leftParen,
arguments: RawLabeledExprListSyntax(
elements: args,
arena: self.arena
),
unexpectedBeforeRParen,
rightParen: rightParen,
arena: self.arena
)
),
arena: self.arena
)
)
continue
}
// Else, parse as a property.
components.append(
RawKeyPathComponentSyntax(
unexpectedPeriod,
Expand All @@ -1128,7 +1175,6 @@ extension Parser {
// No more postfix expressions.
break
}

return RawKeyPathExprSyntax(
unexpectedBeforeBackslash,
backslash: backslash,
Expand Down
5 changes: 5 additions & 0 deletions Sources/SwiftParser/generated/ExperimentalFeatures.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ extension Parser.ExperimentalFeatures {
/// Whether to enable the parsing of 'unsafe' expression.
public static let unsafeExpression = Self (rawValue: 1 << 8)

/// Whether to enable the parsing of keypaths with method members.
public static let keypathWithMethodMembers = Self (rawValue: 1 << 9)

/// Creates a new value representing the experimental feature with the
/// given name, or returns nil if the name is not recognized.
public init?(name: String) {
Expand All @@ -73,6 +76,8 @@ extension Parser.ExperimentalFeatures {
self = .abiAttribute
case "WarnUnsafe":
self = .unsafeExpression
case "KeypathWithMethodMembers":
self = .keypathWithMethodMembers
default:
return nil
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,8 @@ private func childNameForDiagnostics(_ keyPath: AnyKeyPath) -> String? {
return "generic where clause"
case \KeyPathExprSyntax.root:
return "root"
case \KeyPathMethodComponentSyntax.arguments:
return "arguments"
case \KeyPathSubscriptComponentSyntax.arguments:
return "arguments"
case \LabeledExprSyntax.label:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,8 @@ extension SyntaxKind {
return "key path component"
case .keyPathExpr:
return "key path"
case .keyPathMethodComponent:
return "key path method component"
case .keyPathOptionalComponent:
return "key path optional component"
case .keyPathPropertyComponent:
Expand Down
18 changes: 18 additions & 0 deletions Sources/SwiftSyntax/generated/ChildNameForKeyPath.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1901,6 +1901,24 @@ public func childName(_ keyPath: AnyKeyPath) -> String? {
return "components"
case \KeyPathExprSyntax.unexpectedAfterComponents:
return "unexpectedAfterComponents"
case \KeyPathMethodComponentSyntax.unexpectedBeforeDeclName:
return "unexpectedBeforeDeclName"
case \KeyPathMethodComponentSyntax.declName:
return "declName"
case \KeyPathMethodComponentSyntax.unexpectedBetweenDeclNameAndLeftParen:
return "unexpectedBetweenDeclNameAndLeftParen"
case \KeyPathMethodComponentSyntax.leftParen:
return "leftParen"
case \KeyPathMethodComponentSyntax.unexpectedBetweenLeftParenAndArguments:
return "unexpectedBetweenLeftParenAndArguments"
case \KeyPathMethodComponentSyntax.arguments:
return "arguments"
case \KeyPathMethodComponentSyntax.unexpectedBetweenArgumentsAndRightParen:
return "unexpectedBetweenArgumentsAndRightParen"
case \KeyPathMethodComponentSyntax.rightParen:
return "rightParen"
case \KeyPathMethodComponentSyntax.unexpectedAfterRightParen:
return "unexpectedAfterRightParen"
case \KeyPathOptionalComponentSyntax.unexpectedBeforeQuestionOrExclamationMark:
return "unexpectedBeforeQuestionOrExclamationMark"
case \KeyPathOptionalComponentSyntax.questionOrExclamationMark:
Expand Down
10 changes: 10 additions & 0 deletions Sources/SwiftSyntax/generated/SyntaxAnyVisitor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1294,6 +1294,16 @@ open class SyntaxAnyVisitor: SyntaxVisitor {
visitAnyPost(node._syntaxNode)
}

@_spi(ExperimentalLanguageFeatures)
override open func visit(_ node: KeyPathMethodComponentSyntax) -> SyntaxVisitorContinueKind {
return visitAny(node._syntaxNode)
}

@_spi(ExperimentalLanguageFeatures)
override open func visitPost(_ node: KeyPathMethodComponentSyntax) {
visitAnyPost(node._syntaxNode)
}

override open func visit(_ node: KeyPathOptionalComponentSyntax) -> SyntaxVisitorContinueKind {
return visitAny(node._syntaxNode)
}
Expand Down
1 change: 1 addition & 0 deletions Sources/SwiftSyntax/generated/SyntaxBaseNodes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1672,6 +1672,7 @@ extension Syntax {
.node(KeyPathComponentListSyntax.self),
.node(KeyPathComponentSyntax.self),
.node(KeyPathExprSyntax.self),
.node(KeyPathMethodComponentSyntax.self),
.node(KeyPathOptionalComponentSyntax.self),
.node(KeyPathPropertyComponentSyntax.self),
.node(KeyPathSubscriptComponentSyntax.self),
Expand Down
4 changes: 4 additions & 0 deletions Sources/SwiftSyntax/generated/SyntaxEnum.swift
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,8 @@ public enum SyntaxEnum: Sendable {
case keyPathComponentList(KeyPathComponentListSyntax)
case keyPathComponent(KeyPathComponentSyntax)
case keyPathExpr(KeyPathExprSyntax)
@_spi(ExperimentalLanguageFeatures)
case keyPathMethodComponent(KeyPathMethodComponentSyntax)
case keyPathOptionalComponent(KeyPathOptionalComponentSyntax)
case keyPathPropertyComponent(KeyPathPropertyComponentSyntax)
case keyPathSubscriptComponent(KeyPathSubscriptComponentSyntax)
Expand Down Expand Up @@ -625,6 +627,8 @@ extension Syntax {
return .keyPathComponent(KeyPathComponentSyntax(self)!)
case .keyPathExpr:
return .keyPathExpr(KeyPathExprSyntax(self)!)
case .keyPathMethodComponent:
return .keyPathMethodComponent(KeyPathMethodComponentSyntax(self)!)
case .keyPathOptionalComponent:
return .keyPathOptionalComponent(KeyPathOptionalComponentSyntax(self)!)
case .keyPathPropertyComponent:
Expand Down
4 changes: 4 additions & 0 deletions Sources/SwiftSyntax/generated/SyntaxKind.swift
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,8 @@ public enum SyntaxKind: Sendable {
case keyPathComponentList
case keyPathComponent
case keyPathExpr
@_spi(ExperimentalLanguageFeatures)
case keyPathMethodComponent
case keyPathOptionalComponent
case keyPathPropertyComponent
case keyPathSubscriptComponent
Expand Down Expand Up @@ -750,6 +752,8 @@ public enum SyntaxKind: Sendable {
return KeyPathComponentSyntax.self
case .keyPathExpr:
return KeyPathExprSyntax.self
case .keyPathMethodComponent:
return KeyPathMethodComponentSyntax.self
case .keyPathOptionalComponent:
return KeyPathOptionalComponentSyntax.self
case .keyPathPropertyComponent:
Expand Down
17 changes: 17 additions & 0 deletions Sources/SwiftSyntax/generated/SyntaxRewriter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1179,6 +1179,14 @@ open class SyntaxRewriter {
return ExprSyntax(KeyPathExprSyntax(unsafeCasting: visitChildren(node._syntaxNode)))
}

/// Visit a `KeyPathMethodComponentSyntax`.
/// - Parameter node: the node that is being visited
/// - Returns: the rewritten node
@_spi(ExperimentalLanguageFeatures)
open func visit(_ node: KeyPathMethodComponentSyntax) -> KeyPathMethodComponentSyntax {
return KeyPathMethodComponentSyntax(unsafeCasting: visitChildren(node._syntaxNode))
}

/// Visit a ``KeyPathOptionalComponentSyntax``.
/// - Parameter node: the node that is being visited
/// - Returns: the rewritten node
Expand Down Expand Up @@ -2918,6 +2926,11 @@ open class SyntaxRewriter {
Syntax(visit(KeyPathExprSyntax(unsafeCasting: node)))
}

@inline(never)
private func visitKeyPathMethodComponentSyntaxImpl(_ node: Syntax) -> Syntax {
Syntax(visit(KeyPathMethodComponentSyntax(unsafeCasting: node)))
}

@inline(never)
private func visitKeyPathOptionalComponentSyntaxImpl(_ node: Syntax) -> Syntax {
Syntax(visit(KeyPathOptionalComponentSyntax(unsafeCasting: node)))
Expand Down Expand Up @@ -3914,6 +3927,8 @@ open class SyntaxRewriter {
return self.visitKeyPathComponentSyntaxImpl(_:)
case .keyPathExpr:
return self.visitKeyPathExprSyntaxImpl(_:)
case .keyPathMethodComponent:
return self.visitKeyPathMethodComponentSyntaxImpl(_:)
case .keyPathOptionalComponent:
return self.visitKeyPathOptionalComponentSyntaxImpl(_:)
case .keyPathPropertyComponent:
Expand Down Expand Up @@ -4496,6 +4511,8 @@ open class SyntaxRewriter {
return visitKeyPathComponentSyntaxImpl(node)
case .keyPathExpr:
return visitKeyPathExprSyntaxImpl(node)
case .keyPathMethodComponent:
return visitKeyPathMethodComponentSyntaxImpl(node)
case .keyPathOptionalComponent:
return visitKeyPathOptionalComponentSyntaxImpl(node)
case .keyPathPropertyComponent:
Expand Down
26 changes: 26 additions & 0 deletions Sources/SwiftSyntax/generated/SyntaxVisitor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1888,6 +1888,20 @@ open class SyntaxVisitor {
open func visitPost(_ node: KeyPathExprSyntax) {
}

/// Visiting `KeyPathMethodComponentSyntax` specifically.
/// - Parameter node: the node we are visiting.
/// - Returns: how should we continue visiting.
@_spi(ExperimentalLanguageFeatures)
open func visit(_ node: KeyPathMethodComponentSyntax) -> SyntaxVisitorContinueKind {
return .visitChildren
}

/// The function called after visiting `KeyPathMethodComponentSyntax` and its descendants.
/// - node: the node we just finished visiting.
@_spi(ExperimentalLanguageFeatures)
open func visitPost(_ node: KeyPathMethodComponentSyntax) {
}

/// Visiting ``KeyPathOptionalComponentSyntax`` specifically.
/// - Parameter node: the node we are visiting.
/// - Returns: how should we continue visiting.
Expand Down Expand Up @@ -4733,6 +4747,14 @@ open class SyntaxVisitor {
visitPost(KeyPathExprSyntax(unsafeCasting: node))
}

@inline(never)
private func visitKeyPathMethodComponentSyntaxImpl(_ node: Syntax) {
if visit(KeyPathMethodComponentSyntax(unsafeCasting: node)) == .visitChildren {
visitChildren(node)
}
visitPost(KeyPathMethodComponentSyntax(unsafeCasting: node))
}

@inline(never)
private func visitKeyPathOptionalComponentSyntaxImpl(_ node: Syntax) {
if visit(KeyPathOptionalComponentSyntax(unsafeCasting: node)) == .visitChildren {
Expand Down Expand Up @@ -6125,6 +6147,8 @@ open class SyntaxVisitor {
return self.visitKeyPathComponentSyntaxImpl(_:)
case .keyPathExpr:
return self.visitKeyPathExprSyntaxImpl(_:)
case .keyPathMethodComponent:
return self.visitKeyPathMethodComponentSyntaxImpl(_:)
case .keyPathOptionalComponent:
return self.visitKeyPathOptionalComponentSyntaxImpl(_:)
case .keyPathPropertyComponent:
Expand Down Expand Up @@ -6707,6 +6731,8 @@ open class SyntaxVisitor {
self.visitKeyPathComponentSyntaxImpl(node)
case .keyPathExpr:
self.visitKeyPathExprSyntaxImpl(node)
case .keyPathMethodComponent:
self.visitKeyPathMethodComponentSyntaxImpl(node)
case .keyPathOptionalComponent:
self.visitKeyPathOptionalComponentSyntaxImpl(node)
case .keyPathPropertyComponent:
Expand Down
Loading