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

Revert "Replace expression in ClosureCaptureSyntax with initializer clause" #2793

Closed
wants to merge 1 commit into from
Closed
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
12 changes: 9 additions & 3 deletions CodeGeneration/Sources/SyntaxSupport/ExprNodes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -341,13 +341,19 @@ public let EXPR_NODES: [Node] = [
),
Child(
name: "name",
kind: .token(choices: [.token(.identifier), .keyword(.self)])
kind: .token(choices: [.token(.identifier)]),
isOptional: true
),
Child(
name: "initializer",
kind: .node(kind: .initializerClause),
name: "equal",
deprecatedName: "assignToken",
kind: .token(choices: [.token(.equal)]),
isOptional: true
),
Child(
name: "expression",
kind: .node(kind: .expr)
),
Child(
name: "trailingComma",
kind: .token(choices: [.token(.comma)]),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -482,22 +482,22 @@ class ValidateSyntaxNodes: XCTestCase {
ValidationFailure(
node: .enumCaseElement,
message:
"child 'rawValue' is named inconsistently with 'ClosureCaptureSyntax.initializer', which has the same type ('InitializerClauseSyntax')"
"child 'rawValue' is named inconsistently with 'MatchingPatternConditionSyntax.initializer', which has the same type ('InitializerClauseSyntax')"
),
ValidationFailure(
node: .enumCaseParameter,
message:
"child 'defaultValue' is named inconsistently with 'ClosureCaptureSyntax.initializer', which has the same type ('InitializerClauseSyntax')"
"child 'defaultValue' is named inconsistently with 'MatchingPatternConditionSyntax.initializer', which has the same type ('InitializerClauseSyntax')"
),
ValidationFailure(
node: .functionParameter,
message:
"child 'defaultValue' is named inconsistently with 'ClosureCaptureSyntax.initializer', which has the same type ('InitializerClauseSyntax')"
"child 'defaultValue' is named inconsistently with 'MatchingPatternConditionSyntax.initializer', which has the same type ('InitializerClauseSyntax')"
),
ValidationFailure(
node: .macroDecl,
message:
"child 'definition' is named inconsistently with 'ClosureCaptureSyntax.initializer', which has the same type ('InitializerClauseSyntax')"
"child 'definition' is named inconsistently with 'MatchingPatternConditionSyntax.initializer', which has the same type ('InitializerClauseSyntax')"
),
// MARK: Miscellaneous
ValidationFailure(
Expand Down
10 changes: 1 addition & 9 deletions Release Notes/601.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,28 +31,20 @@
- Description: `IncrementalEdit` is being dropped for `SourceEdit`. `SourceEdit` has deprecated compatibility layers to make it API-compatible with `IncrementalEdit`
- Issue: https://github.com/apple/swift-syntax/issues/2532
- Pull request: https://github.com/apple/swift-syntax/pull/2604

- `ClosureCaptureSyntax.init(leadingTrivia:specifier:name:equal:expression:trailingComma:trailingTrivia:)` deprecated in favor of a new `ClosureCaptureSyntax.init(leadingTrivia:_:specifier:_:name:_:initializer:_:trailingComma:_:trailingTrivia:)` initializer.
- Description: `ClosureCaptureSyntax` now has an `initializer` property instead of `equal` and `expression`. Additionally, the `name` property is no longer optional.
- Pull request: https://github.com/swiftlang/swift-syntax/pull/2763

## API-Incompatible Changes

- Moved `Radix` and `IntegerLiteralExprSyntax.radix` from `SwiftRefactor` to `SwiftSyntax`.
- Description: Allows retrieving the radix value from the `literal.text`.
- Issue: https://github.com/apple/swift-syntax/issues/405
- Pull Request: https://github.com/apple/swift-syntax/pull/2605

- `FixIt.Change` gained a new case `replaceChild(data:)`.
- Description: The new case covers the replacement of a child node with another node.
- Issue: https://github.com/swiftlang/swift-syntax/issues/2205
- Pull Request: https://github.com/swiftlang/swift-syntax/pull/2758
- Migration steps: In exhaustive switches over `FixIt.Change`, cover the new case.

- `ClosureCaptureSyntax.name` is no longer optional.
- Description: Due to the new `ClosureCaptureSyntax` node structure, `name` property is non-optional.
- Pull request: https://github.com/swiftlang/swift-syntax/pull/2763

## Template

- *Affected API or two word description*
Expand Down
7 changes: 6 additions & 1 deletion Sources/SwiftLexicalLookup/IdentifiableSyntax.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,12 @@ import SwiftSyntax

@_spi(Experimental) extension ClosureCaptureSyntax: IdentifiableSyntax {
@_spi(Experimental) public var identifier: TokenSyntax {
name
/* Doesn't work with closures like:
_ = { [y=1+2] in
print(y)
}
*/
expression.as(DeclReferenceExprSyntax.self)!.baseName
}
}

Expand Down
45 changes: 27 additions & 18 deletions Sources/SwiftParser/Expressions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1615,7 +1615,7 @@ extension Parser {
}

extension Parser {
mutating func parseInitializerClause() -> RawInitializerClauseSyntax {
mutating func parseDefaultArgument() -> RawInitializerClauseSyntax {
let unexpectedBeforeEq: RawUnexpectedNodesSyntax?
let eq: RawTokenSyntax
if let comparison = self.consumeIfContextualPunctuator("==") {
Expand Down Expand Up @@ -1704,22 +1704,29 @@ extension Parser {
let specifier = self.parseClosureCaptureSpecifiers()

// The thing being capture specified is an identifier, or as an identifier
// followed by an initializer clause.
let (unexpectedBeforeName, name) = self.expect(
.identifier,
TokenSpec(.self),
default: .identifier
)

let initializer: RawInitializerClauseSyntax?
if self.at(.equal) {
// The name is a new declaration with
// initializer clause.
initializer = self.parseInitializerClause()
// followed by an expression.
let unexpectedBeforeName: RawUnexpectedNodesSyntax?
let name: RawTokenSyntax?
let unexpectedBeforeEqual: RawUnexpectedNodesSyntax?
let equal: RawTokenSyntax?
let expression: RawExprSyntax
if self.peek(isAt: .equal) {
// The name is a new declaration.
(unexpectedBeforeName, name) = self.expect(
.identifier,
TokenSpec(.self, remapping: .identifier),
default: .identifier
)
(unexpectedBeforeEqual, equal) = self.expect(.equal)
expression = self.parseExpression(flavor: .basic, pattern: .none)
} else {
// This is the simple case - the identifier is the name and
// the initializer clause is empty.
initializer = nil
// This is the simple case - the identifier is both the name and
// the expression to capture.
unexpectedBeforeName = nil
name = nil
unexpectedBeforeEqual = nil
equal = nil
expression = RawExprSyntax(self.parseIdentifierExpression(flavor: .basic))
}

keepGoing = self.consume(if: .comma)
Expand All @@ -1728,9 +1735,11 @@ extension Parser {
specifier: specifier,
unexpectedBeforeName,
name: name,
initializer: initializer,
unexpectedBeforeEqual,
equal: equal,
expression: expression,
trailingComma: keepGoing,
arena: arena
arena: self.arena
)
)
} while keepGoing != nil && self.hasProgressed(&loopProgress)
Expand Down
4 changes: 2 additions & 2 deletions Sources/SwiftParser/Parameters.swift
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ extension Parser {

let defaultValue: RawInitializerClauseSyntax?
if self.at(.equal) || self.atContextualPunctuator("==") {
defaultValue = self.parseInitializerClause()
defaultValue = self.parseDefaultArgument()
} else {
defaultValue = nil
}
Expand Down Expand Up @@ -234,7 +234,7 @@ extension Parser {

let defaultValue: RawInitializerClauseSyntax?
if self.at(.equal) || self.atContextualPunctuator("==") {
defaultValue = self.parseInitializerClause()
defaultValue = self.parseDefaultArgument()
} else {
defaultValue = nil
}
Expand Down
52 changes: 0 additions & 52 deletions Sources/SwiftParser/generated/Parser+TokenSpecSet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -560,58 +560,6 @@ extension ClosureCaptureSpecifierSyntax {
}
}

extension ClosureCaptureSyntax {
@_spi(Diagnostics)
public enum NameOptions: TokenSpecSet {
case identifier
case `self`

init?(lexeme: Lexer.Lexeme, experimentalFeatures: Parser.ExperimentalFeatures) {
switch PrepareForKeywordMatch(lexeme) {
case TokenSpec(.identifier):
self = .identifier
case TokenSpec(.self):
self = .self
default:
return nil
}
}

public init?(token: TokenSyntax) {
switch token {
case TokenSpec(.identifier):
self = .identifier
case TokenSpec(.self):
self = .self
default:
return nil
}
}

var spec: TokenSpec {
switch self {
case .identifier:
return .identifier
case .self:
return .keyword(.self)
}
}

/// Returns a token that satisfies the `TokenSpec` of this case.
///
/// If the token kind of this spec has variable text, e.g. for an identifier, this returns a token with empty text.
@_spi(Diagnostics)
public var tokenSyntax: TokenSyntax {
switch self {
case .identifier:
return .identifier("")
case .self:
return .keyword(.self)
}
}
}
}

extension ClosureParameterSyntax {
@_spi(Diagnostics)
public enum FirstNameOptions: TokenSpecSet {
Expand Down
27 changes: 27 additions & 0 deletions Sources/SwiftSyntax/Convenience.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,33 @@
//
//===----------------------------------------------------------------------===//

extension ClosureCaptureSyntax {

/// Creates a ``ClosureCaptureSyntax`` with a `name`, and automatically adds an `equal` token to it since the name is non-optional.
///
/// - SeeAlso: ``ClosureCaptureSyntax/init(leadingTrivia:_:specifier:_:name:_:equal:_:expression:_:trailingComma:_:trailingTrivia:)``.
///
public init(
leadingTrivia: Trivia? = nil,
specifier: ClosureCaptureSpecifierSyntax? = nil,
name: TokenSyntax,
equal: TokenSyntax = TokenSyntax.equalToken(),
expression: some ExprSyntaxProtocol,
trailingComma: TokenSyntax? = nil,
trailingTrivia: Trivia? = nil
) {
self.init(
leadingTrivia: leadingTrivia,
specifier: specifier,
name: name as TokenSyntax?,
equal: equal,
expression: expression,
trailingComma: trailingComma,
trailingTrivia: trailingTrivia
)
}
}

extension EnumCaseParameterSyntax {

/// Creates an ``EnumCaseParameterSyntax`` with a `firstName`, and automatically adds a `colon` to it.
Expand Down
Loading