Skip to content

Commit 26abd52

Browse files
committed
[ASTGen] Adjustments to split FunctionParameterSyntax into multiple nodes for function parameters, closure parameters and enum parameters
Companion of swiftlang/swift-syntax#1455
1 parent 8fdde3e commit 26abd52

File tree

2 files changed

+13
-24
lines changed

2 files changed

+13
-24
lines changed

lib/ASTGen/Sources/ASTGen/Decls.swift

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,10 @@ extension ASTGenVisitor {
8989

9090
let firstName: UnsafeMutableRawPointer?
9191
let secondName: UnsafeMutableRawPointer?
92-
let type: UnsafeMutableRawPointer?
93-
94-
if let nodeFirstName = node.firstName,
95-
// Swift AST represnts "_" as nil.
96-
nodeFirstName.text != "_" {
92+
93+
let nodeFirstName = node.firstName
94+
if nodeFirstName.text != "_" {
95+
// Swift AST represnts "_" as nil.
9796
var text = nodeFirstName.text
9897
firstName = text.withUTF8 { buf in
9998
SwiftASTContext_getIdentifier(ctx, buf.baseAddress, buf.count)
@@ -111,11 +110,7 @@ extension ASTGenVisitor {
111110
secondName = nil
112111
}
113112

114-
if let typeSyntax = node.type {
115-
type = visit(typeSyntax).rawValue
116-
} else {
117-
type = nil
118-
}
113+
let type = visit(node.type).rawValue
119114

120115
return .decl(ParamDecl_create(ctx, loc, loc, firstName, loc, secondName, type, declContext))
121116
}

test/Macros/Inputs/syntax_macro_definitions.swift

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -697,15 +697,11 @@ public struct AddCompletionHandler: PeerMacro {
697697
newParameterList = parameterList.appending(completionHandlerParam)
698698
}
699699

700-
let callArguments: [String] = try parameterList.map { param in
701-
guard let argName = param.secondName ?? param.firstName else {
702-
throw CustomError.message(
703-
"@addCompletionHandler argument must have a name"
704-
)
705-
}
700+
let callArguments: [String] = parameterList.map { param in
701+
let argName = param.secondName ?? param.firstName
706702

707-
if let paramName = param.firstName, paramName.text != "_" {
708-
return "\(paramName.text): \(argName.text)"
703+
if param.firstName.text != "_" {
704+
return "\(param.firstName.text): \(argName.text)"
709705
}
710706

711707
return "\(argName.text)"
@@ -826,13 +822,11 @@ public struct WrapInType: PeerMacro {
826822
// Build a new function with the same signature that forwards arguments
827823
// to the the original function.
828824
let parameterList = funcDecl.signature.input.parameterList
829-
let callArguments: [String] = try parameterList.map { param in
830-
guard let argName = param.secondName ?? param.firstName else {
831-
throw CustomError.message("@wrapInType argument must have a name")
832-
}
825+
let callArguments: [String] = parameterList.map { param in
826+
let argName = param.secondName ?? param.firstName
833827

834-
if let paramName = param.firstName, paramName.text != "_" {
835-
return "\(paramName.text): \(argName.text)"
828+
if param.firstName.text != "_" {
829+
return "\(param.firstName.text): \(argName.text)"
836830
}
837831

838832
return "\(argName.text)"

0 commit comments

Comments
 (0)