Skip to content

Commit

Permalink
Rollback constraints in compareAppliedTypeParamRef (#22339)
Browse files Browse the repository at this point in the history
This PR adds a `rollbackConstraintsUnless` in
`compareAppliedTypeParamRef`. It is required in case the call to
`directionalIsSubType` introduces constraints and the call to
`directionalRecur` returns `false`.

Fixes #20519
  • Loading branch information
smarter authored Jan 13, 2025
2 parents af22ce2 + c71b253 commit 5369d1a
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 2 deletions.
5 changes: 3 additions & 2 deletions compiler/src/dotty/tools/dotc/core/TypeComparer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1244,8 +1244,9 @@ class TypeComparer(@constructorOnly initctx: Context) extends ConstraintHandling
tl => otherTycon.appliedTo(bodyArgs(tl)))
else
otherTycon
(assumedTrue(tycon) || directionalIsSubType(tycon, adaptedTycon)) &&
directionalRecur(adaptedTycon.appliedTo(args), other)
rollbackConstraintsUnless:
(assumedTrue(tycon) || directionalIsSubType(tycon, adaptedTycon))
&& directionalRecur(adaptedTycon.appliedTo(args), other)
}
}
end compareAppliedTypeParamRef
Expand Down
10 changes: 10 additions & 0 deletions tests/pos/20519.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class Box[T](val value: T)

def boo[F[_], A](e: F[Box[A]]): F[A] = ???

type Result[G[_], B] = G[Box[B]]

def main =
val b: Result[Option, Int] = ???
val c = boo(b)
c: Option[Int]
16 changes: 16 additions & 0 deletions tests/pos/20519b.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
trait TCl[F[_]]

def boo[F[_], A](e: F[Option[A]], ev: TCl[F]): Unit = ()

type Result[F[_], A] = F[Option[A]]

@main def main =
summon[Result[Option, Int] =:= Option[Option[Int]]]

val ev = new TCl[Option] {}

val b: Result[Option, Int] = None
boo(b, ev)

val b2: Option[Option[Int]] = None
boo(b2, ev)
20 changes: 20 additions & 0 deletions tests/pos/20519c.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
object Main {
trait TCl[F[_]]

implicit class Stx[F[_], A](e: F[Option[A]]) {
def boo(implicit ev: TCl[F]): Unit = ()
}

type Result[F[_], A] = F[Option[A]]

implicit val t: TCl[Option] = new TCl[Option] {}

def main(args: Array[String]): Unit = {
val b: Result[Option, Int] = None
b.boo

// works without the alias:
val b2: Option[Option[Int]] = None
b2.boo
}
}

0 comments on commit 5369d1a

Please sign in to comment.