-
Notifications
You must be signed in to change notification settings - Fork 10.5k
/
Copy pathfunction.swift
134 lines (102 loc) · 4.68 KB
/
function.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
// RUN: %target-typecheck-verify-swift
func f0(_ x: Float) -> Float {}
func f1(_ x: Float) -> Float {}
func f2(_ x: @autoclosure () -> Float) {}
var f : Float
_ = f0(f0(f))
_ = f0(1)
_ = f1(f1(f))
f2(f)
f2(1.0)
func call_lvalue(_ rhs: @autoclosure () -> Bool) -> Bool {
return rhs()
}
// Function returns
func weirdCast<T, U>(_ x: T) -> U {}
func ff() -> (Int) -> (Float) { return weirdCast }
// Block <-> function conversions
var funct: (Int) -> Int = { $0 }
var block: @convention(block) (Int) -> Int = funct
funct = block
block = funct
// Application of implicitly unwrapped optional functions
var optFunc: ((String) -> String)! = { $0 }
var s: String = optFunc("hi")
// <rdar://problem/17652759> Default arguments cause crash with tuple permutation
func testArgumentShuffle(_ first: Int = 7, third: Int = 9) {
}
testArgumentShuffle(third: 1, 2) // expected-error {{unnamed argument #2 must precede argument 'third'}} {{21-21=2, }} {{29-32=}}
func rejectsAssertStringLiteral() {
assert("foo") // expected-error {{cannot convert value of type 'String' to expected argument type 'Bool'}}
precondition("foo") // expected-error {{cannot convert value of type 'String' to expected argument type 'Bool'}}
}
// <rdar://problem/22243469> QoI: Poor error message with throws, default arguments, & overloads
func process(_ line: UInt = #line, _ fn: () -> Void) {}
func process(_ line: UInt = #line) -> Int { return 0 }
func dangerous() throws {}
func test() {
process { // expected-error {{invalid conversion from throwing function of type '() throws -> ()' to non-throwing function type '() -> Void'}}
try dangerous()
test()
}
}
// <rdar://problem/19962010> QoI: argument label mismatches produce not-great diagnostic
class A {
func a(_ text:String) {
}
func a(_ text:String, something:Int?=nil) {
}
}
A().a(text:"sometext") // expected-error{{extraneous argument label 'text:' in call}}{{7-12=}}
// <rdar://problem/22451001> QoI: incorrect diagnostic when argument to print has the wrong type
func r22451001() -> AnyObject {}
print(r22451001(5)) // expected-error {{argument passed to call that takes no arguments}}
// SR-590 Passing two parameters to a function that takes one argument of type Any crashes the compiler
// SR-1028: Segmentation Fault: 11 when superclass init takes parameter of type 'Any'
func sr590(_ x: Any) {} // expected-note {{'sr590' declared here}}
sr590(3,4) // expected-error {{extra argument in call}}
sr590() // expected-error {{missing argument for parameter #1 in call}}
// Make sure calling with structural tuples still works.
sr590(())
sr590((1, 2))
// SR-2657: Poor diagnostics when function arguments should be '@escaping'.
private class SR2657BlockClass<T> { // expected-note 3 {{generic parameters are always considered '@escaping'}}
let f: T
init(f: T) { self.f = f }
}
func takesAny(_: Any) {}
func foo(block: () -> (), other: () -> Int) {
let _ = SR2657BlockClass(f: block)
// expected-error@-1 {{converting non-escaping value to 'T' may allow it to escape}}
let _ = SR2657BlockClass<()->()>(f: block)
// expected-error@-1 {{converting non-escaping parameter 'block' to generic parameter 'T' may allow it to escape}}
let _: SR2657BlockClass<()->()> = SR2657BlockClass(f: block)
// expected-error@-1 {{converting non-escaping parameter 'block' to generic parameter 'T' may allow it to escape}}
let _: SR2657BlockClass<()->()> = SR2657BlockClass<()->()>(f: block)
// expected-error@-1 {{converting non-escaping parameter 'block' to generic parameter 'T' may allow it to escape}}
_ = SR2657BlockClass<Any>(f: block) // expected-error {{converting non-escaping value to 'Any' may allow it to escape}}
_ = SR2657BlockClass<Any>(f: other) // expected-error {{converting non-escaping value to 'Any' may allow it to escape}}
takesAny(block) // expected-error {{converting non-escaping value to 'Any' may allow it to escape}}
takesAny(other) // expected-error {{converting non-escaping value to 'Any' may allow it to escape}}
}
struct S {
init<T>(_ x: T, _ y: T) {} // expected-note {{generic parameters are always considered '@escaping'}}
init(fn: () -> Int) {
self.init({ 0 }, fn) // expected-error {{converting non-escaping parameter 'fn' to generic parameter 'T' may allow it to escape}}
}
}
protocol P {
associatedtype U
}
func test_passing_noescape_function_to_dependent_member() {
struct S<T : P> { // expected-note {{generic parameters are always considered '@escaping'}}
func foo(_: T.U) {}
}
struct Q : P {
typealias U = () -> Int
}
func test(_ s: S<Q>, fn: () -> Int) {
s.foo(fn)
// expected-error@-1 {{converting non-escaping parameter 'fn' to generic parameter 'Q.U' (aka '() -> Int') may allow it to escape}}
}
}