forked from swiftlang/swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcf.swift
162 lines (126 loc) · 5.48 KB
/
cf.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// RUN: %target-swift-frontend -disable-objc-attr-requires-foundation-module -typecheck -verify -import-cf-types -I %S/Inputs/custom-modules %s
// REQUIRES: objc_interop
import CoreCooling
import CFAndObjC
func assertUnmanaged<T>(_ t: Unmanaged<T>) {}
func assertManaged<T: AnyObject>(_ t: T) {}
func test0(_ fridge: CCRefrigerator) {
assertManaged(fridge)
}
func test1(_ power: Unmanaged<CCPowerSupply>) {
assertUnmanaged(power)
let fridge = CCRefrigeratorCreate(power) // expected-error {{cannot convert value of type 'Unmanaged<CCPowerSupply>' to expected argument type 'CCPowerSupply!'}}
assertUnmanaged(fridge)
}
func test2() {
let fridge = CCRefrigeratorCreate(kCCPowerStandard)!
assertUnmanaged(fridge)
}
func test3(_ fridge: CCRefrigerator) {
assertManaged(fridge)
}
func test4() {
// FIXME: this should not require a type annotation
let power: CCPowerSupply = kCCPowerStandard
assertManaged(power)
let fridge = CCRefrigeratorCreate(power)!
assertUnmanaged(fridge)
}
func test5() {
let power: Unmanaged<CCPowerSupply> = .passUnretained(kCCPowerStandard)
assertUnmanaged(power)
_ = CCRefrigeratorCreate(power.takeUnretainedValue())
}
func test6() {
let fridge = CCRefrigeratorCreate(nil)
fridge?.release()
}
func test7() {
let value = CFBottom()!
assertUnmanaged(value)
}
func test8(_ f: CCRefrigerator) {
_ = f as CFTypeRef
_ = f as AnyObject
}
func test9() {
let fridge = CCRefrigeratorCreateMutable(kCCPowerStandard).takeRetainedValue()
let constFridge: CCRefrigerator = fridge
CCRefrigeratorOpen(fridge)
let item = CCRefrigeratorGet(fridge, 0).takeUnretainedValue()
CCRefrigeratorInsert(item, fridge) // expected-error {{cannot convert value of type 'CCItem' to expected argument type 'CCMutableRefrigerator!'}}
CCRefrigeratorInsert(constFridge, item) // expected-error {{cannot convert value of type 'CCRefrigerator' to expected argument type 'CCMutableRefrigerator!'}}
CCRefrigeratorInsert(fridge, item)
CCRefrigeratorClose(fridge)
}
func testProperty(_ k: Kitchen) {
k.fridge = CCRefrigeratorCreate(kCCPowerStandard).takeRetainedValue()
CCRefrigeratorOpen(k.fridge)
CCRefrigeratorClose(k.fridge)
}
func testTollFree0(_ mduct: MutableDuct) {
_ = mduct as CCMutableDuct
let duct = mduct as Duct
_ = duct as CCDuct
}
func testTollFree1(_ ccmduct: CCMutableDuct) {
_ = ccmduct as MutableDuct
let ccduct: CCDuct = ccmduct
_ = ccduct as Duct
}
func testChainedAliases(_ fridge: CCRefrigerator) {
_ = fridge as CCRefrigerator
_ = fridge as CCFridge
_ = fridge as CCFridgeRef // expected-error{{'CCFridgeRef' has been renamed to 'CCFridge'}} {{17-28=CCFridge}}
}
func testBannedImported(_ object: CCOpaqueTypeRef) {
CCRetain(object) // expected-error {{'CCRetain' is unavailable: Core Foundation objects are automatically memory managed}} expected-warning {{result of call to 'CCRetain' is unused}}
CCRelease(object) // expected-error {{'CCRelease' is unavailable: Core Foundation objects are automatically memory managed}}
}
func testOutParametersGood() {
var fridge: CCRefrigerator?
CCRefrigeratorCreateIndirect(&fridge)
var power: CCPowerSupply?
CCRefrigeratorGetPowerSupplyIndirect(fridge!, &power)
var item: Unmanaged<CCItem>?
CCRefrigeratorGetItemUnaudited(fridge!, 0, &item)
}
func testOutParametersBad() {
let fridge: CCRefrigerator?
CCRefrigeratorCreateIndirect(fridge) // expected-error {{cannot convert value of type 'CCRefrigerator?' to expected argument type 'UnsafeMutablePointer<CCRefrigerator?>?'}}
let power: CCPowerSupply?
CCRefrigeratorGetPowerSupplyIndirect(0, power) // expected-error {{cannot convert value of type 'Int' to expected argument type 'CCRefrigerator!'}}
let item: CCItem?
CCRefrigeratorGetItemUnaudited(0, 0, item) // expected-error {{cannot convert value of type 'Int' to expected argument type 'CCRefrigerator!'}}
}
func nameCollisions() {
var objc: MyProblematicObject?
var cf: MyProblematicObjectRef?
cf = objc // expected-error {{cannot assign value of type 'MyProblematicObject?' to type 'MyProblematicObjectRef?'}}
objc = cf // expected-error {{cannot assign value of type 'MyProblematicObjectRef?' to type 'MyProblematicObject?'}}
var cfAlias: MyProblematicAliasRef?
cfAlias = cf // okay
cf = cfAlias // okay
var otherAlias: MyProblematicAlias?
otherAlias = cfAlias // expected-error {{cannot assign value of type 'MyProblematicAliasRef?' to type 'MyProblematicAlias?'}}
cfAlias = otherAlias // expected-error {{cannot assign value of type 'MyProblematicAlias?' to type 'MyProblematicAliasRef?'}}
func isOptionalFloat(_: inout Optional<Float>) {}
isOptionalFloat(&otherAlias) // okay
var np: NotAProblem?
var np2: NotAProblemRef? // expected-error{{'NotAProblemRef' has been renamed to 'NotAProblem'}} {{12-26=NotAProblem}}
np = np2
np2 = np
}
func testNonConstVoid() {
let value: Unmanaged<CFNonConstVoidRef> = CFNonConstBottom()!
assertUnmanaged(value)
}
class NuclearFridge: CCRefrigerator {} // expected-error {{cannot inherit from Core Foundation type 'CCRefrigerator'}}
extension CCRefrigerator {
@objc func foo() {} // expected-error {{method cannot be marked @objc because Core Foundation types are not classes in Objective-C}}
func bar() {} // okay, implicitly non-objc
}
protocol SwiftProto {}
@objc protocol ObjCProto {}
extension CCRefrigerator: ObjCProto {} // expected-error {{Core Foundation class 'CCRefrigerator' cannot conform to @objc protocol 'ObjCProto' because Core Foundation types are not classes in Objective-C}}
extension CCRefrigerator: SwiftProto {}