mirror of
https://github.com/apple/swift.git
synced 2025-12-14 20:36:38 +01:00
Previously, we would get two copies, one accessing the pointee and one when we pass the pointee as a method as the implicit self argument. These copies are unsafe as they might introduce slicing. When addressable paramaters features are enabled, we no longer make these copies for the standard STL types. Custom smart pointers can replicate this by making the lifetime dependency between the implicit object parameter and the returned reference of operator* explicit via a lifetime annotation. rdar://154213694&128293252&112690482
42 lines
1.2 KiB
Swift
42 lines
1.2 KiB
Swift
// RUN: %target-run-simple-swift(-I %S/Inputs -cxx-interoperability-mode=upcoming-swift -enable-experimental-feature AddressableParameters)
|
|
|
|
// REQUIRES: executable_test
|
|
// REQUIRES: swift_feature_AddressableParameters
|
|
|
|
// https://github.com/apple/swift/issues/70226
|
|
// UNSUPPORTED: OS=windows-msvc
|
|
|
|
import StdlibUnittest
|
|
import StdUniquePtr
|
|
import CustomSmartPtr
|
|
import CxxStdlib
|
|
|
|
var AvoidCopiesSuite = TestSuite("AvoidRedundantCopies")
|
|
|
|
AvoidCopiesSuite.test("The pointee does not copy when passed as self") {
|
|
let up = getNonCopyableUniquePtr()
|
|
expectEqual(up.pointee.method(1), 42)
|
|
expectEqual(up.pointee.method(1), 42)
|
|
let cup = getCopyCountedUniquePtr();
|
|
expectEqual(cup.pointee.getCopies(), 0)
|
|
cup.pointee.method()
|
|
cup.pointee.constMethod()
|
|
let _ = cup.pointee.field
|
|
expectEqual(cup.pointee.getCopies(), 0)
|
|
let copy = cup.pointee
|
|
expectEqual(copy.getCopies(), 1)
|
|
}
|
|
|
|
AvoidCopiesSuite.test("The custom smart pointer pointee does not copy when passed as self") {
|
|
let myptr = getPtr();
|
|
expectEqual(myptr.pointee.getCopies(), 0)
|
|
myptr.pointee.method()
|
|
myptr.pointee.constMethod()
|
|
let _ = myptr.pointee.field
|
|
expectEqual(myptr.pointee.getCopies(), 0)
|
|
let copy = myptr.pointee
|
|
expectEqual(copy.getCopies(), 1)
|
|
}
|
|
|
|
runAllTests()
|