Skip to content

Commit f8dec07

Browse files
authored
Merge pull request #13323 from Linyxus/fix/hkt-bounds
Better utilizing GADT bounds for HKTs
2 parents 5b3c98f + d1a04e2 commit f8dec07

File tree

3 files changed

+35
-1
lines changed

3 files changed

+35
-1
lines changed

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

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1167,13 +1167,25 @@ class TypeComparer(@constructorOnly initctx: Context) extends ConstraintHandling
11671167
else
11681168
fallback(tycon2bounds.lo)
11691169

1170+
def byGadtBounds: Boolean =
1171+
{
1172+
tycon2 match
1173+
case tycon2: TypeRef =>
1174+
val tycon2sym = tycon2.symbol
1175+
tycon2sym.onGadtBounds { bounds2 =>
1176+
inFrozenGadt { compareLower(bounds2, tyconIsTypeRef = false) }
1177+
}
1178+
case _ => false
1179+
} && { GADTused = true; true }
1180+
11701181
tycon2 match {
11711182
case param2: TypeParamRef =>
11721183
isMatchingApply(tp1) ||
11731184
canConstrain(param2) && canInstantiate(param2) ||
11741185
compareLower(bounds(param2), tyconIsTypeRef = false)
11751186
case tycon2: TypeRef =>
11761187
isMatchingApply(tp1) ||
1188+
byGadtBounds ||
11771189
defn.isCompiletimeAppliedType(tycon2.symbol) && compareCompiletimeAppliedType(tp2, tp1, fromBelow = true) || {
11781190
tycon2.info match {
11791191
case info2: TypeBounds =>
@@ -1213,11 +1225,18 @@ class TypeComparer(@constructorOnly initctx: Context) extends ConstraintHandling
12131225
isSubType(bounds(param1).hi.applyIfParameterized(args1), tp2, approx.addLow)
12141226
case tycon1: TypeRef =>
12151227
val sym = tycon1.symbol
1228+
1229+
def byGadtBounds: Boolean =
1230+
sym.onGadtBounds { bounds1 =>
1231+
inFrozenGadt { isSubType(bounds1.hi.applyIfParameterized(args1), tp2, approx.addLow) }
1232+
} && { GADTused = true; true }
1233+
1234+
12161235
!sym.isClass && {
12171236
defn.isCompiletimeAppliedType(sym) && compareCompiletimeAppliedType(tp1, tp2, fromBelow = false) ||
12181237
recur(tp1.superType, tp2) ||
12191238
tryLiftedToThis1
1220-
}
1239+
}|| byGadtBounds
12211240
case tycon1: TypeProxy =>
12221241
recur(tp1.superType, tp2)
12231242
case _ =>

tests/pos/gadt-hkt-hi-bounds.scala

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
type Const = [X] =>> Int
2+
3+
trait Expr[-F[_]]
4+
case class ConstExpr() extends Expr[Const]
5+
6+
def foo[F[_], A](e: Expr[F]) = e match
7+
case _: ConstExpr =>
8+
val i: Int = ??? : F[A]

tests/pos/gadt-hkt-lo-bounds.scala

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
type Const = [X] =>> Int
2+
3+
trait Expr[+F[_]]
4+
case class ConstExpr() extends Expr[Const]
5+
6+
def foo[F[_], A](e: Expr[F]): F[A] = e match
7+
case _: ConstExpr => 0

0 commit comments

Comments
 (0)