From ca44242f37293510afcb0e7985ea7bd29366c1f8 Mon Sep 17 00:00:00 2001 From: Roman Artemev Date: Wed, 8 Jul 2020 23:35:32 +0300 Subject: [PATCH] [IR] Fix synthetic declarations generator to make it produce correct type - Fix KT-40126 - Add test --- .../generators/StandaloneDeclarationGenerator.kt | 8 ++++---- .../generators/SyntheticDeclarationsGenerator.kt | 9 ++++----- .../psi2ir/transformations/InsertImplicitCasts.kt | 3 ++- .../es6/semantics/IrBoxJsES6TestGenerated.java | 5 +++++ .../test/ir/semantics/IrBoxJsTestGenerated.java | 5 +++++ .../js/test/semantics/BoxJsTestGenerated.java | 5 +++++ .../delegation/delegationToExternaInterface.kt | 15 +++++++++++++++ 7 files changed, 40 insertions(+), 10 deletions(-) create mode 100644 js/js.translator/testData/box/delegation/delegationToExternaInterface.kt diff --git a/compiler/ir/ir.psi2ir/src/org/jetbrains/kotlin/psi2ir/generators/StandaloneDeclarationGenerator.kt b/compiler/ir/ir.psi2ir/src/org/jetbrains/kotlin/psi2ir/generators/StandaloneDeclarationGenerator.kt index 25ea92a8014..813007e5d49 100644 --- a/compiler/ir/ir.psi2ir/src/org/jetbrains/kotlin/psi2ir/generators/StandaloneDeclarationGenerator.kt +++ b/compiler/ir/ir.psi2ir/src/org/jetbrains/kotlin/psi2ir/generators/StandaloneDeclarationGenerator.kt @@ -133,7 +133,7 @@ class StandaloneDeclarationGenerator(private val context: GeneratorContext) { protected fun generateValueParameterDeclarations( irFunction: IrFunction, functionDescriptor: FunctionDescriptor, - defaultArgumentFactory: IrFunction.(ValueParameterDescriptor) -> IrExpressionBody? + defaultArgumentFactory: IrFunction.(IrValueParameter) -> IrExpressionBody? ) { // TODO: KtElements @@ -150,14 +150,14 @@ class StandaloneDeclarationGenerator(private val context: GeneratorContext) { irFunction.valueParameters = functionDescriptor.valueParameters.map { valueParameterDescriptor -> val ktParameter = DescriptorToSourceUtils.getSourceFromDescriptor(valueParameterDescriptor) as? KtParameter declareParameter(valueParameterDescriptor, ktParameter, irFunction).also { - it.defaultValue = irFunction.defaultArgumentFactory(valueParameterDescriptor) + it.defaultValue = irFunction.defaultArgumentFactory(it) } } } fun generateConstructor( startOffset: Int, endOffset: Int, origin: IrDeclarationOrigin, descriptor: ClassConstructorDescriptor, symbol: IrConstructorSymbol, - defaultArgumentFactory: IrFunction.(ValueParameterDescriptor) -> IrExpressionBody? = { null } + defaultArgumentFactory: IrFunction.(IrValueParameter) -> IrExpressionBody? = { null } ): IrConstructor { val irConstructor = IrConstructorImpl(startOffset, endOffset, origin, symbol, IrUninitializedType, descriptor) irConstructor.metadata = MetadataSource.Function(descriptor) @@ -178,7 +178,7 @@ class StandaloneDeclarationGenerator(private val context: GeneratorContext) { fun generateSimpleFunction( startOffset: Int, endOffset: Int, origin: IrDeclarationOrigin, descriptor: FunctionDescriptor, symbol: IrSimpleFunctionSymbol, - defaultArgumentFactory: IrFunction.(ValueParameterDescriptor) -> IrExpressionBody? = { null } + defaultArgumentFactory: IrFunction.(IrValueParameter) -> IrExpressionBody? = { null } ): IrSimpleFunction { val irFunction = IrFunctionImpl(startOffset, endOffset, origin, symbol, IrUninitializedType, descriptor) irFunction.metadata = MetadataSource.Function(descriptor) diff --git a/compiler/ir/ir.psi2ir/src/org/jetbrains/kotlin/psi2ir/generators/SyntheticDeclarationsGenerator.kt b/compiler/ir/ir.psi2ir/src/org/jetbrains/kotlin/psi2ir/generators/SyntheticDeclarationsGenerator.kt index 23dd2527b4e..81290af1863 100644 --- a/compiler/ir/ir.psi2ir/src/org/jetbrains/kotlin/psi2ir/generators/SyntheticDeclarationsGenerator.kt +++ b/compiler/ir/ir.psi2ir/src/org/jetbrains/kotlin/psi2ir/generators/SyntheticDeclarationsGenerator.kt @@ -31,16 +31,15 @@ class SyntheticDeclarationsGenerator(context: GeneratorContext) : DeclarationDes return this } - private val errorType = IrErrorTypeImpl(null, emptyList(), Variance.INVARIANT) - - private fun IrFunction.defaultArgumentFactory(descriptor: ValueParameterDescriptor): IrExpressionBody? { + private fun IrFunction.defaultArgumentFactory(parameter: IrValueParameter): IrExpressionBody? { + val descriptor = parameter.descriptor as ValueParameterDescriptor if (!descriptor.declaresDefaultValue()) return null val description = "Default Argument Value stub for ${descriptor.name}|${descriptor.index}" - return IrExpressionBodyImpl(IrErrorExpressionImpl(startOffset, endOffset, errorType, description)) + return IrExpressionBodyImpl(IrErrorExpressionImpl(startOffset, endOffset, parameter.type, description)) } - private val defaultFactoryReference: IrFunction.(ValueParameterDescriptor) -> IrExpressionBody? = { defaultArgumentFactory(it) } + private val defaultFactoryReference: IrFunction.(IrValueParameter) -> IrExpressionBody? = { defaultArgumentFactory(it) } override fun visitPackageFragmentDescriptor(descriptor: PackageFragmentDescriptor, data: IrDeclarationContainer?) { error("Unexpected declaration descriptor $descriptor") diff --git a/compiler/ir/ir.psi2ir/src/org/jetbrains/kotlin/psi2ir/transformations/InsertImplicitCasts.kt b/compiler/ir/ir.psi2ir/src/org/jetbrains/kotlin/psi2ir/transformations/InsertImplicitCasts.kt index e31cc7a31e2..284626b3c89 100644 --- a/compiler/ir/ir.psi2ir/src/org/jetbrains/kotlin/psi2ir/transformations/InsertImplicitCasts.kt +++ b/compiler/ir/ir.psi2ir/src/org/jetbrains/kotlin/psi2ir/transformations/InsertImplicitCasts.kt @@ -39,6 +39,7 @@ import org.jetbrains.kotlin.ir.types.impl.originalKotlinType import org.jetbrains.kotlin.ir.util.SymbolTable import org.jetbrains.kotlin.ir.util.TypeTranslator import org.jetbrains.kotlin.ir.util.coerceToUnit +import org.jetbrains.kotlin.ir.util.render import org.jetbrains.kotlin.ir.visitors.IrElementTransformerVoid import org.jetbrains.kotlin.ir.visitors.IrElementVisitorVoid import org.jetbrains.kotlin.ir.visitors.acceptChildrenVoid @@ -341,7 +342,7 @@ internal class InsertImplicitCasts( val notNullableExpectedType = expectedType.makeNotNullable() - val valueType = this.type.originalKotlinType!! + val valueType = this.type.originalKotlinType ?: error("Expecting original kotlin type for IrType ${type.render()}") return when { expectedType.isUnit() -> diff --git a/js/js.tests/test/org/jetbrains/kotlin/js/test/es6/semantics/IrBoxJsES6TestGenerated.java b/js/js.tests/test/org/jetbrains/kotlin/js/test/es6/semantics/IrBoxJsES6TestGenerated.java index 37717bda207..29a500c4fa1 100644 --- a/js/js.tests/test/org/jetbrains/kotlin/js/test/es6/semantics/IrBoxJsES6TestGenerated.java +++ b/js/js.tests/test/org/jetbrains/kotlin/js/test/es6/semantics/IrBoxJsES6TestGenerated.java @@ -1271,6 +1271,11 @@ public class IrBoxJsES6TestGenerated extends AbstractIrBoxJsES6Test { runTest("js/js.translator/testData/box/delegation/delegationExtensionPropertyDelegated.kt"); } + @TestMetadata("delegationToExternaInterface.kt") + public void testDelegationToExternaInterface() throws Exception { + runTest("js/js.translator/testData/box/delegation/delegationToExternaInterface.kt"); + } + @TestMetadata("jsNamePropertyDelegation.kt") public void testJsNamePropertyDelegation() throws Exception { runTest("js/js.translator/testData/box/delegation/jsNamePropertyDelegation.kt"); diff --git a/js/js.tests/test/org/jetbrains/kotlin/js/test/ir/semantics/IrBoxJsTestGenerated.java b/js/js.tests/test/org/jetbrains/kotlin/js/test/ir/semantics/IrBoxJsTestGenerated.java index febde25d59c..3cc11dd6719 100644 --- a/js/js.tests/test/org/jetbrains/kotlin/js/test/ir/semantics/IrBoxJsTestGenerated.java +++ b/js/js.tests/test/org/jetbrains/kotlin/js/test/ir/semantics/IrBoxJsTestGenerated.java @@ -1271,6 +1271,11 @@ public class IrBoxJsTestGenerated extends AbstractIrBoxJsTest { runTest("js/js.translator/testData/box/delegation/delegationExtensionPropertyDelegated.kt"); } + @TestMetadata("delegationToExternaInterface.kt") + public void testDelegationToExternaInterface() throws Exception { + runTest("js/js.translator/testData/box/delegation/delegationToExternaInterface.kt"); + } + @TestMetadata("jsNamePropertyDelegation.kt") public void testJsNamePropertyDelegation() throws Exception { runTest("js/js.translator/testData/box/delegation/jsNamePropertyDelegation.kt"); diff --git a/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/BoxJsTestGenerated.java b/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/BoxJsTestGenerated.java index be76ab90f23..b3411f05640 100644 --- a/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/BoxJsTestGenerated.java +++ b/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/BoxJsTestGenerated.java @@ -1271,6 +1271,11 @@ public class BoxJsTestGenerated extends AbstractBoxJsTest { runTest("js/js.translator/testData/box/delegation/delegationExtensionPropertyDelegated.kt"); } + @TestMetadata("delegationToExternaInterface.kt") + public void testDelegationToExternaInterface() throws Exception { + runTest("js/js.translator/testData/box/delegation/delegationToExternaInterface.kt"); + } + @TestMetadata("jsNamePropertyDelegation.kt") public void testJsNamePropertyDelegation() throws Exception { runTest("js/js.translator/testData/box/delegation/jsNamePropertyDelegation.kt"); diff --git a/js/js.translator/testData/box/delegation/delegationToExternaInterface.kt b/js/js.translator/testData/box/delegation/delegationToExternaInterface.kt new file mode 100644 index 00000000000..05909b67207 --- /dev/null +++ b/js/js.translator/testData/box/delegation/delegationToExternaInterface.kt @@ -0,0 +1,15 @@ +// EXPECTED_REACHABLE_NODES: 1236 +// KJS_WITH_FULL_RUNTIME +// KT-40126 +@file:Suppress("EXTERNAL_DELEGATION") + +@Suppress("NESTED_CLASS_IN_EXTERNAL_INTERFACE") +external interface MySymbol { + companion object : MySymbolConstructor by definedExternally +} +external interface MySymbolConstructor { + @nativeInvoke + operator fun invoke(description: String = definedExternally): Any +} + +fun box() = "OK" \ No newline at end of file