Fix nullability propagation in inline class type mapping

#KT-27096

See https://jetbrains.slack.com/archives/C06E082M6/p1537949572000100
This commit is contained in:
Dmitry Petrov
2018-09-26 12:05:09 +03:00
parent 0b23ddb947
commit ab90b2b901
20 changed files with 843 additions and 15 deletions
+23
View File
@@ -0,0 +1,23 @@
// !LANGUAGE: +InlineClasses
// IGNORE_BACKEND: JVM_IR
// WITH_RUNTIME
inline class Ucn(private val i: UInt)
class PPInput(private val s: ByteArray) {
fun peek(n: UInt = 0u): Ucn? =
if (n >= s.size.toUInt())
null
else
Ucn(s[n.toInt()].toUInt())
}
fun box(): String {
val ppInput = PPInput(byteArrayOf(0.toByte(), 0.toByte(), 0.toByte()))
if (ppInput.peek(0u) == null) throw AssertionError()
if (ppInput.peek(100u) != null) throw AssertionError()
if (ppInput.peek(0u)!!.toString() != "Ucn(i=0)") throw AssertionError(ppInput.peek(0u)!!.toString())
return "OK"
}
@@ -0,0 +1,31 @@
// !LANGUAGE: +InlineClasses
// IGNORE_BACKEND: JVM_IR
enum class En { N, A, B, C }
inline class Z1(val x: En)
inline class Z2(val z: Z1)
inline class ZN(val z: Z1?)
fun wrap1(x: En): Z1? = if (x.ordinal == 0) null else Z1(x)
fun wrap2(x: En): Z2? = if (x.ordinal == 0) null else Z2(Z1(x))
fun wrapN(x: En): ZN? = if (x.ordinal == 0) null else ZN(Z1(x))
fun box(): String {
val n = En.N
val a = En.A
if (wrap1(n) != null) throw AssertionError()
if (wrap1(a) == null) throw AssertionError()
if (wrap1(a)!!.x != a) throw AssertionError()
if (wrap2(n) != null) throw AssertionError()
if (wrap2(a) == null) throw AssertionError()
if (wrap2(a)!!.z.x != a) throw AssertionError()
if (wrapN(n) != null) throw AssertionError()
if (wrapN(a) == null) throw AssertionError()
if (wrapN(a)!!.z!!.x != a) throw AssertionError()
return "OK"
}
@@ -0,0 +1,26 @@
// !LANGUAGE: +InlineClasses
// IGNORE_BACKEND: JVM_IR
inline class Z1(val x: () -> String)
inline class Z2(val z: Z1)
inline class ZN(val z: Z1?)
fun wrap1(x: String): Z1? = if (x.length == 0) null else Z1({ x })
fun wrap2(x: String): Z2? = if (x.length == 0) null else Z2(Z1({ x }))
fun wrapN(x: String): ZN? = if (x.length == 0) null else ZN(Z1({ x }))
fun box(): String {
if (wrap1("") != null) throw AssertionError()
if (wrap1("a") == null) throw AssertionError()
if (wrap1("a")!!.x() != "a") throw AssertionError()
if (wrap2("") != null) throw AssertionError()
if (wrap2("a") == null) throw AssertionError()
if (wrap2("a")!!.z.x() != "a") throw AssertionError()
if (wrapN("") != null) throw AssertionError()
if (wrapN("a") == null) throw AssertionError()
if (wrapN("a")!!.z!!.x() != "a") throw AssertionError()
return "OK"
}
@@ -0,0 +1,42 @@
// !LANGUAGE: +InlineClasses
// IGNORE_BACKEND: JVM_IR
class Outer<X>(val x: X) {
inner class Inner<Y>(val y: Y) {
val hasNull = x == null || y == null
fun outerX() = x
override fun equals(other: Any?): Boolean =
other is Outer<*>.Inner<*> &&
other.outerX() == x &&
other.y == y
}
}
inline class Z1<X, Y>(val x: Outer<X>.Inner<Y>)
inline class Z2<X, Y>(val z: Z1<X, Y>)
inline class ZN<X, Y>(val z: Z1<X, Y>?)
fun <X, Y> wrap1(xy : Outer<X>.Inner<Y>): Z1<X, Y>? = if (xy.hasNull) null else Z1(xy)
fun <X, Y> wrap2(xy : Outer<X>.Inner<Y>): Z2<X, Y>? = if (xy.hasNull) null else Z2(Z1(xy))
fun <X, Y> wrapN(xy : Outer<X>.Inner<Y>): ZN<X, Y>? = if (xy.hasNull) null else ZN(Z1(xy))
fun box(): String {
val n = Outer(null).Inner("a")
val a = Outer("a").Inner("a")
if (wrap1(n) != null) throw AssertionError()
if (wrap1(a) == null) throw AssertionError()
if (wrap1(a)!!.x != a) throw AssertionError()
if (wrap2(n) != null) throw AssertionError()
if (wrap2(a) == null) throw AssertionError()
if (wrap2(a)!!.z.x != a) throw AssertionError()
if (wrapN(n) != null) throw AssertionError()
if (wrapN(a) == null) throw AssertionError()
if (wrapN(a)!!.z!!.x != a) throw AssertionError()
return "OK"
}
@@ -0,0 +1,26 @@
// !LANGUAGE: +InlineClasses
// IGNORE_BACKEND: JVM_IR
inline class Z1(val x: Int?)
inline class Z2(val z: Z1)
inline class ZN(val z: Z1?)
fun wrap1(n: Int): Z1? = if (n < 0) null else Z1(n)
fun wrap2(n: Int): Z2? = if (n < 0) null else Z2(Z1(n))
fun wrapN(n: Int): ZN? = if (n < 0) null else ZN(Z1(n))
fun box(): String {
if (wrap1(-1) != null) throw AssertionError()
if (wrap1(42) == null) throw AssertionError()
if (wrap1(42)!!.x != 42) throw AssertionError()
if (wrap2(-1) != null) throw AssertionError()
if (wrap2(42) == null) throw AssertionError()
if (wrap2(42)!!.z.x != 42) throw AssertionError()
if (wrapN(-1) != null) throw AssertionError()
if (wrapN(42) == null) throw AssertionError()
if (wrapN(42)!!.z!!.x != 42) throw AssertionError()
return "OK"
}
@@ -0,0 +1,26 @@
// !LANGUAGE: +InlineClasses
// IGNORE_BACKEND: JVM_IR
inline class Z1(val x: String?)
inline class Z2(val z: Z1)
inline class ZN(val z: Z1?)
fun wrap1(x: String): Z1? = if (x.length == 0) null else Z1(x)
fun wrap2(x: String): Z2? = if (x.length == 0) null else Z2(Z1(x))
fun wrapN(x: String): ZN? = if (x.length == 0) null else ZN(Z1(x))
fun box(): String {
if (wrap1("") != null) throw AssertionError()
if (wrap1("a") == null) throw AssertionError()
if (wrap1("a")!!.x != "a") throw AssertionError()
if (wrap2("") != null) throw AssertionError()
if (wrap2("a") == null) throw AssertionError()
if (wrap2("a")!!.z.x != "a") throw AssertionError()
if (wrapN("") != null) throw AssertionError()
if (wrapN("a") == null) throw AssertionError()
if (wrapN("a")!!.z!!.x != "a") throw AssertionError()
return "OK"
}
@@ -0,0 +1,32 @@
// !LANGUAGE: +InlineClasses
// IGNORE_BACKEND: JVM_IR
inline class Z1(val x: Int)
inline class Z2(val z: Z1)
inline class ZN(val z: Z1?)
inline class ZN2(val z: ZN)
fun wrap1(n: Int): Z1? = if (n < 0) null else Z1(n)
fun wrap2(n: Int): Z2? = if (n < 0) null else Z2(Z1(n))
fun wrapN(n: Int): ZN? = if (n < 0) null else ZN(Z1(n))
fun wrapN2(n: Int): ZN2? = if (n < 0) null else ZN2(ZN(Z1(n)))
fun box(): String {
if (wrap1(-1) != null) throw AssertionError()
if (wrap1(42) == null) throw AssertionError()
if (wrap1(42)!!.x != 42) throw AssertionError()
if (wrap2(-1) != null) throw AssertionError()
if (wrap2(42) == null) throw AssertionError()
if (wrap2(42)!!.z.x != 42) throw AssertionError()
if (wrapN(-1) != null) throw AssertionError()
if (wrapN(42) == null) throw AssertionError()
if (wrapN(42)!!.z!!.x != 42) throw AssertionError()
if (wrapN2(-1) != null) throw AssertionError()
if (wrapN2(42) == null) throw AssertionError()
if (wrapN2(42)!!.z.z!!.x != 42) throw AssertionError()
return "OK"
}
@@ -0,0 +1,32 @@
// !LANGUAGE: +InlineClasses
// IGNORE_BACKEND: JVM_IR
inline class Z1(val x: String)
inline class Z2(val z: Z1)
inline class ZN(val z: Z1?)
inline class ZN2(val z: ZN)
fun wrap1(x: String): Z1? = if (x.length == 0) null else Z1(x)
fun wrap2(x: String): Z2? = if (x.length == 0) null else Z2(Z1(x))
fun wrapN(x: String): ZN? = if (x.length == 0) null else ZN(Z1(x))
fun wrapN2(x: String): ZN2? = if (x.length == 0) null else ZN2(ZN(Z1(x)))
fun box(): String {
if (wrap1("") != null) throw AssertionError()
if (wrap1("a") == null) throw AssertionError()
if (wrap1("a")!!.x != "a") throw AssertionError()
if (wrap2("") != null) throw AssertionError()
if (wrap2("a") == null) throw AssertionError()
if (wrap2("a")!!.z.x != "a") throw AssertionError()
if (wrapN("") != null) throw AssertionError()
if (wrapN("a") == null) throw AssertionError()
if (wrapN("a")!!.z!!.x != "a") throw AssertionError()
if (wrapN2("") != null) throw AssertionError()
if (wrapN2("a") == null) throw AssertionError()
if (wrapN2("a")!!.z.z!!.x != "a") throw AssertionError()
return "OK"
}
@@ -0,0 +1,18 @@
// !LANGUAGE: +InlineClasses
// IGNORE_BACKEND: JVM_IR
inline class Z1(val x: String)
inline class ZN(val z: Z1?)
inline class ZN2(val z: ZN)
fun zap(b: Boolean): ZN2? = if (b) null else ZN2(ZN(null))
fun eq(a: Any?, b: Any?) = a == b
fun box(): String {
val x = zap(true)
val y = zap(false)
if (eq(x, y)) throw AssertionError()
return "OK"
}