JVM_IR: check for null when converting unboxed inline classes to strings

`C?` can be unboxed into `T?` if if wraps a reference type `T!!`, but in
this case `null` is not a valid value to pass to `toString-impl`.

 #KT-42005 Fixed
This commit is contained in:
pyos
2020-09-24 16:21:39 +02:00
committed by Alexander Udalov
parent d2bdab2ef5
commit bd6ead0467
14 changed files with 125 additions and 39 deletions
@@ -1,13 +0,0 @@
// !LANGUAGE: +InlineClasses
// FILE: Z.kt
inline class Z(val x: Int)
// FILE: test.kt
fun testZ(z: Z) = z.toString()
fun testNZ(z: Z?) = z?.toString()
// @TestKt.class:
// 0 INVOKESTATIC Z\$Erased\.toString
// 0 INVOKESTATIC Z\-Erased\.toString
// 2 INVOKESTATIC Z\.toString-impl \(I\)Ljava/lang/String;
@@ -0,0 +1,15 @@
// !LANGUAGE: +InlineClasses
// FILE: Z.kt
inline class Z(val x: Int)
// FILE: test.kt
fun testZ(z: Z) = z.toString()
fun testZT(z: Z) = "$z"
fun testNZ(z: Z?) = z?.toString() // unboxed into Int after the null check
fun testNZA(z: Z?) = z.toString() // calls Any?.toString() on boxed value
fun testNZT(z: Z?) = "$z" // same
// @TestKt.class:
// 3 INVOKESTATIC Z\.toString-impl \(I\)Ljava/lang/String;
// 2 INVOKESTATIC java/lang/String.valueOf
@@ -0,0 +1,17 @@
// !LANGUAGE: +InlineClasses
// Completely incorrect bytecode - see `box/inlineClasses/toStringOfUnboxedNullable.kt`
// IGNORE_BACKEND: JVM
// FILE: Z.kt
inline class Z(val x: Any)
// FILE: test.kt
fun testZ(z: Z) = z.toString()
fun testZT(z: Z) = "$z"
fun testNZ(z: Z?) = z?.toString() // `Z?` is unboxed into `Any?` even before the null check
fun testNZA(z: Z?) = z.toString() // so all of these call toString-impl
fun testNZT(z: Z?) = "$z"
// @TestKt.class:
// 5 INVOKESTATIC Z\.toString-impl \(Ljava/lang/Object;\)Ljava/lang/String;
// 0 INVOKESTATIC java/lang/String.valueOf