Box/unbox nullable inline class values with null check

When we have a nullable inline class value with non-null underlying
type, corresponding value in unboxed representation is nullable. E.g.:

  inline class Str(val value: String)

  fun test(s: Str?) = listOf(s)

Here 'test(s: Str?)' accepts nullable 'java.lang.String' as a parameter.

When boxing/unboxing nullable values of such inline classes, take care
of nulls.

 #KT-26052 Fixed Target versions 1.3-M2
This commit is contained in:
Dmitry Petrov
2018-08-13 12:28:01 +03:00
parent 56ad091534
commit ebf8ec455d
12 changed files with 360 additions and 1 deletions
@@ -0,0 +1,46 @@
// !LANGUAGE: +InlineClasses
// WITH_RUNTIME
// IGNORE_BACKEND: JVM_IR, JS_IR
inline class X(val x: String)
inline class Y(val y: Number)
inline class NX(val x: String?)
inline class NY(val y: Number?)
fun testNotNull(x: X?, y: Y?) {
val xs = listOf<Any?>(x)
val ys = listOf<Any?>(y)
if (!xs.contains(y)) throw AssertionError()
if (xs[0] != ys[0]) throw AssertionError()
if (xs[0] !== ys[0]) throw AssertionError()
}
fun testNullable(x: NX?, y: NY?) {
val xs = listOf<Any?>(x)
val ys = listOf<Any?>(y)
if (xs.contains(y)) throw AssertionError()
if (xs[0] == ys[0]) throw AssertionError()
if (xs[0] === ys[0]) throw AssertionError()
}
fun testNullsAsNullable(x: NX?, y: NY?) {
val xs = listOf<Any?>(x)
val ys = listOf<Any?>(y)
if (!xs.contains(y)) throw AssertionError()
if (xs[0] != ys[0]) throw AssertionError()
if (xs[0] !== ys[0]) throw AssertionError()
}
fun box(): String {
testNotNull(null, null)
testNullable(NX(null), NY(null))
testNullable(NX(null), null)
testNullable(null, NY(null))
testNullsAsNullable(null, null)
return "OK"
}