Properly reference type parameter descriptors

'descriptor -> descriptor.original' relation is often inconsistent
wrt 'containingDeclaration', parameters, and type parameters,
we have to introduce some workarounds here.
This commit is contained in:
Dmitry Petrov
2019-12-24 14:51:11 +03:00
parent f8fd5092c6
commit a8e9a6a1d0
9 changed files with 83 additions and 5 deletions
@@ -32,10 +32,17 @@ abstract class IrBindableSymbolBase<out D : DeclarationDescriptor, B : IrSymbolO
assert(isOriginalDescriptor(descriptor)) {
"Substituted descriptor $descriptor for ${descriptor.original}"
}
if (descriptor !is WrappedDeclarationDescriptor<*>) {
val containingDeclaration = descriptor.containingDeclaration
assert(containingDeclaration == null || isOriginalDescriptor(containingDeclaration)) {
"Substituted containing declaration: $containingDeclaration\nfor descriptor: $descriptor"
}
}
}
private fun isOriginalDescriptor(descriptor: DeclarationDescriptor): Boolean =
descriptor is WrappedDeclarationDescriptor<*> ||
// TODO fix declaring/referencing value parameters: compute proper original descriptor
descriptor is ValueParameterDescriptor && isOriginalDescriptor(descriptor.containingDeclaration) ||
descriptor == descriptor.original
@@ -24,13 +24,11 @@ import org.jetbrains.kotlin.ir.declarations.impl.IrValueParameterImpl
import org.jetbrains.kotlin.ir.declarations.lazy.*
import org.jetbrains.kotlin.ir.descriptors.WrappedDeclarationDescriptor
import org.jetbrains.kotlin.ir.descriptors.WrappedPropertyDescriptor
import org.jetbrains.kotlin.ir.expressions.impl.IrConstImpl
import org.jetbrains.kotlin.ir.expressions.impl.IrErrorExpressionImpl
import org.jetbrains.kotlin.ir.expressions.impl.IrExpressionBodyImpl
import org.jetbrains.kotlin.ir.symbols.IrFieldSymbol
import org.jetbrains.kotlin.ir.symbols.IrSymbol
import org.jetbrains.kotlin.ir.symbols.IrTypeParameterSymbol
import org.jetbrains.kotlin.ir.types.*
import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.DescriptorUtils
import org.jetbrains.kotlin.serialization.deserialization.descriptors.DescriptorWithContainerSource
@@ -14,6 +14,7 @@ import org.jetbrains.kotlin.descriptors.TypeParameterDescriptor
import org.jetbrains.kotlin.descriptors.annotations.Annotations
import org.jetbrains.kotlin.ir.declarations.IrTypeParametersContainer
import org.jetbrains.kotlin.ir.expressions.IrConstructorCall
import org.jetbrains.kotlin.ir.symbols.IrTypeParameterSymbol
import org.jetbrains.kotlin.ir.types.IrType
import org.jetbrains.kotlin.ir.types.IrTypeAbbreviation
import org.jetbrains.kotlin.ir.types.IrTypeProjection
@@ -54,9 +55,11 @@ class TypeTranslator(
return result
}
private fun resolveTypeParameter(typeParameterDescriptor: TypeParameterDescriptor) =
typeParametersResolver.resolveScopedTypeParameter(typeParameterDescriptor)
?: symbolTable.referenceTypeParameter(typeParameterDescriptor)
private fun resolveTypeParameter(typeParameterDescriptor: TypeParameterDescriptor): IrTypeParameterSymbol {
val originalTypeParameter = typeParameterDescriptor.originalTypeParameter
return typeParametersResolver.resolveScopedTypeParameter(originalTypeParameter)
?: symbolTable.referenceTypeParameter(originalTypeParameter)
}
fun translateType(kotlinType: KotlinType): IrType =
translateType(kotlinType, kotlinType, Variance.INVARIANT).type
@@ -0,0 +1,26 @@
/*
* Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.ir.util
import org.jetbrains.kotlin.descriptors.CallableDescriptor
import org.jetbrains.kotlin.descriptors.ClassifierDescriptorWithTypeParameters
import org.jetbrains.kotlin.descriptors.TypeParameterDescriptor
import org.jetbrains.kotlin.ir.descriptors.WrappedTypeParameterDescriptor
val TypeParameterDescriptor.originalTypeParameter: TypeParameterDescriptor
get() =
if (this is WrappedTypeParameterDescriptor) {
original
} else {
when (val container = containingDeclaration.original) {
is ClassifierDescriptorWithTypeParameters ->
container.declaredTypeParameters[index]
is CallableDescriptor ->
container.typeParameters[index]
else ->
throw AssertionError("Unexpected type parameter container: $container")
}
}