-
Couldn't load subscription status.
- 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?
Changes from all commits
584c069
d93ed6c
19cec83
8840779
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -1921,15 +1921,39 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer | |||||||||||||||
| NoType | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| pt match { | ||||||||||||||||
| case pt: TypeVar | ||||||||||||||||
| if untpd.isFunctionWithUnknownParamType(tree) && !calleeType.exists => | ||||||||||||||||
| // try to instantiate `pt` if this is possible. If it does not | ||||||||||||||||
| // work the error will be reported later in `inferredParam`, | ||||||||||||||||
| // when we try to infer the parameter type. | ||||||||||||||||
| isFullyDefined(pt, ForceDegree.flipBottom) | ||||||||||||||||
| case _ => | ||||||||||||||||
| } | ||||||||||||||||
| /** Try to instantiate one type variable bounded by function types that appear | ||||||||||||||||
| * deeply inside `tp`, including union or intersection types. | ||||||||||||||||
| */ | ||||||||||||||||
| def tryToInstantiateDeeply(tp: Type): Boolean = tp match | ||||||||||||||||
| case tp: AndOrType => | ||||||||||||||||
| tryToInstantiateDeeply(tp.tp1) | ||||||||||||||||
| || tryToInstantiateDeeply(tp.tp2) | ||||||||||||||||
| case tp: FlexibleType => | ||||||||||||||||
| tryToInstantiateDeeply(tp.hi) | ||||||||||||||||
| case tp: TypeVar if isConstrainedByFunctionType(tp) => | ||||||||||||||||
| // Only instantiate if the type variable is constrained by function types | ||||||||||||||||
| isFullyDefined(tp, ForceDegree.flipBottom) | ||||||||||||||||
| case _ => false | ||||||||||||||||
|
|
||||||||||||||||
|
||||||||||||||||
| /** Determines whether the bounds (lower or upper) of the given type variable | |
| * contain function types or SAM types. This is used to decide whether the type | |
| * variable should be instantiated to assist with parameter type inference. | |
| * The check is performed recursively, traversing union, intersection, and flexible types. | |
| */ |
noti0na1 marked this conversation as resolved.
Show resolved
Hide resolved
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
|
|
||
| def f[T](x: T): T = ??? | ||
| def f2[T](x: T | T): T = ??? | ||
| def f3[T](x: T | Null): T = ??? | ||
| def f4[T](x: Int | T): T = ??? | ||
|
|
||
| trait MyOption[+T] | ||
|
|
||
| object MyOption: | ||
| def apply[T](x: T | Null): MyOption[T] = ??? | ||
|
|
||
| def test = | ||
| val g: AnyRef => Boolean = f { | ||
| x => x eq null // ok | ||
| } | ||
| val g2: AnyRef => Boolean = f2 { | ||
| x => x eq null // ok | ||
| } | ||
| val g3: AnyRef => Boolean = f3 { | ||
| x => x eq null // was error | ||
| } | ||
| val g4: AnyRef => Boolean = f4 { | ||
| x => x eq null // was error | ||
| } | ||
|
|
||
| val o1: MyOption[String] = MyOption(null) | ||
| val o2: MyOption[String => Boolean] = MyOption { | ||
| x => x.length > 0 | ||
| } | ||
| val o3: MyOption[(String, String) => Boolean] = MyOption { | ||
| (x, y) => x.length > y.length | ||
| } | ||
|
|
||
|
|
||
| class Box[T] | ||
| val box: Box[Unit] = ??? | ||
| def ff1[T, U](x: T | U, y: Box[U]): T = ??? | ||
| def ff2[T, U](x: T & U): T = ??? | ||
|
|
||
| def test2 = | ||
| val a1: Any => Any = ff1(x => x, box) | ||
| val a2: Any => Any = ff2(x => x) |
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.
The logical OR operator (||) will short-circuit and skip checking
tp.tp2iftp.tp1succeeds. This means if both sides contain type variables that need instantiation, only the first will be instantiated. Consider using bitwise OR (|) or explicitly calling both sides to ensure all nested type variables are instantiated.