Skip to content

Commit f8847e0

Browse files
committed
Better error messages for illegal type tests
1 parent 89ad95f commit f8847e0

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
@@ -209,16 +209,17 @@ object TypeTestsCasts {
209209
* can be true in some cases. Issues a warning or an error otherwise.
210210
*/
211211
def checkSensical(foundClasses: List[Symbol])(using Context): Boolean =
212+
def exprType = i"type ${expr.tpe.widen.stripAnnots}"
212213
def check(foundCls: Symbol): Boolean =
213214
if (!isCheckable(foundCls)) true
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
@@ -227,7 +228,7 @@ object TypeTestsCasts {
227228
val foundEffectiveClass = effectiveClass(expr.tpe.widen)
228229

229230
if foundEffectiveClass.isPrimitiveValueClass && !testCls.isPrimitiveValueClass then
230-
ctx.error("cannot test if value types are references", tree.sourcePos)
231+
ctx.error(i"cannot test if value of $exprType is a reference of $testCls", tree.sourcePos)
231232
false
232233
else foundClasses.exists(check)
233234
end checkSensical

0 commit comments

Comments
 (0)