Skip to content

[ASTGen/Macros] Adjustments to split FunctionParameterSyntax into multiple nodes for function parameters, closure parameters and enum parameters #64697

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 5 additions & 10 deletions lib/ASTGen/Sources/ASTGen/Decls.swift
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,10 @@ extension ASTGenVisitor {

let firstName: UnsafeMutableRawPointer?
let secondName: UnsafeMutableRawPointer?
let type: UnsafeMutableRawPointer?

if let nodeFirstName = node.firstName,
// Swift AST represnts "_" as nil.
nodeFirstName.text != "_" {

let nodeFirstName = node.firstName
if nodeFirstName.text != "_" {
// Swift AST represents "_" as nil.
var text = nodeFirstName.text
firstName = text.withUTF8 { buf in
SwiftASTContext_getIdentifier(ctx, buf.baseAddress, buf.count)
Expand All @@ -111,11 +110,7 @@ extension ASTGenVisitor {
secondName = nil
}

if let typeSyntax = node.type {
type = visit(typeSyntax).rawValue
} else {
type = nil
}
let type = visit(node.type).rawValue

return .decl(ParamDecl_create(ctx, loc, loc, firstName, loc, secondName, type, declContext))
}
Expand Down
22 changes: 8 additions & 14 deletions test/Macros/Inputs/syntax_macro_definitions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -697,15 +697,11 @@ public struct AddCompletionHandler: PeerMacro {
newParameterList = parameterList.appending(completionHandlerParam)
}

let callArguments: [String] = try parameterList.map { param in
guard let argName = param.secondName ?? param.firstName else {
throw CustomError.message(
"@addCompletionHandler argument must have a name"
)
}
let callArguments: [String] = parameterList.map { param in
let argName = param.secondName ?? param.firstName

if let paramName = param.firstName, paramName.text != "_" {
return "\(paramName.text): \(argName.text)"
if param.firstName.text != "_" {
return "\(param.firstName.text): \(argName.text)"
}

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

if let paramName = param.firstName, paramName.text != "_" {
return "\(paramName.text): \(argName.text)"
if param.firstName.text != "_" {
return "\(param.firstName.text): \(argName.text)"
}

return "\(argName.text)"
Expand Down