PSI2IR KT-51036 fix lambda return value generation

This commit is contained in:
Dmitry Petrov
2022-01-31 12:51:53 +03:00
committed by Space
parent 02d731255c
commit beb4cf3d99
19 changed files with 222 additions and 9 deletions
@@ -44803,6 +44803,12 @@ public class FirBlackBoxCodegenTestGenerated extends AbstractFirBlackBoxCodegenT
runTest("compiler/testData/codegen/box/unit/kt4265.kt");
}
@Test
@TestMetadata("kt51036.kt")
public void testKt51036() throws Exception {
runTest("compiler/testData/codegen/box/unit/kt51036.kt");
}
@Test
@TestMetadata("nullableUnit.kt")
public void testNullableUnit() throws Exception {
@@ -1676,6 +1676,12 @@ public class Fir2IrTextTestGenerated extends AbstractFir2IrTextTest {
runTest("compiler/testData/ir/irText/expressions/kt50028.kt");
}
@Test
@TestMetadata("kt51036.kt")
public void testKt51036() throws Exception {
runTest("compiler/testData/ir/irText/expressions/kt51036.kt");
}
@Test
@TestMetadata("lambdaInCAO.kt")
public void testLambdaInCAO() throws Exception {
@@ -34,10 +34,10 @@ import org.jetbrains.kotlin.psi.synthetics.findClassDescriptor
import org.jetbrains.kotlin.psi2ir.intermediate.VariableLValue
import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.bindingContextUtil.isUsedAsExpression
import org.jetbrains.kotlin.resolve.bindingContextUtil.isUsedAsResultOfLambda
import org.jetbrains.kotlin.resolve.calls.model.ResolvedCall
import org.jetbrains.kotlin.resolve.descriptorUtil.getSuperClassOrAny
import org.jetbrains.kotlin.types.KotlinType
import org.jetbrains.kotlin.types.typeUtil.isUnit
class BodyGenerator(
val scopeOwnerSymbol: IrSymbol,
@@ -74,7 +74,7 @@ class BodyGenerator(
fun generateExpressionBody(ktExpression: KtExpression): IrExpressionBody =
context.irFactory.createExpressionBody(createStatementGenerator().generateExpression(ktExpression))
fun generateLambdaBody(ktFun: KtFunctionLiteral): IrBody {
fun generateLambdaBody(ktFun: KtFunctionLiteral, lambdaDescriptor: SimpleFunctionDescriptor): IrBody {
val statementGenerator = createStatementGenerator()
val ktBody = ktFun.bodyExpression!!
@@ -101,7 +101,14 @@ class BodyGenerator(
val ktReturnedValue = ktBodyStatements.last()
val irReturnedValue = statementGenerator.generateStatement(ktReturnedValue)
irBlockBody.statements.add(
if (ktReturnedValue.isUsedAsResultOfLambda(context.bindingContext) && irReturnedValue is IrExpression) {
// We used to determine whether the last expression in a lambda is used as a return value with 'isUsedAsResultOfLambda',
// but it's in fact rather unreliable (see, for example, KT-51306).
// Instead, we just check whether lambda is expected to return a non-Unit value,
// and check that the last expression is not 'return' or 'throw'.
if (!lambdaDescriptor.returnType!!.isUnit() &&
irReturnedValue is IrExpression &&
irReturnedValue !is IrReturn && irReturnedValue !is IrThrow
) {
generateReturnExpression(irReturnedValue.startOffset, irReturnedValue.endOffset, irReturnedValue)
} else {
irReturnedValue
@@ -57,16 +57,18 @@ class FunctionGenerator(declarationGenerator: DeclarationGenerator) : Declaratio
ktFunction.bodyExpression?.let { generateFunctionBody(it) }
}
fun generateLambdaFunctionDeclaration(ktFunction: KtFunctionLiteral): IrSimpleFunction =
declareSimpleFunction(
fun generateLambdaFunctionDeclaration(ktFunction: KtFunctionLiteral): IrSimpleFunction {
val lambdaDescriptor = getOrFail(BindingContext.FUNCTION, ktFunction)
return declareSimpleFunction(
ktFunction,
null,
emptyList(),
IrDeclarationOrigin.LOCAL_FUNCTION_FOR_LAMBDA,
getOrFail(BindingContext.FUNCTION, ktFunction)
lambdaDescriptor
) {
generateLambdaBody(ktFunction)
generateLambdaBody(ktFunction, lambdaDescriptor)
}
}
fun generateFakeOverrideFunction(functionDescriptor: FunctionDescriptor, ktElement: KtPureElement): IrSimpleFunction? =
functionDescriptor.takeIf { it.visibility != DescriptorVisibilities.INVISIBLE_FAKE }
+14
View File
@@ -0,0 +1,14 @@
// WITH_STDLIB
// IGNORE_BACKEND: WASM
// ^ Unresolved reference: synchronized
fun box(): String {
A().close()
return "OK"
}
class A {
companion object;
operator fun String.invoke() = Unit
fun close() = synchronized(this) { "abc" }()
}
@@ -20,7 +20,8 @@ FILE fqName:<root> fileName:/badBreakContinue.kt
FUN LOCAL_FUNCTION_FOR_LAMBDA name:<anonymous> visibility:local modality:FINAL <> () returnType:kotlin.Nothing
BLOCK_BODY
ERROR_EXPR 'Loop not found for break expression: break@L1' type=kotlin.Nothing
ERROR_EXPR 'Loop not found for continue expression: continue@L1' type=kotlin.Nothing
RETURN type=kotlin.Nothing from='local final fun <anonymous> (): kotlin.Nothing declared in <root>.test3'
ERROR_EXPR 'Loop not found for continue expression: continue@L1' type=kotlin.Nothing
FUN name:test4 visibility:public modality:FINAL <> () returnType:kotlin.Unit
BLOCK_BODY
WHILE label=null origin=WHILE_LOOP
@@ -14,7 +14,7 @@ fun test3() {
L1@ while (true) { // BLOCK
val lambda: Function0<Nothing> = local fun <anonymous>(): Nothing {
error("") /* ErrorExpression */
error("") /* ErrorExpression */
return error("") /* ErrorExpression */
}
}
@@ -0,0 +1,59 @@
FILE fqName:<root> fileName:/kt51036.kt
CLASS CLASS name:A modality:FINAL visibility:public superTypes:[kotlin.Any]
$this: VALUE_PARAMETER INSTANCE_RECEIVER name:<this> type:<root>.A
CONSTRUCTOR visibility:public <> () returnType:<root>.A [primary]
BLOCK_BODY
DELEGATING_CONSTRUCTOR_CALL 'public constructor <init> () [primary] declared in kotlin.Any'
INSTANCE_INITIALIZER_CALL classDescriptor='CLASS CLASS name:A modality:FINAL visibility:public superTypes:[kotlin.Any]'
CLASS OBJECT name:Companion modality:FINAL visibility:public [companion] superTypes:[kotlin.Any]
$this: VALUE_PARAMETER INSTANCE_RECEIVER name:<this> type:<root>.A.Companion
CONSTRUCTOR visibility:private <> () returnType:<root>.A.Companion [primary]
BLOCK_BODY
DELEGATING_CONSTRUCTOR_CALL 'public constructor <init> () [primary] declared in kotlin.Any'
INSTANCE_INITIALIZER_CALL classDescriptor='CLASS OBJECT name:Companion modality:FINAL visibility:public [companion] superTypes:[kotlin.Any]'
FUN FAKE_OVERRIDE name:equals visibility:public modality:OPEN <> ($this:kotlin.Any, other:kotlin.Any?) returnType:kotlin.Boolean [fake_override,operator]
overridden:
public open fun equals (other: kotlin.Any?): kotlin.Boolean [operator] declared in kotlin.Any
$this: VALUE_PARAMETER name:<this> type:kotlin.Any
VALUE_PARAMETER name:other index:0 type:kotlin.Any?
FUN FAKE_OVERRIDE name:hashCode visibility:public modality:OPEN <> ($this:kotlin.Any) returnType:kotlin.Int [fake_override]
overridden:
public open fun hashCode (): kotlin.Int declared in kotlin.Any
$this: VALUE_PARAMETER name:<this> type:kotlin.Any
FUN FAKE_OVERRIDE name:toString visibility:public modality:OPEN <> ($this:kotlin.Any) returnType:kotlin.String [fake_override]
overridden:
public open fun toString (): kotlin.String declared in kotlin.Any
$this: VALUE_PARAMETER name:<this> type:kotlin.Any
FUN name:invoke visibility:public modality:FINAL <> ($this:<root>.A, $receiver:kotlin.String) returnType:kotlin.Unit [operator]
$this: VALUE_PARAMETER name:<this> type:<root>.A
$receiver: VALUE_PARAMETER name:<this> type:kotlin.String
BLOCK_BODY
RETURN type=kotlin.Nothing from='public final fun invoke (): kotlin.Unit [operator] declared in <root>.A'
GET_OBJECT 'CLASS IR_EXTERNAL_DECLARATION_STUB OBJECT name:Unit modality:FINAL visibility:public superTypes:[kotlin.Any]' type=kotlin.Unit
FUN name:close visibility:public modality:FINAL <> ($this:<root>.A) returnType:kotlin.Unit
$this: VALUE_PARAMETER name:<this> type:<root>.A
BLOCK_BODY
RETURN type=kotlin.Nothing from='public final fun close (): kotlin.Unit declared in <root>.A'
CALL 'public final fun invoke (): kotlin.Unit [operator] declared in <root>.A' type=kotlin.Unit origin=null
$this: GET_VAR '<this>: <root>.A declared in <root>.A.close' type=<root>.A origin=null
$receiver: CALL 'public final fun synchronized <R> (lock: kotlin.Any, block: kotlin.Function0<R of kotlin.StandardKt.synchronized>): R of kotlin.StandardKt.synchronized [inline] declared in kotlin.StandardKt' type=kotlin.String origin=null
<R>: kotlin.String
lock: GET_VAR '<this>: <root>.A declared in <root>.A.close' type=<root>.A origin=null
block: FUN_EXPR type=kotlin.Function0<kotlin.String> origin=LAMBDA
FUN LOCAL_FUNCTION_FOR_LAMBDA name:<anonymous> visibility:local modality:FINAL <> () returnType:kotlin.String
BLOCK_BODY
RETURN type=kotlin.Nothing from='local final fun <anonymous> (): kotlin.String declared in <root>.A.close'
CONST String type=kotlin.String value="Abc"
FUN FAKE_OVERRIDE name:equals visibility:public modality:OPEN <> ($this:kotlin.Any, other:kotlin.Any?) returnType:kotlin.Boolean [fake_override,operator]
overridden:
public open fun equals (other: kotlin.Any?): kotlin.Boolean [operator] declared in kotlin.Any
$this: VALUE_PARAMETER name:<this> type:kotlin.Any
VALUE_PARAMETER name:other index:0 type:kotlin.Any?
FUN FAKE_OVERRIDE name:hashCode visibility:public modality:OPEN <> ($this:kotlin.Any) returnType:kotlin.Int [fake_override]
overridden:
public open fun hashCode (): kotlin.Int declared in kotlin.Any
$this: VALUE_PARAMETER name:<this> type:kotlin.Any
FUN FAKE_OVERRIDE name:toString visibility:public modality:OPEN <> ($this:kotlin.Any) returnType:kotlin.String [fake_override]
overridden:
public open fun toString (): kotlin.String declared in kotlin.Any
$this: VALUE_PARAMETER name:<this> type:kotlin.Any
+59
View File
@@ -0,0 +1,59 @@
FILE fqName:<root> fileName:/kt51036.kt
CLASS CLASS name:A modality:FINAL visibility:public superTypes:[kotlin.Any]
$this: VALUE_PARAMETER INSTANCE_RECEIVER name:<this> type:<root>.A
CONSTRUCTOR visibility:public <> () returnType:<root>.A [primary]
BLOCK_BODY
DELEGATING_CONSTRUCTOR_CALL 'public constructor <init> () [primary] declared in kotlin.Any'
INSTANCE_INITIALIZER_CALL classDescriptor='CLASS CLASS name:A modality:FINAL visibility:public superTypes:[kotlin.Any]'
CLASS OBJECT name:Companion modality:FINAL visibility:public [companion] superTypes:[kotlin.Any]
$this: VALUE_PARAMETER INSTANCE_RECEIVER name:<this> type:<root>.A.Companion
CONSTRUCTOR visibility:private <> () returnType:<root>.A.Companion [primary]
BLOCK_BODY
DELEGATING_CONSTRUCTOR_CALL 'public constructor <init> () [primary] declared in kotlin.Any'
INSTANCE_INITIALIZER_CALL classDescriptor='CLASS OBJECT name:Companion modality:FINAL visibility:public [companion] superTypes:[kotlin.Any]'
FUN FAKE_OVERRIDE name:equals visibility:public modality:OPEN <> ($this:kotlin.Any, other:kotlin.Any?) returnType:kotlin.Boolean [fake_override,operator]
overridden:
public open fun equals (other: kotlin.Any?): kotlin.Boolean [operator] declared in kotlin.Any
$this: VALUE_PARAMETER name:<this> type:kotlin.Any
VALUE_PARAMETER name:other index:0 type:kotlin.Any?
FUN FAKE_OVERRIDE name:hashCode visibility:public modality:OPEN <> ($this:kotlin.Any) returnType:kotlin.Int [fake_override]
overridden:
public open fun hashCode (): kotlin.Int declared in kotlin.Any
$this: VALUE_PARAMETER name:<this> type:kotlin.Any
FUN FAKE_OVERRIDE name:toString visibility:public modality:OPEN <> ($this:kotlin.Any) returnType:kotlin.String [fake_override]
overridden:
public open fun toString (): kotlin.String declared in kotlin.Any
$this: VALUE_PARAMETER name:<this> type:kotlin.Any
FUN name:invoke visibility:public modality:FINAL <> ($this:<root>.A, $receiver:kotlin.String) returnType:kotlin.Unit [operator]
$this: VALUE_PARAMETER name:<this> type:<root>.A
$receiver: VALUE_PARAMETER name:<this> type:kotlin.String
BLOCK_BODY
RETURN type=kotlin.Nothing from='public final fun invoke (): kotlin.Unit [operator] declared in <root>.A'
GET_OBJECT 'CLASS IR_EXTERNAL_DECLARATION_STUB OBJECT name:Unit modality:FINAL visibility:public superTypes:[kotlin.Any]' type=kotlin.Unit
FUN name:close visibility:public modality:FINAL <> ($this:<root>.A) returnType:kotlin.Unit
$this: VALUE_PARAMETER name:<this> type:<root>.A
BLOCK_BODY
RETURN type=kotlin.Nothing from='public final fun close (): kotlin.Unit declared in <root>.A'
CALL 'public final fun invoke (): kotlin.Unit [operator] declared in <root>.A' type=kotlin.Unit origin=INVOKE
$this: GET_VAR '<this>: <root>.A declared in <root>.A.close' type=<root>.A origin=null
$receiver: CALL 'public final fun synchronized <R> (lock: kotlin.Any, block: kotlin.Function0<R of kotlin.StandardKt.synchronized>): R of kotlin.StandardKt.synchronized [inline] declared in kotlin.StandardKt' type=kotlin.String origin=null
<R>: kotlin.String
lock: GET_VAR '<this>: <root>.A declared in <root>.A.close' type=<root>.A origin=null
block: FUN_EXPR type=kotlin.Function0<kotlin.String> origin=LAMBDA
FUN LOCAL_FUNCTION_FOR_LAMBDA name:<anonymous> visibility:local modality:FINAL <> () returnType:kotlin.String
BLOCK_BODY
RETURN type=kotlin.Nothing from='local final fun <anonymous> (): kotlin.String declared in <root>.A.close'
CONST String type=kotlin.String value="Abc"
FUN FAKE_OVERRIDE name:equals visibility:public modality:OPEN <> ($this:kotlin.Any, other:kotlin.Any?) returnType:kotlin.Boolean [fake_override,operator]
overridden:
public open fun equals (other: kotlin.Any?): kotlin.Boolean [operator] declared in kotlin.Any
$this: VALUE_PARAMETER name:<this> type:kotlin.Any
VALUE_PARAMETER name:other index:0 type:kotlin.Any?
FUN FAKE_OVERRIDE name:hashCode visibility:public modality:OPEN <> ($this:kotlin.Any) returnType:kotlin.Int [fake_override]
overridden:
public open fun hashCode (): kotlin.Int declared in kotlin.Any
$this: VALUE_PARAMETER name:<this> type:kotlin.Any
FUN FAKE_OVERRIDE name:toString visibility:public modality:OPEN <> ($this:kotlin.Any) returnType:kotlin.String [fake_override]
overridden:
public open fun toString (): kotlin.String declared in kotlin.Any
$this: VALUE_PARAMETER name:<this> type:kotlin.Any
+8
View File
@@ -0,0 +1,8 @@
// WITH_STDLIB
// SKIP_KT_DUMP
class A {
companion object;
operator fun String.invoke() = Unit
fun close() = synchronized(this) { "Abc" }()
}
@@ -44341,6 +44341,12 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
runTest("compiler/testData/codegen/box/unit/kt4265.kt");
}
@Test
@TestMetadata("kt51036.kt")
public void testKt51036() throws Exception {
runTest("compiler/testData/codegen/box/unit/kt51036.kt");
}
@Test
@TestMetadata("nullableUnit.kt")
public void testNullableUnit() throws Exception {
@@ -44803,6 +44803,12 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes
runTest("compiler/testData/codegen/box/unit/kt4265.kt");
}
@Test
@TestMetadata("kt51036.kt")
public void testKt51036() throws Exception {
runTest("compiler/testData/codegen/box/unit/kt51036.kt");
}
@Test
@TestMetadata("nullableUnit.kt")
public void testNullableUnit() throws Exception {
@@ -1676,6 +1676,12 @@ public class IrTextTestGenerated extends AbstractIrTextTest {
runTest("compiler/testData/ir/irText/expressions/kt50028.kt");
}
@Test
@TestMetadata("kt51036.kt")
public void testKt51036() throws Exception {
runTest("compiler/testData/ir/irText/expressions/kt51036.kt");
}
@Test
@TestMetadata("lambdaInCAO.kt")
public void testLambdaInCAO() throws Exception {
@@ -35951,6 +35951,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes
runTest("compiler/testData/codegen/box/unit/kt4265.kt");
}
@TestMetadata("kt51036.kt")
public void testKt51036() throws Exception {
runTest("compiler/testData/codegen/box/unit/kt51036.kt");
}
@TestMetadata("nullableUnit.kt")
public void testNullableUnit() throws Exception {
runTest("compiler/testData/codegen/box/unit/nullableUnit.kt");
@@ -1257,6 +1257,11 @@ public class KlibTextTestCaseGenerated extends AbstractKlibTextTestCase {
runTest("compiler/testData/ir/irText/expressions/kt50028.kt");
}
@TestMetadata("kt51036.kt")
public void testKt51036() throws Exception {
runTest("compiler/testData/ir/irText/expressions/kt51036.kt");
}
@TestMetadata("lambdaInCAO.kt")
public void testLambdaInCAO() throws Exception {
runTest("compiler/testData/ir/irText/expressions/lambdaInCAO.kt");
@@ -32079,6 +32079,12 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest {
runTest("compiler/testData/codegen/box/unit/kt4265.kt");
}
@Test
@TestMetadata("kt51036.kt")
public void testKt51036() throws Exception {
runTest("compiler/testData/codegen/box/unit/kt51036.kt");
}
@Test
@TestMetadata("nullableUnit.kt")
public void testNullableUnit() throws Exception {
@@ -32181,6 +32181,12 @@ public class IrJsCodegenBoxTestGenerated extends AbstractIrJsCodegenBoxTest {
runTest("compiler/testData/codegen/box/unit/kt4265.kt");
}
@Test
@TestMetadata("kt51036.kt")
public void testKt51036() throws Exception {
runTest("compiler/testData/codegen/box/unit/kt51036.kt");
}
@Test
@TestMetadata("nullableUnit.kt")
public void testNullableUnit() throws Exception {
@@ -26923,6 +26923,11 @@ public class IrCodegenBoxWasmTestGenerated extends AbstractIrCodegenBoxWasmTest
runTest("compiler/testData/codegen/box/unit/kt4265.kt");
}
@TestMetadata("kt51036.kt")
public void testKt51036() throws Exception {
runTest("compiler/testData/codegen/box/unit/kt51036.kt");
}
@TestMetadata("nullableUnit.kt")
public void testNullableUnit() throws Exception {
runTest("compiler/testData/codegen/box/unit/nullableUnit.kt");
@@ -34432,6 +34432,12 @@ public class ExternalTestGenerated extends AbstractExternalNativeBlackBoxTest {
runTest("compiler/testData/codegen/box/unit/kt4265.kt");
}
@Test
@TestMetadata("kt51036.kt")
public void testKt51036() throws Exception {
runTest("compiler/testData/codegen/box/unit/kt51036.kt");
}
@Test
@TestMetadata("nullableUnit.kt")
public void testNullableUnit() throws Exception {