Closure fail to infer parameter type in a rare situation

(Yilei He) #1


I found a very rare situation that closure can’t infer the type of its parameter. See the code below:

func withReference<T, Result>(_ value: inout T, _ execute: (inout T) throws -> Result) rethrows -> Result {
    return try execute(&value)

struct Person {
    var name: String

var p = Person(name: "a")

withReference(&p) { (v)  in = "1"


The code can be compiled and produce correct result in Swift 3(Xcode 8) playground, but it can’t be compiled in Swift 4(Xcode 9 beta2).
The error says:

error: bug.playground:11:7: error: type of expression is ambiguous without more context = "1"

Is this related to exclusive access rule introduced to Swift 4?

Yilei He