From d02d4b86dfe281cca18760d49f026a2a9b332e53 Mon Sep 17 00:00:00 2001 From: Dale Wijnand Date: Wed, 25 Sep 2024 16:43:07 +0100 Subject: [PATCH] Avoid erasure/preErasure issues around Any in transformIsInstanceOf The testType Any is erased to Object, but the expr type Int isn't erased to Integer, so then it fails as Int !<: Object. We avoid the problem by feeding in AnyVal, leading to a (possibly elided) non-null test only. --- .../src/dotty/tools/dotc/transform/TypeTestsCasts.scala | 6 +++++- tests/pos/i21544.scala | 2 ++ 2 files changed, 7 insertions(+), 1 deletion(-) create mode 100644 tests/pos/i21544.scala diff --git a/compiler/src/dotty/tools/dotc/transform/TypeTestsCasts.scala b/compiler/src/dotty/tools/dotc/transform/TypeTestsCasts.scala index 082c239c6443..45596e1d47f6 100644 --- a/compiler/src/dotty/tools/dotc/transform/TypeTestsCasts.scala +++ b/compiler/src/dotty/tools/dotc/transform/TypeTestsCasts.scala @@ -358,7 +358,11 @@ object TypeTestsCasts { report.error(em"$untestable cannot be used in runtime type tests", tree.srcPos) constant(expr, Literal(Constant(false))) case _ => - val erasedTestType = erasure(testType) + val erasedTestType = + if testType.isAny && expr.tpe.isPrimitiveValueType then + defn.AnyValType + else + erasure(testType) transformIsInstanceOf(expr, erasedTestType, erasedTestType, flagUnrelated) } diff --git a/tests/pos/i21544.scala b/tests/pos/i21544.scala new file mode 100644 index 000000000000..45da101e7490 --- /dev/null +++ b/tests/pos/i21544.scala @@ -0,0 +1,2 @@ +class Test(): + def m1(xs: List[Boolean]) = for (x: Any) <- xs yield x