diff --git a/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirBlackBoxCodegenTestGenerated.java b/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirBlackBoxCodegenTestGenerated.java index 30c43440af1..937b3eecd34 100644 --- a/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirBlackBoxCodegenTestGenerated.java +++ b/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirBlackBoxCodegenTestGenerated.java @@ -23278,6 +23278,12 @@ public class FirBlackBoxCodegenTestGenerated extends AbstractFirBlackBoxCodegenT runTest("compiler/testData/codegen/box/javaInterop/superCallOfPrintStackTrace.kt"); } + @Test + @TestMetadata("unresolvedJavaClassInDifferentFile.kt") + public void testUnresolvedJavaClassInDifferentFile() throws Exception { + runTest("compiler/testData/codegen/box/javaInterop/unresolvedJavaClassInDifferentFile.kt"); + } + @Nested @TestMetadata("compiler/testData/codegen/box/javaInterop/generics") @TestDataPath("$PROJECT_ROOT") diff --git a/compiler/ir/ir.psi2ir/src/org/jetbrains/kotlin/psi2ir/generators/FunctionGenerator.kt b/compiler/ir/ir.psi2ir/src/org/jetbrains/kotlin/psi2ir/generators/FunctionGenerator.kt index 4109288074b..811bd84036a 100644 --- a/compiler/ir/ir.psi2ir/src/org/jetbrains/kotlin/psi2ir/generators/FunctionGenerator.kt +++ b/compiler/ir/ir.psi2ir/src/org/jetbrains/kotlin/psi2ir/generators/FunctionGenerator.kt @@ -18,6 +18,10 @@ import org.jetbrains.kotlin.ir.expressions.impl.IrGetFieldImpl import org.jetbrains.kotlin.ir.expressions.impl.IrGetValueImpl import org.jetbrains.kotlin.ir.expressions.impl.IrReturnImpl import org.jetbrains.kotlin.ir.expressions.impl.IrSetFieldImpl +import org.jetbrains.kotlin.ir.types.IrErrorType +import org.jetbrains.kotlin.ir.types.IrSimpleType +import org.jetbrains.kotlin.ir.types.IrType +import org.jetbrains.kotlin.ir.types.IrTypeProjection import org.jetbrains.kotlin.ir.types.impl.IrUninitializedType import org.jetbrains.kotlin.ir.util.declareSimpleFunctionWithOverrides import org.jetbrains.kotlin.name.Name @@ -28,7 +32,6 @@ import org.jetbrains.kotlin.psi.psiUtil.pureStartOffset import org.jetbrains.kotlin.psi2ir.isConstructorDelegatingToSuper import org.jetbrains.kotlin.resolve.BindingContext import org.jetbrains.kotlin.resolve.DescriptorToSourceUtils -import org.jetbrains.kotlin.resolve.DescriptorUtils import org.jetbrains.kotlin.resolve.calls.util.getResolvedCall import org.jetbrains.kotlin.resolve.constants.evaluate.ConstantExpressionEvaluator import org.jetbrains.kotlin.resolve.descriptorUtil.isAnnotationConstructor @@ -67,11 +70,35 @@ class FunctionGenerator(declarationGenerator: DeclarationGenerator) : Declaratio fun generateFakeOverrideFunction(functionDescriptor: FunctionDescriptor, ktElement: KtPureElement): IrSimpleFunction? = functionDescriptor.takeIf { it.visibility != DescriptorVisibilities.INVISIBLE_FAKE } ?.let { - declareSimpleFunctionInner(it, ktElement, IrDeclarationOrigin.FAKE_OVERRIDE).buildWithScope { irFunction -> - generateFunctionParameterDeclarationsAndReturnType(irFunction, ktElement, null) - } + declareSimpleFunctionInner(it, ktElement, IrDeclarationOrigin.FAKE_OVERRIDE) + .buildWithScope { irFunction -> + generateFunctionParameterDeclarationsAndReturnType(irFunction, ktElement, null) + } + .takeUnless { irFunction -> + irFunction.containsErrorTypesInSignature() + } } + private fun IrSimpleFunction.containsErrorTypesInSignature(): Boolean { + if (this.typeParameters.any { it.superTypes.any { it.containsErrorType() } }) return true + this.dispatchReceiverParameter?.let { + if (it.type.containsErrorType()) return true + } + this.extensionReceiverParameter?.let { + if (it.type.containsErrorType()) return true + } + if (this.valueParameters.any { it.type.containsErrorType() }) return true + if (this.returnType.containsErrorType()) return true + return false + } + + private fun IrType.containsErrorType(): Boolean = + when (this) { + is IrErrorType -> true + is IrSimpleType -> arguments.any { it is IrTypeProjection && it.type.containsErrorType() } + else -> false + } + private inline fun declareSimpleFunction( ktFunction: KtFunction, ktReceiver: KtElement?, @@ -370,7 +397,12 @@ class FunctionGenerator(declarationGenerator: DeclarationGenerator) : Declaratio return call.resultingDescriptor.name.asString() } - private fun declareParameter(descriptor: ParameterDescriptor, ktElement: KtPureElement?, irOwnerElement: IrElement, name: Name? = null) = + private fun declareParameter( + descriptor: ParameterDescriptor, + ktElement: KtPureElement?, + irOwnerElement: IrElement, + name: Name? = null + ) = context.symbolTable.declareValueParameter( ktElement?.pureStartOffset ?: irOwnerElement.startOffset, ktElement?.pureEndOffset ?: irOwnerElement.endOffset, 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 606b2e2bdb8..ad605f10f20 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 @@ -20,6 +20,7 @@ import org.jetbrains.kotlin.ir.types.IrType import org.jetbrains.kotlin.ir.types.IrTypeAbbreviation import org.jetbrains.kotlin.ir.types.IrTypeProjection import org.jetbrains.kotlin.ir.types.impl.* +import org.jetbrains.kotlin.resolve.descriptorUtil.module import org.jetbrains.kotlin.types.* import org.jetbrains.kotlin.types.typeUtil.replaceArgumentsWithStarProjections import org.jetbrains.kotlin.types.typesApproximation.approximateCapturedTypes @@ -143,6 +144,10 @@ abstract class TypeTranslator( val lowerTypeDescriptor = lowerType.constructor.declarationDescriptor as? ClassDescriptor ?: throw AssertionError("No class descriptor for lower type $lowerType of $approximatedType") + annotations = translateTypeAnnotations(upperType, approximatedType) + if (lowerTypeDescriptor is NotFoundClasses.MockClassDescriptor) { + return IrErrorTypeImpl(approximatedType, annotations, variance) + } classifier = symbolTable.referenceClass(lowerTypeDescriptor) arguments = when { approximatedType is RawType -> @@ -152,7 +157,7 @@ abstract class TypeTranslator( else -> translateTypeArguments(upperType.arguments) } - annotations = translateTypeAnnotations(upperType, approximatedType) + } else -> diff --git a/compiler/testData/codegen/box/javaInterop/unresolvedJavaClassInDifferentFile.kt b/compiler/testData/codegen/box/javaInterop/unresolvedJavaClassInDifferentFile.kt new file mode 100644 index 00000000000..787ae8c3599 --- /dev/null +++ b/compiler/testData/codegen/box/javaInterop/unresolvedJavaClassInDifferentFile.kt @@ -0,0 +1,30 @@ +// TARGET_BACKEND: JVM + +// FILE: unresolvedJavaClassInDifferentFile.kt +import j.Base + +class Derived : Base() { + fun ok() = "OK" +} + +fun box() = + Derived().ok() + +// FILE: j/Foo.java +package j; + +// NB package-private class 'j.Bar' in file 'j/Foo.java' +class Bar { +} + +// FILE: j/Base.java +package j; + +public class Base { + protected Bar bar() { + return new Bar(); + } + + protected void bar(Bar b) { + } +} \ No newline at end of file diff --git a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/BlackBoxCodegenTestGenerated.java b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/BlackBoxCodegenTestGenerated.java index b9d97b2d7a9..a814ad64d2e 100644 --- a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/BlackBoxCodegenTestGenerated.java +++ b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/BlackBoxCodegenTestGenerated.java @@ -23140,6 +23140,12 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest { runTest("compiler/testData/codegen/box/javaInterop/superCallOfPrintStackTrace.kt"); } + @Test + @TestMetadata("unresolvedJavaClassInDifferentFile.kt") + public void testUnresolvedJavaClassInDifferentFile() throws Exception { + runTest("compiler/testData/codegen/box/javaInterop/unresolvedJavaClassInDifferentFile.kt"); + } + @Nested @TestMetadata("compiler/testData/codegen/box/javaInterop/generics") @TestDataPath("$PROJECT_ROOT") diff --git a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/IrBlackBoxCodegenTestGenerated.java b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/IrBlackBoxCodegenTestGenerated.java index a602a2ce5b9..a78b78a3be3 100644 --- a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/IrBlackBoxCodegenTestGenerated.java +++ b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/IrBlackBoxCodegenTestGenerated.java @@ -23278,6 +23278,12 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes runTest("compiler/testData/codegen/box/javaInterop/superCallOfPrintStackTrace.kt"); } + @Test + @TestMetadata("unresolvedJavaClassInDifferentFile.kt") + public void testUnresolvedJavaClassInDifferentFile() throws Exception { + runTest("compiler/testData/codegen/box/javaInterop/unresolvedJavaClassInDifferentFile.kt"); + } + @Nested @TestMetadata("compiler/testData/codegen/box/javaInterop/generics") @TestDataPath("$PROJECT_ROOT") diff --git a/compiler/tests-gen/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java b/compiler/tests-gen/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java index 2cff2c2051f..ac27625646c 100644 --- a/compiler/tests-gen/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java +++ b/compiler/tests-gen/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java @@ -19417,6 +19417,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes runTest("compiler/testData/codegen/box/javaInterop/superCallOfPrintStackTrace.kt"); } + @TestMetadata("unresolvedJavaClassInDifferentFile.kt") + public void testUnresolvedJavaClassInDifferentFile() throws Exception { + runTest("compiler/testData/codegen/box/javaInterop/unresolvedJavaClassInDifferentFile.kt"); + } + @TestMetadata("compiler/testData/codegen/box/javaInterop/generics") @TestDataPath("$PROJECT_ROOT") @RunWith(JUnit3RunnerWithInners.class)