Skip to content

Commit e190c06

Browse files
committed
Localize GADTused variable
1 parent 93e4416 commit e190c06

File tree

2 files changed

+21
-12
lines changed

2 files changed

+21
-12
lines changed

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

+10-1
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
@@ -2426,6 +2432,9 @@ class TypeComparer(using val comparerCtx: Context) extends ConstraintHandling wi
24262432

24272433
object TypeComparer {
24282434

2435+
enum CompareResult:
2436+
case OK, Fail, OKwithGADTUsed
2437+
24292438
/** Class for unification variables used in `natValue`. */
24302439
private class AnyConstantType extends UncachedGroundType with ValueType {
24312440
var tpe: Type = NoType

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

+11-11
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
@@ -3248,23 +3249,22 @@ class Typer extends Namer
32483249
|To turn this error into a warning, pass -Xignore-scala2-macros to the compiler""".stripMargin, tree.sourcePos.startPos)
32493250
tree
32503251
}
3251-
else if (tree.tpe.widenExpr <:< pt) {
3252-
if (ctx.typeComparer.GADTused && pt.isValueType)
3252+
else ctx.typeComparer.testSubType(tree.tpe.widenExpr, pt) match
3253+
case CompareResult.Fail =>
3254+
wtp match
3255+
case wtp: MethodType => missingArgs(wtp)
3256+
case _ =>
3257+
typr.println(i"adapt to subtype ${tree.tpe} !<:< $pt")
3258+
//typr.println(TypeComparer.explained(tree.tpe <:< pt))
3259+
adaptToSubType(wtp)
3260+
case CompareResult.OKwithGADTUsed if pt.isValueType =>
32533261
// Insert an explicit cast, so that -Ycheck in later phases succeeds.
32543262
// I suspect, but am not 100% sure that this might affect inferred types,
32553263
// if the expected type is a supertype of the GADT bound. It would be good to come
32563264
// up with a test case for this.
32573265
tree.cast(pt)
3258-
else
3259-
tree
3260-
}
3261-
else wtp match {
3262-
case wtp: MethodType => missingArgs(wtp)
32633266
case _ =>
3264-
typr.println(i"adapt to subtype ${tree.tpe} !<:< $pt")
3265-
//typr.println(TypeComparer.explained(tree.tpe <:< pt))
3266-
adaptToSubType(wtp)
3267-
}
3267+
tree
32683268
}
32693269

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

0 commit comments

Comments
 (0)