[swift-dev] Help needed: SE-0035 design detail

Daniel Duan daniel at duan.org
Mon Apr 11 15:28:03 CDT 2016


> Joe Groff via swift-dev <swift-dev <at> swift.org> writes:
>
>   return local // returning forms a closure, so ref is escapable

My plan was to check all return statements with FuncDecl as results, if
any of them has inout captures, complain.

But this diagnosis is too coarse. A function can capture from any level of
outer scope, so sometimes it's safe to let a inout escape, as long as the
reference is still inside the scope it's captured from. Example:

func a(inout x: Int) -> () -> Void {
    func b() -> () -> Void {
        func c() {
            _ = x
        }
        return c // is this safe? We'll seeā€¦
    }

    let f = b() // 'x' captured by f hasn't *really* escaped.

    return f // now we have a problem
}

It's unclear whether the statement 'return c' is problematic.

So there are two paths:

1. make *any* escaping inout capture an error
2. track down original scope of each inout capture, compare it with the return
   statement.

At the moment I haven't looked into how feasible 2 is.

What's your opinion?


More information about the swift-dev mailing list