Files
swift-mirror/test/expr/closure/anonymous.swift
Pavel Yaskevich d111f119d8 [ConstraintSystem] Detect and diagnose inability to infer type of closure parameter(s)
Detect situation when it's impossible to determine types for
closure parameters used in the body from the context. E.g.
when a call closure is associated with refers to a missing
member.

```swift
struct S {
}

S.foo { a, b in } // `S` doesn't have static member `foo`

let _ = { v in } // not enough context to infer type of `v`

_ = .foo { v in } // base type for `.foo` couldn't be determined
```

Resolves: [SR-12815](https://bugs.swift.org/browse/SR-12815)
Resolves: rdar://problem/63230293
2020-05-15 01:14:30 -07: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...) -> ()) { }
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 {{unable to infer type of a closure parameter $0 in the current context}}
takesVariadicIntInt({_ = $0; takesIntArray($1)})
takesVariadicIntInt({_ = $0; let _: [Int] = $1})
}