Closure fail to infer parameter type in a rare situation


(Yilei He) #1

Hi,

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
    v.name = "1"
}

print(p)

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
v.name = "1"
~~^~~~

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

Regards,
Yilei He