-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Try to instantiate TypeVars inside pt when possible
#24231
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Try to instantiate TypeVars inside pt when possible
#24231
Conversation
pt if this is possible by looking into partspt when possible
| // when we try to infer the parameter type. | ||
| isFullyDefined(pt, ForceDegree.flipBottom) | ||
| def tryToInstantiateInUnion(tp: Type): Unit = tp match | ||
| case tp: OrType => |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
At least, we should put AndOrType here to cover such cases:
def f[T, U](x: T & U): T = ???
val _: AnyRef => AnyRef = f(x => x)|
@noti0na1 I've been working on this a bit before seeing this work and I see that we reached the same conclusion for where the fix should be. In my opinion, this approach constraints too much inference when using union (could be extended to intersections). If we have a lambda as a first parameter, we will basically already instantiate now the type parameters even if we can refine them later. For example, in this PR, we cannot do the following: class Box[T]
val box: Box[Unit] = ???
def f[T, U](x: T | U, y: Box[U]): T = ???
val _: Any => Any = f(x => x, box)I'm not sure yet how to find the best compromise to also make this work. |
|
I would argue we should probably use |
|
Even if we fix the inference issue, I don't we can keep Option.apply nullable, since it will still break the macro writer by users. |
And they will have to migrate to it. That very specific thing should not be considered (again in my opinion) the blocker to have the right signature for |
…mNullable` (#24238) Yes, this is not the most ideal signature for `Option.apply` but it allows at least to drop `Option.fromNullable` and an easier migration to explicit nulls (no need to add `fromNullable` everywhere). Superseed #24231 in the sense where we focus here on the nullability issue rather than the more global issue. Reverts #24230 Relates to #24206
This PR improves type inference for functions with unknown parameter types when the expected type contains type variables within union types. Previously, the compiler would only attempt to instantiate top-level
TypeVars, but now it recursively searches through union types (OrType) and flexible types to find and instantiate nested type variables.