diff --git a/analysis/analysis-api-fir/src/org/jetbrains/kotlin/analysis/api/fir/KtSymbolByFirBuilder.kt b/analysis/analysis-api-fir/src/org/jetbrains/kotlin/analysis/api/fir/KtSymbolByFirBuilder.kt index f73f5108d9e..e9f12518ca5 100644 --- a/analysis/analysis-api-fir/src/org/jetbrains/kotlin/analysis/api/fir/KtSymbolByFirBuilder.kt +++ b/analysis/analysis-api-fir/src/org/jetbrains/kotlin/analysis/api/fir/KtSymbolByFirBuilder.kt @@ -64,6 +64,7 @@ import kotlin.contracts.ExperimentalContracts import kotlin.contracts.contract import org.jetbrains.kotlin.analysis.api.fir.signatures.KtFirFunctionLikeSubstitutorBasedSignature import org.jetbrains.kotlin.analysis.api.fir.signatures.KtFirVariableLikeSubstitutorBasedSignature +import org.jetbrains.kotlin.fir.scopes.impl.importedFromObjectOrStaticData /** * Maps FirElement to KtSymbol & ConeType to KtType, thread safe @@ -334,6 +335,9 @@ internal class KtSymbolByFirBuilder constructor( fun buildFieldSymbol(firSymbol: FirFieldSymbol): KtFirJavaFieldSymbol { + if (firSymbol.origin == FirDeclarationOrigin.ImportedFromObjectOrStatic) { + return buildFieldSymbol(firSymbol.fir.importedFromObjectOrStaticData!!.original.symbol) + } checkRequirementForBuildingSymbol(firSymbol, firSymbol.fir.isJavaFieldOrSubstitutionOverrideOfJavaField()) return symbolsCache.cache(firSymbol) { KtFirJavaFieldSymbol(firSymbol, analysisSession) } } diff --git a/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirLightTreeBlackBoxCodegenTestGenerated.java b/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirLightTreeBlackBoxCodegenTestGenerated.java index 5d1eafdef75..c8f1fe7fcbf 100644 --- a/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirLightTreeBlackBoxCodegenTestGenerated.java +++ b/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirLightTreeBlackBoxCodegenTestGenerated.java @@ -18609,6 +18609,30 @@ public class FirLightTreeBlackBoxCodegenTestGenerated extends AbstractFirLightTr runTest("compiler/testData/codegen/box/fir/SamWithReceiverMavenProjectImportHandler.kt"); } + @Test + @TestMetadata("staticImportFromEnum.kt") + public void testStaticImportFromEnum() throws Exception { + runTest("compiler/testData/codegen/box/fir/staticImportFromEnum.kt"); + } + + @Test + @TestMetadata("staticImportFromEnumJava.kt") + public void testStaticImportFromEnumJava() throws Exception { + runTest("compiler/testData/codegen/box/fir/staticImportFromEnumJava.kt"); + } + + @Test + @TestMetadata("staticImportFromObject.kt") + public void testStaticImportFromObject() throws Exception { + runTest("compiler/testData/codegen/box/fir/staticImportFromObject.kt"); + } + + @Test + @TestMetadata("staticImportViaInheritance.kt") + public void testStaticImportViaInheritance() throws Exception { + runTest("compiler/testData/codegen/box/fir/staticImportViaInheritance.kt"); + } + @Test @TestMetadata("SuspendExtension.kt") public void testSuspendExtension() throws Exception { diff --git a/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirPsiBlackBoxCodegenTestGenerated.java b/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirPsiBlackBoxCodegenTestGenerated.java index d7e377fb503..12b54d63dd6 100644 --- a/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirPsiBlackBoxCodegenTestGenerated.java +++ b/compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirPsiBlackBoxCodegenTestGenerated.java @@ -18609,6 +18609,30 @@ public class FirPsiBlackBoxCodegenTestGenerated extends AbstractFirPsiBlackBoxCo runTest("compiler/testData/codegen/box/fir/SamWithReceiverMavenProjectImportHandler.kt"); } + @Test + @TestMetadata("staticImportFromEnum.kt") + public void testStaticImportFromEnum() throws Exception { + runTest("compiler/testData/codegen/box/fir/staticImportFromEnum.kt"); + } + + @Test + @TestMetadata("staticImportFromEnumJava.kt") + public void testStaticImportFromEnumJava() throws Exception { + runTest("compiler/testData/codegen/box/fir/staticImportFromEnumJava.kt"); + } + + @Test + @TestMetadata("staticImportFromObject.kt") + public void testStaticImportFromObject() throws Exception { + runTest("compiler/testData/codegen/box/fir/staticImportFromObject.kt"); + } + + @Test + @TestMetadata("staticImportViaInheritance.kt") + public void testStaticImportViaInheritance() throws Exception { + runTest("compiler/testData/codegen/box/fir/staticImportViaInheritance.kt"); + } + @Test @TestMetadata("SuspendExtension.kt") public void testSuspendExtension() throws Exception { diff --git a/compiler/fir/providers/src/org/jetbrains/kotlin/fir/scopes/impl/FirAbstractImportingScope.kt b/compiler/fir/providers/src/org/jetbrains/kotlin/fir/scopes/impl/FirAbstractImportingScope.kt index 22fa9bad2ee..c787bc84a25 100644 --- a/compiler/fir/providers/src/org/jetbrains/kotlin/fir/scopes/impl/FirAbstractImportingScope.kt +++ b/compiler/fir/providers/src/org/jetbrains/kotlin/fir/scopes/impl/FirAbstractImportingScope.kt @@ -8,17 +8,17 @@ package org.jetbrains.kotlin.fir.scopes.impl import org.jetbrains.kotlin.descriptors.ClassKind import org.jetbrains.kotlin.fir.FirSession import org.jetbrains.kotlin.fir.declarations.* +import org.jetbrains.kotlin.fir.declarations.builder.buildFieldCopy import org.jetbrains.kotlin.fir.declarations.builder.buildPropertyCopy import org.jetbrains.kotlin.fir.declarations.builder.buildSimpleFunctionCopy import org.jetbrains.kotlin.fir.declarations.utils.expandedConeType import org.jetbrains.kotlin.fir.declarations.utils.isStatic +import org.jetbrains.kotlin.fir.render import org.jetbrains.kotlin.fir.resolve.ScopeSession import org.jetbrains.kotlin.fir.resolve.toSymbol import org.jetbrains.kotlin.fir.scopes.FirContainingNamesAwareScope import org.jetbrains.kotlin.fir.scopes.unsubstitutedScope import org.jetbrains.kotlin.fir.symbols.impl.* -import org.jetbrains.kotlin.fir.types.ConeClassLikeType -import org.jetbrains.kotlin.fir.types.coneTypeSafe import org.jetbrains.kotlin.name.CallableId import org.jetbrains.kotlin.name.ClassId import org.jetbrains.kotlin.name.FqName @@ -62,7 +62,7 @@ abstract class FirAbstractImportingScope( } } - private inline fun > processCallablesFromImportsByName( + private inline fun > processCallablesFromImportsByName( name: Name?, imports: List, crossinline processor: (S) -> Unit, @@ -101,7 +101,7 @@ abstract class FirAbstractImportingScope( name, imports, processor, - { classId -> fir.buildImportedCopy(classId).symbol }, + { classId -> fir.buildImportedVersion(classId).symbol }, FirContainingNamesAwareScope::processFunctionsByName, provider::getTopLevelFunctionSymbols ) @@ -112,37 +112,48 @@ abstract class FirAbstractImportingScope( name, imports, processor, - { classId -> fir.buildImportedCopy(classId).symbol }, - { importedName, importedProcessor -> - processPropertiesByName(importedName) { - if (it is FirPropertySymbol) { - importedProcessor(it) - } else { - processor(it) - } - } - }, + { classId -> fir.buildImportedVersion(classId).symbol }, + FirContainingNamesAwareScope::processPropertiesByName, provider::getTopLevelPropertySymbols ) } } -internal fun FirSimpleFunction.buildImportedCopy(importedClassId: ClassId): FirSimpleFunction { +internal fun FirSimpleFunction.buildImportedVersion(importedClassId: ClassId): FirSimpleFunction { return buildSimpleFunctionCopy(this) { origin = FirDeclarationOrigin.ImportedFromObjectOrStatic this.symbol = FirNamedFunctionSymbol(CallableId(importedClassId, name)) }.apply { - importedFromObjectOrStaticData = ImportedFromObjectOrStaticData(importedClassId, this@buildImportedCopy) + importedFromObjectOrStaticData = ImportedFromObjectOrStaticData(importedClassId, this@buildImportedVersion) } } -internal fun FirProperty.buildImportedCopy(importedClassId: ClassId): FirProperty { - return buildPropertyCopy(this) { - origin = FirDeclarationOrigin.ImportedFromObjectOrStatic - this.symbol = FirPropertySymbol(CallableId(importedClassId, name)) - this.delegateFieldSymbol = null - }.apply { - importedFromObjectOrStaticData = ImportedFromObjectOrStaticData(importedClassId, this@buildImportedCopy) +internal fun FirVariable.buildImportedVersion(importedClassId: ClassId): FirVariable { + return when (this) { + is FirProperty -> { + buildPropertyCopy(this) { + origin = FirDeclarationOrigin.ImportedFromObjectOrStatic + this.symbol = FirPropertySymbol(CallableId(importedClassId, name)) + this.delegateFieldSymbol = null + }.apply { + importedFromObjectOrStaticData = ImportedFromObjectOrStaticData(importedClassId, this@buildImportedVersion) + } + } + is FirField -> { + buildFieldCopy(this) { + origin = FirDeclarationOrigin.ImportedFromObjectOrStatic + this.symbol = FirFieldSymbol(CallableId(importedClassId, name)) + }.apply { + importedFromObjectOrStaticData = ImportedFromObjectOrStaticData(importedClassId, this@buildImportedVersion) + } + } + is FirEnumEntry -> { + // It's not important to create an imported copy of FirEnumEntry + this + } + else -> { + throw IllegalStateException("Unexpected variable in buildImportedCopy: ${render()} of type ${this::class.java}") + } } } diff --git a/compiler/fir/tree/gen/org/jetbrains/kotlin/fir/declarations/builder/FirFieldBuilder.kt b/compiler/fir/tree/gen/org/jetbrains/kotlin/fir/declarations/builder/FirFieldBuilder.kt index 343fcf96386..1d5807905c5 100644 --- a/compiler/fir/tree/gen/org/jetbrains/kotlin/fir/declarations/builder/FirFieldBuilder.kt +++ b/compiler/fir/tree/gen/org/jetbrains/kotlin/fir/declarations/builder/FirFieldBuilder.kt @@ -31,6 +31,7 @@ import org.jetbrains.kotlin.fir.declarations.UnresolvedDeprecationProvider import org.jetbrains.kotlin.fir.declarations.asResolveState import org.jetbrains.kotlin.fir.declarations.builder.FirVariableBuilder import org.jetbrains.kotlin.fir.declarations.impl.FirFieldImpl +import org.jetbrains.kotlin.fir.declarations.resolvePhase import org.jetbrains.kotlin.fir.expressions.FirAnnotation import org.jetbrains.kotlin.fir.expressions.FirExpression import org.jetbrains.kotlin.fir.references.FirControlFlowGraphReference @@ -128,3 +129,30 @@ inline fun buildField(init: FirFieldBuilder.() -> Unit): FirField { } return FirFieldBuilder().apply(init).build() } + +@OptIn(ExperimentalContracts::class) +inline fun buildFieldCopy(original: FirField, init: FirFieldBuilder.() -> Unit): FirField { + contract { + callsInPlace(init, kotlin.contracts.InvocationKind.EXACTLY_ONCE) + } + val copyBuilder = FirFieldBuilder() + copyBuilder.source = original.source + copyBuilder.resolvePhase = original.resolvePhase + copyBuilder.moduleData = original.moduleData + copyBuilder.origin = original.origin + copyBuilder.attributes = original.attributes.copy() + copyBuilder.typeParameters.addAll(original.typeParameters) + copyBuilder.status = original.status + copyBuilder.returnTypeRef = original.returnTypeRef + copyBuilder.deprecationsProvider = original.deprecationsProvider + copyBuilder.containerSource = original.containerSource + copyBuilder.dispatchReceiverType = original.dispatchReceiverType + copyBuilder.contextReceivers.addAll(original.contextReceivers) + copyBuilder.name = original.name + copyBuilder.initializer = original.initializer + copyBuilder.isVar = original.isVar + copyBuilder.backingField = original.backingField + copyBuilder.annotations.addAll(original.annotations) + copyBuilder.symbol = original.symbol + return copyBuilder.apply(init).build() +} diff --git a/compiler/fir/tree/tree-generator/src/org/jetbrains/kotlin/fir/tree/generator/BuilderConfigurator.kt b/compiler/fir/tree/tree-generator/src/org/jetbrains/kotlin/fir/tree/generator/BuilderConfigurator.kt index 40d4f459981..c4653c73b67 100644 --- a/compiler/fir/tree/tree-generator/src/org/jetbrains/kotlin/fir/tree/generator/BuilderConfigurator.kt +++ b/compiler/fir/tree/tree-generator/src/org/jetbrains/kotlin/fir/tree/generator/BuilderConfigurator.kt @@ -239,6 +239,7 @@ object BuilderConfigurator : AbstractBuilderConfigurator(FirTree parents += variableBuilder default("resolvePhase", "FirResolvePhase.DECLARATIONS") openBuilder() + withCopy() } builder(typeOperatorCall) { diff --git a/compiler/testData/codegen/box/fir/staticImportFromEnum.kt b/compiler/testData/codegen/box/fir/staticImportFromEnum.kt new file mode 100644 index 00000000000..0cf3f8f323a --- /dev/null +++ b/compiler/testData/codegen/box/fir/staticImportFromEnum.kt @@ -0,0 +1,19 @@ +// TARGET_BACKEND: JVM_IR + +// FILE: dependency/Base.kt + +package dependency; + +enum class Base(val s: String) { + FIRST("O"), + SECOND("K") +} + +// FILE: test/test.kt + +package test + +import dependency.Base.FIRST +import dependency.Base.SECOND + +fun box(): String = FIRST.s + SECOND.s diff --git a/compiler/testData/codegen/box/fir/staticImportFromEnumJava.kt b/compiler/testData/codegen/box/fir/staticImportFromEnumJava.kt new file mode 100644 index 00000000000..3d6219a1d5d --- /dev/null +++ b/compiler/testData/codegen/box/fir/staticImportFromEnumJava.kt @@ -0,0 +1,18 @@ +// TARGET_BACKEND: JVM_IR + +// FILE: dependency/Base.java + +package dependency; + +public enum Base { + O, K; +} + +// FILE: test/test.kt + +package test + +import dependency.Base.O +import dependency.Base.K + +fun box(): String = O.name + K.name diff --git a/compiler/testData/codegen/box/fir/staticImportFromObject.kt b/compiler/testData/codegen/box/fir/staticImportFromObject.kt new file mode 100644 index 00000000000..70f8ef19615 --- /dev/null +++ b/compiler/testData/codegen/box/fir/staticImportFromObject.kt @@ -0,0 +1,21 @@ +// TARGET_BACKEND: JVM_IR +// ISSUE: KT-58980 + +// FILE: dependency/Base.java + +package dependency; + +public class Base { + public String string = "OK"; +} + +// FILE: test/test.kt + +package test + +import dependency.Base +import test.KtObject.string + +object KtObject : Base() + +fun box(): String = string diff --git a/compiler/testData/diagnostics/tests/j+k/staticImportViaInheritance.fir.kt b/compiler/testData/codegen/box/fir/staticImportViaInheritance.kt similarity index 55% rename from compiler/testData/diagnostics/tests/j+k/staticImportViaInheritance.fir.kt rename to compiler/testData/codegen/box/fir/staticImportViaInheritance.kt index 66ffec3ebe5..4e6da0ea48b 100644 --- a/compiler/testData/diagnostics/tests/j+k/staticImportViaInheritance.fir.kt +++ b/compiler/testData/codegen/box/fir/staticImportViaInheritance.kt @@ -1,3 +1,4 @@ +// TARGET_BACKEND: JVM_IR // ISSUE: KT-59140 // FILE: pkg/Foo.java @@ -5,17 +6,15 @@ package pkg; abstract class CommonFoo { - public static final int BAR = 1; + public static final String BAR = "OK"; } public class Foo extends CommonFoo {} // FILE: test.kt -import pkg.Foo import pkg.Foo.BAR -fun test() { - val bar = BAR - val fooBar = Foo.BAR +fun box(): String { + return BAR } diff --git a/compiler/testData/diagnostics/tests/j+k/staticImportViaInheritance.kt b/compiler/testData/diagnostics/tests/j+k/staticImportViaInheritance.kt index c66804ca33d..12c35a1fa6f 100644 --- a/compiler/testData/diagnostics/tests/j+k/staticImportViaInheritance.kt +++ b/compiler/testData/diagnostics/tests/j+k/staticImportViaInheritance.kt @@ -1,3 +1,4 @@ +// FIR_IDENTICAL // ISSUE: KT-59140 // FILE: pkg/Foo.java 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 c9c2c181f16..7b62a6bb0f5 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 @@ -18609,6 +18609,30 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes runTest("compiler/testData/codegen/box/fir/SamWithReceiverMavenProjectImportHandler.kt"); } + @Test + @TestMetadata("staticImportFromEnum.kt") + public void testStaticImportFromEnum() throws Exception { + runTest("compiler/testData/codegen/box/fir/staticImportFromEnum.kt"); + } + + @Test + @TestMetadata("staticImportFromEnumJava.kt") + public void testStaticImportFromEnumJava() throws Exception { + runTest("compiler/testData/codegen/box/fir/staticImportFromEnumJava.kt"); + } + + @Test + @TestMetadata("staticImportFromObject.kt") + public void testStaticImportFromObject() throws Exception { + runTest("compiler/testData/codegen/box/fir/staticImportFromObject.kt"); + } + + @Test + @TestMetadata("staticImportViaInheritance.kt") + public void testStaticImportViaInheritance() throws Exception { + runTest("compiler/testData/codegen/box/fir/staticImportViaInheritance.kt"); + } + @Test @TestMetadata("SuspendExtension.kt") public void testSuspendExtension() throws Exception { diff --git a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/IrBlackBoxCodegenWithIrInlinerTestGenerated.java b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/IrBlackBoxCodegenWithIrInlinerTestGenerated.java index 4362c3b3811..21d57a239eb 100644 --- a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/IrBlackBoxCodegenWithIrInlinerTestGenerated.java +++ b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/IrBlackBoxCodegenWithIrInlinerTestGenerated.java @@ -18609,6 +18609,30 @@ public class IrBlackBoxCodegenWithIrInlinerTestGenerated extends AbstractIrBlack runTest("compiler/testData/codegen/box/fir/SamWithReceiverMavenProjectImportHandler.kt"); } + @Test + @TestMetadata("staticImportFromEnum.kt") + public void testStaticImportFromEnum() throws Exception { + runTest("compiler/testData/codegen/box/fir/staticImportFromEnum.kt"); + } + + @Test + @TestMetadata("staticImportFromEnumJava.kt") + public void testStaticImportFromEnumJava() throws Exception { + runTest("compiler/testData/codegen/box/fir/staticImportFromEnumJava.kt"); + } + + @Test + @TestMetadata("staticImportFromObject.kt") + public void testStaticImportFromObject() throws Exception { + runTest("compiler/testData/codegen/box/fir/staticImportFromObject.kt"); + } + + @Test + @TestMetadata("staticImportViaInheritance.kt") + public void testStaticImportViaInheritance() throws Exception { + runTest("compiler/testData/codegen/box/fir/staticImportViaInheritance.kt"); + } + @Test @TestMetadata("SuspendExtension.kt") public void testSuspendExtension() throws Exception {