diff --git a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/symbols/impl/IrSymbolBase.kt b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/symbols/impl/IrSymbolBase.kt index 4d9a9b179d7..e343d7a681d 100644 --- a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/symbols/impl/IrSymbolBase.kt +++ b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/symbols/impl/IrSymbolBase.kt @@ -32,10 +32,17 @@ abstract class IrBindableSymbolBase) { + 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 diff --git a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/util/DeclarationStubGenerator.kt b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/util/DeclarationStubGenerator.kt index 7830d982131..6c60cae1696 100644 --- a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/util/DeclarationStubGenerator.kt +++ b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/util/DeclarationStubGenerator.kt @@ -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 diff --git a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/util/TypeTranslator.kt b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/util/TypeTranslator.kt index 0cb9083d830..e6476d8efd5 100644 --- a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/util/TypeTranslator.kt +++ b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/util/TypeTranslator.kt @@ -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 diff --git a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/util/originalDescriptors.kt b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/util/originalDescriptors.kt new file mode 100644 index 00000000000..e780a8e418c --- /dev/null +++ b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/util/originalDescriptors.kt @@ -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") + } + } diff --git a/compiler/testData/codegen/box/javaInterop/generics/javaNestedSamInterface.kt b/compiler/testData/codegen/box/javaInterop/generics/javaNestedSamInterface.kt new file mode 100644 index 00000000000..61d53a58196 --- /dev/null +++ b/compiler/testData/codegen/box/javaInterop/generics/javaNestedSamInterface.kt @@ -0,0 +1,24 @@ +// !LANGUAGE: +NewInference +// TARGET_BACKEND: JVM +// IGNORE_BACKEND_FIR: JVM_IR +// FILE: javaNestedSamInterface.kt +import test.A + +fun box(): String = A(42).get { "OK" } + +// FILE: test/A.java +package test; + +public class A { + private final X x; + + public A(X x) { + this.x = x; + } + + public interface I { + T compute(); + } + + public T get(I value) { return value.compute(); } +} diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java index 4f5f8d76903..3b87cac7ea6 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java @@ -14470,6 +14470,11 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest { public void testInvariantArgumentsNoWildcard() throws Exception { runTest("compiler/testData/codegen/box/javaInterop/generics/invariantArgumentsNoWildcard.kt"); } + + @TestMetadata("javaNestedSamInterface.kt") + public void testJavaNestedSamInterface() throws Exception { + runTest("compiler/testData/codegen/box/javaInterop/generics/javaNestedSamInterface.kt"); + } } @TestMetadata("compiler/testData/codegen/box/javaInterop/notNullAssertions") diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java index 43be2a98925..1dc87b41523 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java @@ -14470,6 +14470,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes public void testInvariantArgumentsNoWildcard() throws Exception { runTest("compiler/testData/codegen/box/javaInterop/generics/invariantArgumentsNoWildcard.kt"); } + + @TestMetadata("javaNestedSamInterface.kt") + public void testJavaNestedSamInterface() throws Exception { + runTest("compiler/testData/codegen/box/javaInterop/generics/javaNestedSamInterface.kt"); + } } @TestMetadata("compiler/testData/codegen/box/javaInterop/notNullAssertions") diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/ir/FirBlackBoxCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/ir/FirBlackBoxCodegenTestGenerated.java index 8b8ef7931a0..1a11f9efbd3 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/ir/FirBlackBoxCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/ir/FirBlackBoxCodegenTestGenerated.java @@ -13320,6 +13320,11 @@ public class FirBlackBoxCodegenTestGenerated extends AbstractFirBlackBoxCodegenT public void testInvariantArgumentsNoWildcard() throws Exception { runTest("compiler/testData/codegen/box/javaInterop/generics/invariantArgumentsNoWildcard.kt"); } + + @TestMetadata("javaNestedSamInterface.kt") + public void testJavaNestedSamInterface() throws Exception { + runTest("compiler/testData/codegen/box/javaInterop/generics/javaNestedSamInterface.kt"); + } } @TestMetadata("compiler/testData/codegen/box/javaInterop/notNullAssertions") diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java index 39b4c4b2767..dc96509c4ee 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java @@ -13320,6 +13320,11 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes public void testInvariantArgumentsNoWildcard() throws Exception { runTest("compiler/testData/codegen/box/javaInterop/generics/invariantArgumentsNoWildcard.kt"); } + + @TestMetadata("javaNestedSamInterface.kt") + public void testJavaNestedSamInterface() throws Exception { + runTest("compiler/testData/codegen/box/javaInterop/generics/javaNestedSamInterface.kt"); + } } @TestMetadata("compiler/testData/codegen/box/javaInterop/notNullAssertions")