Skip to content

Commit 1ca2d81

Browse files
committed
Better cast elision
We can get rid of `asInstanceOf[T]` whenever the erased type of expression is a subtype of T. This allows us to produce leaner bytecode and also fixes #8712 since unnecessary asInstanceOf calls were preventing tailrec from working.
1 parent bd1fff2 commit 1ca2d81

File tree

2 files changed

+7
-7
lines changed

2 files changed

+7
-7
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ object TypeTestsCasts {
240240

241241
def transformAsInstanceOf(testType: Type): Tree = {
242242
def testCls = testType.widen.classSymbol
243-
if (expr.tpe <:< testType)
243+
if (erasure(expr.tpe) <:< testType)
244244
Typed(expr, tree.args.head)
245245
else if (testCls eq defn.BoxedUnitClass)
246246
// as a special case, casting to Unit always successfully returns Unit

compiler/test/dotty/tools/backend/jvm/ArrayApplyOptTest.scala

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ class ArrayApplyOptTest extends DottyBytecodeTest {
5151
@Test def testArrayApplyBoolean = {
5252
val init = List(Op(DUP), Op(ICONST_0), Op(ICONST_1), Op(BASTORE), Op(DUP), Op(ICONST_1), Op(ICONST_0), Op(BASTORE))
5353
test("Array(true, false)", newArray2Opcodes(T_BOOLEAN, init))
54-
test("IArray(true, false)", newArray2Opcodes(T_BOOLEAN, init :+ TypeOp(CHECKCAST, "[Z")))
54+
test("IArray(true, false)", newArray2Opcodes(T_BOOLEAN, init))
5555
}
5656

5757
@Test def testArrayApplyByte = {
@@ -69,7 +69,7 @@ class ArrayApplyOptTest extends DottyBytecodeTest {
6969
@Test def testArrayApplyInt = {
7070
val init = List(Op(DUP), Op(ICONST_0), Op(ICONST_1), Op(IASTORE), Op(DUP), Op(ICONST_1), Op(ICONST_2), Op(IASTORE))
7171
test("Array(1, 2)", newArray2Opcodes(T_INT, init))
72-
test("IArray(1, 2)", newArray2Opcodes(T_INT, init :+ TypeOp(CHECKCAST, "[I")))
72+
test("IArray(1, 2)", newArray2Opcodes(T_INT, init))
7373

7474
val init2 = List(Op(DUP), Op(ICONST_0), Field(GETSTATIC, "Foo$", "MODULE$", "LFoo$;"), Invoke(INVOKEVIRTUAL, "Foo$", "t", "()I", false), Op(IASTORE), Op(DUP), Op(ICONST_1), Field(GETSTATIC, "Foo$", "MODULE$", "LFoo$;"), Invoke(INVOKEVIRTUAL, "Foo$", "t", "()I", false), Op(IASTORE))
7575
test("""Array[T](t, t)""", newArray2Opcodes(T_INT, init2))
@@ -79,25 +79,25 @@ class ArrayApplyOptTest extends DottyBytecodeTest {
7979
@Test def testArrayApplyLong = {
8080
val init = List(Op(DUP), Op(ICONST_0), Ldc(LDC, 2), Op(LASTORE), Op(DUP), Op(ICONST_1), Ldc(LDC, 3), Op(LASTORE))
8181
test("Array(2L, 3L)", newArray2Opcodes(T_LONG, init))
82-
test("IArray(2L, 3L)", newArray2Opcodes(T_LONG, init :+ TypeOp(CHECKCAST, "[J")))
82+
test("IArray(2L, 3L)", newArray2Opcodes(T_LONG, init))
8383
}
8484

8585
@Test def testArrayApplyFloat = {
8686
val init = List(Op(DUP), Op(ICONST_0), Ldc(LDC, 2.1f), Op(FASTORE), Op(DUP), Op(ICONST_1), Ldc(LDC, 3.1f), Op(FASTORE))
8787
test("Array(2.1f, 3.1f)", newArray2Opcodes(T_FLOAT, init))
88-
test("IArray(2.1f, 3.1f)", newArray2Opcodes(T_FLOAT, init :+ TypeOp(CHECKCAST, "[F")))
88+
test("IArray(2.1f, 3.1f)", newArray2Opcodes(T_FLOAT, init))
8989
}
9090

9191
@Test def testArrayApplyDouble = {
9292
val init = List(Op(DUP), Op(ICONST_0), Ldc(LDC, 2.2d), Op(DASTORE), Op(DUP), Op(ICONST_1), Ldc(LDC, 3.2d), Op(DASTORE))
9393
test("Array(2.2d, 3.2d)", newArray2Opcodes(T_DOUBLE, init))
94-
test("IArray(2.2d, 3.2d)", newArray2Opcodes(T_DOUBLE, init :+ TypeOp(CHECKCAST, "[D")))
94+
test("IArray(2.2d, 3.2d)", newArray2Opcodes(T_DOUBLE, init))
9595
}
9696

9797
@Test def testArrayApplyChar = {
9898
val init = List(Op(DUP), Op(ICONST_0), IntOp(BIPUSH, 120), Op(CASTORE), Op(DUP), Op(ICONST_1), IntOp(BIPUSH, 121), Op(CASTORE))
9999
test("Array('x', 'y')", newArray2Opcodes(T_CHAR, init))
100-
test("IArray('x', 'y')", newArray2Opcodes(T_CHAR, init :+ TypeOp(CHECKCAST, "[C")))
100+
test("IArray('x', 'y')", newArray2Opcodes(T_CHAR, init))
101101
}
102102

103103
@Test def testArrayApplyUnit = {

0 commit comments

Comments
 (0)