- Notifications
You must be signed in to change notification settings - Fork10.6k
Open
Description
Description
When callingdeinitialize(count:) on anUnsafeMutablePointer containing a generic non-copyable type, the deinit of said type is not called unless optimizations are enabled. Callingdeinitialize(count:) works properly when applied to anUnsafeMutablePointer containing either a non-generic non-copyable type or a copyable class type.
Reproduction
structUnique<T:~Copyable>:~Copyable{publicvarvalue:Tpublicinit(_ value:consumingT){print("Initializing Unique<\(T.self)>")self.value= value}deinit{print("Deinitializing Unique<\(T.self)>")}}structNumber:~Copyable{publicvarvalue:Intpublicinit(_ value:consumingInt){print("Initializing Number(\(copy value))")self.value= value}deinit{print("Deinitializing Number(\(value))")}}finalclassNoisy<T:~Copyable>{publicvarvalue:Tpublicinit(_ value:consumingT){self.value= valueprint("Initializng Noisy<\(T.self)>")}deinit{print("Deinitializing Noisy<\(T.self)>")}}do{leta= UnsafeMutablePointer<Unique<Int>>.allocate(capacity:1) a.initialize(to:Unique(42)) a.deinitialize(count:1) // deinit should be called here. a.deallocate()}do{leta= UnsafeMutablePointer<Number>.allocate(capacity:1) a.initialize(to:Number(42)) a.deinitialize(count:1) // deinit is called here. a.deallocate()}do{leta= UnsafeMutablePointer<Noisy<Int>>.allocate(capacity:1) a.initialize(to:Noisy(42)) a.deinitialize(count:1) // deinit is called here. a.deallocate()}
Expected behavior
Expected output at all optimization levels:
Initializing Unique<Int>Deinitializing Unique<Int>Initializing Number(42)Deinitializing Number(42)Initializng Noisy<Int>Deinitializing Noisy<Int>Actual output at -ONone:
Initializing Unique<Int>Initializing Number(42)Deinitializing Number(42)Initializng Noisy<Int>Deinitializing Noisy<Int>Environment
Swiftc 6.2 x86-64 Linux
Additional information
No response