mirror of
https://github.com/apple/swift.git
synced 2025-12-14 20:36:38 +01:00
withoutActuallyEscaping has a signature like `<T..., U, V, W> (@nonescaping (T...) throws<U> -> V, (@escaping (T...) throws<U> -> V) -> W) -> W, but our type system for functions unfortunately isn't quite that expressive yet, so we need to special-case it. Set up the necessary type system when resolving an overload set to reference withoutActuallyEscaping, and if a type check succeeds, build a MakeTemporarilyEscapableExpr to represent it in the type-checked AST.
67 lines
2.0 KiB
Swift
67 lines
2.0 KiB
Swift
// RUN: %target-swift-frontend -module-name main -typecheck -verify -swift-version 3 %s
|
|
// RUN: %target-swift-frontend -module-name main -typecheck -verify -swift-version 4 %s
|
|
|
|
// Some convenient test points to escape closures to
|
|
var x: (Int) -> Int = { $0 }
|
|
var y: (inout Int, Int) -> Void = { $0 = $1 }
|
|
var z: (Int, Int, Int) throws -> Int = { $2 }
|
|
|
|
func escapeX(_ xx: (Int) -> Int, _ value: Int) { // expected-note* {{non-escaping}}
|
|
x = xx // expected-error{{non-escaping parameter}}
|
|
withoutActuallyEscaping(xx) { escapableXX in
|
|
x = xx // expected-error{{non-escaping parameter}}
|
|
x = escapableXX
|
|
x = xx // expected-error{{non-escaping parameter}}
|
|
|
|
_ = x(value)
|
|
_ = xx(value)
|
|
_ = escapableXX(value)
|
|
}
|
|
withoutActuallyEscaping(xx, do: { escapableXX in
|
|
x = escapableXX
|
|
})
|
|
Swift.withoutActuallyEscaping(xx) { escapableXX in
|
|
x = escapableXX
|
|
}
|
|
Swift.withoutActuallyEscaping(xx, do: { escapableXX in
|
|
x = escapableXX
|
|
})
|
|
x = xx // expected-error{{non-escaping parameter}}
|
|
}
|
|
|
|
func escapeY(_ yy: (inout Int, Int) -> Void, _ value: inout Int) { // expected-note{{non-escaping}}
|
|
y = yy // expected-error{{non-escaping parameter}}
|
|
withoutActuallyEscaping(yy) { escapableYY in
|
|
y = escapableYY
|
|
|
|
y(&value, value)
|
|
yy(&value, value)
|
|
escapableYY(&value, value)
|
|
}
|
|
}
|
|
|
|
func escapeZ(_ zz: (Int, Int, Int) throws -> Int) { // expected-note{{non-escaping}}
|
|
z = zz // expected-error{{non-escaping parameter}}
|
|
withoutActuallyEscaping(zz) { escapableZZ in
|
|
z = escapableZZ
|
|
}
|
|
}
|
|
|
|
func returnThroughWAE(_ xx: (Int) -> Int, _ value: Int) -> Int {
|
|
return withoutActuallyEscaping(xx) { escapableXX in
|
|
x = escapableXX
|
|
return x(value)
|
|
}
|
|
}
|
|
|
|
func rethrowThroughWAE(_ zz: (Int, Int, Int) throws -> Int, _ value: Int) throws {
|
|
try withoutActuallyEscaping(zz) { escapableZZ in
|
|
_ = try zz(value, value, value)
|
|
_ = try escapableZZ(value, value, value)
|
|
}
|
|
}
|
|
|
|
let _: ((Int) -> Int, (@escaping (Int) -> Int) -> ()) -> ()
|
|
= withoutActuallyEscaping(_:do:) // expected-error{{}}
|
|
|