Skip to content

Commit b36fbc3

Browse files
authored
Merge pull request #1646 from ahoppen/ahoppen/opaque-parameter-types
Migrate generics to opaque parameter types where possible
2 parents 8cd23e5 + 178a9d8 commit b36fbc3

File tree

51 files changed

+945
-974
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+945
-974
lines changed

CodeGeneration/Sources/Utils/SyntaxBuildableType.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ public struct SyntaxBuildableType: Hashable {
160160
}
161161

162162
/// Wraps a type in an optional depending on whether `isOptional` is true.
163-
public func optionalWrapped<TypeNode: TypeSyntaxProtocol>(type: TypeNode) -> TypeSyntax {
163+
public func optionalWrapped(type: some TypeSyntaxProtocol) -> TypeSyntax {
164164
if isOptional {
165165
return TypeSyntax(OptionalTypeSyntax(wrappedType: type))
166166
} else {
@@ -169,7 +169,7 @@ public struct SyntaxBuildableType: Hashable {
169169
}
170170

171171
/// Wraps a type in an optional chaining depending on whether `isOptional` is true.
172-
public func optionalChained<ExprNode: ExprSyntaxProtocol>(expr: ExprNode) -> ExprSyntax {
172+
public func optionalChained(expr: some ExprSyntaxProtocol) -> ExprSyntax {
173173
if isOptional {
174174
return ExprSyntax(OptionalChainingExprSyntax(expression: expr))
175175
} else {
@@ -178,7 +178,7 @@ public struct SyntaxBuildableType: Hashable {
178178
}
179179

180180
/// Wraps a type in a force unwrap expression depending on whether `isOptional` is true.
181-
public func forceUnwrappedIfNeeded<ExprNode: ExprSyntaxProtocol>(expr: ExprNode) -> ExprSyntax {
181+
public func forceUnwrappedIfNeeded(expr: some ExprSyntaxProtocol) -> ExprSyntax {
182182
if isOptional {
183183
return ExprSyntax(ForcedValueExprSyntax(expression: expr))
184184
} else {

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ let rawSyntaxNodesFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
7272
}
7373
}
7474

75-
try InitializerDeclSyntax("public init?<T>(_ other: T) where T : RawSyntaxNodeProtocol") {
75+
try InitializerDeclSyntax("public init?(_ other: some RawSyntaxNodeProtocol)") {
7676
for (swiftName, typeName) in choices {
7777
StmtSyntax(
7878
"""
@@ -145,7 +145,7 @@ let rawSyntaxNodesFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
145145

146146
DeclSyntax(
147147
"""
148-
public init?<Node: RawSyntaxNodeProtocol>(_ other: Node) {
148+
public init?(_ other: some RawSyntaxNodeProtocol) {
149149
guard Self.isKindOf(other.raw) else { return nil }
150150
self.init(unchecked: other.raw)
151151
}
@@ -155,7 +155,7 @@ let rawSyntaxNodesFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
155155
if node.isBase {
156156
DeclSyntax(
157157
"""
158-
public init<Node: Raw\(raw: node.name)NodeProtocol>(_ other: Node) {
158+
public init(_ other: some Raw\(raw: node.name)NodeProtocol) {
159159
self.init(unchecked: other.raw)
160160
}
161161
"""

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ let syntaxBaseNodesFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
6363
DeclSyntax(
6464
"""
6565
/// Create a `\(raw: node.name)` node from a specialized syntax node.
66-
public init<S: \(raw: node.name)Protocol>(_ syntax: S) {
66+
public init(_ syntax: some \(raw: node.name)Protocol) {
6767
// We know this cast is going to succeed. Go through init(_: SyntaxData)
6868
// to do a sanity check and verify the kind matches in debug builds and get
6969
// maximum performance in release builds.
@@ -75,7 +75,7 @@ let syntaxBaseNodesFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
7575
DeclSyntax(
7676
"""
7777
/// Create a `\(raw: node.name)` node from a specialized optional syntax node.
78-
public init?<S: \(raw: node.name)Protocol>(_ syntax: S?) {
78+
public init?(_ syntax: (some \(raw: node.name)Protocol)?) {
7979
guard let syntax = syntax else { return nil }
8080
self.init(syntax)
8181
}
@@ -103,7 +103,7 @@ let syntaxBaseNodesFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
103103
"""
104104
)
105105

106-
try InitializerDeclSyntax("public init?<S: SyntaxProtocol>(_ node: S)") {
106+
try InitializerDeclSyntax("public init?(_ node: some SyntaxProtocol)") {
107107
try SwitchExprSyntax("switch node.raw.kind") {
108108
SwitchCaseListSyntax {
109109
SwitchCaseSyntax(

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ let syntaxCollectionsFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
8181
if choiceNode.isBase {
8282
DeclSyntax(
8383
"""
84-
public init<Node: \(raw: choiceNode.name)Protocol>(_ node: Node) {
84+
public init(_ node: some \(raw: choiceNode.name)Protocol) {
8585
self = .\(raw: choiceNode.swiftSyntaxKind)(\(raw: choiceNode.name)(node))
8686
}
8787
"""
@@ -98,7 +98,7 @@ let syntaxCollectionsFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
9898
}
9999
}
100100

101-
try InitializerDeclSyntax("public init?<S: SyntaxProtocol>(_ node: S)") {
101+
try InitializerDeclSyntax("public init?(_ node: some SyntaxProtocol)") {
102102
for choiceName in node.collectionElementChoices ?? [] {
103103
let choiceNode = SYNTAX_NODE_MAP[choiceName]!
104104
StmtSyntax(
@@ -144,7 +144,7 @@ let syntaxCollectionsFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
144144

145145
DeclSyntax(
146146
"""
147-
public init?<S: SyntaxProtocol>(_ node: S) {
147+
public init?(_ node: some SyntaxProtocol) {
148148
guard node.raw.kind == .\(raw: node.swiftSyntaxKind) else { return nil }
149149
self._syntaxNode = node._syntaxNode
150150
}

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

Lines changed: 18 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func syntaxNode(emitKind: String) -> SourceFileSyntax {
5858

5959
DeclSyntax(
6060
"""
61-
public init?<S: SyntaxProtocol>(_ node: S) {
61+
public init?(_ node: some SyntaxProtocol) {
6262
guard node.raw.kind == .\(raw: node.swiftSyntaxKind) else { return nil }
6363
self._syntaxNode = node._syntaxNode
6464
}
@@ -306,7 +306,7 @@ private func generateSyntaxChildChoices(for child: Child) throws -> EnumDeclSynt
306306
if let choiceNode = SYNTAX_NODE_MAP[choice.syntaxKind], choiceNode.isBase {
307307
DeclSyntax(
308308
"""
309-
public init<Node: \(raw: choiceNode.name)Protocol>(_ node: Node) {
309+
public init(_ node: some \(raw: choiceNode.name)Protocol) {
310310
self = .\(raw: choice.swiftName)(\(raw: choiceNode.name)(node))
311311
}
312312
"""
@@ -323,7 +323,7 @@ private func generateSyntaxChildChoices(for child: Child) throws -> EnumDeclSynt
323323
}
324324
}
325325

326-
try! InitializerDeclSyntax("public init?<S: SyntaxProtocol>(_ node: S)") {
326+
try! InitializerDeclSyntax("public init?(_ node: some SyntaxProtocol)") {
327327
for choice in choices {
328328
StmtSyntax(
329329
"""
@@ -358,44 +358,34 @@ fileprivate extension Node {
358358
return "public init()"
359359
}
360360

361-
var genericParamNames: [String: Int] = [:]
362-
var genericParams: [String] = []
363-
364361
func createFunctionParameterSyntax(for child: Child) -> FunctionParameterSyntax {
365-
var paramType: String
362+
var paramType: TypeSyntax
366363
if !child.kind.isNodeChoicesEmpty {
367-
paramType = child.name
364+
paramType = "\(raw: child.name)"
368365
} else if child.hasBaseType {
369366
if optionalBaseAsMissing {
370-
paramType = "Missing\(child.typeName)"
367+
paramType = "Missing\(raw: child.typeName)"
371368
} else {
372-
// If we have a base type, make the initializer generic over its
373-
// protocol instead.
374-
let index = child.swiftName.index(child.swiftName.startIndex, offsetBy: 1)
375-
paramType = child.swiftName[..<index].uppercased()
376-
377-
let paramCount = (genericParamNames[paramType] ?? 0) + 1
378-
genericParamNames[paramType] = paramCount
379-
380-
if paramCount > 1 {
381-
paramType += "\(paramCount)"
382-
}
383-
genericParams.append("\(paramType): \(child.typeName)Protocol")
369+
paramType = "some \(raw: child.typeName)Protocol"
384370
}
385371
} else {
386-
paramType = child.typeName
372+
paramType = "\(raw: child.typeName)"
387373
}
388374

389375
if child.isOptional {
390-
paramType += "?"
376+
if paramType.is(ConstrainedSugarTypeSyntax.self) {
377+
paramType = "(\(paramType))?"
378+
} else {
379+
paramType = "\(paramType)?"
380+
}
391381
}
392382

393383
return FunctionParameterSyntax(
394384
leadingTrivia: .newline,
395385
firstName: child.isUnexpectedNodes ? .wildcardToken(trailingTrivia: .space) : .identifier(child.swiftName),
396386
secondName: child.isUnexpectedNodes ? .identifier(child.swiftName) : nil,
397387
colon: .colonToken(),
398-
type: TypeSyntax(stringLiteral: paramType),
388+
type: paramType,
399389
defaultArgument: child.defaultInitialization
400390
)
401391
}
@@ -411,26 +401,10 @@ fileprivate extension Node {
411401
.with(\.leadingTrivia, .newline)
412402
}
413403

414-
if genericParams.isEmpty {
415-
return """
416-
public init(
417-
\(params)
418-
)
419-
"""
420-
} else {
421-
let generics = GenericParameterClauseSyntax(
422-
genericParameterList: GenericParameterListSyntax {
423-
for param in genericParams {
424-
GenericParameterSyntax(name: .identifier(param))
425-
}
426-
}
404+
return """
405+
public init(
406+
\(params)
427407
)
428-
429-
return """
430-
public init\(generics)(
431-
\(params)
432-
)
433-
"""
434-
}
408+
"""
435409
}
436410
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,15 +96,15 @@ let syntaxTransformFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
9696

9797
DeclSyntax(
9898
"""
99-
public func visit<T: SyntaxChildChoices>(_ node: T) -> ResultType {
99+
public func visit(_ node: some SyntaxChildChoices) -> ResultType {
100100
return visit(Syntax(node))
101101
}
102102
"""
103103
)
104104

105105
DeclSyntax(
106106
"""
107-
public func visitChildren<SyntaxType: SyntaxProtocol>(_ node: SyntaxType) -> [ResultType] {
107+
public func visitChildren(_ node: some SyntaxProtocol) -> [ResultType] {
108108
let syntaxNode = Syntax(node)
109109
return NonNilRawSyntaxChildren(syntaxNode, viewMode: .sourceAccurate).map { rawChild in
110110
let child = Syntax(SyntaxData(rawChild, parent: syntaxNode))

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ let syntaxVisitorFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
4444
"""
4545
/// Walk all nodes of the given syntax tree, calling the corresponding `visit`
4646
/// function for every node that is being visited.
47-
public func walk<SyntaxType: SyntaxProtocol>(_ node: SyntaxType) {
47+
public func walk(_ node: some SyntaxProtocol) {
4848
visit(node.data)
4949
}
5050
"""
@@ -135,7 +135,7 @@ let syntaxVisitorFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
135135

136136
DeclSyntax(
137137
"""
138-
private func visitChildren<SyntaxType: SyntaxProtocol>(_ node: SyntaxType) {
138+
private func visitChildren(_ node: some SyntaxProtocol) {
139139
let syntaxNode = Syntax(node)
140140
for childRaw in NonNilRawSyntaxChildren(syntaxNode, viewMode: viewMode) {
141141
let childData = SyntaxData(childRaw, parent: syntaxNode)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ let triviaPiecesFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
5555
/// Prints the provided trivia as they would be written in a source file.
5656
///
5757
/// - Parameter stream: The stream to which to print the trivia.
58-
public func write<Target>(to target: inout Target) where Target: TextOutputStream
58+
public func write(to target: inout some TextOutputStream)
5959
"""
6060
) {
6161
DeclSyntax(

Sources/SwiftBasicFormat/BasicFormat.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ open class BasicFormat: SyntaxRewriter {
139139
// MARK: - Customization points
140140

141141
/// Whether a leading newline on `token` should be added.
142-
open func requiresIndent<T: SyntaxProtocol>(_ node: T) -> Bool {
142+
open func requiresIndent(_ node: some SyntaxProtocol) -> Bool {
143143
return node.requiresIndent
144144
}
145145

Sources/SwiftCompilerPluginMessageHandling/Macros.swift

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,21 +46,21 @@ extension CompilerPluginMessageHandler {
4646

4747
switch macroDefinition {
4848
case let exprMacroDef as ExpressionMacro.Type:
49-
func _expand<Node: FreestandingMacroExpansionSyntax>(node: Node) throws -> ExprSyntax {
49+
func _expand(node: some FreestandingMacroExpansionSyntax) throws -> ExprSyntax {
5050
try exprMacroDef.expansion(of: node, in: context)
5151
}
5252
let rewritten = try _openExistential(macroSyntax, do: _expand)
5353
expandedSource = rewritten.formattedExpansion(macroDefinition.formatMode)
5454

5555
case let declMacroDef as DeclarationMacro.Type:
56-
func _expand<Node: FreestandingMacroExpansionSyntax>(node: Node) throws -> [DeclSyntax] {
56+
func _expand(node: some FreestandingMacroExpansionSyntax) throws -> [DeclSyntax] {
5757
try declMacroDef.expansion(of: node, in: context)
5858
}
5959
let rewritten = try _openExistential(macroSyntax, do: _expand)
6060
expandedSource = CodeBlockItemListSyntax(rewritten.map { CodeBlockItemSyntax(item: .decl($0)) }).formattedExpansion(macroDefinition.formatMode)
6161

6262
case let codeItemMacroDef as CodeItemMacro.Type:
63-
func _expand<Node: FreestandingMacroExpansionSyntax>(node: Node) throws -> [CodeBlockItemSyntax] {
63+
func _expand(node: some FreestandingMacroExpansionSyntax) throws -> [CodeBlockItemSyntax] {
6464
try codeItemMacroDef.expansion(of: node, in: context)
6565
}
6666
let rewritten = try _openExistential(macroSyntax, do: _expand)
@@ -128,8 +128,8 @@ extension CompilerPluginMessageHandler {
128128

129129
// Local function to expand a member atribute macro once we've opened up
130130
// the existential.
131-
func expandMemberAttributeMacro<Node: DeclGroupSyntax>(
132-
_ node: Node
131+
func expandMemberAttributeMacro(
132+
_ node: some DeclGroupSyntax
133133
) throws -> [AttributeSyntax] {
134134
return try attachedMacro.expansion(
135135
of: attributeNode,
@@ -158,8 +158,8 @@ extension CompilerPluginMessageHandler {
158158

159159
// Local function to expand a member macro once we've opened up
160160
// the existential.
161-
func expandMemberMacro<Node: DeclGroupSyntax>(
162-
_ node: Node
161+
func expandMemberMacro(
162+
_ node: some DeclGroupSyntax
163163
) throws -> [DeclSyntax] {
164164
return try attachedMacro.expansion(
165165
of: attributeNode,
@@ -196,8 +196,8 @@ extension CompilerPluginMessageHandler {
196196

197197
// Local function to expand a conformance macro once we've opened up
198198
// the existential.
199-
func expandConformanceMacro<Node: DeclGroupSyntax>(
200-
_ node: Node
199+
func expandConformanceMacro(
200+
_ node: some DeclGroupSyntax
201201
) throws -> [(TypeSyntax, GenericWhereClauseSyntax?)] {
202202
return try attachedMacro.expansion(
203203
of: attributeNode,

Sources/SwiftCompilerPluginMessageHandling/PluginMacroExpansionContext.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -242,8 +242,8 @@ extension PluginMacroExpansionContext: MacroExpansionContext {
242242
diagnostics.append(diagnostic)
243243
}
244244

245-
public func location<Node: SyntaxProtocol>(
246-
of node: Node,
245+
public func location(
246+
of node: some SyntaxProtocol,
247247
at positionMode: PositionInSyntaxNode,
248248
filePathMode: SourceLocationFilePathMode
249249
) -> AbstractSourceLocation? {

Sources/SwiftDiagnostics/DiagnosticsFormatter.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,8 @@ public struct DiagnosticsFormatter {
8181
self.colorize = colorize
8282
}
8383

84-
public static func annotatedSource<SyntaxType: SyntaxProtocol>(
85-
tree: SyntaxType,
84+
public static func annotatedSource(
85+
tree: some SyntaxProtocol,
8686
diags: [Diagnostic],
8787
contextSize: Int = 2,
8888
colorize: Bool = false
@@ -92,10 +92,10 @@ public struct DiagnosticsFormatter {
9292
}
9393

9494
/// Colorize the given source line by applying highlights from diagnostics.
95-
private func colorizeSourceLine<SyntaxType: SyntaxProtocol>(
95+
private func colorizeSourceLine(
9696
_ annotatedLine: AnnotatedSourceLine,
9797
lineNumber: Int,
98-
tree: SyntaxType,
98+
tree: some SyntaxProtocol,
9999
sourceLocationConverter slc: SourceLocationConverter
100100
) -> String {
101101
guard colorize, !annotatedLine.diagnostics.isEmpty else {
@@ -181,9 +181,9 @@ public struct DiagnosticsFormatter {
181181
/// - Parameters:
182182
/// - suffixTexts: suffix text to be printed at the given absolute
183183
/// locations within the source file.
184-
func annotatedSource<SyntaxType: SyntaxProtocol>(
184+
func annotatedSource(
185185
fileName: String?,
186-
tree: SyntaxType,
186+
tree: some SyntaxProtocol,
187187
diags: [Diagnostic],
188188
indentString: String,
189189
suffixTexts: [AbsolutePosition: String],
@@ -313,8 +313,8 @@ public struct DiagnosticsFormatter {
313313
}
314314

315315
/// Print given diagnostics for a given syntax tree on the command line
316-
public func annotatedSource<SyntaxType: SyntaxProtocol>(
317-
tree: SyntaxType,
316+
public func annotatedSource(
317+
tree: some SyntaxProtocol,
318318
diags: [Diagnostic]
319319
) -> String {
320320
return annotatedSource(

0 commit comments

Comments
 (0)