Files
swift-mirror/test/expr/closure/anonymous.swift
Pavel Yaskevich 07a69c425f [ConstraintSystem] Make variadics work with anonymous closure parameters
Since opening closure body is now delayed until contextual type becomes
available it's possible to infer anonymous parameters as being variadic
based on context and propagate that information down to the closure body.

Resolves: rdar://problem/41416758
2020-02-05 09:35:49 -08:00

40 lines
1.2 KiB
Swift

// RUN: %target-typecheck-verify-swift
func takeIntToInt(_ f: (Int) -> Int) { }
func takeIntIntToInt(_ f: (Int, Int) -> Int) { }
// Simple closures with anonymous arguments
func simple() {
takeIntToInt({return $0 + 1})
takeIntIntToInt({return $0 + $1 + 1})
}
func takesIntArray(_: [Int]) { }
func takesVariadicInt(_: (Int...) -> ()) { }
func takesVariadicIntInt(_: (Int, Int...) -> ()) { }
func takesVariadicGeneric<T>(_ f: (T...) -> ()) { } // expected-note {{in call to function 'takesVariadicGeneric'}}
func variadic() {
// These work
takesVariadicInt({let _ = $0})
takesVariadicInt({let _: [Int] = $0})
let _: (Int...) -> () = {let _: [Int] = $0}
takesVariadicInt({takesIntArray($0)})
let _: (Int...) -> () = {takesIntArray($0)}
takesVariadicGeneric({takesIntArray($0)})
// FIXME: Problem here is related to multi-statement closure body not being type-checked together with
// enclosing context. We could have inferred `$0` to be `[Int]` if `let` was a part of constraint system.
takesVariadicGeneric({let _: [Int] = $0})
// expected-error@-1 {{generic parameter 'T' could not be inferred}}
takesVariadicIntInt({_ = $0; takesIntArray($1)})
takesVariadicIntInt({_ = $0; let _: [Int] = $1})
}