[swift-users] Compiler refuses non-escaping closure calls in nested function

Joe Groff jgroff at apple.com
Mon Oct 10 12:17:08 CDT 2016


> On Oct 9, 2016, at 5:07 PM, Jean-Denis Muys via swift-users <swift-users at swift.org> wrote:
> 
> Here is a contrived reduction of my problem
> 
> func mainFunction(closure:(Int) -> Int) -> Int {
> 
>     func closureDoubled(_ n: Int) -> Int {
>         let result = closure(n)
>         return 2*result
>     }
> 
>     let temp1 = closure(1)
>     let temp2 = closureDoubled(1)
>     return temp1+temp2
> }
> 
> The line "let result = closure(n)" is refused by the compiler with the error message "declaration over non closing parameter may allow it to escape".
> 
> First off, while I know what an escaping or a non-escaping closure is, I find this error message utterly impossible to understand. To begin with, the sentence "non closing parameter" is meaningless to me.
> 
> In any case, my main function is passed a non-escaping closure. I want to call it from inside it, the compiler is ok with. I want also to call it from a nested function, but the compiler disagrees.
> 
> I believe the compiler should not complain here. Did I miss anything?

I agrre that the compiler should be able to determine that the closure is nonescaping in this case, and that that diagnostic is in dire need of revision. Would you be able to file a couple of bug reports, one to allow this example, and one to improve the error message?

-Joe

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20161010/56caa9b6/attachment.html>


More information about the swift-users mailing list