[swift-evolution] [swift-evolution-announce] [Review] SE-0176: Enforce Exclusive Access to Memory

Devin Coughlin dcoughlin at apple.com
Tue May 16 21:47:31 CDT 2017


> On May 16, 2017, at 5:50 PM, Jordan Rose via swift-evolution <swift-evolution at swift.org> wrote:
> 
>> 
>> On May 16, 2017, at 17:37, John McCall <rjmccall at apple.com <mailto:rjmccall at apple.com>> wrote:
>> 
>> I'm not sure what you're proposing to disallow here.  I don't think we should add any extra restrictions in order to avoid problems with unfortunate uses of withoutActuallyEscaping, though.
> 
> Here's a small, currently-legal, re-entrant case that only calls the wrapped function once.
> 
> var global: (() -> Void)?
> func problem(_ fn: () -> Void) {
>   withoutActuallyEscaping(fn) { wrappedFn in
>     global = wrappedFn
>     fn() // note: not wrappedFn
>   }
> }
> func test() {
>   var local = 0
>   problem() {
>     local += 1
>     if let callback = global {
>       global = nil
>       callback()
>     }
>     print(local)
>   }
> }
> test()
> 
> We could prevent this by saying it's not legal to refer to 'fn' inside the callback for withoutActuallyEscaping, and you have to use 'wrappedFn' instead.

This rule seems quite reasonable to me and possible to check statically. It is only feasible to enforce the rule statically when the closure passed to the ‘do’ argument of withoutActuallyEscaping(_:do:) hasn’t itself escaped — but since ‘fn’ here is noescape it can’t be referred to inside a closure that escapes.

Devin
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170516/f50d4599/attachment.html>


More information about the swift-evolution mailing list