func test(v: inout [Int]) {
let closure = {
v += [1]
}
closure()
}
var a = [0]
test(v: &a)
It won't compile, the error is: "escaping closure captures 'inout' parameter 'v'". But why, though? Is closure actually escapes anywhere? Is it how it's supposed to be, or it's a compiler bug?
My swiftc is 5.7, swiftc --version is swift-driver version: 1.62.1 Apple Swift version 5.7 (swiftlang-5.7.0.123.7 clang-1400.0.29.50).
Thank you @taylorswift . Yes, it seems I didn't quite understand what an "escaping" closure means precisely in Swift, because the docs are a bit muddy on that (they didn't list this rule at all). Thanks.
The analysis whether a closure is escaping is not very sophisticated currently, and doesn't look past the immediate context of where the closure literal appears. When you pass the closure as an immediate argument to a method call that takes a nonescaping parameter, or you immediately apply the closure literal, then we can determine it's nonescaping. In pretty much any other case, we assume it may escape.