|
| 1 | +// RUN: %target-typecheck-verify-swift -enable-parser-lookup |
| 2 | + |
| 3 | +// Test various tuple constraints. |
| 4 | + |
| 5 | +func f0(x: Int, y: Float) {} |
| 6 | + |
| 7 | +var i : Int |
| 8 | +var j : Int |
| 9 | +var f : Float |
| 10 | + |
| 11 | +func f1(y: Float, rest: Int...) {} |
| 12 | + |
| 13 | +func f2(_: (_ x: Int, _ y: Int) -> Int) {} |
| 14 | +func f2xy(x: Int, y: Int) -> Int {} |
| 15 | +func f2ab(a: Int, b: Int) -> Int {} |
| 16 | +func f2yx(y: Int, x: Int) -> Int {} |
| 17 | + |
| 18 | +func f3(_ x: (_ x: Int, _ y: Int) -> ()) {} |
| 19 | +func f3a(_ x: Int, y: Int) {} |
| 20 | +func f3b(_: Int) {} |
| 21 | + |
| 22 | +func f4(_ rest: Int...) {} |
| 23 | +func f5(_ x: (Int, Int)) {} |
| 24 | + |
| 25 | +func f6(_: (i: Int, j: Int), k: Int = 15) {} |
| 26 | + |
| 27 | +//===----------------------------------------------------------------------===// |
| 28 | +// Conversions and shuffles |
| 29 | +//===----------------------------------------------------------------------===// |
| 30 | + |
| 31 | +func foo(a : [(some: Int, (key: Int, value: String))]) -> String { |
| 32 | + for (i , (j, k)) in a { |
| 33 | + if i == j { return k } |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +func rdar28207648() -> [(Int, CustomStringConvertible)] { |
| 38 | + let v : [(Int, Int)] = [] |
| 39 | + return v as [(Int, CustomStringConvertible)] |
| 40 | +} |
| 41 | + |
| 42 | +class rdar28207648Base {} |
| 43 | +class rdar28207648Derived : rdar28207648Base {} |
| 44 | + |
| 45 | +func rdar28207648(x: (Int, rdar28207648Derived)) -> (Int, rdar28207648Base) { |
| 46 | + return x as (Int, rdar28207648Base) |
| 47 | +} |
| 48 | + |
| 49 | +public typealias Success<T, V> = (response: T, data: V?) |
| 50 | + |
| 51 | +public enum Result { |
| 52 | + case success(Success<Any, Any>) |
| 53 | + case error(Error) |
| 54 | +} |
| 55 | + |
| 56 | + |
| 57 | +let a = Success<Int, Int>(response: 3, data: 3) |
| 58 | +let success: Result = .success(a) |
| 59 | + |
| 60 | +// Variadic functions. |
| 61 | +f4() |
| 62 | +f4(1) |
| 63 | +f4(1, 2, 3) |
| 64 | + |
| 65 | +f2(f2xy) |
| 66 | +f2(f2ab) |
| 67 | +f2(f2yx) |
| 68 | + |
| 69 | +f3(f3a) |
| 70 | +f3(f3b) // expected-error{{cannot convert value of type '(Int) -> ()' to expected argument type '(Int, Int) -> ()'}} |
| 71 | + |
| 72 | +func getIntFloat() -> (int: Int, float: Float) {} |
| 73 | +var values = getIntFloat() |
| 74 | +func wantFloat(_: Float) {} |
| 75 | +wantFloat(values.float) |
| 76 | + |
| 77 | +var e : (x: Int..., y: Int) // expected-error{{cannot create a variadic tuple}} |
| 78 | + |
| 79 | +typealias Interval = (a:Int, b:Int) |
| 80 | +func takeInterval(_ x: Interval) {} |
| 81 | +takeInterval(Interval(1, 2)) |
| 82 | + |
| 83 | +f5((1,1)) |
| 84 | + |
| 85 | +// Tuples with existentials |
| 86 | +var any : Any = () |
| 87 | +any = (1, 2) |
| 88 | +any = (label: 4) // expected-error {{cannot create a single-element tuple with an element label}} |
| 89 | + |
| 90 | +// Scalars don't have .0/.1/etc |
| 91 | +i = j.0 // expected-error{{value of type 'Int' has no member '0'}} |
| 92 | +any.1 // expected-error{{value of type 'Any' has no member '1'}} |
| 93 | +// expected-note@-1{{cast 'Any' to 'AnyObject' or use 'as!' to force downcast to a more specific type to access members}} |
| 94 | +any = (5.0, 6.0) as (Float, Float) |
| 95 | +_ = (any as! (Float, Float)).1 |
| 96 | + |
| 97 | +// Fun with tuples |
| 98 | +protocol PosixErrorReturn { |
| 99 | + static func errorReturnValue() -> Self |
| 100 | +} |
| 101 | + |
| 102 | +extension Int : PosixErrorReturn { |
| 103 | + static func errorReturnValue() -> Int { return -1 } |
| 104 | +} |
| 105 | + |
| 106 | +func posixCantFail<A, T : Comparable & PosixErrorReturn> |
| 107 | + (_ f: @escaping (A) -> T) -> (_ args:A) -> T |
| 108 | +{ |
| 109 | + return { args in |
| 110 | + let result = f(args) |
| 111 | + assert(result != T.errorReturnValue()) |
| 112 | + return result |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +func open(_ name: String, oflag: Int) -> Int { } |
| 117 | + |
| 118 | +var foo: Int = 0 |
| 119 | + |
| 120 | +var fd = posixCantFail(open)(("foo", 0)) |
| 121 | + |
| 122 | +// Tuples and lvalues |
| 123 | +class C { |
| 124 | + init() {} |
| 125 | + func f(_: C) {} |
| 126 | +} |
| 127 | + |
| 128 | +func testLValue(_ c: C) { |
| 129 | + var c = c |
| 130 | + c.f(c) |
| 131 | + |
| 132 | + let x = c |
| 133 | + c = x |
| 134 | +} |
| 135 | + |
| 136 | + |
| 137 | +// <rdar://problem/21444509> Crash in TypeChecker::coercePatternToType |
| 138 | +func invalidPatternCrash(_ k : Int) { |
| 139 | + switch k { |
| 140 | + case (k, cph_: k) as UInt8: // expected-error {{tuple pattern cannot match values of the non-tuple type 'UInt8'}} expected-warning {{cast from 'Int' to unrelated type 'UInt8' always fails}} |
| 141 | + break |
| 142 | + } |
| 143 | +} |
| 144 | + |
| 145 | +// <rdar://problem/21875219> Tuple to tuple conversion with IdentityExpr / AnyTryExpr hang |
| 146 | +class Paws { |
| 147 | + init() throws {} |
| 148 | +} |
| 149 | + |
| 150 | +func scruff() -> (AnyObject?, Error?) { |
| 151 | + do { |
| 152 | + return try (Paws(), nil) |
| 153 | + } catch { |
| 154 | + return (nil, error) |
| 155 | + } |
| 156 | +} |
| 157 | + |
| 158 | +// Test variadics with trailing closures. |
| 159 | +func variadicWithTrailingClosure(_ x: Int..., y: Int = 2, fn: (Int, Int) -> Int) { |
| 160 | +} |
| 161 | + |
| 162 | +variadicWithTrailingClosure(1, 2, 3) { $0 + $1 } |
| 163 | +variadicWithTrailingClosure(1) { $0 + $1 } |
| 164 | +variadicWithTrailingClosure() { $0 + $1 } |
| 165 | +variadicWithTrailingClosure { $0 + $1 } |
| 166 | + |
| 167 | +variadicWithTrailingClosure(1, 2, 3, y: 0) { $0 + $1 } |
| 168 | +variadicWithTrailingClosure(1, y: 0) { $0 + $1 } |
| 169 | +variadicWithTrailingClosure(y: 0) { $0 + $1 } |
| 170 | + |
| 171 | +variadicWithTrailingClosure(1, 2, 3, y: 0, fn: +) |
| 172 | +variadicWithTrailingClosure(1, y: 0, fn: +) |
| 173 | +variadicWithTrailingClosure(y: 0, fn: +) |
| 174 | + |
| 175 | +variadicWithTrailingClosure(1, 2, 3, fn: +) |
| 176 | +variadicWithTrailingClosure(1, fn: +) |
| 177 | +variadicWithTrailingClosure(fn: +) |
| 178 | + |
| 179 | + |
| 180 | +// <rdar://problem/23700031> QoI: Terrible diagnostic in tuple assignment |
| 181 | +func gcd_23700031<T>(_ a: T, b: T) { |
| 182 | + var a = a |
| 183 | + var b = b |
| 184 | + (a, b) = (b, a % b) // expected-error {{binary operator '%' cannot be applied to two 'T' operands}} |
| 185 | +} |
| 186 | + |
| 187 | +// <rdar://problem/24210190> |
| 188 | +// Don't ignore tuple labels in same-type constraints or stronger. |
| 189 | +protocol Kingdom { |
| 190 | + associatedtype King |
| 191 | +} |
| 192 | +struct Victory<General> { |
| 193 | + init<K: Kingdom>(_ king: K) where K.King == General {} // expected-note {{where 'General' = '(x: Int, y: Int)', 'K.King' = 'MagicKingdom<(Int, Int)>.King' (aka '(Int, Int)')}} |
| 194 | +} |
| 195 | +struct MagicKingdom<K> : Kingdom { |
| 196 | + typealias King = K |
| 197 | +} |
| 198 | +func magify<T>(_ t: T) -> MagicKingdom<T> { return MagicKingdom() } |
| 199 | +func foo(_ pair: (Int, Int)) -> Victory<(x: Int, y: Int)> { |
| 200 | + return Victory(magify(pair)) // expected-error {{initializer 'init(_:)' requires the types '(x: Int, y: Int)' and 'MagicKingdom<(Int, Int)>.King' (aka '(Int, Int)') be equivalent}} |
| 201 | +} |
| 202 | + |
| 203 | + |
| 204 | +// https://bugs.swift.org/browse/SR-596 |
| 205 | +// Compiler crashes when accessing a non-existent property of a closure parameter |
| 206 | +func call(_ f: (C) -> Void) {} |
| 207 | +func makeRequest() { |
| 208 | + call { obj in |
| 209 | + print(obj.invalidProperty) // expected-error {{value of type 'C' has no member 'invalidProperty'}} |
| 210 | + } |
| 211 | +} |
| 212 | + |
| 213 | +// <rdar://problem/25271859> QoI: Misleading error message when expression result can't be inferred from closure |
| 214 | +struct r25271859<T> { |
| 215 | +} |
| 216 | + |
| 217 | +extension r25271859 { |
| 218 | + func map<U>(f: (T) -> U) -> r25271859<U> { |
| 219 | + } |
| 220 | + |
| 221 | + func andThen<U>(f: (T) -> r25271859<U>) { // expected-note {{in call to function 'andThen(f:)'}} |
| 222 | + } |
| 223 | +} |
| 224 | + |
| 225 | +func f(a : r25271859<(Float, Int)>) { |
| 226 | + a.map { $0.0 } // expected-error {{generic parameter 'U' could not be inferred}} (This is related to how solver is setup with multiple statements) |
| 227 | + .andThen { _ in |
| 228 | + print("hello") // comment this out and it runs, leave any form of print in and it doesn't |
| 229 | + return r25271859<String>() |
| 230 | + } |
| 231 | +} |
| 232 | + |
| 233 | +// LValue to rvalue conversions. |
| 234 | + |
| 235 | +func takesRValue(_: (Int, (Int, Int))) {} |
| 236 | +func takesAny(_: Any) {} |
| 237 | + |
| 238 | +var x = 0 |
| 239 | +var y = 0 |
| 240 | + |
| 241 | +let _ = (x, (y, 0)) |
| 242 | +takesRValue((x, (y, 0))) |
| 243 | +takesAny((x, (y, 0))) |
| 244 | + |
| 245 | +// SR-2600 - Closure cannot infer tuple parameter names |
| 246 | +typealias Closure<A, B> = ((a: A, b: B)) -> String |
| 247 | + |
| 248 | +func invoke<A, B>(a: A, b: B, _ closure: Closure<A,B>) { |
| 249 | + print(closure((a, b))) |
| 250 | +} |
| 251 | + |
| 252 | +invoke(a: 1, b: "B") { $0.b } |
| 253 | + |
| 254 | +invoke(a: 1, b: "B") { $0.1 } |
| 255 | + |
| 256 | +invoke(a: 1, b: "B") { (c: (a: Int, b: String)) in |
| 257 | + return c.b |
| 258 | +} |
| 259 | + |
| 260 | +invoke(a: 1, b: "B") { c in |
| 261 | + return c.b |
| 262 | +} |
| 263 | + |
| 264 | +// Crash with one-element tuple with labeled element |
| 265 | +class Dinner {} |
| 266 | + |
| 267 | +func microwave() -> Dinner? { |
| 268 | + let d: Dinner? = nil |
| 269 | + return (n: d) // expected-error{{cannot convert return expression of type '(n: Dinner?)' to return type 'Dinner?'}} |
| 270 | +} |
| 271 | + |
| 272 | +func microwave() -> Dinner { |
| 273 | + let d: Dinner? = nil |
| 274 | + return (n: d) // expected-error{{cannot convert return expression of type '(n: Dinner?)' to return type 'Dinner'}} |
| 275 | +} |
| 276 | + |
| 277 | +// Tuple conversion with an optional |
| 278 | +func f(b: Bool) -> (a: Int, b: String)? { |
| 279 | + let x = 3 |
| 280 | + let y = "" |
| 281 | + return b ? (x, y) : nil |
| 282 | +} |
| 283 | + |
| 284 | +// Single element tuple expressions |
| 285 | +func singleElementTuple() { |
| 286 | + let _ = (label: 123) // expected-error {{cannot create a single-element tuple with an element label}} {{12-19=}} |
| 287 | + let _ = (label: 123).label // expected-error {{cannot create a single-element tuple with an element label}} {{12-19=}} |
| 288 | + let _ = ((label: 123)) // expected-error {{cannot create a single-element tuple with an element label}} {{13-20=}} |
| 289 | + let _ = ((label: 123)).label // expected-error {{cannot create a single-element tuple with an element label}} {{13-20=}} |
| 290 | +} |
| 291 | + |
| 292 | +// Tuples with duplicate labels |
| 293 | + |
| 294 | +let dupLabel1: (foo: Int, foo: Int) = (foo: 1, foo: 2) // expected-error 2{{cannot create a tuple with a duplicate element label}} |
| 295 | + |
| 296 | +func dupLabel2(x a: Int, x b: Int) -> (y: Int, y: Int) { // expected-error {{cannot create a tuple with a duplicate element label}} |
| 297 | + return (a, b) |
| 298 | +} |
| 299 | + |
| 300 | +let _ = (bar: 0, bar: "") // expected-error {{cannot create a tuple with a duplicate element label}} |
| 301 | + |
| 302 | +let zeroTuple = (0,0) |
| 303 | + |
| 304 | +if case (foo: let x, foo: let y) = zeroTuple { print(x+y) } // expected-error {{cannot create a tuple with a duplicate element label}} |
| 305 | +// expected-warning@-1 {{'if' condition is always true}} |
| 306 | + |
| 307 | +enum BishBash { case bar(foo: Int, foo: String) } |
| 308 | +let enumLabelDup: BishBash = .bar(foo: 0, foo: "") // expected-error {{cannot create a tuple with a duplicate element label}} |
| 309 | + |
| 310 | +func dupLabelClosure(_ fn: () -> Void) {} |
| 311 | +dupLabelClosure { print((bar: "", bar: 5).bar) } // expected-error {{cannot create a tuple with a duplicate element label}} |
| 312 | + |
| 313 | +struct DupLabelSubscript { |
| 314 | + subscript(foo x: Int, foo y: Int) -> Int { |
| 315 | + return 0 |
| 316 | + } |
| 317 | +} |
| 318 | + |
| 319 | +let dupLabelSubscriptStruct = DupLabelSubscript() |
| 320 | +let _ = dupLabelSubscriptStruct[foo: 5, foo: 5] // ok |
| 321 | + |
| 322 | +// SR-12869 |
| 323 | + |
| 324 | +var dict: [String: (Int, Int)] = [:] |
| 325 | +let bignum: Int64 = 1337 |
| 326 | +dict["test"] = (bignum, 1) // expected-error {{cannot assign value of type '(Int64, Int)' to subscript of type '(Int, Int)'}} |
| 327 | + |
| 328 | +var tuple: (Int, Int) |
| 329 | +tuple = (bignum, 1) // expected-error {{cannot assign value of type '(Int64, Int)' to type '(Int, Int)'}} |
| 330 | + |
| 331 | +var optionalTuple: (Int, Int)? |
| 332 | +var optionalTuple2: (Int64, Int)? = (bignum, 1) |
| 333 | +var optionalTuple3: (UInt64, Int)? = (bignum, 1) // expected-error {{cannot convert value of type '(Int64, Int)' to specified type '(UInt64, Int)?'}} |
| 334 | + |
| 335 | +optionalTuple = (bignum, 1) // expected-error {{cannot assign value of type '(Int64, Int)' to type '(Int, Int)'}} |
| 336 | +// Optional to Optional |
| 337 | +optionalTuple = optionalTuple2 // expected-error {{cannot assign value of type '(Int64, Int)?' to type '(Int, Int)?'}} |
0 commit comments