Files
swift-mirror/test/SILOptimizer/moveonly_copyable_wrapper_capture.swift
Joe Groff 90e1ecb864 Consistently mark borrowing and consuming parameters for move-only checking when captured.
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
2024-05-01 13:41:28 -07:00

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}}
}
}