Skip to content

Commit 11a0d30

Browse files
committed
Add disableSameTypeForUniqueChoice on Node
When elementChoices of a Node has a single syntax node as a choice, it's `Element` type is of that syntax node type. Add a flag disableSameTypeForUniqueChoice to disable this behavior. This is added for a TypeSpecifier node to avoid the api break from deleting LifetimeTypeSpecifier from its elementChoices.
1 parent 6f334d0 commit 11a0d30

File tree

5 files changed

+16
-4
lines changed

5 files changed

+16
-4
lines changed

CodeGeneration/Sources/SyntaxSupport/Node.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ public class Node: NodeChoiceConvertible {
5555
/// function that should be invoked to create this node.
5656
public let parserFunction: TokenSyntax?
5757

58+
public let disableSameTypeForUniqueChoice: Bool
59+
5860
public var syntaxNodeKind: SyntaxNodeKind {
5961
self.kind
6062
}
@@ -121,6 +123,7 @@ public class Node: NodeChoiceConvertible {
121123
experimentalFeature: ExperimentalFeature? = nil,
122124
nameForDiagnostics: String?,
123125
documentation: String? = nil,
126+
disableSameTypeForUniqueChoice: Bool = false,
124127
parserFunction: TokenSyntax? = nil,
125128
traits: [String] = [],
126129
children: [Child] = []
@@ -133,6 +136,7 @@ public class Node: NodeChoiceConvertible {
133136
self.experimentalFeature = experimentalFeature
134137
self.nameForDiagnostics = nameForDiagnostics
135138
self.documentation = SwiftSyntax.Trivia.docCommentTrivia(from: documentation)
139+
self.disableSameTypeForUniqueChoice = disableSameTypeForUniqueChoice
136140
self.parserFunction = parserFunction
137141

138142
let childrenWithUnexpected: [Child]
@@ -278,6 +282,7 @@ public class Node: NodeChoiceConvertible {
278282
experimentalFeature: ExperimentalFeature? = nil,
279283
nameForDiagnostics: String?,
280284
documentation: String? = nil,
285+
disableSameTypeForUniqueChoice: Bool = false,
281286
parserFunction: TokenSyntax? = nil,
282287
elementChoices: [SyntaxNodeKind]
283288
) {
@@ -287,6 +292,7 @@ public class Node: NodeChoiceConvertible {
287292
self.experimentalFeature = experimentalFeature
288293
self.nameForDiagnostics = nameForDiagnostics
289294
self.documentation = SwiftSyntax.Trivia.docCommentTrivia(from: documentation)
295+
self.disableSameTypeForUniqueChoice = disableSameTypeForUniqueChoice
290296
self.parserFunction = parserFunction
291297

292298
assert(!elementChoices.isEmpty)

CodeGeneration/Sources/SyntaxSupport/TypeNodes.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,7 @@ public let TYPE_NODES: [Node] = [
527527
kind: .typeSpecifierList,
528528
base: .syntaxCollection,
529529
nameForDiagnostics: nil,
530+
disableSameTypeForUniqueChoice: true,
530531
elementChoices: [.simpleTypeSpecifier]
531532
),
532533
]

CodeGeneration/Sources/generate-swift-syntax/ChildNodeChoices.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,10 @@ extension Node {
8989
return nil
9090
}
9191
}
92-
} else if let node = self.collectionNode, node.elementChoices.count > 1 {
92+
} else if let node = self.collectionNode, node.elementChoices.count >= 1 {
93+
if !node.disableSameTypeForUniqueChoice && node.elementChoices.count == 1 {
94+
return []
95+
}
9396
return [
9497
ChildNodeChoices(
9598
name: "Element",

CodeGeneration/Sources/generate-swift-syntax/templates/swiftsyntax/RawSyntaxNodesFile.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,9 @@ func rawSyntaxNodesFile(nodesStartingWith: [Character]) -> SourceFileSyntax {
116116
}
117117

118118
if let node = node.collectionNode {
119-
let element = node.elementChoices.only != nil ? node.elementChoices.only!.raw.syntaxType : "Element"
119+
let element =
120+
!node.disableSameTypeForUniqueChoice && node.elementChoices.only != nil
121+
? node.elementChoices.only!.raw.syntaxType : "Element"
120122
DeclSyntax(
121123
"""
122124
public init(elements: [\(element)], arena: __shared SyntaxArena) {

CodeGeneration/Sources/generate-swift-syntax/templates/swiftsyntax/SyntaxCollectionsFile.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ let syntaxCollectionsFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
3131
public struct \(node.kind.syntaxType): SyntaxCollection, SyntaxHashable
3232
"""
3333
) {
34-
if let onlyElement = node.elementChoices.only {
35-
DeclSyntax("public typealias Element = \(onlyElement.syntaxType)")
34+
if !node.disableSameTypeForUniqueChoice && node.elementChoices.only != nil {
35+
DeclSyntax("public typealias Element = \(node.elementChoices.only!.syntaxType)")
3636
} else {
3737
for childNodeChoices in node.node.childrenNodeChoices() {
3838
childNodeChoices.enumDecl

0 commit comments

Comments
 (0)