mirror of
https://github.com/apple/swift.git
synced 2025-12-14 20:36:38 +01:00
* Migrate from `UnsafePointer<Void>` to `UnsafeRawPointer`. As proposed in SE-0107: UnsafeRawPointer. `void*` imports as `UnsafeMutableRawPointer`. `const void*` imports as `UnsafeRawPointer`. Occurrences of `UnsafePointer<Void>` are replaced with UnsafeRawPointer. * Migrate overlays from UnsafePointer<Void> to UnsafeRawPointer. This requires explicit memory binding in several places, particularly in NSData and CoreAudio. * Fix a bunch of test cases for Void->Raw migration. * qsort takes IUO values * Bridge `Unsafe[Mutable]RawPointer as `void [const] *`. * Parse #dsohandle as UnsafeMutableRawPointer * Update a bunch of test cases for Void->Raw migration. * Trivial fix for the SceneKit test case. * Add an UnsafeRawPointer self initializer. This is unfortunately necessary for assignment between types imported from C. * Tiny simplification of the initializer.
54 lines
1.5 KiB
Swift
54 lines
1.5 KiB
Swift
// RUN: %target-run-simple-swift | FileCheck %s
|
|
// REQUIRES: executable_test
|
|
|
|
// REQUIRES: objc_interop
|
|
|
|
import Foundation
|
|
|
|
class Canary: NSObject {
|
|
deinit {
|
|
print("died")
|
|
}
|
|
}
|
|
|
|
var CanaryAssocObjectHandle: UInt8 = 0
|
|
|
|
|
|
// Attach an associated object with a loud deinit so we can see that the
|
|
// object died.
|
|
func hangCanary(_ o: AnyObject) {
|
|
objc_setAssociatedObject(o, &CanaryAssocObjectHandle, Canary(),
|
|
.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
|
|
}
|
|
|
|
// CHECK-LABEL: NSData:
|
|
print("NSData:")
|
|
autoreleasepool {
|
|
var bytes: UnsafeMutablePointer<UInt8>
|
|
repeat {
|
|
let data = NSData(bytes: [2, 3, 5, 7] as [UInt8], length: 4)
|
|
hangCanary(data)
|
|
bytes = UnsafeMutablePointer<UInt8>(data.bytes.assumingMemoryBound(to: UInt8.self))
|
|
} while false // CHECK-NOT: died
|
|
print(bytes[0]) // CHECK: 2
|
|
print(bytes[1]) // CHECK-NEXT: 3
|
|
print(bytes[2]) // CHECK-NEXT: 5
|
|
print(bytes[3]) // CHECK-NEXT: 7
|
|
} // CHECK-NEXT: died
|
|
|
|
// CHECK-LABEL: AnyObject:
|
|
print("AnyObject:")
|
|
autoreleasepool {
|
|
var bytes: UnsafeMutablePointer<UInt8>
|
|
repeat {
|
|
let data = NSData(bytes: [11, 13, 17, 19] as [UInt8], length: 4)
|
|
hangCanary(data)
|
|
let dataAsAny: AnyObject = data
|
|
bytes = UnsafeMutablePointer<UInt8>(dataAsAny.bytes!.assumingMemoryBound(to: UInt8.self))
|
|
} while false // CHECK-NOT: died
|
|
print(bytes[0]) // CHECK: 11
|
|
print(bytes[1]) // CHECK-NEXT: 13
|
|
print(bytes[2]) // CHECK-NEXT: 17
|
|
print(bytes[3]) // CHECK-NEXT: 19
|
|
} // CHECK-NEXT: died
|