-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Tweak tparam unification to work with lambda cleanup #22031
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
Conversation
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.
It looks reasonable to me. @smarter WDYT? You wrote the original code.
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 can reproduce the problem with the change in this PR if I change the receiver of map2
to be a tuple of type parameters (K1, K2)
instead of a single parameter K
:
trait Ops[F[_], A]:
def map0[B](f0: A => B): F[B] = ???
trait Functor1[G[_]]
trait Functor2[H[_]]:
extension [C1, C2](hc: H[(C1, C2)])
def map2[D](f1: (C1, C2) => D): H[D]
trait Ref[I[_], +E]
final class Cov[+F]
class Test:
given [J[_]](using J: Functor1[J]): Functor2[J] with
extension [K1, K2](jk: J[(K1, K2)])
def map2[L](f2: (K1, K2) => L): J[L] = ???
def t1[
M[_[t]],
N[_],
](using N: Functor1[N]): Unit =
val x3: Ops[N, M[[t] =>> Ref[N, t]]] = ???
val x2: N[(M[N], M[[t] =>> Ref[N, t]])] = x3
.map0 { refs => (???, refs) }
.map2 { case (not, refs) => (???, refs) }
The root cause of the regression is that the removal of type variables in https://github.com/scala/scala3/pull/21466/files#diff-73257963d8a6d79cd4878ae4ff164e33c36629f77821886ffc7cea2e2e38af39R78 does not check that the removed type variables are not in use somewhere. I spent some time looking into it but couldn't find a robust way to do so (maybe a version of Inferencing#interpolateTypeVars that doesn't require a tree argument could be used on pt
in normalizedCompatible
to do this clean-up?), so I think for now it'd be best to just revert #21466
29d89ce
to
18e6a95
Compare
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 think we should still consider a revert of #21466 for the time being.
31b91f3
to
4c9f92a
Compare
// (that we're not also trying to instantiate) | ||
// For example, in tests/pos/i21981.scala | ||
// when testing the compatibility of `.map2[?K]` on receiver `map0[?B]` | ||
// the tvars for B and K unified, instantiating `B := K`, |
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 check tvar1.instanceOpt == tvar
accounts for that, but it doesn't account for B := Foo[K]
. Here's a test case with B := Contra[K]
:
case class Inv[T](x: T)
class Contra[-ContraParam](x: ContraParam)
trait Ops[F[_], A]:
def map0[B](f0: A => Contra[B]): F[B] = ???
trait Functor1[G[_]]
trait Functor2[H[_]]
trait Ref[I[_], +E]
class Test:
given [J[_]](using J: Functor1[J]): Functor2[J] with
extension [K](jk: J[Contra[K]])
def map2[L](f2: K => L): J[L] = ???
def t1[
M[_[t]],
N[_],
](using N: Functor1[N]): Unit =
val x3: Ops[N, M[[t] =>> Ref[N, t]]] = ???
val x2: N[(M[N], M[[t] =>> Ref[N, t]])] = x3
.map0 { refs => Contra[Contra[(Nothing, M[[t] =>> Ref[N, t]])]](???) }
.map2 { case (not, refs) => (???, refs) }
Now this could probably be fixed by checking tvar.occursIn(tvar1.instanceOpt)
but at this point I'd started getting worried about time complexity: we're already inside two nested loops over all type variables and now we're traversing a type (in fact this is the traversal that dependsOn
avoids for uninstantiated type variables).
Thanks for all the review @smarter, it's been great feedback. Do you have another idea how we can deal with the original issue (the extra lambda polluting the constraint) if we revert #21466? |
In
scala3/compiler/src/dotty/tools/dotc/typer/Inferencing.scala Lines 723 to 729 in 399d008
In tests/pos/i21981.scala if I call diff --git compiler/src/dotty/tools/dotc/typer/Inferencing.scala compiler/src/dotty/tools/dotc/typer/Inferencing.scala
index 2ebcd96d5bd..96634cdb1ac 100644
--- compiler/src/dotty/tools/dotc/typer/Inferencing.scala
+++ compiler/src/dotty/tools/dotc/typer/Inferencing.scala
@@ -586,7 +586,7 @@ object Inferencing {
if (vmap1 eq vmap) vmap else propagate(vmap1)
}
- propagate(accu(accu(VarianceMap.empty, tp), pt.finalResultType))
+ propagate(accu(accu(VarianceMap.empty, tp), pt))
}
/** Run the transformation after dealiasing but return the original type if it was a no-op. */ Then we do get I don't think I had a good reason for using |
Thanks for the suggestion. It looks like it works... until I encountered val fr = cpsAsync[Future] {
val y = asyncPlus(1,Future successful 2).asInstanceOf[Int]
y+1
} We end up looking for the implicit |
Fixes #21981