How does `associatedtype` inference work

I don't think this is the same as SR-10158. In that bug, an associated type is inferred from a constrained extension's constraints; in these examples, it's inferred from a typealias within a constrained extension. I don't know why (1) doesn't compile or why (2) does (I agree that the behavior is wrong!), but if I recall correctly it's been a longstanding weirdness that associated type checking considers typealiases in protocol extensions without looking at constraints, which is…bizarre, but related to type lookup not doing overload resolution:

protocol P {}
extension P where Self: AdditiveArithmetic { typealias A = Bool }
extension P where Self: BinaryFloatingPoint { typealias A = Float }
extension Float: P {}

let x: Float.A? = nil // should be the same as 'Float?' rather than 'Bool?', right?

But I haven't worked on this part of the compiler, so someone else should really answer.

EDIT: Oh, and my conclusion is that no one should use typealiases in constrained protocol extensions until this gets worked out, i.e. until there's an actual design. But that's just my opinion.

4 Likes