Skip to content

Address default parameter regressions and add tests for ForInStmtSyntax #1517

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

Merged
merged 7 commits into from
Apr 13, 2023
Merged
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
2 changes: 1 addition & 1 deletion CodeGeneration/Sources/SyntaxSupport/KeywordSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public let KEYWORDS: [KeywordSpec] = [
KeywordSpec("autoclosure"),
KeywordSpec("availability"),
KeywordSpec("available"),
KeywordSpec("await"),
KeywordSpec("await", requiresTrailingSpace: true),
KeywordSpec("backDeployed"),
KeywordSpec("before"),
KeywordSpec("block"),
Expand Down
7 changes: 3 additions & 4 deletions CodeGeneration/Sources/SyntaxSupport/StmtNodes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -290,18 +290,17 @@ public let STMT_NODES: [Node] = [
),
Child(
name: "TryKeyword",
kind: .node(kind: "TryToken"),
kind: .token(choices: [.keyword(text: "try")]),
isOptional: true
),
Child(
name: "AwaitKeyword",
kind: .token(choices: [.keyword(text: "await")]),
isOptional: true,
classification: "Keyword"
isOptional: true
),
Child(
name: "CaseKeyword",
kind: .node(kind: "CaseToken"),
kind: .token(choices: [.keyword(text: "case")]),
isOptional: true
),
Child(
Expand Down
41 changes: 27 additions & 14 deletions CodeGeneration/Sources/Utils/SyntaxBuildableChild.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,21 +45,34 @@ public extension Child {
flattened(indentedDocumentation: description ?? "")
}

var defaultInitialization: ExprSyntax? {
switch kind {
case .token(choices: let choices, requiresLeadingSpace: _, requiresTrailingSpace: _):
if choices.count == 1, case .keyword(text: let text) = choices.first {
var textChoice = text
if textChoice == "init" {
textChoice = "`init`"
}
return ExprSyntax(".keyword(.\(raw: textChoice))")
} else {
return type.defaultInitialization
}
default:
return type.defaultInitialization
/// If the child node has a default value, return an expression of the form
/// ` = default_value` that can be used as the default value to for a
/// function parameter. Otherwise, return `nil`.
var defaultInitialization: InitializerClauseSyntax? {
if isOptional || isUnexpectedNodes {
return InitializerClauseSyntax(value: NilLiteralExprSyntax())
}
guard let token = token, isToken else {
return type.defaultValue.map { InitializerClauseSyntax(value: $0) }
}
if token.isKeyword {
return InitializerClauseSyntax(value: ExprSyntax(".\(raw: token.swiftKind)()"))
}
if token.text != nil {
return InitializerClauseSyntax(value: ExprSyntax(".\(raw: token.swiftKind)Token()"))
}
guard case .token(let choices, _, _) = kind, choices.count == 1, token.associatedValueClass != nil else {
return nil
}
var textChoice: String
switch choices[0] {
case .keyword(let text), .token(let text):
textChoice = text
}
if textChoice == "init" {
textChoice = "`init`"
}
return InitializerClauseSyntax(value: ExprSyntax(".\(raw: token.swiftKind)(.\(raw: textChoice))"))
}

/// If this node is a token that can't contain arbitrary text, generate a Swift
Expand Down
2 changes: 1 addition & 1 deletion CodeGeneration/Sources/Utils/SyntaxBuildableNode.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public extension Node {

/// Assuming this node has a single child without a default value, that child.
var singleNonDefaultedChild: Child {
let nonDefaultedParams = children.filter { $0.type.defaultInitialization == nil }
let nonDefaultedParams = children.filter { $0.defaultInitialization == nil }
precondition(nonDefaultedParams.count == 1)
return nonDefaultedParams[0]
}
Expand Down
7 changes: 3 additions & 4 deletions CodeGeneration/Sources/Utils/SyntaxBuildableType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,9 @@ public struct SyntaxBuildableType: Hashable {
}

/// If the type has a default value (because it is optional or a token
/// with fixed test), return an expression of the form ` = defaultValue`
/// that can be used as the default value for a function parameter.
/// Otherwise, return the empty string.
public var defaultInitialization: ExprSyntax? {
/// with fixed test), return an expression that can be used as the
/// default value for a function parameter. Otherwise, return `nil`.
public var defaultValue: ExprSyntax? {
if isOptional {
return ExprSyntax(NilLiteralExprSyntax())
} else if let token = token {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ let rawSyntaxNodesFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
secondName: .identifier(child.swiftName),
colon: .colonToken(),
type: child.rawParameterType,
defaultArgument: child.isUnexpectedNodes ? child.defaultInitialization.map { InitializerClauseSyntax(value: $0) } : nil
defaultArgument: child.isUnexpectedNodes ? child.defaultInitialization : nil
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -438,57 +438,3 @@ fileprivate extension Node {
}
}
}

fileprivate extension Child {
/// If the type has a default value, return an expression of the form
/// ` = default_value` that can be used as the default value to for a
/// function parameter. Otherwise, return an empty string.
var defaultInitialization: InitializerClauseSyntax? {
// Note that this should be Optional<BaseType>.none for defaulted generic,
// but that doesn't work in Swift 5.6. To keep source compatibility with
// previous SwiftSyntax, we instead create a second initializer that uses
// `Missing<Node>` and defaults that to `nil` instead (and `Missing` is
// used so that they can't be implicitly converted from a literal).

if isOptional || isUnexpectedNodes {
return InitializerClauseSyntax(value: NilLiteralExprSyntax())
}

guard let token = token, isToken else {
return nil
}

if token.isKeyword {
return InitializerClauseSyntax(value: ExprSyntax(".\(raw: token.swiftKind)()"))
}

if token.text != nil {
return InitializerClauseSyntax(value: ExprSyntax(".\(raw: token.swiftKind)Token()"))
}

guard case .token(let choices, _, _) = kind else {
return nil
}

guard choices.count == 1 else {
return nil
}

var textChoice: String

switch choices[0] {
case .keyword(let text),
.token(let text):
textChoice = text
}

if token.associatedValueClass != nil {
if textChoice == "init" {
textChoice = "`init`"
}
return InitializerClauseSyntax(value: ExprSyntax(".\(raw: token.swiftKind)(.\(raw: textChoice))"))
} else {
return InitializerClauseSyntax(value: ExprSyntax(#".\#(raw: token.swiftKind)("\#(raw: textChoice)")"#))
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ private func createConvenienceInitializer(node: Node) throws -> InitializerDeclS
firstName: .identifier(child.swiftName),
colon: .colonToken(),
type: child.parameterType,
defaultArgument: child.defaultInitialization.map { InitializerClauseSyntax(value: $0) }
defaultArgument: child.defaultInitialization
)
)
}
Expand Down
2 changes: 0 additions & 2 deletions Sources/IDEUtils/generated/SyntaxClassification.swift
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,6 @@ extension SyntaxClassification {
return (.stringInterpolationAnchor, true)
case \ExpressionSegmentSyntax.rightParen:
return (.stringInterpolationAnchor, true)
case \ForInStmtSyntax.awaitKeyword:
return (.keyword, false)
case \IfConfigClauseSyntax.poundKeyword:
return (.buildConfigId, false)
case \IfConfigClauseSyntax.condition:
Expand Down
2 changes: 2 additions & 0 deletions Sources/SwiftBasicFormat/generated/BasicFormat.swift
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,8 @@ open class BasicFormat: SyntaxRewriter {
return true
case .keyword(.async):
return true
case .keyword(.await):
return true
case .keyword(.`break`):
return true
case .keyword(.`case`):
Expand Down
1 change: 0 additions & 1 deletion Sources/SwiftParser/Statements.swift
Original file line number Diff line number Diff line change
Expand Up @@ -614,7 +614,6 @@ extension Parser {
public mutating func parseForEachStatement(forHandle: RecoveryConsumptionHandle) -> RawForInStmtSyntax {
let (unexpectedBeforeForKeyword, forKeyword) = self.eat(forHandle)
let tryKeyword = self.consume(if: .keyword(.try))

let awaitKeyword = self.consume(if: .keyword(.await))

// Parse the pattern. This is either 'case <refutable pattern>' or just a
Expand Down
4 changes: 2 additions & 2 deletions Sources/SwiftSyntax/generated/raw/RawSyntaxValidation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1200,11 +1200,11 @@ func validateLayout(layout: RawSyntaxBuffer, as kind: SyntaxKind) {
assertNoError(kind, 0, verify(layout[0], as: RawUnexpectedNodesSyntax?.self))
assertNoError(kind, 1, verify(layout[1], as: RawTokenSyntax.self, tokenChoices: [.keyword("for")]))
assertNoError(kind, 2, verify(layout[2], as: RawUnexpectedNodesSyntax?.self))
assertNoError(kind, 3, verify(layout[3], as: RawTokenSyntax?.self))
assertNoError(kind, 3, verify(layout[3], as: RawTokenSyntax?.self, tokenChoices: [.keyword("try")]))
assertNoError(kind, 4, verify(layout[4], as: RawUnexpectedNodesSyntax?.self))
assertNoError(kind, 5, verify(layout[5], as: RawTokenSyntax?.self, tokenChoices: [.keyword("await")]))
assertNoError(kind, 6, verify(layout[6], as: RawUnexpectedNodesSyntax?.self))
assertNoError(kind, 7, verify(layout[7], as: RawTokenSyntax?.self))
assertNoError(kind, 7, verify(layout[7], as: RawTokenSyntax?.self, tokenChoices: [.keyword("case")]))
assertNoError(kind, 8, verify(layout[8], as: RawUnexpectedNodesSyntax?.self))
assertNoError(kind, 9, verify(layout[9], as: RawPatternSyntax.self))
assertNoError(kind, 10, verify(layout[10], as: RawUnexpectedNodesSyntax?.self))
Expand Down
22 changes: 11 additions & 11 deletions Sources/SwiftSyntax/generated/syntaxNodes/SyntaxDeclNodes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ public struct ActorDeclSyntax: DeclSyntaxProtocol, SyntaxHashable {
_ unexpectedBetweenModifiersAndActorKeyword: UnexpectedNodesSyntax? = nil,
actorKeyword: TokenSyntax = .keyword(.actor),
_ unexpectedBetweenActorKeywordAndIdentifier: UnexpectedNodesSyntax? = nil,
identifier: TokenSyntax = .identifier("IdentifierToken"),
identifier: TokenSyntax,
_ unexpectedBetweenIdentifierAndGenericParameterClause: UnexpectedNodesSyntax? = nil,
genericParameterClause: GenericParameterClauseSyntax? = nil,
_ unexpectedBetweenGenericParameterClauseAndInheritanceClause: UnexpectedNodesSyntax? = nil,
Expand Down Expand Up @@ -633,7 +633,7 @@ public struct AssociatedtypeDeclSyntax: DeclSyntaxProtocol, SyntaxHashable {
_ unexpectedBetweenModifiersAndAssociatedtypeKeyword: UnexpectedNodesSyntax? = nil,
associatedtypeKeyword: TokenSyntax = .keyword(.associatedtype),
_ unexpectedBetweenAssociatedtypeKeywordAndIdentifier: UnexpectedNodesSyntax? = nil,
identifier: TokenSyntax = .identifier("IdentifierToken"),
identifier: TokenSyntax,
_ unexpectedBetweenIdentifierAndInheritanceClause: UnexpectedNodesSyntax? = nil,
inheritanceClause: TypeInheritanceClauseSyntax? = nil,
_ unexpectedBetweenInheritanceClauseAndInitializer: UnexpectedNodesSyntax? = nil,
Expand Down Expand Up @@ -938,7 +938,7 @@ public struct ClassDeclSyntax: DeclSyntaxProtocol, SyntaxHashable {
_ unexpectedBetweenModifiersAndClassKeyword: UnexpectedNodesSyntax? = nil,
classKeyword: TokenSyntax = .keyword(.class),
_ unexpectedBetweenClassKeywordAndIdentifier: UnexpectedNodesSyntax? = nil,
identifier: TokenSyntax = .identifier("IdentifierToken"),
identifier: TokenSyntax,
_ unexpectedBetweenIdentifierAndGenericParameterClause: UnexpectedNodesSyntax? = nil,
genericParameterClause: GenericParameterClauseSyntax? = nil,
_ unexpectedBetweenGenericParameterClauseAndInheritanceClause: UnexpectedNodesSyntax? = nil,
Expand Down Expand Up @@ -1486,7 +1486,7 @@ public struct EditorPlaceholderDeclSyntax: DeclSyntaxProtocol, SyntaxHashable {
public init(
leadingTrivia: Trivia? = nil,
_ unexpectedBeforeIdentifier: UnexpectedNodesSyntax? = nil,
identifier: TokenSyntax = .identifier("IdentifierToken"),
identifier: TokenSyntax,
_ unexpectedAfterIdentifier: UnexpectedNodesSyntax? = nil,
trailingTrivia: Trivia? = nil

Expand Down Expand Up @@ -1822,7 +1822,7 @@ public struct EnumDeclSyntax: DeclSyntaxProtocol, SyntaxHashable {
_ unexpectedBetweenModifiersAndEnumKeyword: UnexpectedNodesSyntax? = nil,
enumKeyword: TokenSyntax = .keyword(.enum),
_ unexpectedBetweenEnumKeywordAndIdentifier: UnexpectedNodesSyntax? = nil,
identifier: TokenSyntax = .identifier("IdentifierToken"),
identifier: TokenSyntax,
_ unexpectedBetweenIdentifierAndGenericParameters: UnexpectedNodesSyntax? = nil,
genericParameters: GenericParameterClauseSyntax? = nil,
_ unexpectedBetweenGenericParametersAndInheritanceClause: UnexpectedNodesSyntax? = nil,
Expand Down Expand Up @@ -3548,7 +3548,7 @@ public struct MacroDeclSyntax: DeclSyntaxProtocol, SyntaxHashable {
_ unexpectedBetweenModifiersAndMacroKeyword: UnexpectedNodesSyntax? = nil,
macroKeyword: TokenSyntax = .keyword(.macro),
_ unexpectedBetweenMacroKeywordAndIdentifier: UnexpectedNodesSyntax? = nil,
identifier: TokenSyntax = .identifier("IdentifierToken"),
identifier: TokenSyntax,
_ unexpectedBetweenIdentifierAndGenericParameterClause: UnexpectedNodesSyntax? = nil,
genericParameterClause: GenericParameterClauseSyntax? = nil,
_ unexpectedBetweenGenericParameterClauseAndSignature: UnexpectedNodesSyntax? = nil,
Expand Down Expand Up @@ -3877,7 +3877,7 @@ public struct MacroExpansionDeclSyntax: DeclSyntaxProtocol, SyntaxHashable {
_ unexpectedBeforePoundToken: UnexpectedNodesSyntax? = nil,
poundToken: TokenSyntax = .poundToken(),
_ unexpectedBetweenPoundTokenAndMacro: UnexpectedNodesSyntax? = nil,
macro: TokenSyntax = .identifier("IdentifierToken"),
macro: TokenSyntax,
_ unexpectedBetweenMacroAndGenericArguments: UnexpectedNodesSyntax? = nil,
genericArguments: GenericArgumentClauseSyntax? = nil,
_ unexpectedBetweenGenericArgumentsAndLeftParen: UnexpectedNodesSyntax? = nil,
Expand Down Expand Up @@ -4815,7 +4815,7 @@ public struct PrecedenceGroupDeclSyntax: DeclSyntaxProtocol, SyntaxHashable {
_ unexpectedBetweenModifiersAndPrecedencegroupKeyword: UnexpectedNodesSyntax? = nil,
precedencegroupKeyword: TokenSyntax = .keyword(.precedencegroup),
_ unexpectedBetweenPrecedencegroupKeywordAndIdentifier: UnexpectedNodesSyntax? = nil,
identifier: TokenSyntax = .identifier("IdentifierToken"),
identifier: TokenSyntax,
_ unexpectedBetweenIdentifierAndLeftBrace: UnexpectedNodesSyntax? = nil,
leftBrace: TokenSyntax = .leftBraceToken(),
_ unexpectedBetweenLeftBraceAndGroupAttributes: UnexpectedNodesSyntax? = nil,
Expand Down Expand Up @@ -5143,7 +5143,7 @@ public struct ProtocolDeclSyntax: DeclSyntaxProtocol, SyntaxHashable {
_ unexpectedBetweenModifiersAndProtocolKeyword: UnexpectedNodesSyntax? = nil,
protocolKeyword: TokenSyntax = .keyword(.protocol),
_ unexpectedBetweenProtocolKeywordAndIdentifier: UnexpectedNodesSyntax? = nil,
identifier: TokenSyntax = .identifier("IdentifierToken"),
identifier: TokenSyntax,
_ unexpectedBetweenIdentifierAndPrimaryAssociatedTypeClause: UnexpectedNodesSyntax? = nil,
primaryAssociatedTypeClause: PrimaryAssociatedTypeClauseSyntax? = nil,
_ unexpectedBetweenPrimaryAssociatedTypeClauseAndInheritanceClause: UnexpectedNodesSyntax? = nil,
Expand Down Expand Up @@ -5476,7 +5476,7 @@ public struct StructDeclSyntax: DeclSyntaxProtocol, SyntaxHashable {
_ unexpectedBetweenModifiersAndStructKeyword: UnexpectedNodesSyntax? = nil,
structKeyword: TokenSyntax = .keyword(.struct),
_ unexpectedBetweenStructKeywordAndIdentifier: UnexpectedNodesSyntax? = nil,
identifier: TokenSyntax = .identifier("IdentifierToken"),
identifier: TokenSyntax,
_ unexpectedBetweenIdentifierAndGenericParameterClause: UnexpectedNodesSyntax? = nil,
genericParameterClause: GenericParameterClauseSyntax? = nil,
_ unexpectedBetweenGenericParameterClauseAndInheritanceClause: UnexpectedNodesSyntax? = nil,
Expand Down Expand Up @@ -6184,7 +6184,7 @@ public struct TypealiasDeclSyntax: DeclSyntaxProtocol, SyntaxHashable {
_ unexpectedBetweenModifiersAndTypealiasKeyword: UnexpectedNodesSyntax? = nil,
typealiasKeyword: TokenSyntax = .keyword(.typealias),
_ unexpectedBetweenTypealiasKeywordAndIdentifier: UnexpectedNodesSyntax? = nil,
identifier: TokenSyntax = .identifier("IdentifierToken"),
identifier: TokenSyntax,
_ unexpectedBetweenIdentifierAndGenericParameterClause: UnexpectedNodesSyntax? = nil,
genericParameterClause: GenericParameterClauseSyntax? = nil,
_ unexpectedBetweenGenericParameterClauseAndInitializer: UnexpectedNodesSyntax? = nil,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -733,7 +733,7 @@ public struct BinaryOperatorExprSyntax: ExprSyntaxProtocol, SyntaxHashable {
public init(
leadingTrivia: Trivia? = nil,
_ unexpectedBeforeOperatorToken: UnexpectedNodesSyntax? = nil,
operatorToken: TokenSyntax = .binaryOperator("BinaryOperatorToken"),
operatorToken: TokenSyntax,
_ unexpectedAfterOperatorToken: UnexpectedNodesSyntax? = nil,
trailingTrivia: Trivia? = nil

Expand Down Expand Up @@ -1517,7 +1517,7 @@ public struct EditorPlaceholderExprSyntax: ExprSyntaxProtocol, SyntaxHashable {
public init(
leadingTrivia: Trivia? = nil,
_ unexpectedBeforeIdentifier: UnexpectedNodesSyntax? = nil,
identifier: TokenSyntax = .identifier("IdentifierToken"),
identifier: TokenSyntax,
_ unexpectedAfterIdentifier: UnexpectedNodesSyntax? = nil,
trailingTrivia: Trivia? = nil

Expand Down Expand Up @@ -1603,7 +1603,7 @@ public struct FloatLiteralExprSyntax: ExprSyntaxProtocol, SyntaxHashable {
public init(
leadingTrivia: Trivia? = nil,
_ unexpectedBeforeFloatingDigits: UnexpectedNodesSyntax? = nil,
floatingDigits: TokenSyntax = .floatingLiteral("FloatingLiteralToken"),
floatingDigits: TokenSyntax,
_ unexpectedAfterFloatingDigits: UnexpectedNodesSyntax? = nil,
trailingTrivia: Trivia? = nil

Expand Down Expand Up @@ -2774,7 +2774,7 @@ public struct IntegerLiteralExprSyntax: ExprSyntaxProtocol, SyntaxHashable {
public init(
leadingTrivia: Trivia? = nil,
_ unexpectedBeforeDigits: UnexpectedNodesSyntax? = nil,
digits: TokenSyntax = .integerLiteral("IntegerLiteralToken"),
digits: TokenSyntax,
_ unexpectedAfterDigits: UnexpectedNodesSyntax? = nil,
trailingTrivia: Trivia? = nil

Expand Down Expand Up @@ -3226,7 +3226,7 @@ public struct MacroExpansionExprSyntax: ExprSyntaxProtocol, SyntaxHashable {
_ unexpectedBeforePoundToken: UnexpectedNodesSyntax? = nil,
poundToken: TokenSyntax = .poundToken(),
_ unexpectedBetweenPoundTokenAndMacro: UnexpectedNodesSyntax? = nil,
macro: TokenSyntax = .identifier("IdentifierToken"),
macro: TokenSyntax,
_ unexpectedBetweenMacroAndGenericArguments: UnexpectedNodesSyntax? = nil,
genericArguments: GenericArgumentClauseSyntax? = nil,
_ unexpectedBetweenGenericArgumentsAndLeftParen: UnexpectedNodesSyntax? = nil,
Expand Down Expand Up @@ -4597,7 +4597,7 @@ public struct PostfixUnaryExprSyntax: ExprSyntaxProtocol, SyntaxHashable {
_ unexpectedBeforeExpression: UnexpectedNodesSyntax? = nil,
expression: E,
_ unexpectedBetweenExpressionAndOperatorToken: UnexpectedNodesSyntax? = nil,
operatorToken: TokenSyntax = .postfixOperator("PostfixOperatorToken"),
operatorToken: TokenSyntax,
_ unexpectedAfterOperatorToken: UnexpectedNodesSyntax? = nil,
trailingTrivia: Trivia? = nil

Expand Down Expand Up @@ -4853,7 +4853,7 @@ public struct RegexLiteralExprSyntax: ExprSyntaxProtocol, SyntaxHashable {
_ unexpectedBetweenOpeningPoundsAndOpenSlash: UnexpectedNodesSyntax? = nil,
openSlash: TokenSyntax = .regexSlashToken(),
_ unexpectedBetweenOpenSlashAndRegexPattern: UnexpectedNodesSyntax? = nil,
regexPattern: TokenSyntax = .regexLiteralPattern("RegexLiteralPatternToken"),
regexPattern: TokenSyntax,
_ unexpectedBetweenRegexPatternAndCloseSlash: UnexpectedNodesSyntax? = nil,
closeSlash: TokenSyntax = .regexSlashToken(),
_ unexpectedBetweenCloseSlashAndClosingPounds: UnexpectedNodesSyntax? = nil,
Expand Down
Loading