Skip to content

Commit 2b2af9f

Browse files
committed
Add more tests, stop logging
1 parent a679471 commit 2b2af9f

File tree

5 files changed

+43
-14
lines changed

5 files changed

+43
-14
lines changed

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

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1017,13 +1017,19 @@ class TypeComparer(initctx: Context) extends ConstraintHandling[AbsentContext] {
10171017
else if (tp1 eq tp2) true
10181018
else {
10191019
val saved = constraint
1020+
var savedGadt: GadtConstraint = null
1021+
if (ctx.mode.is(Mode.GADTflexible)) savedGadt = ctx.gadt.fresh
10201022
val savedSuccessCount = successCount
10211023
try {
10221024
recCount = recCount + 1
10231025
if (recCount >= Config.LogPendingSubTypesThreshold) monitored = true
10241026
val result = if (monitored) monitoredIsSubType else firstTry
10251027
recCount = recCount - 1
1026-
if (!result) state.resetConstraintTo(saved)
1028+
if (!result) {
1029+
state.resetConstraintTo(saved)
1030+
if (savedGadt ne null)
1031+
ctx.gadt.restore(savedGadt)
1032+
}
10271033
else if (recCount == 0 && needsGc) {
10281034
state.gc()
10291035
needsGc = false
@@ -2093,18 +2099,18 @@ class TypeComparer(initctx: Context) extends ConstraintHandling[AbsentContext] {
20932099
}
20942100

20952101
def notIntersection(tp: Type, pt: Type): Boolean =
2096-
trace(i"notIntersection($tp, $pt)")
2102+
// trace.force(i"notIntersection($tp, $pt)")
20972103
{
2104+
import config.Printers.debug
20982105
import typer.Inferencing._
20992106

21002107
def incompatibleClasses: Boolean = {
21012108
import Flags._
2102-
val tpClassSym = tp.classSymbol
2103-
val ptClassSym = pt.classSymbol
2104-
println(i"tpClassSym=$tpClassSym, fin=${tpClassSym.is(Final)}")
2105-
println(i"ptClassSym=$ptClassSym, fin=${ptClassSym.is(Final)}")
2109+
val tpClassSym = tp.widenSingleton.classSymbol
2110+
val ptClassSym = pt.widenSingleton.classSymbol
2111+
debug.println(i"tpClassSym=$tpClassSym, fin=${tpClassSym.is(Final)}")
2112+
debug.println(i"pt=$pt {${pt.getClass}}, ptClassSym=$ptClassSym, fin=${ptClassSym.is(Final)}")
21062113
tpClassSym.exists && ptClassSym.exists && {
2107-
println("here")
21082114
if (tpClassSym.is(Final)) !tpClassSym.derivesFrom(ptClassSym)
21092115
else if (ptClassSym.is(Final)) !ptClassSym.derivesFrom(tpClassSym)
21102116
else if (!tpClassSym.is(Flags.Trait) && !ptClassSym.is(Flags.Trait))
@@ -2114,7 +2120,7 @@ class TypeComparer(initctx: Context) extends ConstraintHandling[AbsentContext] {
21142120
}
21152121

21162122
def loop(tp: Type): Boolean =
2117-
trace.force(i"loop($tp) // ${tp.toString}")
2123+
// trace.force(i"loop($tp) // ${tp.toString}")
21182124
{
21192125
if (constrainPatternType(pt, tp)) true
21202126
else if (incompatibleClasses) {
@@ -2130,7 +2136,7 @@ class TypeComparer(initctx: Context) extends ConstraintHandling[AbsentContext] {
21302136
case tp @ AppliedType(tycon: TypeRef, _) if tycon.symbol.isClass =>
21312137
val ptClassSym = pt.classSymbol
21322138
def firstParentSharedWithPt(tp: Type, tpClassSym: ClassSymbol): Symbol =
2133-
trace.force(i"f($tp)")
2139+
// trace.force(i"f($tp)")
21342140
{
21352141
var parents = tpClassSym.info.parents
21362142
// println(i"parents of $tpClassSym = $parents%, %")

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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 4 additions & 4 deletions
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
}
Lines changed: 10 additions & 0 deletions
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

Lines changed: 13 additions & 0 deletions
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)