Files
swift-mirror/test/SILOptimizer/di_property_wrappers_leak.swift
Erik Eckstein 9055e93af9 SIL: some improvements/fixes around assign_by_wrapper
* Refactoring: replace "Destination" and the ownership qualifier by a single "Mode".  This represents much better the mode how the instruction is to be lowered. NFC
* Make assign_by_wrapper printable and parseable.
* Fix lowering of the assign modes for indirect results of the init-closure: The indirect result was initialized and not assigned to. The fix is to insert a destroy_addr before calling the init closure. This fixes a memory lifetime error and/or a memory leak. Found by inspection.
* Fix an iterator-invalidation crash in RawSILInstLowering
* Add tests for lowering assign_by_wrapper.
2021-03-03 18:57:02 +01:00

78 lines
1.6 KiB
Swift

// RUN: %empty-directory(%t)
// RUN: %target-build-swift %s -o %t/a.out
// RUN: %target-codesign %t/a.out
// RUN: %target-run %t/a.out
// REQUIRES: executable_test
import StdlibUnittest
@propertyWrapper
struct Wrapper<T> {
var wrappedValue: T
}
struct TestWrappedValueLeak {
@Wrapper var wrapped: LifetimeTracked = LifetimeTracked(0)
var str: String
init() {
wrapped = LifetimeTracked(42)
str = ""
wrapped = LifetimeTracked(27)
}
init(conditionalInit: Bool) {
if (conditionalInit) {
wrapped = LifetimeTracked(42)
}
str = ""
wrapped = LifetimeTracked(27)
}
}
var propertyWrapperTests = TestSuite("Property Wrapper DI")
propertyWrapperTests.test("test wrapped value leak") {
_ = TestWrappedValueLeak()
_ = TestWrappedValueLeak(conditionalInit: true)
_ = TestWrappedValueLeak(conditionalInit: false)
}
protocol IntInitializable {
init(_ i: Int)
}
extension LifetimeTracked : IntInitializable {
convenience init(_ i: Int) {
self.init(i, identity: 0)
}
}
struct TestWrappedValueLeakGeneric<T : IntInitializable> {
@Wrapper var wrapped: T = T(0)
var str: String
init() {
wrapped = T(42)
str = ""
wrapped = T(27)
}
init(conditionalInit: Bool) {
if (conditionalInit) {
wrapped = T(42)
}
str = ""
wrapped = T(27)
}
}
propertyWrapperTests.test("test wrapped value leak - generic") {
_ = TestWrappedValueLeakGeneric<LifetimeTracked>()
_ = TestWrappedValueLeakGeneric<LifetimeTracked>(conditionalInit: true)
_ = TestWrappedValueLeakGeneric<LifetimeTracked>(conditionalInit: false)
}
runAllTests()