Wouldn’t that work if Inv was covariant? Is (A | B) <: Base? (I assume the comment should say A|B not X|Y.)
In any case I don’t think that argues it “breaks” implicit search, rather it affects it. The reverse is true after all: There could be implicits that would be found if the union were preserved and won’t be found if it’s not preserved.
Unless you mean relative to Scala 2, but aren’t we breaking type inference and implicit resolution a lot already?
Alternatively, couldn’t implicit search be smarter? That is, first try to type the getInv expression with T being the union type, then if implicit resolution fails, try to retype it with T as the LUB as a fallback. Does Scala 3 fix T once it assigns it based on the first parameter list (similar to foldLeft type inference issue)? If the second parameter list can still affect T then this wouldn’t need a lot of changes to type checking, it would be more about expanding implicit resolution, although I guess it’s more complicated because not every implicit search for a union type is valid resolving via its LUB. It’s valid here since it isn’t set in stone that we want specifically the union type; if implicit search only finds a result for the LUB then that should typecheck. Maybe it’s also valid when the union and the LUB are equivalent, such as a sealed trait hierarchy? I guess that probably has holes.
Maybe a better idea is the type could be resolved more lazily? That is, the expression
if (true) new A else new B should produce neither A|B nor Base, but rather a special type that basically says “this could go either way, potentially A|B and potentially Base,” and then after processing the first parameter list of getInv T is “fixed” to this ‘quantum’ type. Next implicit search would be performed with this T, giving a union type resolution higher priority. The result would settle T to either a union type or the LUB, depending what it found. If nothing is found the error message should reflect that it tried both.
There would have to be a rule that says that if once typing succeeds a type is still in this “quantum” state, the type should be settled. At this point perhaps LUB wins, or perhaps it depends on the complexity of the union type.