Files
swift-mirror/test/Interpreter/SDK/objc_inner_pointer.swift
Dave Abrahams 1fb0f889d7 [stdlib] Make UnsafePointer conversions explicit
Previously, it was possible to write Unsafe[Mutable]Pointer(x) and have
Swift deduce the pointee type based on context.  Since reinterpreting
memory is a fundamentally type-unsafe operation, it's better to be
explicit about conversions from Unsafe[Mutable]Pointer<T> to
Unsafe[Mutable]Pointer<U>.  This change is consistent with the move from
reinterpretCast(x) to unsafeBitCast(x, T.self).

Also, we've encoded the operations of explicitly adding or removing
mutability as properties, so that adding mutability can be separated
from wild reinterpretCast'ing, a much more severe form of unsafety.

Swift SVN r21324
2014-08-20 23:15:56 +00:00

49 lines
1.4 KiB
Swift

// RUN: %target-run-simple-swift | FileCheck %s
import Foundation
class Canary: NSObject {
deinit {
println("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_AssociationPolicy(OBJC_ASSOCIATION_RETAIN_NONATOMIC))
}
// CHECK-LABEL: NSData:
println("NSData:")
autoreleasepool {
var bytes: UnsafeMutablePointer<UInt8>
do {
let data = NSData(bytes: [2, 3, 5, 7] as [UInt8], length: 4)
hangCanary(data)
bytes = data.bytes.asPointerTo(UInt8.self).asMutablePointer
} while false // CHECK-NOT: died
println(bytes[0]) // CHECK: 2
println(bytes[1]) // CHECK-NEXT: 3
println(bytes[2]) // CHECK-NEXT: 5
println(bytes[3]) // CHECK-NEXT: 7
} // CHECK-NEXT: died
// CHECK-LABEL: AnyObject:
println("AnyObject:")
autoreleasepool {
var bytes: UnsafeMutablePointer<UInt8>
do {
let data = NSData(bytes: [11, 13, 17, 19] as [UInt8], length: 4)
hangCanary(data)
let dataAsAny: AnyObject = data
bytes = dataAsAny.bytes!.asPointerTo(UInt8.self).asMutablePointer
} while false // CHECK-NOT: died
println(bytes[0]) // CHECK: 11
println(bytes[1]) // CHECK-NEXT: 13
println(bytes[2]) // CHECK-NEXT: 17
println(bytes[3]) // CHECK-NEXT: 19
} // CHECK-NEXT: died