Skip to content

Commit df28f8a

Browse files
committed
Move check for casting a primitive to a non-primitive type from erasure to refchecks
1 parent 7b407e5 commit df28f8a

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,9 @@ trait TypeTestsCasts {
4747
def argCls = argType.classSymbol
4848
if (expr.tpe <:< argType)
4949
Literal(Constant(true)) withPos tree.pos
50-
else if (qualCls.isPrimitiveValueClass) {
51-
if (argCls.isPrimitiveValueClass) Literal(Constant(qualCls == argCls))
52-
else errorTree(tree, "isInstanceOf cannot test if value types are references")
53-
}
5450
else if (argCls.isPrimitiveValueClass)
55-
transformIsInstanceOf(expr, defn.boxedClass(argCls).typeRef)
51+
if (qualCls.isPrimitiveValueClass) Literal(Constant(qualCls == argCls))
52+
else transformIsInstanceOf(expr, defn.boxedClass(argCls).typeRef)
5653
else argType.dealias match {
5754
case _: SingletonType =>
5855
val cmpOp = if (argType derivesFrom defn.AnyValClass) defn.Any_equals else defn.Object_eq

src/dotty/tools/dotc/typer/RefChecks.scala

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -814,6 +814,22 @@ class RefChecks extends MiniPhase with SymTransformer { thisTransformer =>
814814
currentLevel.enterReference(tree.tpe.typeSymbol, tree.pos)
815815
tree
816816
}
817+
818+
override def transformTypeApply(tree: tpd.TypeApply)(implicit ctx: Context, info: TransformerInfo): tpd.Tree = {
819+
tree.fun match {
820+
case fun@Select(qual, selector) =>
821+
val sym = tree.symbol
822+
823+
if (sym == defn.Any_isInstanceOf) {
824+
val argType = tree.args.head.tpe
825+
val qualCls = qual.tpe.widen.classSymbol
826+
val argCls = argType.classSymbol
827+
if (qualCls.isPrimitiveValueClass && !argCls.isPrimitiveValueClass) ctx.error("isInstanceOf cannot test if value types are references", tree.pos)
828+
}
829+
case _ =>
830+
}
831+
tree
832+
}
817833
}
818834
}
819835

0 commit comments

Comments
 (0)