Skip to content

Commit e10d8a7

Browse files
committed
Localize GADTused variable
1 parent 15ae0aa commit e10d8a7

File tree

2 files changed

+21
-12
lines changed

2 files changed

+21
-12
lines changed

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,12 @@ class TypeComparer(using val comparerCtx: Context) extends ConstraintHandling wi
133133
}
134134
}
135135

136+
def testSubType(tp1: Type, tp2: Type): CompareResult =
137+
GADTused = false
138+
if !topLevelSubType(tp1, tp2) then CompareResult.Fail
139+
else if GADTused then CompareResult.OKwithGADTUsed
140+
else CompareResult.OK
141+
136142
/** The current approximation state. See `ApproxState`. */
137143
private var approx: ApproxState = FreshApprox
138144
protected def approxState: ApproxState = approx
@@ -141,7 +147,7 @@ class TypeComparer(using val comparerCtx: Context) extends ConstraintHandling wi
141147
* every time we compare components of the previous pair of types.
142148
* This type is used for capture conversion in `isSubArgs`.
143149
*/
144-
private [this] var leftRoot: Type = _
150+
private [this] var leftRoot: Type = null
145151

146152
/** Are we forbidden from recording GADT constraints? */
147153
private var frozenGadt = false
@@ -2493,6 +2499,9 @@ class TypeComparer(using val comparerCtx: Context) extends ConstraintHandling wi
24932499

24942500
object TypeComparer {
24952501

2502+
enum CompareResult:
2503+
case OK, Fail, OKwithGADTUsed
2504+
24962505
/** Class for unification variables used in `natValue`. */
24972506
private class AnyConstantType extends UncachedGroundType with ValueType {
24982507
var tpe: Type = NoType

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

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import ErrorReporting._
2626
import Checking._
2727
import Inferencing._
2828
import EtaExpansion.etaExpand
29+
import TypeComparer.CompareResult
2930
import util.Spans._
3031
import util.common._
3132
import util.{Property, SimpleIdentityMap, SrcPos}
@@ -3282,23 +3283,22 @@ class Typer extends Namer
32823283
|To turn this error into a warning, pass -Xignore-scala2-macros to the compiler""".stripMargin, tree.srcPos.startPos)
32833284
tree
32843285
}
3285-
else if (tree.tpe.widenExpr <:< pt) {
3286-
if (ctx.typeComparer.GADTused && pt.isValueType)
3286+
else ctx.typeComparer.testSubType(tree.tpe.widenExpr, pt) match
3287+
case CompareResult.Fail =>
3288+
wtp match
3289+
case wtp: MethodType => missingArgs(wtp)
3290+
case _ =>
3291+
typr.println(i"adapt to subtype ${tree.tpe} !<:< $pt")
3292+
//typr.println(TypeComparer.explained(tree.tpe <:< pt))
3293+
adaptToSubType(wtp)
3294+
case CompareResult.OKwithGADTUsed if pt.isValueType =>
32873295
// Insert an explicit cast, so that -Ycheck in later phases succeeds.
32883296
// I suspect, but am not 100% sure that this might affect inferred types,
32893297
// if the expected type is a supertype of the GADT bound. It would be good to come
32903298
// up with a test case for this.
32913299
tree.cast(pt)
3292-
else
3293-
tree
3294-
}
3295-
else wtp match {
3296-
case wtp: MethodType => missingArgs(wtp)
32973300
case _ =>
3298-
typr.println(i"adapt to subtype ${tree.tpe} !<:< $pt")
3299-
//typr.println(TypeComparer.explained(tree.tpe <:< pt))
3300-
adaptToSubType(wtp)
3301-
}
3301+
tree
33023302
}
33033303

33043304
// Follow proxies and approximate type paramrefs by their upper bound

0 commit comments

Comments
 (0)