Do not unbox nullable Result, since before usage it is coerced

#KT-46505
This commit is contained in:
Ilmir Usmanov
2021-05-10 13:07:23 +02:00
parent 656c2496a6
commit 640d263ae1
19 changed files with 254 additions and 27 deletions
@@ -6,17 +6,23 @@ object Foo {
}
fun bar(value: Value?) {
res = value?.value as String
res = value?.value as String?
}
}
var res = "FAIL"
var res: String? = "FAIL"
fun box(): String {
Value("OK").let(Foo::foo)
if (res != "OK") return "FAIL 1: $res"
res = "FAIL"
res = "FAIL 2"
Value("OK").let(Foo::bar)
return res
if (res != "OK") return "FAIL 3: $res"
res = "FAIL 4"
null.let(Foo::bar)
if (res != null) return "FAIL 5: $res"
return "OK"
}
@@ -6,17 +6,23 @@ object Foo {
}
fun bar(value: Value?) {
res = value?.value as String
res = value?.value as String?
}
}
var res = "FAIL"
var res: String? = "FAIL"
fun box(): String {
Value("OK").let(Foo::foo)
if (res != "OK") return "FAIL 1: $res"
res = "FAIL"
res = "FAIL 2"
Value("OK").let(Foo::bar)
return res
if (res != "OK") return "FAIL 3: $res"
res = "FAIL 4"
null.let(Foo::bar)
if (res != null) return "FAIL 5: $res"
return "OK"
}
@@ -6,11 +6,11 @@ object Foo {
}
fun bar(value: Value?) {
res = value?.value!!
res = value?.value
}
}
var res = 0
var res: Int? = 0
fun box(): String {
Value(42).let(Foo::foo)
@@ -19,6 +19,10 @@ fun box(): String {
Value(42).let(Foo::bar)
if (res != 42) return "FAIL 2 $res"
res = 0
null.let(Foo::bar)
if (res != null) return "FAIL 3: $res"
return "OK"
}
@@ -2,15 +2,15 @@ inline class Value(val value: Int?)
object Foo {
fun foo(value: Value) {
res = value.value!!
res = value.value
}
fun bar(value: Value?) {
res = value?.value!!
res = value?.value
}
}
var res = 0
var res: Int? = 0
fun box(): String {
Value(42).let(Foo::foo)
@@ -19,6 +19,10 @@ fun box(): String {
Value(42).let(Foo::bar)
if (res != 42) return "FAIL 2 $res"
res = 0
null.let(Foo::bar)
if (res != null) return "FAIL 3: $res"
return "OK"
}
@@ -4,21 +4,26 @@
object Foo {
fun foo(result: Result<String>) {
res = result.getOrNull()!!
res = result.getOrNull()
}
fun bar(result: Result<String>?) {
res = result?.getOrNull()!!
res = result?.getOrNull()
}
}
var res = "FAIL"
var res: String? = "FAIL"
fun box(): String {
Result.success("OK").let(Foo::foo)
if (res != "OK") return "FAIL 1 $res"
res = "FAIL"
res = "FAIL 2"
Result.success("OK").let(Foo::bar)
return res
if (res != "OK") return "FAIL 3 $res"
res = "FAIL 4"
null.let(Foo::bar)
if (res != null) return "FAIL 5: $res"
return "OK"
}
@@ -6,17 +6,22 @@ object Foo {
}
fun bar(value: Value?) {
res = value?.value!!
res = value?.value
}
}
var res = "FAIL"
var res: String? = "FAIL"
fun box(): String {
Value("OK").let(Foo::foo)
if (res != "OK") return "FAIL 1: $res"
res = "FAIL"
res = "FAIL 2"
Value("OK").let(Foo::bar)
return res
if (res != "OK") return "FAIL 3: $res"
res = "FAIL 4"
null.let(Foo::bar)
if (res != null) return "FAIL 3: $res"
return "OK"
}
@@ -2,21 +2,26 @@ inline class Value(val value: String?)
object Foo {
fun foo(value: Value) {
res = value.value!!
res = value.value
}
fun bar(value: Value?) {
res = value?.value!!
res = value?.value
}
}
var res = "FAIL"
var res: String? = "FAIL"
fun box(): String {
Value("OK").let(Foo::foo)
if (res != "OK") return "FAIL 1: $res"
res = "FAIL"
res = "FAIL 2"
Value("OK").let(Foo::bar)
return res
if (res != "OK") return "FAIL 3: $res"
res = "FAIL 4"
null.let(Foo::bar)
if (res != null) return "FAIL 3: $res"
return "OK"
}
@@ -0,0 +1,24 @@
// DONT_TARGET_EXACT_BACKEND: WASM
// WASM_MUTE_REASON: SAM_CONVERSIONS
// !LANGUAGE: +InlineClasses
// WITH_RUNTIME
fun <T> foo(a: Result<T>?): T? = bar(a) {
it?.getOrThrow()
}
fun interface FunIFace<T, R> {
fun call(ic: T): R
}
fun <T, R> bar(value: T, f: FunIFace<T, R>): R {
return f.call(value)
}
fun box(): String {
var res = foo<Int>(Result.success(40))?.plus(2)
if (res != 42) return "FAIL $res"
res = foo<Int>(null)
if (res != null) return "FAIL $res"
return "OK"
}
@@ -0,0 +1,18 @@
// !LANGUAGE: +InlineClasses
// WITH_RUNTIME
fun <T> foo(a: Result<T>?): T? = bar(a) {
it?.getOrThrow()
}
fun <T, R> bar(value: T, f: (T) -> R): R {
return f(value)
}
fun box(): String {
var res = foo<Int>(Result.success(40))?.plus(2)
if (res != 42) return "FAIL $res"
res = foo<Int>(null)
if (res != null) return "FAIL $res"
return "OK"
}
@@ -0,0 +1,24 @@
// !LANGUAGE: +InlineClasses
// WITH_RUNTIME
// IGNORE_BACKEND: JVM
// IGNORE_LIGHT_ANALYSIS
fun <T> foo(a: Result<T>?): T? = bar(a, object : IFace<Result<T>, T> {
override fun call(ic: Result<T>?): T? = ic?.getOrThrow()
})
interface IFace<T, R> {
fun call(ic: T?): R?
}
fun <T, R> bar(value: T?, f: IFace<T, R>): R? {
return f.call(value)
}
fun box(): String {
var res = foo<Int>(Result.success(40))?.plus(2)
if (res != 42) return "FAIL $res"
res = foo<Int>(null)
if (res != null) return "FAIL $res"
return "OK"
}