[IR] Fix unbound type parameter symbol for jvm corner cases

This commit is contained in:
Roman Artemev
2020-03-26 16:48:46 +03:00
committed by romanart
parent 48fb279721
commit 4bebfd33b9
7 changed files with 21 additions and 19 deletions
@@ -343,6 +343,7 @@ class AssignmentGenerator(statementGenerator: StatementGenerator) : StatementGen
scope, scope,
ktExpression.startOffsetSkippingComments, ktExpression.endOffset, origin, ktExpression.startOffsetSkippingComments, ktExpression.endOffset, origin,
context.symbolTable.referenceField(unwrappedPropertyDescriptor.original), context.symbolTable.referenceField(unwrappedPropertyDescriptor.original),
unwrappedPropertyDescriptor,
propertyIrType, propertyIrType,
propertyReceiver, propertyReceiver,
superQualifierSymbol superQualifierSymbol
@@ -187,7 +187,7 @@ class CallGenerator(statementGenerator: StatementGenerator) : StatementGenerator
dispatchReceiverValue?.load(), dispatchReceiverValue?.load(),
IrStatementOrigin.GET_PROPERTY, IrStatementOrigin.GET_PROPERTY,
superQualifierSymbol superQualifierSymbol
) ).also { context.callToSubstitutedDescriptorMap[it] = descriptor }
} }
} else { } else {
call.callReceiver.adjustForCallee(getMethodDescriptor).call { dispatchReceiverValue, extensionReceiverValue -> call.callReceiver.adjustForCallee(getMethodDescriptor).call { dispatchReceiverValue, extensionReceiverValue ->
@@ -12,7 +12,7 @@ import org.jetbrains.kotlin.descriptors.ModuleDescriptor
import org.jetbrains.kotlin.descriptors.NotFoundClasses import org.jetbrains.kotlin.descriptors.NotFoundClasses
import org.jetbrains.kotlin.ir.builders.IrGeneratorContext import org.jetbrains.kotlin.ir.builders.IrGeneratorContext
import org.jetbrains.kotlin.ir.descriptors.IrBuiltIns import org.jetbrains.kotlin.ir.descriptors.IrBuiltIns
import org.jetbrains.kotlin.ir.expressions.IrMemberAccessExpression import org.jetbrains.kotlin.ir.expressions.IrDeclarationReference
import org.jetbrains.kotlin.ir.util.ConstantValueGenerator import org.jetbrains.kotlin.ir.util.ConstantValueGenerator
import org.jetbrains.kotlin.ir.util.IdSignatureComposer import org.jetbrains.kotlin.ir.util.IdSignatureComposer
import org.jetbrains.kotlin.ir.util.SymbolTable import org.jetbrains.kotlin.ir.util.SymbolTable
@@ -60,14 +60,14 @@ class GeneratorContext(
override val irBuiltIns: IrBuiltIns override val irBuiltIns: IrBuiltIns
) : IrGeneratorContext() { ) : IrGeneratorContext() {
val callToSubstitutedDescriptorMap = mutableMapOf<IrMemberAccessExpression, CallableDescriptor>() val callToSubstitutedDescriptorMap = mutableMapOf<IrDeclarationReference, CallableDescriptor>()
val sourceManager = PsiSourceManager() val sourceManager = PsiSourceManager()
// TODO: inject a correct StorageManager instance, or store NotFoundClasses inside ModuleDescriptor // TODO: inject a correct StorageManager instance, or store NotFoundClasses inside ModuleDescriptor
val reflectionTypes = ReflectionTypes(moduleDescriptor, NotFoundClasses(LockBasedStorageManager.NO_LOCKS, moduleDescriptor)) val reflectionTypes = ReflectionTypes(moduleDescriptor, NotFoundClasses(LockBasedStorageManager.NO_LOCKS, moduleDescriptor))
fun IrMemberAccessExpression.commitSubstituted(descriptor: CallableDescriptor) { fun IrDeclarationReference.commitSubstituted(descriptor: CallableDescriptor) {
callToSubstitutedDescriptorMap[this] = descriptor callToSubstitutedDescriptorMap[this] = descriptor
} }
} }
@@ -17,6 +17,7 @@
package org.jetbrains.kotlin.psi2ir.intermediate package org.jetbrains.kotlin.psi2ir.intermediate
import org.jetbrains.kotlin.descriptors.FunctionDescriptor import org.jetbrains.kotlin.descriptors.FunctionDescriptor
import org.jetbrains.kotlin.descriptors.PropertyDescriptor
import org.jetbrains.kotlin.ir.builders.Scope import org.jetbrains.kotlin.ir.builders.Scope
import org.jetbrains.kotlin.ir.expressions.IrExpression import org.jetbrains.kotlin.ir.expressions.IrExpression
import org.jetbrains.kotlin.ir.expressions.IrMemberAccessExpression import org.jetbrains.kotlin.ir.expressions.IrMemberAccessExpression
@@ -72,6 +73,7 @@ class FieldPropertyLValue(
endOffset: Int, endOffset: Int,
origin: IrStatementOrigin?, origin: IrStatementOrigin?,
val field: IrFieldSymbol, val field: IrFieldSymbol,
val descriptor: PropertyDescriptor,
type: IrType, type: IrType,
callReceiver: CallReceiver, callReceiver: CallReceiver,
superQualifier: IrClassSymbol? superQualifier: IrClassSymbol?
@@ -88,7 +90,7 @@ class FieldPropertyLValue(
dispatchReceiverValue?.load(), dispatchReceiverValue?.load(),
origin, origin,
superQualifier superQualifier
) ).also { context.callToSubstitutedDescriptorMap[it] = descriptor }
} }
override fun store(irExpression: IrExpression) = override fun store(irExpression: IrExpression) =
@@ -102,7 +104,7 @@ class FieldPropertyLValue(
context.irBuiltIns.unitType, context.irBuiltIns.unitType,
origin, origin,
superQualifier superQualifier
) ).also { context.callToSubstitutedDescriptorMap[it] = descriptor }
} }
override fun withReceiver(dispatchReceiver: VariableLValue?, extensionReceiver: VariableLValue?): PropertyLValueBase = override fun withReceiver(dispatchReceiver: VariableLValue?, extensionReceiver: VariableLValue?): PropertyLValueBase =
@@ -110,6 +112,7 @@ class FieldPropertyLValue(
context, context,
scope, startOffset, endOffset, origin, scope, startOffset, endOffset, origin,
field, field,
descriptor,
type, type,
SimpleCallReceiver(dispatchReceiver, extensionReceiver), SimpleCallReceiver(dispatchReceiver, extensionReceiver),
superQualifier superQualifier
@@ -19,10 +19,7 @@ package org.jetbrains.kotlin.psi2ir.transformations
import org.jetbrains.kotlin.builtins.KotlinBuiltIns import org.jetbrains.kotlin.builtins.KotlinBuiltIns
import org.jetbrains.kotlin.builtins.isFunctionType import org.jetbrains.kotlin.builtins.isFunctionType
import org.jetbrains.kotlin.builtins.isSuspendFunctionType import org.jetbrains.kotlin.builtins.isSuspendFunctionType
import org.jetbrains.kotlin.descriptors.CallableDescriptor import org.jetbrains.kotlin.descriptors.*
import org.jetbrains.kotlin.descriptors.CallableMemberDescriptor
import org.jetbrains.kotlin.descriptors.ClassDescriptor
import org.jetbrains.kotlin.descriptors.FunctionDescriptor
import org.jetbrains.kotlin.descriptors.impl.AnonymousFunctionDescriptor import org.jetbrains.kotlin.descriptors.impl.AnonymousFunctionDescriptor
import org.jetbrains.kotlin.ir.IrElement import org.jetbrains.kotlin.ir.IrElement
import org.jetbrains.kotlin.ir.IrStatement import org.jetbrains.kotlin.ir.IrStatement
@@ -62,7 +59,7 @@ internal class InsertImplicitCasts(
private val builtIns: KotlinBuiltIns, private val builtIns: KotlinBuiltIns,
private val irBuiltIns: IrBuiltIns, private val irBuiltIns: IrBuiltIns,
private val typeTranslator: TypeTranslator, private val typeTranslator: TypeTranslator,
private val callToSubstitutedDescriptorMap: Map<IrMemberAccessExpression, CallableDescriptor>, private val callToSubstitutedDescriptorMap: Map<IrDeclarationReference, CallableDescriptor>,
private val generatorExtensions: GeneratorExtensions private val generatorExtensions: GeneratorExtensions
) : IrElementTransformerVoid() { ) : IrElementTransformerVoid() {
@@ -84,7 +81,7 @@ internal class InsertImplicitCasts(
private fun KotlinType.toIrType() = typeTranslator.translateType(this) private fun KotlinType.toIrType() = typeTranslator.translateType(this)
private val IrMemberAccessExpression.substitutedDescriptor private val IrDeclarationReference.substitutedDescriptor
get() = callToSubstitutedDescriptorMap[this] ?: symbol.descriptor as CallableDescriptor get() = callToSubstitutedDescriptorMap[this] ?: symbol.descriptor as CallableDescriptor
override fun visitCallableReference(expression: IrCallableReference): IrExpression { override fun visitCallableReference(expression: IrCallableReference): IrExpression {
@@ -189,13 +186,14 @@ internal class InsertImplicitCasts(
override fun visitGetField(expression: IrGetField): IrExpression = override fun visitGetField(expression: IrGetField): IrExpression =
expression.transformPostfix { expression.transformPostfix {
receiver = receiver?.cast(getEffectiveDispatchReceiverType(expression.symbol.descriptor)) receiver = receiver?.cast(getEffectiveDispatchReceiverType(expression.substitutedDescriptor))
} }
override fun visitSetField(expression: IrSetField): IrExpression = override fun visitSetField(expression: IrSetField): IrExpression =
expression.transformPostfix { expression.transformPostfix {
receiver = receiver?.cast(getEffectiveDispatchReceiverType(expression.symbol.descriptor)) val substituted = expression.substitutedDescriptor as PropertyDescriptor
value = value.cast(expression.symbol.descriptor.type) receiver = receiver?.cast(getEffectiveDispatchReceiverType(substituted))
value = value.cast(substituted.type)
} }
override fun visitVariable(declaration: IrVariable): IrVariable = override fun visitVariable(declaration: IrVariable): IrVariable =
@@ -117,7 +117,7 @@ class ConstantValueGenerator(
IrClassReferenceImpl( IrClassReferenceImpl(
startOffset, endOffset, startOffset, endOffset,
constantValue.getType(moduleDescriptor).toIrType(), constantValue.getType(moduleDescriptor).toIrType(),
classifierDescriptor.defaultType.toIrType().classifierOrFail, symbolTable.referenceClassifier(classifierDescriptor),
classifierKtType.toIrType() classifierKtType.toIrType()
) )
} }
@@ -10,9 +10,9 @@ FILE fqName:<root> fileName:/smartCastOnFieldReceiverOfGenericType.kt
TYPE_OP type=kotlin.String origin=CAST typeOperand=kotlin.String TYPE_OP type=kotlin.String origin=CAST typeOperand=kotlin.String
GET_VAR 'b: kotlin.Any declared in <root>.testSetField' type=kotlin.Any origin=null GET_VAR 'b: kotlin.Any declared in <root>.testSetField' type=kotlin.Any origin=null
SET_FIELD 'FIELD IR_EXTERNAL_JAVA_DECLARATION_STUB name:value type:T of <root>.JCell? visibility:public' type=kotlin.Unit origin=EQ SET_FIELD 'FIELD IR_EXTERNAL_JAVA_DECLARATION_STUB name:value type:T of <root>.JCell? visibility:public' type=kotlin.Unit origin=EQ
receiver: TYPE_OP type=<root>.JCell<T of <root>.JCell> origin=IMPLICIT_CAST typeOperand=<root>.JCell<T of <root>.JCell> receiver: TYPE_OP type=<root>.JCell<kotlin.String> origin=IMPLICIT_CAST typeOperand=<root>.JCell<kotlin.String>
GET_VAR 'a: kotlin.Any declared in <root>.testSetField' type=kotlin.Any origin=null GET_VAR 'a: kotlin.Any declared in <root>.testSetField' type=kotlin.Any origin=null
value: TYPE_OP type=T of <root>.JCell origin=IMPLICIT_CAST typeOperand=T of <root>.JCell value: TYPE_OP type=kotlin.String origin=IMPLICIT_CAST typeOperand=kotlin.String
GET_VAR 'b: kotlin.Any declared in <root>.testSetField' type=kotlin.Any origin=null GET_VAR 'b: kotlin.Any declared in <root>.testSetField' type=kotlin.Any origin=null
FUN name:testGetField visibility:public modality:FINAL <> (a:kotlin.Any) returnType:kotlin.String FUN name:testGetField visibility:public modality:FINAL <> (a:kotlin.Any) returnType:kotlin.String
VALUE_PARAMETER name:a index:0 type:kotlin.Any VALUE_PARAMETER name:a index:0 type:kotlin.Any
@@ -23,5 +23,5 @@ FILE fqName:<root> fileName:/smartCastOnFieldReceiverOfGenericType.kt
RETURN type=kotlin.Nothing from='public final fun testGetField (a: kotlin.Any): kotlin.String declared in <root>' RETURN type=kotlin.Nothing from='public final fun testGetField (a: kotlin.Any): kotlin.String declared in <root>'
TYPE_OP type=kotlin.String origin=IMPLICIT_NOTNULL typeOperand=kotlin.String TYPE_OP type=kotlin.String origin=IMPLICIT_NOTNULL typeOperand=kotlin.String
GET_FIELD 'FIELD IR_EXTERNAL_JAVA_DECLARATION_STUB name:value type:T of <root>.JCell? visibility:public' type=kotlin.String? origin=GET_PROPERTY GET_FIELD 'FIELD IR_EXTERNAL_JAVA_DECLARATION_STUB name:value type:T of <root>.JCell? visibility:public' type=kotlin.String? origin=GET_PROPERTY
receiver: TYPE_OP type=<root>.JCell<T of <root>.JCell> origin=IMPLICIT_CAST typeOperand=<root>.JCell<T of <root>.JCell> receiver: TYPE_OP type=<root>.JCell<kotlin.String> origin=IMPLICIT_CAST typeOperand=<root>.JCell<kotlin.String>
GET_VAR 'a: kotlin.Any declared in <root>.testGetField' type=kotlin.Any origin=null GET_VAR 'a: kotlin.Any declared in <root>.testGetField' type=kotlin.Any origin=null