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

Refine GADT casts with GADT approximation of singleton types #15533

Closed
wants to merge 2 commits 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
10 changes: 9 additions & 1 deletion compiler/src/dotty/tools/dotc/core/GadtConstraint.scala
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,15 @@ final class ProperGadtConstraint private(
def isNarrowing: Boolean = wasConstrained

override def approximation(sym: Symbol, fromBelow: Boolean)(using Context): Type = {
val res = approximation(tvarOrError(sym).origin, fromBelow = fromBelow)
val res =
approximation(tvarOrError(sym).origin, fromBelow = fromBelow) match
case tpr: TypeParamRef =>
// Here we do externalization when the returned type is a TypeParamRef,
// b/c ConstraintHandling.approximation may return internal types when
// the type variable is instantiated. See #15531.
externalize(tpr)
case tp => tp
dwijnand marked this conversation as resolved.
Show resolved Hide resolved

gadts.println(i"approximating $sym ~> $res")
res
}
Expand Down
8 changes: 7 additions & 1 deletion compiler/src/dotty/tools/dotc/typer/Typer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3767,7 +3767,13 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
// up with a test case for this.
val target =
if tree.tpe.isSingleton then
val conj = AndType(tree.tpe, pt)
// In the target type, when the singleton type is intersected, we also intersect
// the GADT-approximated type of the singleton to avoid the loss of
// information. See #14776.
val gadtApprox = Inferencing.approximateGADT(tree.tpe.widen)
gadts.println(i"gadt approx $wtp ~~~ $gadtApprox")
val conj =
AndType(AndType(tree.tpe, gadtApprox), pt)
if tree.tpe.isStable && !conj.isStable then
// this is needed for -Ycheck. Without the annotation Ycheck will
// skolemize the result type which will lead to different types before
Expand Down
13 changes: 13 additions & 0 deletions tests/pos/gadt-cast-singleton.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
enum SUB[-A, +B]:
case Refl[S]() extends SUB[S, S]

trait R {
type Data
}
trait L extends R

def f(x: L): x.Data = ???

def g[T <: R](x: T, ev: T SUB L): x.Data = ev match
case SUB.Refl() =>
f(x)
15 changes: 15 additions & 0 deletions tests/pos/i14776-patmat.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
trait T1
trait T2 extends T1

trait Expr[T] { val data: T = ??? }
case class Tag2() extends Expr[T2]

def flag: Boolean = ???

def foo[T](e: Expr[T]): T1 = e match {
case Tag2() =>
flag match
case true => new T2 {}
case false => e.data
}

16 changes: 16 additions & 0 deletions tests/pos/i14776.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
trait T1
trait T2 extends T1

trait Expr[T] { val data: T = ??? }
case class Tag2() extends Expr[T2]

def flag: Boolean = ???

def foo[T](e: Expr[T]): T1 = e match {
case Tag2() =>
if flag then
new T2 {}
else
e.data
}

9 changes: 9 additions & 0 deletions tests/pos/i15531.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
trait Tag { val data: Int }

enum EQ[A, B]:
case Refl[C]() extends EQ[C, C]

def foo[T, B <: Tag](ev: EQ[T, B], x: T) = ev match
case EQ.Refl() =>
val i: Int = x.data