[FIR2IR] Fix codegen for lambda with dynamic receiver

#KT-57835 Fixed
This commit is contained in:
Kirill Rakhman
2023-04-11 18:04:19 +02:00
committed by Space Team
parent 1c96a87dca
commit aab1959cc4
17 changed files with 219 additions and 4 deletions
@@ -483,20 +483,20 @@ class CallAndReferenceGenerator(
private fun convertToIrSetCallForDynamic( private fun convertToIrSetCallForDynamic(
variableAssignment: FirVariableAssignment, variableAssignment: FirVariableAssignment,
explicitReceiverExpression: IrExpression, receiverExpression: IrExpression,
type: IrType, type: IrType,
calleeReference: FirReference, calleeReference: FirReference,
symbol: FirBasedSymbol<*>, symbol: FirBasedSymbol<*>,
assignedValue: IrExpression, assignedValue: IrExpression,
): IrExpression { ): IrExpression {
var convertedExplicitReceiver = explicitReceiverExpression var convertedExplicitReceiver = receiverExpression
return variableAssignment.convertWithOffsets { startOffset, endOffset -> return variableAssignment.convertWithOffsets { startOffset, endOffset ->
when (symbol) { when (symbol) {
is FirPropertySymbol -> { is FirPropertySymbol -> {
val name = calleeReference.resolved?.name ?: error("There must be a name") val name = calleeReference.resolved?.name ?: error("There must be a name")
convertedExplicitReceiver = IrDynamicMemberExpressionImpl( convertedExplicitReceiver = IrDynamicMemberExpressionImpl(
startOffset, endOffset, type, name.identifier, explicitReceiverExpression startOffset, endOffset, type, name.identifier, receiverExpression
) )
IrDynamicOperatorExpressionImpl(startOffset, endOffset, type, IrDynamicOperator.EQ).apply { IrDynamicOperatorExpressionImpl(startOffset, endOffset, type, IrDynamicOperator.EQ).apply {
arguments.add(assignedValue) arguments.add(assignedValue)
@@ -520,9 +520,13 @@ class CallAndReferenceGenerator(
val isDynamicAccess = firSymbol?.origin == FirDeclarationOrigin.DynamicScope val isDynamicAccess = firSymbol?.origin == FirDeclarationOrigin.DynamicScope
if (isDynamicAccess) { if (isDynamicAccess) {
val receiverExpression = (explicitReceiverExpression
?: (variableAssignment.dispatchReceiver as? FirThisReceiverExpression)?.let(visitor::convertToIrExpression)
?: error("Must've had a receiver"))
return convertToIrSetCallForDynamic( return convertToIrSetCallForDynamic(
variableAssignment, variableAssignment,
explicitReceiverExpression ?: error("Must've had a receiver"), receiverExpression,
type, type,
calleeReference, calleeReference,
firSymbol ?: error("Must've had a symbol"), firSymbol ?: error("Must've had a symbol"),
@@ -29772,6 +29772,16 @@ public class FirLightTreeBlackBoxCodegenTestGenerated extends AbstractFirLightTr
} }
} }
@Nested
@TestMetadata("compiler/testData/codegen/box/js")
@TestDataPath("$PROJECT_ROOT")
public class Js {
@Test
public void testAllFilesPresentInJs() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/js"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JVM_IR, true);
}
}
@Nested @Nested
@TestMetadata("compiler/testData/codegen/box/jvm8") @TestMetadata("compiler/testData/codegen/box/jvm8")
@TestDataPath("$PROJECT_ROOT") @TestDataPath("$PROJECT_ROOT")
@@ -29772,6 +29772,16 @@ public class FirPsiBlackBoxCodegenTestGenerated extends AbstractFirPsiBlackBoxCo
} }
} }
@Nested
@TestMetadata("compiler/testData/codegen/box/js")
@TestDataPath("$PROJECT_ROOT")
public class Js {
@Test
public void testAllFilesPresentInJs() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/js"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JVM_IR, true);
}
}
@Nested @Nested
@TestMetadata("compiler/testData/codegen/box/jvm8") @TestMetadata("compiler/testData/codegen/box/jvm8")
@TestDataPath("$PROJECT_ROOT") @TestDataPath("$PROJECT_ROOT")
@@ -0,0 +1,13 @@
// TARGET_BACKEND: JS
fun jso(
block: dynamic.() -> Unit,
): dynamic {
val o = js("{}")
block(o)
return o
}
fun box() = jso {
bar = "OK"
}.bar
@@ -28356,6 +28356,16 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
} }
} }
@Nested
@TestMetadata("compiler/testData/codegen/box/js")
@TestDataPath("$PROJECT_ROOT")
public class Js {
@Test
public void testAllFilesPresentInJs() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/js"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JVM, true);
}
}
@Nested @Nested
@TestMetadata("compiler/testData/codegen/box/jvm8") @TestMetadata("compiler/testData/codegen/box/jvm8")
@TestDataPath("$PROJECT_ROOT") @TestDataPath("$PROJECT_ROOT")
@@ -29772,6 +29772,16 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes
} }
} }
@Nested
@TestMetadata("compiler/testData/codegen/box/js")
@TestDataPath("$PROJECT_ROOT")
public class Js {
@Test
public void testAllFilesPresentInJs() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/js"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JVM_IR, true);
}
}
@Nested @Nested
@TestMetadata("compiler/testData/codegen/box/jvm8") @TestMetadata("compiler/testData/codegen/box/jvm8")
@TestDataPath("$PROJECT_ROOT") @TestDataPath("$PROJECT_ROOT")
@@ -29772,6 +29772,16 @@ public class IrBlackBoxCodegenWithIrInlinerTestGenerated extends AbstractIrBlack
} }
} }
@Nested
@TestMetadata("compiler/testData/codegen/box/js")
@TestDataPath("$PROJECT_ROOT")
public class Js {
@Test
public void testAllFilesPresentInJs() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/js"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JVM_IR, true);
}
}
@Nested @Nested
@TestMetadata("compiler/testData/codegen/box/jvm8") @TestMetadata("compiler/testData/codegen/box/jvm8")
@TestDataPath("$PROJECT_ROOT") @TestDataPath("$PROJECT_ROOT")
@@ -23991,6 +23991,19 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes
} }
} }
@TestMetadata("compiler/testData/codegen/box/js")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class Js extends AbstractLightAnalysisModeTest {
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest(this::doTest, TargetBackend.JVM, testDataFilePath);
}
public void testAllFilesPresentInJs() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/js"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JVM, true);
}
}
@TestMetadata("compiler/testData/codegen/box/jvm8") @TestMetadata("compiler/testData/codegen/box/jvm8")
@TestDataPath("$PROJECT_ROOT") @TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class) @RunWith(JUnit3RunnerWithInners.class)
@@ -21626,6 +21626,22 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest {
} }
} }
@Nested
@TestMetadata("compiler/testData/codegen/box/js")
@TestDataPath("$PROJECT_ROOT")
public class Js {
@Test
public void testAllFilesPresentInJs() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/js"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JS, true);
}
@Test
@TestMetadata("lambdaWithDynamicReceiver.kt")
public void testLambdaWithDynamicReceiver() throws Exception {
runTest("compiler/testData/codegen/box/js/lambdaWithDynamicReceiver.kt");
}
}
@Nested @Nested
@TestMetadata("compiler/testData/codegen/box/jvm8") @TestMetadata("compiler/testData/codegen/box/jvm8")
@TestDataPath("$PROJECT_ROOT") @TestDataPath("$PROJECT_ROOT")
@@ -21764,6 +21764,22 @@ public class FirJsCodegenBoxTestGenerated extends AbstractFirJsCodegenBoxTest {
} }
} }
@Nested
@TestMetadata("compiler/testData/codegen/box/js")
@TestDataPath("$PROJECT_ROOT")
public class Js {
@Test
public void testAllFilesPresentInJs() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/js"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JS_IR, true);
}
@Test
@TestMetadata("lambdaWithDynamicReceiver.kt")
public void testLambdaWithDynamicReceiver() throws Exception {
runTest("compiler/testData/codegen/box/js/lambdaWithDynamicReceiver.kt");
}
}
@Nested @Nested
@TestMetadata("compiler/testData/codegen/box/jvm8") @TestMetadata("compiler/testData/codegen/box/jvm8")
@TestDataPath("$PROJECT_ROOT") @TestDataPath("$PROJECT_ROOT")
@@ -21764,6 +21764,22 @@ public class IrJsCodegenBoxTestGenerated extends AbstractIrJsCodegenBoxTest {
} }
} }
@Nested
@TestMetadata("compiler/testData/codegen/box/js")
@TestDataPath("$PROJECT_ROOT")
public class Js {
@Test
public void testAllFilesPresentInJs() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/js"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JS_IR, true);
}
@Test
@TestMetadata("lambdaWithDynamicReceiver.kt")
public void testLambdaWithDynamicReceiver() throws Exception {
runTest("compiler/testData/codegen/box/js/lambdaWithDynamicReceiver.kt");
}
}
@Nested @Nested
@TestMetadata("compiler/testData/codegen/box/jvm8") @TestMetadata("compiler/testData/codegen/box/jvm8")
@TestDataPath("$PROJECT_ROOT") @TestDataPath("$PROJECT_ROOT")
@@ -21764,6 +21764,22 @@ public class IrJsES6CodegenBoxTestGenerated extends AbstractIrJsES6CodegenBoxTes
} }
} }
@Nested
@TestMetadata("compiler/testData/codegen/box/js")
@TestDataPath("$PROJECT_ROOT")
public class Js {
@Test
public void testAllFilesPresentInJs() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/js"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JS_IR_ES6, true);
}
@Test
@TestMetadata("lambdaWithDynamicReceiver.kt")
public void testLambdaWithDynamicReceiver() throws Exception {
runTest("compiler/testData/codegen/box/js/lambdaWithDynamicReceiver.kt");
}
}
@Nested @Nested
@TestMetadata("compiler/testData/codegen/box/jvm8") @TestMetadata("compiler/testData/codegen/box/jvm8")
@TestDataPath("$PROJECT_ROOT") @TestDataPath("$PROJECT_ROOT")
@@ -24998,6 +24998,20 @@ public class FirNativeCodegenBoxTestGenerated extends AbstractNativeCodegenBoxTe
} }
} }
@Nested
@TestMetadata("compiler/testData/codegen/box/js")
@TestDataPath("$PROJECT_ROOT")
@Tag("codegenK2")
@Tag("firCodegen")
@UseExtTestCaseGroupProvider()
@FirPipeline()
public class Js {
@Test
public void testAllFilesPresentInJs() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/js"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.NATIVE, true);
}
}
@Nested @Nested
@TestMetadata("compiler/testData/codegen/box/jvm8") @TestMetadata("compiler/testData/codegen/box/jvm8")
@TestDataPath("$PROJECT_ROOT") @TestDataPath("$PROJECT_ROOT")
@@ -25508,6 +25508,22 @@ public class FirNativeCodegenBoxTestNoPLGenerated extends AbstractNativeCodegenB
} }
} }
@Nested
@TestMetadata("compiler/testData/codegen/box/js")
@TestDataPath("$PROJECT_ROOT")
@Tag("codegenK2")
@Tag("firCodegen")
@UseExtTestCaseGroupProvider()
@FirPipeline()
@UsePartialLinkage(mode = Mode.DISABLED)
@Tag("no-partial-linkage-may-be-skipped")
public class Js {
@Test
public void testAllFilesPresentInJs() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/js"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.NATIVE, true);
}
}
@Nested @Nested
@TestMetadata("compiler/testData/codegen/box/jvm8") @TestMetadata("compiler/testData/codegen/box/jvm8")
@TestDataPath("$PROJECT_ROOT") @TestDataPath("$PROJECT_ROOT")
@@ -24743,6 +24743,19 @@ public class NativeCodegenBoxTestGenerated extends AbstractNativeCodegenBoxTest
} }
} }
@Nested
@TestMetadata("compiler/testData/codegen/box/js")
@TestDataPath("$PROJECT_ROOT")
@Tag("codegen")
@Tag("k1Codegen")
@UseExtTestCaseGroupProvider()
public class Js {
@Test
public void testAllFilesPresentInJs() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/js"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.NATIVE, true);
}
}
@Nested @Nested
@TestMetadata("compiler/testData/codegen/box/jvm8") @TestMetadata("compiler/testData/codegen/box/jvm8")
@TestDataPath("$PROJECT_ROOT") @TestDataPath("$PROJECT_ROOT")
@@ -25253,6 +25253,21 @@ public class NativeCodegenBoxTestNoPLGenerated extends AbstractNativeCodegenBoxT
} }
} }
@Nested
@TestMetadata("compiler/testData/codegen/box/js")
@TestDataPath("$PROJECT_ROOT")
@Tag("codegen")
@Tag("k1Codegen")
@UseExtTestCaseGroupProvider()
@UsePartialLinkage(mode = Mode.DISABLED)
@Tag("no-partial-linkage-may-be-skipped")
public class Js {
@Test
public void testAllFilesPresentInJs() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/js"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.NATIVE, true);
}
}
@Nested @Nested
@TestMetadata("compiler/testData/codegen/box/jvm8") @TestMetadata("compiler/testData/codegen/box/jvm8")
@TestDataPath("$PROJECT_ROOT") @TestDataPath("$PROJECT_ROOT")
@@ -19244,6 +19244,19 @@ public class IrCodegenBoxWasmTestGenerated extends AbstractIrCodegenBoxWasmTest
} }
} }
@TestMetadata("compiler/testData/codegen/box/js")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class Js extends AbstractIrCodegenBoxWasmTest {
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest0(this::doTest, TargetBackend.WASM, testDataFilePath);
}
public void testAllFilesPresentInJs() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/box/js"), Pattern.compile("^([^_](.+))\\.kt$"), null, TargetBackend.WASM, true);
}
}
@TestMetadata("compiler/testData/codegen/box/jvm8") @TestMetadata("compiler/testData/codegen/box/jvm8")
@TestDataPath("$PROJECT_ROOT") @TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class) @RunWith(JUnit3RunnerWithInners.class)