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
@@ -19950,6 +19950,12 @@ public class FirBlackBoxCodegenTestGenerated extends AbstractFirBlackBoxCodegenT
runTest("compiler/testData/codegen/box/inlineClasses/unboxGenericParameter/funInterface/ifaceChild.kt");
}
@Test
@TestMetadata("nullableResult.kt")
public void testNullableResult() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/unboxGenericParameter/funInterface/nullableResult.kt");
}
@Test
@TestMetadata("primitive.kt")
public void testPrimitive() throws Exception {
@@ -20008,6 +20014,12 @@ public class FirBlackBoxCodegenTestGenerated extends AbstractFirBlackBoxCodegenT
runTest("compiler/testData/codegen/box/inlineClasses/unboxGenericParameter/lambda/ifaceChild.kt");
}
@Test
@TestMetadata("nullableResult.kt")
public void testNullableResult() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/unboxGenericParameter/lambda/nullableResult.kt");
}
@Test
@TestMetadata("primitive.kt")
public void testPrimitive() throws Exception {
@@ -20066,6 +20078,12 @@ public class FirBlackBoxCodegenTestGenerated extends AbstractFirBlackBoxCodegenT
runTest("compiler/testData/codegen/box/inlineClasses/unboxGenericParameter/objectLiteral/ifaceChild.kt");
}
@Test
@TestMetadata("nullableResult.kt")
public void testNullableResult() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/unboxGenericParameter/objectLiteral/nullableResult.kt");
}
@Test
@TestMetadata("primitive.kt")
public void testPrimitive() throws Exception {
@@ -654,6 +654,8 @@ class ExpressionCodegen(
// bridge to unbox it. Instead, we unbox it in the non-mangled function manually.
private fun unboxResultIfNeeded(arg: IrGetValue) {
if (arg.type.erasedUpperBound.fqNameWhenAvailable != StandardNames.RESULT_FQ_NAME) return
// Unlike inline callable reference arguments, nullable Result arguments are unboxed during coercion to not-null Result
if (arg.type.isNullable()) return
// Do not unbox arguments of lambda, but unbox arguments of callable references
if (irFunction.origin == IrDeclarationOrigin.LOCAL_FUNCTION_FOR_LAMBDA) return
if (!onlyResultInlineClassParameters()) return
@@ -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"
}
@@ -19926,6 +19926,12 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
runTest("compiler/testData/codegen/box/inlineClasses/unboxGenericParameter/funInterface/ifaceChild.kt");
}
@Test
@TestMetadata("nullableResult.kt")
public void testNullableResult() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/unboxGenericParameter/funInterface/nullableResult.kt");
}
@Test
@TestMetadata("primitive.kt")
public void testPrimitive() throws Exception {
@@ -19984,6 +19990,12 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
runTest("compiler/testData/codegen/box/inlineClasses/unboxGenericParameter/lambda/ifaceChild.kt");
}
@Test
@TestMetadata("nullableResult.kt")
public void testNullableResult() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/unboxGenericParameter/lambda/nullableResult.kt");
}
@Test
@TestMetadata("primitive.kt")
public void testPrimitive() throws Exception {
@@ -20042,6 +20054,12 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
runTest("compiler/testData/codegen/box/inlineClasses/unboxGenericParameter/objectLiteral/ifaceChild.kt");
}
@Test
@TestMetadata("nullableResult.kt")
public void testNullableResult() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/unboxGenericParameter/objectLiteral/nullableResult.kt");
}
@Test
@TestMetadata("primitive.kt")
public void testPrimitive() throws Exception {
@@ -19950,6 +19950,12 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes
runTest("compiler/testData/codegen/box/inlineClasses/unboxGenericParameter/funInterface/ifaceChild.kt");
}
@Test
@TestMetadata("nullableResult.kt")
public void testNullableResult() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/unboxGenericParameter/funInterface/nullableResult.kt");
}
@Test
@TestMetadata("primitive.kt")
public void testPrimitive() throws Exception {
@@ -20008,6 +20014,12 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes
runTest("compiler/testData/codegen/box/inlineClasses/unboxGenericParameter/lambda/ifaceChild.kt");
}
@Test
@TestMetadata("nullableResult.kt")
public void testNullableResult() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/unboxGenericParameter/lambda/nullableResult.kt");
}
@Test
@TestMetadata("primitive.kt")
public void testPrimitive() throws Exception {
@@ -20066,6 +20078,12 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes
runTest("compiler/testData/codegen/box/inlineClasses/unboxGenericParameter/objectLiteral/ifaceChild.kt");
}
@Test
@TestMetadata("nullableResult.kt")
public void testNullableResult() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/unboxGenericParameter/objectLiteral/nullableResult.kt");
}
@Test
@TestMetadata("primitive.kt")
public void testPrimitive() throws Exception {
@@ -16604,6 +16604,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes
runTest("compiler/testData/codegen/box/inlineClasses/unboxGenericParameter/funInterface/ifaceChild.kt");
}
@TestMetadata("nullableResult.kt")
public void testNullableResult() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/unboxGenericParameter/funInterface/nullableResult.kt");
}
@TestMetadata("primitive.kt")
public void testPrimitive() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/unboxGenericParameter/funInterface/primitive.kt");
@@ -16657,6 +16662,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes
runTest("compiler/testData/codegen/box/inlineClasses/unboxGenericParameter/lambda/ifaceChild.kt");
}
@TestMetadata("nullableResult.kt")
public void testNullableResult() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/unboxGenericParameter/lambda/nullableResult.kt");
}
@TestMetadata("primitive.kt")
public void testPrimitive() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/unboxGenericParameter/lambda/primitive.kt");
@@ -16682,6 +16692,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class ObjectLiteral extends AbstractLightAnalysisModeTest {
@TestMetadata("nullableResult.kt")
public void ignoreNullableResult() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/unboxGenericParameter/objectLiteral/nullableResult.kt");
}
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest(this::doTest, TargetBackend.JVM, testDataFilePath);
}
@@ -14613,6 +14613,11 @@ public class IrJsCodegenBoxES6TestGenerated extends AbstractIrJsCodegenBoxES6Tes
runTest("compiler/testData/codegen/box/inlineClasses/unboxGenericParameter/funInterface/ifaceChild.kt");
}
@TestMetadata("nullableResult.kt")
public void testNullableResult() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/unboxGenericParameter/funInterface/nullableResult.kt");
}
@TestMetadata("primitive.kt")
public void testPrimitive() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/unboxGenericParameter/funInterface/primitive.kt");
@@ -14666,6 +14671,11 @@ public class IrJsCodegenBoxES6TestGenerated extends AbstractIrJsCodegenBoxES6Tes
runTest("compiler/testData/codegen/box/inlineClasses/unboxGenericParameter/lambda/ifaceChild.kt");
}
@TestMetadata("nullableResult.kt")
public void testNullableResult() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/unboxGenericParameter/lambda/nullableResult.kt");
}
@TestMetadata("primitive.kt")
public void testPrimitive() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/unboxGenericParameter/lambda/primitive.kt");
@@ -14719,6 +14729,11 @@ public class IrJsCodegenBoxES6TestGenerated extends AbstractIrJsCodegenBoxES6Tes
runTest("compiler/testData/codegen/box/inlineClasses/unboxGenericParameter/objectLiteral/ifaceChild.kt");
}
@TestMetadata("nullableResult.kt")
public void testNullableResult() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/unboxGenericParameter/objectLiteral/nullableResult.kt");
}
@TestMetadata("primitive.kt")
public void testPrimitive() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/unboxGenericParameter/objectLiteral/primitive.kt");
@@ -14024,6 +14024,11 @@ public class IrJsCodegenBoxTestGenerated extends AbstractIrJsCodegenBoxTest {
runTest("compiler/testData/codegen/box/inlineClasses/unboxGenericParameter/funInterface/ifaceChild.kt");
}
@TestMetadata("nullableResult.kt")
public void testNullableResult() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/unboxGenericParameter/funInterface/nullableResult.kt");
}
@TestMetadata("primitive.kt")
public void testPrimitive() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/unboxGenericParameter/funInterface/primitive.kt");
@@ -14077,6 +14082,11 @@ public class IrJsCodegenBoxTestGenerated extends AbstractIrJsCodegenBoxTest {
runTest("compiler/testData/codegen/box/inlineClasses/unboxGenericParameter/lambda/ifaceChild.kt");
}
@TestMetadata("nullableResult.kt")
public void testNullableResult() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/unboxGenericParameter/lambda/nullableResult.kt");
}
@TestMetadata("primitive.kt")
public void testPrimitive() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/unboxGenericParameter/lambda/primitive.kt");
@@ -14130,6 +14140,11 @@ public class IrJsCodegenBoxTestGenerated extends AbstractIrJsCodegenBoxTest {
runTest("compiler/testData/codegen/box/inlineClasses/unboxGenericParameter/objectLiteral/ifaceChild.kt");
}
@TestMetadata("nullableResult.kt")
public void testNullableResult() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/unboxGenericParameter/objectLiteral/nullableResult.kt");
}
@TestMetadata("primitive.kt")
public void testPrimitive() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/unboxGenericParameter/objectLiteral/primitive.kt");
@@ -14089,6 +14089,11 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest {
runTest("compiler/testData/codegen/box/inlineClasses/unboxGenericParameter/funInterface/ifaceChild.kt");
}
@TestMetadata("nullableResult.kt")
public void testNullableResult() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/unboxGenericParameter/funInterface/nullableResult.kt");
}
@TestMetadata("primitive.kt")
public void testPrimitive() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/unboxGenericParameter/funInterface/primitive.kt");
@@ -14142,6 +14147,11 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest {
runTest("compiler/testData/codegen/box/inlineClasses/unboxGenericParameter/lambda/ifaceChild.kt");
}
@TestMetadata("nullableResult.kt")
public void testNullableResult() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/unboxGenericParameter/lambda/nullableResult.kt");
}
@TestMetadata("primitive.kt")
public void testPrimitive() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/unboxGenericParameter/lambda/primitive.kt");
@@ -14195,6 +14205,11 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest {
runTest("compiler/testData/codegen/box/inlineClasses/unboxGenericParameter/objectLiteral/ifaceChild.kt");
}
@TestMetadata("nullableResult.kt")
public void testNullableResult() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/unboxGenericParameter/objectLiteral/nullableResult.kt");
}
@TestMetadata("primitive.kt")
public void testPrimitive() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/unboxGenericParameter/objectLiteral/primitive.kt");
@@ -8017,6 +8017,11 @@ public class IrCodegenBoxWasmTestGenerated extends AbstractIrCodegenBoxWasmTest
runTest("compiler/testData/codegen/box/inlineClasses/unboxGenericParameter/lambda/ifaceChild.kt");
}
@TestMetadata("nullableResult.kt")
public void testNullableResult() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/unboxGenericParameter/lambda/nullableResult.kt");
}
@TestMetadata("primitive.kt")
public void testPrimitive() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/unboxGenericParameter/lambda/primitive.kt");
@@ -8070,6 +8075,11 @@ public class IrCodegenBoxWasmTestGenerated extends AbstractIrCodegenBoxWasmTest
runTest("compiler/testData/codegen/box/inlineClasses/unboxGenericParameter/objectLiteral/ifaceChild.kt");
}
@TestMetadata("nullableResult.kt")
public void testNullableResult() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/unboxGenericParameter/objectLiteral/nullableResult.kt");
}
@TestMetadata("primitive.kt")
public void testPrimitive() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/unboxGenericParameter/objectLiteral/primitive.kt");