Skip to content

Commit

Permalink
Remove use of simplified in MT reduction
Browse files Browse the repository at this point in the history
I gave match type's redux an extension to the normalize algorithm, in
order to make the test suite pass, as well as add an extra recursion to
the simplify algorithm.  A few neg tests tweaked and some other code
changes could be dropped.
  • Loading branch information
dwijnand committed Jul 1, 2023
1 parent 9726e1a commit 129cfdf
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 13 deletions.
13 changes: 9 additions & 4 deletions compiler/src/dotty/tools/dotc/core/TypeComparer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3136,15 +3136,15 @@ class TrackingTypeComparer(initctx: Context) extends TypeComparer(initctx) {
case param @ TypeParamRef(b, n) if b eq caseLambda =>
insts(n) =
if canApprox then
approximation(param, fromBelow = variance >= 0, Int.MaxValue).simplified
approximation(param, fromBelow = variance >= 0, Int.MaxValue).normalized
else constraint.entry(param) match
case entry: TypeBounds =>
val lo = fullLowerBound(param)
val hi = fullUpperBound(param)
if !poisoned(param) && isSubType(hi, lo) then lo.simplified else Range(lo, hi)
if !poisoned(param) && isSubType(hi, lo) then lo.normalized else Range(lo, hi)
case inst =>
assert(inst.exists, i"param = $param\nconstraint = $constraint")
if !poisoned(param) then inst.simplified else Range(inst, inst)
if !poisoned(param) then inst.normalized else Range(inst, inst)
insts
case _ =>
foldOver(insts, t)
Expand All @@ -3166,6 +3166,11 @@ class TrackingTypeComparer(initctx: Context) extends TypeComparer(initctx) {
}
}

def normalizeHard(tp: Type): Type = tp.tryNormalize.orElse(tp match {
case tp: AppliedType => tp.map(normalizeHard)
case _ => tp
})

/** Match a single case. */
def matchCase(cas: Type): MatchResult = trace(i"$scrut match ${MatchTypeTrace.caseText(cas)}", matchTypes, show = true) {
val cas1 = cas match {
Expand Down Expand Up @@ -3229,7 +3234,7 @@ class TrackingTypeComparer(initctx: Context) extends TypeComparer(initctx) {
MatchTypeTrace.noInstance(scrut, cas, fails)
NoType
case MatchResult.Reduced(tp) =>
tp.simplified
normalizeHard(tp)
case Nil =>
val casesText = MatchTypeTrace.noMatchesText(scrut, cases)
ErrorType(reporting.MatchTypeNoCases(casesText))
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/core/TypeOps.scala
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ object TypeOps:
else tp.derivedAnnotatedType(parent1, annot)
case _: MatchType =>
val normed = tp.tryNormalize
if (normed.exists) normed else mapOver
if (normed.exists) simplify(normed, theMap) else mapOver
case tp: MethodicType =>
// See documentation of `Types#simplified`
val addTypeVars = new TypeMap with IdempotentCaptRefMap:
Expand Down
3 changes: 1 addition & 2 deletions compiler/src/dotty/tools/dotc/core/Types.scala
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ object Types {
* (since these are relevant for inference or resolution) but never consider prefixes
* (since these often do not constrain the search space anyway).
*/
def unusableForInference(using Context): Boolean = try widenDealias match
def unusableForInference(using Context): Boolean = widenDealias match
case AppliedType(tycon, args) => tycon.unusableForInference || args.exists(_.unusableForInference)
case RefinedType(parent, _, rinfo) => parent.unusableForInference || rinfo.unusableForInference
case TypeBounds(lo, hi) => lo.unusableForInference || hi.unusableForInference
Expand All @@ -369,7 +369,6 @@ object Types {
case CapturingType(parent, refs) => parent.unusableForInference || refs.elems.exists(_.unusableForInference)
case _: ErrorType => true
case _ => false
catch case ex: Throwable => handleRecursive("unusableForInference", show, ex)

/** Does the type carry an annotation that is an instance of `cls`? */
@tailrec final def hasAnnotation(cls: ClassSymbol)(using Context): Boolean = stripTypeVar match {
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/typer/Inferencing.scala
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ object Inferencing {
case tp => foldOver(x, tp)
}
catch case ex: Throwable =>
handleRecursive("check fully defined", tp.showSummary(20), ex)
handleRecursive("check fully defined", tp.show, ex)
}

def process(tp: Type): Boolean =
Expand Down
8 changes: 4 additions & 4 deletions tests/neg-custom-args/isInstanceOf/i17435.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ object Test:
type JsonArray = mutable.Buffer[Json]

def encode(x: Json): Int = x match
case str: String => 1 // error
case b: Boolean => 2 // error
case i: Int => 3 // error
case d: Double => 4 // error
case str: String => 1
case b: Boolean => 2
case i: Int => 3
case d: Double => 4
case arr: JsonArray => 5 // error
case obj: JsonObject => 6 // error
case _ => 7
2 changes: 1 addition & 1 deletion tests/neg/i15158.scala
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,6 @@ class Spec {
JsonPrimitive
]

val arr = new mutable.ArrayBuffer[Json](8) // error
val arr = new mutable.ArrayBuffer[Json](8)
}
}

0 comments on commit 129cfdf

Please sign in to comment.