Skip to content

Allow poly-kinded GADT vars to infer constraints #19916

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

Closed
wants to merge 1 commit into from
Closed
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
3 changes: 3 additions & 0 deletions compiler/src/dotty/tools/dotc/core/SymDenotations.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2287,6 +2287,9 @@ object SymDenotations {
case CapturingType(parent, refs) =>
tp.derivedCapturingType(recur(parent), refs)

case tp: HKLambda =>
NoType

case tp: TypeProxy =>
def computeTypeProxy = {
val superTp = tp.superType
Expand Down
9 changes: 3 additions & 6 deletions compiler/src/dotty/tools/dotc/core/TypeComparer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -580,12 +580,9 @@ class TypeComparer(@constructorOnly initctx: Context) extends ConstraintHandling
if (cls2.typeParams.isEmpty) {
if (cls2 eq AnyKindClass) return true
if (isBottom(tp1)) return true
if (tp1.isLambdaSub) return false
// Note: We would like to replace this by `if (tp1.hasHigherKind)`
// but right now we cannot since some parts of the standard library rely on the
// idiom that e.g. `List <: Any`. We have to bootstrap without scalac first.
if cls2 eq AnyClass then return true
if cls2 == defn.SingletonClass && tp1.isStable then return true
if tp1.hasSimpleKind then
if cls2 eq AnyClass then return true
if cls2 == defn.SingletonClass && tp1.isStable then return true
return tryBaseType(cls2)
Comment on lines +583 to 586
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was this change actually necessary? Isn't tryBaseType always going to fail anyway when tp1.hasSimpleKind is false? (given that you added the case HKTypeLambda => false in baseType, which makes sense)

Copy link
Member Author

@dwijnand dwijnand Mar 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, because then it cascades to more "tries", including allowing a gadt variable bounded by AnyKind inferring a gadt bound.

}
else if (cls2.is(JavaDefined)) {
Expand Down
21 changes: 21 additions & 0 deletions tests/pos/i19904.orig.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
sealed trait Wrap[A <: AnyKind]

trait Foo[T <: AnyKind, X[_ <: T]]

trait Bar[T <: AnyKind, X[_ <: T]] {

def foo: Foo[
Wrap[? <: T],
[x <: Wrap[? <: T]] =>> x match { case Wrap[a] => X[a] }
]

}

object Qux extends Bar[Any, [_] =>> Unit] {

override def foo: Foo[
Wrap[? <: Any],
[x <: Wrap[? <: Any]] =>> x match { case Wrap[a] => Unit }
] = ???

}
15 changes: 15 additions & 0 deletions tests/pos/i19904.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
sealed trait Bx[AK1 <: AnyKind]
trait C1[AK2 <: AnyKind, X[_ <: AK2]]
trait C2[AK3 <: AnyKind, Y[_ <: AK3]]:
def mm: C1[Bx[? <: AK3], [x <: Bx[? <: AK3]] =>> x match { case Bx[a] => Y[a] }]
class C3 extends C2[Any, [_] =>> Unit]:
override def mm: C1[Bx[? <: Any], [y <: Bx[? <: Any]] =>> y match { case Bx[b] => Unit }] = ???

/*
C1[Bx[?], [y <: Bx[?]] =>> y match { case Bx[b] => Unit } <: Unit] <: C1[Bx[?], [x <: Bx[?]] =>> x match { case Bx[a] => Unit } <: Unit]
[x <: Bx[?]] =>> x match { case Bx[a] => Unit } <: Unit <: [y <: Bx[?]] =>> y match { case Bx[b] => Unit } <: Unit
x match { case Bx[a] => Unit } <: Unit <: x match { case Bx[b] => Unit } <: Unit
[a] =>> case Bx[a] => Unit <: [b <: AnyKind] =>> case Bx[b] => Unit
[ <: AnyKind] <: []
AnyKind <: Any = false
*/