How interesting! "Bug" is probably a bit too strong, but the behavior of that no-op ^ operator certainly makes it look like something odd is going on.
Here's a simplified version that avoids the \ notation at the point of ambiguity and uses == instead of ~=:
struct S {
var a = 42
var b = "swift"
}
func path() -> KeyPath<S, Int> {
return \.a
}
func path() -> KeyPath<S, String> {
return \.b
}
\S.a == path() // Error: ambiguous use of 'path()'
My guess is that since == and similar functions are perfectly happy to compare any pair of key paths, regardless of type, there is in fact an ambiguity here. I'm not sure how to look up the signatures for operator functions in Xcode, but my guess would be that adding an overload for == of the form
func ==<S, T, U>(_ a: KeyPath<S, T>, _ b: KeyPath<S, U>) -> Bool
would disambiguate because the signature is more specific than the current one, which is probably more like
func ==<S, T, U, V>(_ a: KeyPath<S, T>, _ b: KeyPath<U, V>) -> Bool
Likewise, an overload such as
func ==<S, T>(_ a: KeyPath<S, T>, _ b: KeyPath<S, T>) -> Bool
might remove even more ambiguity.
I have no idea why the ^ operator works here.