-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Parameter packs for functions with arbitrary parameters causes compiler crash when passed a function with more than zero parameters #69028
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
Comments
// Define a function that takes a closure with variadic parameters
func processVariadicFunction(_ function: (Int...) -> Int) {
// Create an array of integers
let numbers = [1, 2, 3, 4, 5]
// Call the closure with the array as variadic arguments
let result = function(numbers[0], numbers[1], numbers[2], numbers[3], numbers[4])
// Print the result
print("Result: \(result)")
}
// Define a closure that calculates the sum of integers
let sumClosure: (Int...) -> Int = { numbers in
return numbers.reduce(0, +)
}
// Call the processVariadicFunction with the sumClosure
processVariadicFunction(sumClosure) |
@Nau56 That's different from what I'm trying to do. Your example uses a variadic parameter, and mine uses a parameter pack. |
typealias MyFunctionPointer = (Int, Int) -> Void
func bar(func: MyFunctionPointer) {
func(1, 2)
}
let myFunction: MyFunctionPointer = { a, b in
print(a + b)
}
bar(func: myFunction) |
@Nau56 Sorry I'm not sure what that's supposed to be. |
Stack dump update and slightly simpler example: func callable<each Input>(_: ((repeat each Input)) -> Void) {}
func test(x: Int) {}
do {
callable(test)
}
|
I believe this is a known issue with reabstraction for tuple parameters containing pack expansions, but @slavapestov can probably confirm |
This was fixed in #70681. |
@rjmccall My snippet with the closure is still broken in the 6.0 beta that ships with Xcode 16.0 beta 1. The trace appears to be different from the previous two reported. Should we reopen this issue, or should I open a new one? func callable<each Input, Output>(wrappedValue: @escaping ((repeat each Input)) -> Output) {
}
func doSomething() {
callable(wrappedValue: { (num: Int) in num })
}
|
I have another repro, but I'm unsure if it's different or the same issue. public protocol SpyProtocol {
associatedtype Params
/// Number of times the function was called
var callCount: Int { get }
/// Params passed to the function
var callParams: [Params] { get }
}
public class Spy<each T>: SpyProtocol {
public private(set) var callCount: Int = 0
public private(set) var callParams: [(repeat each T)] = []
func increment() {
self.callCount += 1
}
func recordCall(_ params: (repeat each T)) {
self.callParams.append(params)
}
}
public func anyspy<each T, Result>(
_ closure: @escaping @Sendable (repeat each T) -> Result
) -> (Spy<repeat each T>, @Sendable (repeat each T) -> Result) {
let spy = Spy<repeat each T>()
func fn(_ params: repeat each T) -> Result {
let param = (repeat each params)
spy.increment()
spy.recordCall(param)
return closure()
}
return (spy, fn)
}
|
If it helps, here's my use case: |
Description
I'm trying to use parameter packs to create a function that takes and arbitrary function as a parameter. It's possible to create that function, but it's not possible to call it with a function or closure that has any parameters. Doing so causes the compiler to crash.
Stack trace passing a function with a parameter:
Steps to reproduce
This builds
This builds
This does not build
This does not build
Expected behavior
I would expect each of the above examples to build successfully.
Environment
Deployment target: macOS 14.0
Additional context: Development is being done in a Swift Package using Xcode.
The text was updated successfully, but these errors were encountered: