mirror of
https://github.com/apple/swift.git
synced 2025-12-14 20:36:38 +01:00
When a `borrowing` or `consuming` parameter is captured by a closure, we emit references to the binding within the closure as if it is non-implicitly copyable, but we didn't mark the bindings inside the closure for move-only checking to ensure the uses were correct, so improper consumes would go undiagnosed and lead to assertion failures, compiler crashes, and/or miscompiles. Fixes rdar://127382105
41 lines
1.2 KiB
Swift
41 lines
1.2 KiB
Swift
// RUN: %target-swift-frontend -emit-sil -verify %s
|
|
|
|
class Class {}
|
|
//struct Class : ~Copyable {}
|
|
|
|
func consume(_: consuming Class) {}
|
|
func nonescapingClosure(_ body: () -> ()) {
|
|
body()
|
|
}
|
|
|
|
func testNonescapingCaptureConsuming(x: consuming Class) { // expected-error{{}}
|
|
nonescapingClosure { consume(x) } // expected-note{{consumed here}}
|
|
}
|
|
|
|
// TODO: `defer` should be allowed to consume local bindings
|
|
func testDeferCaptureConsuming(x: consuming Class) { // expected-error{{}}
|
|
defer { consume(x) } // expected-note{{consumed here}}
|
|
do {}
|
|
}
|
|
|
|
func testLocalFunctionCaptureConsuming(x: consuming Class) {
|
|
func local() {
|
|
consume(x) // expected-error{{cannot be consumed when captured by an escaping closure}}
|
|
}
|
|
}
|
|
|
|
func testNonescapingCaptureBorrowing(x: borrowing Class) { // expected-error{{}}
|
|
nonescapingClosure { consume(x) } // expected-note{{consumed here}}
|
|
}
|
|
|
|
func testDeferCaptureBorrowing(x: borrowing Class) { // expected-error{{}}
|
|
defer { consume(x) } // expected-note{{consumed here}}
|
|
do {}
|
|
}
|
|
|
|
func testLocalFunctionCaptureBorrowing(x: borrowing Class) { // expected-error{{borrowed and cannot be consumed}}
|
|
func local() {
|
|
consume(x) // expected-note{{consumed here}}
|
|
}
|
|
}
|