Skip to content

Commit

Permalink
JVM IR: do not optimize casts for primitives in TypeOperatorLowering
Browse files Browse the repository at this point in the history
 #KT-48659 Fixed

(cherry picked from commit 64b911e)
  • Loading branch information
udalov authored and Space committed Sep 10, 2021
1 parent f2109a7 commit 91af581
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 1 deletion.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ private class TypeOperatorLowering(private val context: JvmBackendContext) : Fil
)
}
}
argument.type.isSubtypeOfClass(type.erasedUpperBound.symbol) ->
// Do not optimize casts for values of primitive types because it can affect their identity and thus change behavior.
!argument.type.isPrimitiveType() && argument.type.isSubtypeOfClass(type.erasedUpperBound.symbol) ->
argument
else ->
builder.irAs(argument, type)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// TARGET_BACKEND: JVM

fun int(a: Int, b: Int): Boolean = (a as Any) === (b as Any)
fun short(a: Short, b: Short): Boolean = (a as Any) === (b as Any)
fun char(a: Char, b: Char): Boolean = (a as Any) === (b as Any)
fun long(a: Long, b: Long): Boolean = (a as Any) === (b as Any)
fun float(a: Float, b: Float): Boolean = (a as Any) === (b as Any)
fun double(a: Double, b: Double): Boolean = (a as Any) === (b as Any)

fun byte(a: Byte, b: Byte): Boolean = (a as Any) === (b as Any)
fun boolean(a: Boolean, b: Boolean): Boolean = (a as Any) === (b as Any)

fun box(): String {
if (int(2021, 2021)) return "Fail int"
if (short(32042, 32042)) return "Fail short"
if (char(4242.toChar(), 4242.toChar())) return "Fail char"
if (long(4242424242L, 4242424242L)) return "Fail long"
if (float(3.14f, 3.14f)) return "Fail float"
if (double(2.72, 2.72)) return "Fail double"

// All byte and boolean values are cached in java.lang.Byte/Boolean.valueOf
if (!byte(127, 127)) return "Fail byte"
if (!boolean(true, true)) return "Fail boolean"

return "OK"
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 91af581

Please sign in to comment.