Skip to content

Commit 0ab6e77

Browse files
committed
Make SyntaxCollection conform to ExpressibleByArrayLiteral
This hoists the ability to create syntax collections from `SwiftSyntaxBuilder` to the `SwiftSyntax` module. Impact on API compatibility: Initializing an `ExprListSyntax` or `UnexpectedNodesSyntax` now requires the nodes to be `ExprSyntax` and `Syntax` opposed to `ExprSyntaxProtocol` or `SyntaxProtocol`, respectively. `ExprListSyntax` is used as the child of a `SequenceExprSyntax`. I think this is OK because - Users don’t genrally construct `UnexpectedNodesSyntax` - To construct a `SequenceExprSyntax`, you can use the result builder on `SequenceExprSyntax`, which takes `ExprSyntaxProtocol`
1 parent 4294feb commit 0ab6e77

File tree

9 files changed

+50
-418
lines changed

9 files changed

+50
-418
lines changed

CodeGeneration/Sources/generate-swiftsyntax/GenerateSwiftSyntax.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,6 @@ struct GenerateSwiftSyntax: ParsableCommand {
120120
GeneratedFileSpec(swiftSyntaxGeneratedDir + ["TriviaPieces.swift"], triviaPiecesFile),
121121

122122
// SwiftSyntaxBuilder
123-
GeneratedFileSpec(swiftSyntaxBuilderGeneratedDir + ["BuildableCollectionNodes.swift"], buildableCollectionNodesFile),
124123
GeneratedFileSpec(swiftSyntaxBuilderGeneratedDir + ["BuildableNodes.swift"], buildableNodesFile),
125124
GeneratedFileSpec(swiftSyntaxBuilderGeneratedDir + ["ResultBuilders.swift"], resultBuildersFile),
126125
GeneratedFileSpec(

CodeGeneration/Sources/generate-swiftsyntax/templates/swiftsyntaxbuilder/BuildableCollectionNodesFile.swift

Lines changed: 0 additions & 60 deletions
This file was deleted.

Sources/SwiftSyntax/SyntaxCollection.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13-
public protocol SyntaxCollection: SyntaxProtocol, BidirectionalCollection where Element: SyntaxProtocol, Index == SyntaxChildrenIndex {
13+
public protocol SyntaxCollection: SyntaxProtocol, BidirectionalCollection, ExpressibleByArrayLiteral
14+
where Element: SyntaxProtocol, Index == SyntaxChildrenIndex {
1415
associatedtype Iterator = SyntaxCollectionIterator<Element>
1516

1617
/// The ``SyntaxKind`` of the syntax node that conforms to ``SyntaxCollection``.
@@ -58,6 +59,10 @@ extension SyntaxCollection {
5859
self.init(SyntaxData.forRoot(raw, rawNodeArena: arena))
5960
}
6061

62+
public init(arrayLiteral elements: Element...) {
63+
self.init(elements)
64+
}
65+
6166
/// The number of elements, `present` or `missing`, in this collection.
6267
public var count: Int {
6368
return layoutView.children.count

Sources/SwiftSyntaxBuilder/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ add_swift_host_library(SwiftSyntaxBuilder
1919
WithTrailingCommaSyntax+EnsuringTrailingComma.swift
2020

2121

22-
generated/BuildableCollectionNodes.swift
2322
generated/BuildableNodes.swift
2423
generated/ResultBuilders.swift
2524
generated/RenamedChildrenBuilderCompatibility.swift

Sources/SwiftSyntaxBuilder/ResultBuilderExtensions.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,21 @@ extension MemberBlockItemListBuilder {
4949
return buildExpression(MemberBlockItemSyntax(decl: expression))
5050
}
5151
}
52+
53+
// MARK: Initializing collections from protocols
54+
// These initializers allow the creation of syntax collections that have a base
55+
// node as their element from the corresponding protocol type.
56+
// These are used by the result builders.
57+
// Since we only have two of these, it doesn’t make sense to generate them.
58+
59+
extension ExprListSyntax {
60+
init(_ elements: [ExprSyntaxProtocol]) {
61+
self = ExprListSyntax(elements.map { ExprSyntax(fromProtocol: $0) } as [ExprSyntax])
62+
}
63+
}
64+
65+
extension UnexpectedNodesSyntax {
66+
public init(_ elements: [SyntaxProtocol]) {
67+
self = UnexpectedNodesSyntax(elements.map { Syntax(fromProtocol: $0) } as [Syntax])
68+
}
69+
}

0 commit comments

Comments
 (0)