Skip to content

Commit 33e6274

Browse files
committed
Make all AdditionalTrailingClosures children non-optional
1 parent 0ab6e77 commit 33e6274

25 files changed

+124
-92
lines changed

CodeGeneration/Sources/SyntaxSupport/Child.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public enum ChildKind {
3232
/// The child always contains a node that matches one of the `choices`.
3333
case nodeChoices(choices: [Child])
3434
/// The child is a collection of `kind`.
35-
case collection(kind: SyntaxNodeKind, collectionElementName: String, deprecatedCollectionElementName: String? = nil)
35+
case collection(kind: SyntaxNodeKind, collectionElementName: String, defaultsToEmpty: Bool = false, deprecatedCollectionElementName: String? = nil)
3636
/// The child is a token that matches one of the given `choices`.
3737
/// If `requiresLeadingSpace` or `requiresTrailingSpace` is not `nil`, it
3838
/// overrides the default leading/trailing space behavior of the token.
@@ -91,7 +91,7 @@ public class Child {
9191
return kind
9292
case .nodeChoices:
9393
return .syntax
94-
case .collection(kind: let kind, _, _):
94+
case .collection(kind: let kind, _, _, _):
9595
return kind
9696
case .token:
9797
return .token
@@ -150,7 +150,7 @@ public class Child {
150150
/// Whether this child has syntax kind `UnexpectedNodes`.
151151
public var isUnexpectedNodes: Bool {
152152
switch kind {
153-
case .collection(kind: .unexpectedNodes, _, _):
153+
case .collection(kind: .unexpectedNodes, _, _, _):
154154
return true
155155
default:
156156
return false
@@ -165,7 +165,7 @@ public class Child {
165165
return choices.isEmpty
166166
case .node(let kind):
167167
return kind.isBase
168-
case .collection(let kind, _, _):
168+
case .collection(kind: let kind, _, _, _):
169169
return kind.isBase
170170
case .token:
171171
return false

CodeGeneration/Sources/SyntaxSupport/DeclNodes.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1450,8 +1450,7 @@ public let DECL_NODES: [Node] = [
14501450
),
14511451
Child(
14521452
name: "AdditionalTrailingClosures",
1453-
kind: .collection(kind: .multipleTrailingClosureElementList, collectionElementName: "AdditionalTrailingClosure"),
1454-
isOptional: true
1453+
kind: .collection(kind: .multipleTrailingClosureElementList, collectionElementName: "AdditionalTrailingClosure", defaultsToEmpty: true)
14551454
),
14561455
]
14571456
),

CodeGeneration/Sources/SyntaxSupport/ExprNodes.swift

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -828,9 +828,8 @@ public let EXPR_NODES: [Node] = [
828828
),
829829
Child(
830830
name: "AdditionalTrailingClosures",
831-
kind: .collection(kind: .multipleTrailingClosureElementList, collectionElementName: "AdditionalTrailingClosure"),
832-
nameForDiagnostics: "trailing closures",
833-
isOptional: true
831+
kind: .collection(kind: .multipleTrailingClosureElementList, collectionElementName: "AdditionalTrailingClosure", defaultsToEmpty: true),
832+
nameForDiagnostics: "trailing closures"
834833
),
835834
]
836835
),
@@ -1179,8 +1178,7 @@ public let EXPR_NODES: [Node] = [
11791178
),
11801179
Child(
11811180
name: "AdditionalTrailingClosures",
1182-
kind: .collection(kind: .multipleTrailingClosureElementList, collectionElementName: "AdditionalTrailingClosure"),
1183-
isOptional: true
1181+
kind: .collection(kind: .multipleTrailingClosureElementList, collectionElementName: "AdditionalTrailingClosure", defaultsToEmpty: true)
11841182
),
11851183
]
11861184
),
@@ -1583,9 +1581,8 @@ public let EXPR_NODES: [Node] = [
15831581
),
15841582
Child(
15851583
name: "AdditionalTrailingClosures",
1586-
kind: .collection(kind: .multipleTrailingClosureElementList, collectionElementName: "AdditionalTrailingClosure"),
1587-
nameForDiagnostics: "trailing closures",
1588-
isOptional: true
1584+
kind: .collection(kind: .multipleTrailingClosureElementList, collectionElementName: "AdditionalTrailingClosure", defaultsToEmpty: true),
1585+
nameForDiagnostics: "trailing closures"
15891586
),
15901587
]
15911588
),

CodeGeneration/Sources/SyntaxSupport/GrammarGenerator.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ struct GrammarGenerator {
3636
case .nodeChoices(let choices):
3737
let choicesDescriptions = choices.map { grammar(for: $0) }
3838
return "(\(choicesDescriptions.joined(separator: " | ")))\(optionality)"
39-
case .collection(let kind, _, _):
39+
case .collection(kind: let kind, _, _, _):
4040
return "``\(kind.syntaxType)``"
4141
case .token(let choices, _, _):
4242
if choices.count == 1 {

CodeGeneration/Sources/SyntaxSupport/Node.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ fileprivate extension Child {
367367
return [kind]
368368
case .nodeChoices(let choices):
369369
return choices.flatMap(\.kinds)
370-
case .collection(let kind, _, _):
370+
case .collection(kind: let kind, _, _, _):
371371
return [kind]
372372
case .token:
373373
return [.token]

CodeGeneration/Sources/SyntaxSupport/Traits.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public let TRAITS: [Trait] = [
6969
Child(name: "ArgumentList", kind: .node(kind: .labeledExprList)),
7070
Child(name: "RightParen", kind: .token(choices: [.token(.rightParen)]), isOptional: true),
7171
Child(name: "TrailingClosure", kind: .node(kind: .closureExpr), isOptional: true),
72-
Child(name: "AdditionalTrailingClosures", kind: .node(kind: .multipleTrailingClosureElementList), isOptional: true),
72+
Child(name: "AdditionalTrailingClosures", kind: .node(kind: .multipleTrailingClosureElementList)),
7373
]
7474
),
7575
Trait(

CodeGeneration/Sources/Utils/SyntaxBuildableChild.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public extension Child {
3333
buildableKind = .node(kind: kind)
3434
case .nodeChoices:
3535
buildableKind = .node(kind: .syntax)
36-
case .collection(let kind, _, _):
36+
case .collection(kind: let kind, _, _, _):
3737
buildableKind = .node(kind: kind)
3838
case .token:
3939
buildableKind = .token(self.tokenKind!)
@@ -65,6 +65,9 @@ public extension Child {
6565
return ExprSyntax("nil")
6666
}
6767
}
68+
if case .collection(_, _, defaultsToEmpty: true, _) = kind {
69+
return ExprSyntax("[]")
70+
}
6871
guard let token = token, isToken else {
6972
return type.defaultValue
7073
}

CodeGeneration/Sources/generate-swiftsyntax/templates/swiftsyntax/RenamedChildrenCompatibilityFile.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ let renamedChildrenCompatibilityFile = try! SourceFileSyntax(leadingTrivia: copy
3838
)
3939
if let childNode = SYNTAX_NODE_MAP[child.syntaxNodeKind]?.collectionNode,
4040
!child.isUnexpectedNodes,
41-
case .collection(_, let collectionElementName, let deprecatedCollectionElementName) = child.kind,
41+
case .collection(_, collectionElementName: let collectionElementName, _, deprecatedCollectionElementName: let deprecatedCollectionElementName) =
42+
child.kind,
4243
let deprecatedCollectionElementName
4344
{
4445
let childEltType = childNode.collectionElementType.syntaxBaseName

CodeGeneration/Sources/generate-swiftsyntax/templates/swiftsyntax/SyntaxNodesFile.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ func syntaxNode(emitKind: SyntaxNodeKind) -> SourceFileSyntax {
193193
// If needed, this could be added in the future, but for now withUnexpected should be sufficient.
194194
if let childNode = SYNTAX_NODE_MAP[child.syntaxNodeKind]?.collectionNode,
195195
!child.isUnexpectedNodes,
196-
case .collection(_, let childElt, _) = child.kind
196+
case .collection(_, collectionElementName: let childElt, _, _) = child.kind
197197
{
198198
let childEltType = childNode.collectionElementType.syntaxBaseName
199199

CodeGeneration/Tests/ValidateSyntaxNodes/ValidateSyntaxNodes.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,13 @@ fileprivate extension ChildKind {
5656
return kind == otherKind
5757
case (.nodeChoices(let choices), .nodeChoices(let otherChoices)):
5858
return choices.count == otherChoices.count && zip(choices, otherChoices).allSatisfy { $0.hasSameType(as: $1) }
59-
case (.collection(let kind, _, _), .collection(let otherKind, _, _)):
59+
case (.collection(kind: let kind, _, _, _), .collection(kind: let otherKind, _, _, _)):
6060
return kind == otherKind
6161
case (.token(let choices, _, _), .token(let otherChoices, _, _)):
6262
return choices == otherChoices
63-
case (.node(let kind), .collection(let otherKind, _, _)):
63+
case (.node(let kind), .collection(kind: let otherKind, _, _, _)):
6464
return kind == otherKind
65-
case (.collection(let kind, _, _), .node(let otherKind)):
65+
case (.collection(kind: let kind, _, _, _), .node(let otherKind)):
6666
return kind == otherKind
6767
default:
6868
return false

Sources/SwiftParser/Declarations.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1917,15 +1917,15 @@ extension Parser {
19171917

19181918
// Parse the optional trailing closures.
19191919
let trailingClosure: RawClosureExprSyntax?
1920-
let additionalTrailingClosures: RawMultipleTrailingClosureElementListSyntax?
1920+
let additionalTrailingClosures: RawMultipleTrailingClosureElementListSyntax
19211921
if self.at(.leftBrace),
19221922
self.withLookahead({ $0.atValidTrailingClosure(.trailingClosure) })
19231923
{
19241924
(trailingClosure, additionalTrailingClosures) =
19251925
self.parseTrailingClosures(.trailingClosure)
19261926
} else {
19271927
trailingClosure = nil
1928-
additionalTrailingClosures = nil
1928+
additionalTrailingClosures = self.emptyCollection(RawMultipleTrailingClosureElementListSyntax.self)
19291929
}
19301930

19311931
return RawMacroExpansionDeclSyntax(

Sources/SwiftParser/Expressions.swift

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -733,12 +733,12 @@ extension Parser {
733733

734734
// If we can parse trailing closures, do so.
735735
let trailingClosure: RawClosureExprSyntax?
736-
let additionalTrailingClosures: RawMultipleTrailingClosureElementListSyntax?
736+
let additionalTrailingClosures: RawMultipleTrailingClosureElementListSyntax
737737
if case .trailingClosure = flavor, self.at(.leftBrace), self.withLookahead({ $0.atValidTrailingClosure(flavor) }) {
738738
(trailingClosure, additionalTrailingClosures) = self.parseTrailingClosures(flavor)
739739
} else {
740740
trailingClosure = nil
741-
additionalTrailingClosures = nil
741+
additionalTrailingClosures = self.emptyCollection(RawMultipleTrailingClosureElementListSyntax.self)
742742
}
743743

744744
leadingExpr = RawExprSyntax(
@@ -769,12 +769,12 @@ extension Parser {
769769

770770
// If we can parse trailing closures, do so.
771771
let trailingClosure: RawClosureExprSyntax?
772-
let additionalTrailingClosures: RawMultipleTrailingClosureElementListSyntax?
772+
let additionalTrailingClosures: RawMultipleTrailingClosureElementListSyntax
773773
if case .trailingClosure = flavor, self.at(.leftBrace), self.withLookahead({ $0.atValidTrailingClosure(flavor) }) {
774774
(trailingClosure, additionalTrailingClosures) = self.parseTrailingClosures(flavor)
775775
} else {
776776
trailingClosure = nil
777-
additionalTrailingClosures = nil
777+
additionalTrailingClosures = self.emptyCollection(RawMultipleTrailingClosureElementListSyntax.self)
778778
}
779779

780780
leadingExpr = RawExprSyntax(
@@ -1320,12 +1320,12 @@ extension Parser {
13201320

13211321
// Parse the optional trailing closures.
13221322
let trailingClosure: RawClosureExprSyntax?
1323-
let additionalTrailingClosures: RawMultipleTrailingClosureElementListSyntax?
1323+
let additionalTrailingClosures: RawMultipleTrailingClosureElementListSyntax
13241324
if case .trailingClosure = flavor, self.at(.leftBrace), self.withLookahead({ $0.atValidTrailingClosure(flavor) }) {
13251325
(trailingClosure, additionalTrailingClosures) = self.parseTrailingClosures(flavor)
13261326
} else {
13271327
trailingClosure = nil
1328-
additionalTrailingClosures = nil
1328+
additionalTrailingClosures = self.emptyCollection(RawMultipleTrailingClosureElementListSyntax.self)
13291329
}
13301330

13311331
return RawMacroExpansionExprSyntax(
@@ -1908,7 +1908,7 @@ extension Parser {
19081908

19091909
extension Parser {
19101910
/// Parse the trailing closure(s) following a call expression.
1911-
mutating func parseTrailingClosures(_ flavor: ExprFlavor) -> (RawClosureExprSyntax, RawMultipleTrailingClosureElementListSyntax?) {
1911+
mutating func parseTrailingClosures(_ flavor: ExprFlavor) -> (RawClosureExprSyntax, RawMultipleTrailingClosureElementListSyntax) {
19121912
// Parse the closure.
19131913
let closure = self.parseClosureExpression()
19141914

@@ -1931,7 +1931,10 @@ extension Parser {
19311931
)
19321932
}
19331933

1934-
let trailing = elements.isEmpty ? nil : RawMultipleTrailingClosureElementListSyntax(elements: elements, arena: self.arena)
1934+
let trailing =
1935+
elements.isEmpty
1936+
? self.emptyCollection(RawMultipleTrailingClosureElementListSyntax.self)
1937+
: RawMultipleTrailingClosureElementListSyntax(elements: elements, arena: self.arena)
19351938
return (closure, trailing)
19361939
}
19371940
}

Sources/SwiftParser/Parser.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,19 @@ public struct Parser {
129129
static let defaultMaximumNestingLevel = 256
130130
#endif
131131

132+
var _emptyRawMultipleTrailingClosureElementListSyntax: RawMultipleTrailingClosureElementListSyntax?
133+
134+
/// Create an empty collection of the given type.
135+
///
136+
/// These empty collections are only created once and the same node is returned
137+
/// on subsequent calls, reducing memory usage.
138+
mutating func emptyCollection(_: RawMultipleTrailingClosureElementListSyntax.Type) -> RawMultipleTrailingClosureElementListSyntax {
139+
if _emptyRawMultipleTrailingClosureElementListSyntax == nil {
140+
_emptyRawMultipleTrailingClosureElementListSyntax = RawMultipleTrailingClosureElementListSyntax(elements: [], arena: self.arena)
141+
}
142+
return _emptyRawMultipleTrailingClosureElementListSyntax!
143+
}
144+
132145
/// The delegated initializer for the parser.
133146
///
134147
/// - Parameters

Sources/SwiftRefactor/CallToTrailingClosures.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public struct CallToTrailingClosures: SyntaxRefactoringProvider {
5454

5555
extension FunctionCallExprSyntax {
5656
fileprivate func convertToTrailingClosures(from startAtArgument: Int) -> FunctionCallExprSyntax? {
57-
guard trailingClosure == nil, additionalTrailingClosures == nil, leftParen != nil, rightParen != nil else {
57+
guard trailingClosure == nil, additionalTrailingClosures.isEmpty, leftParen != nil, rightParen != nil else {
5858
// Already have trailing closures
5959
return nil
6060
}

Sources/SwiftSyntax/generated/RenamedChildrenCompatibility.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3024,7 +3024,7 @@ extension FunctionCallExprSyntax {
30243024
_ unexpectedBetweenRightParenAndTrailingClosure: UnexpectedNodesSyntax? = nil,
30253025
trailingClosure: ClosureExprSyntax? = nil,
30263026
_ unexpectedBetweenTrailingClosureAndAdditionalTrailingClosures: UnexpectedNodesSyntax? = nil,
3027-
additionalTrailingClosures: MultipleTrailingClosureElementListSyntax? = nil,
3027+
additionalTrailingClosures: MultipleTrailingClosureElementListSyntax = [],
30283028
_ unexpectedAfterAdditionalTrailingClosures: UnexpectedNodesSyntax? = nil,
30293029
trailingTrivia: Trivia? = nil
30303030

@@ -4949,7 +4949,7 @@ extension MacroExpansionDeclSyntax {
49494949
_ unexpectedBetweenRightParenAndTrailingClosure: UnexpectedNodesSyntax? = nil,
49504950
trailingClosure: ClosureExprSyntax? = nil,
49514951
_ unexpectedBetweenTrailingClosureAndAdditionalTrailingClosures: UnexpectedNodesSyntax? = nil,
4952-
additionalTrailingClosures: MultipleTrailingClosureElementListSyntax? = nil,
4952+
additionalTrailingClosures: MultipleTrailingClosureElementListSyntax = [],
49534953
_ unexpectedAfterAdditionalTrailingClosures: UnexpectedNodesSyntax? = nil,
49544954
trailingTrivia: Trivia? = nil
49554955

@@ -5102,7 +5102,7 @@ extension MacroExpansionExprSyntax {
51025102
_ unexpectedBetweenRightParenAndTrailingClosure: UnexpectedNodesSyntax? = nil,
51035103
trailingClosure: ClosureExprSyntax? = nil,
51045104
_ unexpectedBetweenTrailingClosureAndAdditionalTrailingClosures: UnexpectedNodesSyntax? = nil,
5105-
additionalTrailingClosures: MultipleTrailingClosureElementListSyntax? = nil,
5105+
additionalTrailingClosures: MultipleTrailingClosureElementListSyntax = [],
51065106
_ unexpectedAfterAdditionalTrailingClosures: UnexpectedNodesSyntax? = nil,
51075107
trailingTrivia: Trivia? = nil
51085108

@@ -7573,7 +7573,7 @@ extension SubscriptCallExprSyntax {
75737573
_ unexpectedBetweenRightBracketAndTrailingClosure: UnexpectedNodesSyntax? = nil,
75747574
trailingClosure: ClosureExprSyntax? = nil,
75757575
_ unexpectedBetweenTrailingClosureAndAdditionalTrailingClosures: UnexpectedNodesSyntax? = nil,
7576-
additionalTrailingClosures: MultipleTrailingClosureElementListSyntax? = nil,
7576+
additionalTrailingClosures: MultipleTrailingClosureElementListSyntax = [],
75777577
_ unexpectedAfterAdditionalTrailingClosures: UnexpectedNodesSyntax? = nil,
75787578
trailingTrivia: Trivia? = nil
75797579

Sources/SwiftSyntax/generated/SyntaxTraits.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ public protocol FreestandingMacroExpansionSyntax: SyntaxProtocol {
208208
set
209209
}
210210

211-
var additionalTrailingClosures: MultipleTrailingClosureElementListSyntax? {
211+
var additionalTrailingClosures: MultipleTrailingClosureElementListSyntax {
212212
get
213213
set
214214
}

0 commit comments

Comments
 (0)