Swift: Address of static Void variable changes across multiple withUnsafePointers

Originator:jtbandes
Number:rdar://22410209 Date Originated:8/24/2015
Status:Closed Resolved:Behaves correctly
Product: Product Version:
Classification: Reproducible:Always
 
Motivation: a nice way to generate a unique "context" pointer to use for KVO.

Proposed solution: a static class variable of type Void (aka empty tuple).



class MyObject
{
    private static var KVOContext: Void
    
    init() {
        withUnsafePointer(&MyObject.KVOContext){
            print("context 1: \($0)")
        }
        withUnsafePointer(&MyObject.KVOContext){
            print("context 2: \($0)")
        }
    }
}

let o = MyObject()



Expected Results:
Address of MyObject.KVOContext is consistent across invocations



Actual Results:
Console output shows that the address varies:

context 1: 0x00007fff5d391060
context 2: 0xffffffffffffffff


Version:
Xcode 7.0 beta 6 (7A192o), OS X 10.10.5 (14F27)

Comments

"This issue behaves as intended based on the following: We don’t allocate storage for empty types, and don’t produce a stable address. If you need a unique address, you’ll need to at least use an Int8."


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!