Skip to content

Commit b6b409c

Browse files
committed
Add more tests, stop logging
1 parent eed1a73 commit b6b409c

File tree

5 files changed

+43
-14
lines changed

5 files changed

+43
-14
lines changed

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

+15-9
Original file line numberDiff line numberDiff line change
@@ -1033,13 +1033,19 @@ class TypeComparer(initctx: Context) extends ConstraintHandling[AbsentContext] {
10331033
else if (tp1 eq tp2) true
10341034
else {
10351035
val saved = constraint
1036+
var savedGadt: GadtConstraint = null
1037+
if (ctx.mode.is(Mode.GADTflexible)) savedGadt = ctx.gadt.fresh
10361038
val savedSuccessCount = successCount
10371039
try {
10381040
recCount = recCount + 1
10391041
if (recCount >= Config.LogPendingSubTypesThreshold) monitored = true
10401042
val result = if (monitored) monitoredIsSubType else firstTry
10411043
recCount = recCount - 1
1042-
if (!result) state.resetConstraintTo(saved)
1044+
if (!result) {
1045+
state.resetConstraintTo(saved)
1046+
if (savedGadt ne null)
1047+
ctx.gadt.restore(savedGadt)
1048+
}
10431049
else if (recCount == 0 && needsGc) {
10441050
state.gc()
10451051
needsGc = false
@@ -2132,18 +2138,18 @@ class TypeComparer(initctx: Context) extends ConstraintHandling[AbsentContext] {
21322138
}
21332139

21342140
def notIntersection(tp: Type, pt: Type): Boolean =
2135-
trace(i"notIntersection($tp, $pt)")
2141+
// trace.force(i"notIntersection($tp, $pt)")
21362142
{
2143+
import config.Printers.debug
21372144
import typer.Inferencing._
21382145

21392146
def incompatibleClasses: Boolean = {
21402147
import Flags._
2141-
val tpClassSym = tp.classSymbol
2142-
val ptClassSym = pt.classSymbol
2143-
println(i"tpClassSym=$tpClassSym, fin=${tpClassSym.is(Final)}")
2144-
println(i"ptClassSym=$ptClassSym, fin=${ptClassSym.is(Final)}")
2148+
val tpClassSym = tp.widenSingleton.classSymbol
2149+
val ptClassSym = pt.widenSingleton.classSymbol
2150+
debug.println(i"tpClassSym=$tpClassSym, fin=${tpClassSym.is(Final)}")
2151+
debug.println(i"pt=$pt {${pt.getClass}}, ptClassSym=$ptClassSym, fin=${ptClassSym.is(Final)}")
21452152
tpClassSym.exists && ptClassSym.exists && {
2146-
println("here")
21472153
if (tpClassSym.is(Final)) !tpClassSym.derivesFrom(ptClassSym)
21482154
else if (ptClassSym.is(Final)) !ptClassSym.derivesFrom(tpClassSym)
21492155
else if (!tpClassSym.is(Flags.Trait) && !ptClassSym.is(Flags.Trait))
@@ -2153,7 +2159,7 @@ class TypeComparer(initctx: Context) extends ConstraintHandling[AbsentContext] {
21532159
}
21542160

21552161
def loop(tp: Type): Boolean =
2156-
trace.force(i"loop($tp) // ${tp.toString}")
2162+
// trace.force(i"loop($tp) // ${tp.toString}")
21572163
{
21582164
if (constrainPatternType(pt, tp)) true
21592165
else if (incompatibleClasses) {
@@ -2169,7 +2175,7 @@ class TypeComparer(initctx: Context) extends ConstraintHandling[AbsentContext] {
21692175
case tp @ AppliedType(tycon: TypeRef, _) if tycon.symbol.isClass =>
21702176
val ptClassSym = pt.classSymbol
21712177
def firstParentSharedWithPt(tp: Type, tpClassSym: ClassSymbol): Symbol =
2172-
trace.force(i"f($tp)")
2178+
// trace.force(i"f($tp)")
21732179
{
21742180
var parents = tpClassSym.info.parents
21752181
// println(i"parents of $tpClassSym = $parents%, %")

compiler/src/dotty/tools/dotc/typer/Typer.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -3082,7 +3082,7 @@ class Typer extends Namer
30823082
case _: RefTree | _: Literal
30833083
if !isVarPattern(tree) &&
30843084
!(pt <:< tree.tpe) &&
3085-
!(tree.tpe <:< pt)(ctx.addMode(Mode.GADTflexible)) =>
3085+
!ctx.addMode(Mode.GADTflexible).typeComparer.notIntersection(pt, tree.tpe) =>
30863086
val cmp =
30873087
untpd.Apply(
30883088
untpd.Select(untpd.TypedSplice(tree), nme.EQ),

tests/neg/nontrivial-intersection-gadt.scala

+4-4
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,18 @@ object Test {
55
final class Fin
66

77
def foo1[T](x: Unit | Const[T]): T = x match {
8-
case _: IntExpr => 0
8+
case _: IntExpr => 0 // error
99
}
1010

1111
def bar1[T](x: Const[T]): T = x match {
12-
case _: (Unit | IntExpr) => 0
12+
case _: (Unit | IntExpr) => 0 // error
1313
}
1414

1515
def foo2[T](x: Fin | Const[T]): T = x match {
16-
case _: IntExpr => 0
16+
case _: IntExpr => 0 // error
1717
}
1818

1919
def bar2[T](x: Const[T]): T = x match {
20-
case _: (Fin | IntExpr) => 0
20+
case _: (Fin | IntExpr) => 0 // error
2121
}
2222
}
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
object Test {
2+
sealed trait Expr[+T]
3+
case class IntVal[+T <: Int]() extends Expr[T]
4+
case object ActualInt extends Expr[Int]
5+
def eval[T](e: Expr[T]): T = e match {
6+
case IntVal() => 0 // error
7+
case ActualInt => 0
8+
}
9+
}
10+

tests/neg/unsound-union-gadt.scala

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
object Test {
2+
enum Expr[+T] {
3+
case StrLit() extends Expr[String]
4+
case IntLit() extends Expr[Int]
5+
}
6+
import Expr._
7+
8+
def foo[T](e: Expr[T]) = e match {
9+
case _: (StrLit | IntLit) =>
10+
val str: T = "" // error
11+
val int: T = 42 // error
12+
}
13+
}

0 commit comments

Comments
 (0)