Skip to content

Commit 5dda2a3

Browse files
committed
Tweak tparam unification to work with lambda cleanup
1 parent 81e057a commit 5dda2a3

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

compiler/src/dotty/tools/dotc/core/ConstraintHandling.scala

+11-2
Original file line numberDiff line numberDiff line change
@@ -436,8 +436,17 @@ trait ConstraintHandling {
436436

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

442451
val down = constraint.exclusiveLower(p2, p1)
443452
val up = constraint.exclusiveUpper(p1, p2)

tests/pos/i21981.scala

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
trait Ops[F[_], A]:
2+
def map0[B](f0: A => B): F[B] = ???
3+
4+
trait Functor1[G[_]]
5+
6+
trait Functor2[H[_]]:
7+
extension [C](hc: H[C])
8+
def map2[D](f1: C => D): H[D]
9+
10+
trait Ref[I[_], +E]
11+
12+
final class Cov[+F]
13+
14+
class Test:
15+
given [J[_]](using J: Functor1[J]): Functor2[J] with
16+
extension [K](jk: J[K])
17+
def map2[L](f2: K => L): J[L] = ???
18+
19+
def t1[
20+
M[_[t]],
21+
N[_],
22+
](using N: Functor1[N]): Unit =
23+
24+
val x3: Ops[N, M[[t] =>> Ref[N, t]]] = ???
25+
26+
val x2: N[(M[N], M[[t] =>> Ref[N, t]])] = x3
27+
.map0 { refs => (???, refs) }
28+
.map2 { case (not, refs) => (???, refs) }

0 commit comments

Comments
 (0)