Possible major bug in parameter packs when used with closures

I do not know whether this category is right or not but I just had a minor and a major problem regarding the parameter packs.

The minor one is that the @autoclosure annotation is being ignored when being used with repeat keyword so that when:

func foo<T>(_ bar: repeat @autoclosure () -> each T)

is being called as

foo("some string", 1234, true)

Swift throws error

Cannot convert value of type 'String' to expected argument type '() -> _'

But when it is being called as

foo({"some string"}, {1234}, {true})

There is no error.

BUT the big problem I just uncovered about the parameter packs is about escaping/nonescaping properties of the closures.

func foo<T>(_ bar: repeat () -> each T) {
   someEscapingClosureMethod {
      SomeType.someOtherParameterPackMethod(repeat (each bar)())
   }
}

The bar closure apparently should be annotated @escaping since it is captured by an escaping closure. But it isn’t and the code builds without any errors. But when the code runs it throws EXC_BAD_ACCESS.

func foo<T>(_ bar: repeat @escaping () -> each T) {
   someEscapingClosureMethod {
      SomeType.someOtherParameterPackMethod(repeat (each bar)())
   }
}

builds and runs without any problems.

4 Likes

Interesting. Would you be able to file issues on GitHub for these discoveries?

1 Like

FWIW the unimplemented interaction between parameter packs and autoclosure already has a bug so there is no need to file it again: @autoclosure is not working for variadic generics · Issue #66413 · swiftlang/swift · GitHub. The other bug I don't recall seeing duplicates of so please do file an issue for it!

4 Likes

@xwu @Slava_Pestov Thank you both for the suggestions, I have filed an issue on GitHub for the escaping closure bug.

2 Likes