-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Fix #10123: Try to fully define qualifier type #10205
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
Changes from all commits
ad4c1ce
907950e
5d1e468
c3c6533
ee631c2
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 | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -81,6 +81,28 @@ object Inferencing { | |||||||||||
if (depVars.nonEmpty) instantiateSelected(tp, depVars.toList) | ||||||||||||
} | ||||||||||||
|
||||||||||||
/** If `tp` is top-level type variable with a lower bound in the current constraint, | ||||||||||||
* instantiate it from below. We also look for TypeVars whereever their instantiation | ||||||||||||
* could uncover new type members. | ||||||||||||
*/ | ||||||||||||
def couldInstantiateTypeVar(tp: Type)(using Context): Boolean = tp.dealias match | ||||||||||||
case tvar: TypeVar | ||||||||||||
if !tvar.isInstantiated | ||||||||||||
&& ctx.typerState.constraint.contains(tvar) | ||||||||||||
&& tvar.hasLowerBound => | ||||||||||||
tvar.instantiate(fromBelow = true) | ||||||||||||
Comment on lines
+92
to
+93
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When trying to select from something that doesn't have a lower-bound, rather than giving up I think it makes sense to try the upper bound, since it might be precise enough, also we could take
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am not sure that will help. The situation is that we try to select a member that does not exist on the type variable. Instantiating to the upper bound would not help in this case. If the member existed on the upper bound, it would also exist on the variable itself. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Ah indeed, because There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This could be a problem. But (1) it's not related to this issue.(2) I don't know how to solve it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the change I'm proposing together with removing the recursion on the upper bound in |
||||||||||||
true | ||||||||||||
case AppliedType(tycon, _) => | ||||||||||||
couldInstantiateTypeVar(tycon) | ||||||||||||
case RefinedType(parent, _, _) => | ||||||||||||
couldInstantiateTypeVar(parent) | ||||||||||||
case tp: AndOrType => | ||||||||||||
couldInstantiateTypeVar(tp.tp1) || couldInstantiateTypeVar(tp.tp2) | ||||||||||||
case AnnotatedType(tp, _) => | ||||||||||||
couldInstantiateTypeVar(tp) | ||||||||||||
case _ => | ||||||||||||
false | ||||||||||||
|
||||||||||||
/** The accumulator which forces type variables using the policy encoded in `force` | ||||||||||||
* and returns whether the type is fully defined. The direction in which | ||||||||||||
* a type variable is instantiated is determined as follows: | ||||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
class D | ||
class C[+T](x: T) | ||
|
||
class Foo() { | ||
val status: Int = 0 | ||
} | ||
|
||
object Main { | ||
implicit class RichC[T](c: C[T]) { | ||
def await(implicit d: D = ???): T = ??? | ||
} | ||
|
||
def test1: Int = { | ||
val foo = new C(new Foo()).await | ||
foo.status | ||
} | ||
|
||
val test2 = new C(new Foo()).await.status | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// object Test { | ||
// val x: Int => Int = identity | ||
// } | ||
|
||
trait Foo[F[_]] { | ||
def foo[G[x] >: F[x]]: G[Unit] | ||
} | ||
|
||
trait M[A] { | ||
def bla: Int = 1 | ||
def baz(f: Int => Int): Int = f(1) | ||
} | ||
|
||
object Test { | ||
def bar(x: Foo[M]): Unit = { | ||
// error: value bla is not a member of G[Unit], where: G is a type variable with constraint >: M and <: [x] =>> Any | ||
x.foo.bla | ||
|
||
// error: value bla is not a member of G[Unit], where: G is a type variable with constraint >: M and <: [x] =>> Any | ||
x.foo.baz(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.
I don't think that's quite right either since given
?F[?A]
I need to instantiate at least?F
, and then if it's a type lambda I may or may not need to instantaite?A
too. Also since this is a selection prefix, we want to avoidNothing
if possible, in fact in all situation where we're trying to avoidNothing
, we only want to avoid it at the top-level. This is what I came up last time I was looking at this: https://github.com/dotty-staging/dotty/blob/63c7bad7f01c5fccf4620376aa7c8f70bc90b66d/compiler/src/dotty/tools/dotc/typer/Inferencing.scala#L49-L83but I was a bit concerned that this duplicated code from IsFullyDefinedAccumulator.
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.
Right. In this particular case we need to instantiate the
?F
. The?A
can come later when we recursively calltypedSelect
on the new call. I have taken your changes and adapted them tocouldInstantiateTypeVar
. You don't need to check forLazyRef
in either code, since that is already eliminated bydealias
. On the other hand, one should look inside AndTypes, OrTypes, and AnnotatedTypes.