Skip to content

Commit c1f8bda

Browse files
committed
Better error messages for illegal type tests
1 parent a323ed4 commit c1f8bda

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -206,19 +206,20 @@ object TypeTestsCasts {
206206
* can be true in some cases. Issues a warning or an error otherwise.
207207
*/
208208
def checkSensical(foundCls: Symbol)(using Context): Boolean =
209+
def exprType = i"type ${expr.tpe.widen.stripAnnots}"
209210
if (!isCheckable(foundCls)) true
210211
else if (foundCls.isPrimitiveValueClass && !testCls.isPrimitiveValueClass) {
211-
ctx.error("cannot test if value types are references", tree.sourcePos)
212+
ctx.error(i"cannot test if value of $exprType is a reference of $testCls", tree.sourcePos)
212213
false
213214
}
214215
else if (!foundCls.derivesFrom(testCls)) {
215216
val unrelated = !testCls.derivesFrom(foundCls) && (
216217
testCls.is(Final) || !testCls.is(Trait) && !foundCls.is(Trait)
217218
)
218219
if (foundCls.is(Final))
219-
unreachable(i"type ${expr.tpe.widen} is not a subclass of $testCls")
220+
unreachable(i"$exprType is not a subclass of $testCls")
220221
else if (unrelated)
221-
unreachable(i"type ${expr.tpe.widen} and $testCls are unrelated")
222+
unreachable(i"$exprType and $testCls are unrelated")
222223
else true
223224
}
224225
else true

0 commit comments

Comments
 (0)