Skip to content

Commit 010fdad

Browse files
Backport "Consistently use TypeMismatch in TreeChecker" to LTS (#22119)
Backports #21529 to the 3.3.5. PR submitted by the release tooling. [skip ci]
2 parents 977d6db + 3b2435f commit 010fdad

File tree

1 file changed

+17
-31
lines changed

1 file changed

+17
-31
lines changed

compiler/src/dotty/tools/dotc/transform/TreeChecker.scala

+17-31
Original file line numberDiff line numberDiff line change
@@ -397,19 +397,8 @@ object TreeChecker {
397397
promote(tree)
398398
case _ =>
399399
val tree1 = super.typedUnadapted(tree, pt, locked)
400-
def isSubType(tp1: Type, tp2: Type) =
401-
(tp1 eq tp2) || // accept NoType / NoType
402-
(tp1 <:< tp2)
403-
def divergenceMsg(tp1: Type, tp2: Type) =
404-
s"""Types differ
405-
|Original type : ${tree.typeOpt.show}
406-
|After checking: ${tree1.tpe.show}
407-
|Original tree : ${tree.show}
408-
|After checking: ${tree1.show}
409-
|Why different :
410-
""".stripMargin + core.TypeComparer.explained(_.isSubType(tp1, tp2))
411-
if (tree.hasType) // it might not be typed because Typer sometimes constructs new untyped trees and resubmits them to typedUnadapted
412-
assert(isSubType(tree1.tpe, tree.typeOpt), divergenceMsg(tree1.tpe, tree.typeOpt))
400+
if tree.hasType then // it might not be typed because Typer sometimes constructs new untyped trees and resubmits them to typedUnadapted
401+
checkType(tree1.tpe, tree.typeOpt, tree, "typedUnadapted")
413402
tree1
414403
checkNoOrphans(res.tpe)
415404
phasesToCheck.foreach(_.checkPostCondition(res))
@@ -747,28 +736,25 @@ object TreeChecker {
747736
override def adapt(tree: Tree, pt: Type, locked: TypeVars)(using Context): Tree = {
748737
def isPrimaryConstructorReturn =
749738
ctx.owner.isPrimaryConstructor && pt.isRef(ctx.owner.owner) && tree.tpe.isRef(defn.UnitClass)
750-
def infoStr(tp: Type) = tp match {
751-
case tp: TypeRef =>
752-
val sym = tp.symbol
753-
i"${sym.showLocated} with ${tp.designator}, flags = ${sym.flagsString}, underlying = ${tp.underlyingIterator.toList}%, %"
754-
case _ =>
755-
"??"
756-
}
757-
if (ctx.mode.isExpr &&
758-
!tree.isEmpty &&
759-
!isPrimaryConstructorReturn &&
760-
!pt.isInstanceOf[FunOrPolyProto])
761-
assert(tree.tpe <:< pt, {
762-
val mismatch = TypeMismatch(tree.tpe, pt, Some(tree))
763-
i"""|${mismatch.msg}
764-
|found: ${infoStr(tree.tpe)}
765-
|expected: ${infoStr(pt)}
766-
|tree = $tree""".stripMargin
767-
})
739+
if ctx.mode.isExpr
740+
&& !tree.isEmpty
741+
&& !isPrimaryConstructorReturn
742+
&& !pt.isInstanceOf[FunOrPolyProto]
743+
then
744+
checkType(tree.tpe, pt, tree, "adapt")
768745
tree
769746
}
770747

771748
override def simplify(tree: Tree, pt: Type, locked: TypeVars)(using Context): tree.type = tree
749+
750+
private def checkType(tp1: Type, tp2: Type, tree: untpd.Tree, step: String)(using Context) =
751+
// Accept NoType <:< NoType as true
752+
assert((tp1 eq tp2) || (tp1 <:< tp2), {
753+
val mismatch = TypeMismatch(tp1, tp2, None)
754+
i"""|Type Mismatch (while checking $step):
755+
|${mismatch.message}${mismatch.explanation}
756+
|tree = $tree ${tree.className}""".stripMargin
757+
})
772758
}
773759

774760
/** Tree checker that can be applied to a local tree. */

0 commit comments

Comments
 (0)