- Notifications
You must be signed in to change notification settings - Fork 10.5k
/
Copy pathc_function_pointers.swift
57 lines (44 loc) · 2.6 KB
/
c_function_pointers.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
// RUN: %target-swift-frontend -typecheck -verify -module-name main %s
func global()->Int{return0}
structS{
staticfunc staticMethod()->Int{return0}
}
classC{
staticfunc staticMethod()->Int{return0}
classfunc classMethod()->Int{return0}
}
iftrue{
func local()->Int{return0}
leta:@convention(c)()->Int= global
let _:@convention(c)()->Int= main.global
let _:@convention(c)()->Int={0}
let _:@convention(c)()->Int= local
// Can't convert a closure value to a C function pointer
letglobal2= global
let _:@convention(c)()->Int= global2 // expected-error{{can only be formed from a reference to a 'func' or a literal closure}}
letglobalBlock:@convention(block)()->Int= global
let _:@convention(c)()->Int= globalBlock // expected-error{{can only be formed from a reference to a 'func' or a literal closure}}
// Can convert a function pointer to a block or closure, or assign to another
// C function pointer
let _:@convention(c)()->Int= a
let _:@convention(block)()->Int= a
let _:()->Int= a
// Can't convert a C function pointer from a method.
// TODO: Could handle static methods.
let _:@convention(c)()->Int=S.staticMethod // expected-error{{}}
let _:@convention(c)()->Int=C.staticMethod // expected-error{{}}
let _:@convention(c)()->Int=C.classMethod // expected-error{{}}
// <rdar://problem/22181714> Crash when typing "signal"
letiuo_global:(()->Int)!= global
let _:(@convention(c)()->Int)!= iuo_global // expected-error{{a C function pointer can only be formed from a reference to a 'func' or a literal closure}}
func handler(_ callback:(@convention(c)()->Int)!){}
handler(iuo_global) // expected-error{{a C function pointer can only be formed from a reference to a 'func' or a literal closure}}
}
func genericFunc<T>(_ t:T)->T{return t }
letf:@convention(c)(Int)->Int= genericFunc // expected-error{{cannot be formed from a reference to a generic function}}
func ct1()->(){print("")}
letct1ref0:@convention(c, cType:"void (*)(void)")()->()= ct1
letct1ref1:@convention(c, cType:"void (*)(void)")= ct1 // expected-error{{expected type}}
letct1ref2:@convention(c,)()->()= ct1 // expected-error{{expected 'cType' label in 'convention' attribute}}
letct1ref3:@convention(c, cType)()->()= ct1 // expected-error{{expected ':' after 'cType' for 'convention' attribute}}
letct1ref4:@convention(c, cType:)()->()= ct1 // expected-error{{expected string literal containing clang type for 'cType' in 'convention' attribute}}