Skip to content
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

Tweak tparam unification to work with lambda cleanup #22031

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions compiler/src/dotty/tools/dotc/core/ConstraintHandling.scala
Original file line number Diff line number Diff line change
Expand Up @@ -436,8 +436,17 @@ trait ConstraintHandling {

val level1 = nestingLevel(p1)
val level2 = nestingLevel(p2)
val pKept = if level1 <= level2 then p1 else p2
val pRemoved = if level1 <= level2 then p2 else p1
val p1Wins = if level1 == level2 then
// If the nesting levels match, then we would prefer to unify to the outer most parameter.
// For instance in pos/i21981, while running `normalizedCompatible` against `.map2`,
// we want to unify to B over K, to allow easily removing K by just instantiating it.
def preferP1(ctx: Context): Boolean =
val c = ctx.typerState.constraint
!c.contains(p2) || c.contains(p1) && preferP1(ctx.outer)
preferP1(ctx)
else level1 <= level2
val pKept = if p1Wins then p1 else p2
val pRemoved = if p1Wins then p2 else p1

val down = constraint.exclusiveLower(p2, p1)
val up = constraint.exclusiveUpper(p1, p2)
Expand Down
28 changes: 28 additions & 0 deletions tests/pos/i21981.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
trait Ops[F[_], A]:
def map0[B](f0: A => B): F[B] = ???

trait Functor1[G[_]]

trait Functor2[H[_]]:
extension [C](hc: H[C])
def map2[D](f1: C => D): H[D]

trait Ref[I[_], +E]

final class Cov[+F]

class Test:
given [J[_]](using J: Functor1[J]): Functor2[J] with
extension [K](jk: J[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 => (???, refs) }
.map2 { case (not, refs) => (???, refs) }