[IR] Fix synthetic declarations generator to make it produce correct type

- Fix KT-40126
 - Add test
This commit is contained in:
Roman Artemev
2020-07-08 23:35:32 +03:00
committed by romanart
parent d8f5b50cd8
commit ca44242f37
7 changed files with 40 additions and 10 deletions
@@ -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)
@@ -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")
@@ -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() ->
@@ -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");
@@ -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");
@@ -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");
@@ -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"