From 62f2a09d70d29540ed41bf0884c8f7402dcacb94 Mon Sep 17 00:00:00 2001 From: Ilya Chernikov Date: Tue, 24 Jan 2023 12:29:03 +0100 Subject: [PATCH] FIR: fix use-site file in supertypes resolution manifested in the IC tests by a seemingly redundant lookup, although theoretically may lead to more severe issues. Also fixes lookup recording ignoring logic, since the old one may break with the new us-site file calculation. #KT-55195 fixed --- ...IncrementalPassThroughLookupTrackerComponent.kt | 7 ++++--- .../transformers/FirSupertypesResolution.kt | 7 +++++-- .../tests/typealias/privateInFile.fir.kt | 2 +- .../jetbrains/kotlin/jps/GenerateJpsPluginTests.kt | 6 +++--- ...ementalK2FirICLightTreeJvmJpsTestGenerated.java | 14 ++++++++++++-- .../build/IncrementalK2JvmJpsTestGenerated.java | 14 ++++++++++++-- .../IncrementalK2LightTreeJvmJpsTestGenerated.java | 14 ++++++++++++-- 7 files changed, 49 insertions(+), 15 deletions(-) diff --git a/compiler/fir/entrypoint/src/org/jetbrains/kotlin/fir/IncrementalPassThroughLookupTrackerComponent.kt b/compiler/fir/entrypoint/src/org/jetbrains/kotlin/fir/IncrementalPassThroughLookupTrackerComponent.kt index 03e5bccdc8a..b47268e40b9 100644 --- a/compiler/fir/entrypoint/src/org/jetbrains/kotlin/fir/IncrementalPassThroughLookupTrackerComponent.kt +++ b/compiler/fir/entrypoint/src/org/jetbrains/kotlin/fir/IncrementalPassThroughLookupTrackerComponent.kt @@ -24,9 +24,10 @@ class IncrementalPassThroughLookupTrackerComponent( private val sourceToFilePathsCache = ConcurrentHashMap() override fun recordLookup(name: Name, inScopes: List, source: KtSourceElement?, fileSource: KtSourceElement?) { - assert(fileSource != null || source is KtPsiSourceElement) // finding file for a source only possible for PSI, here it means - // that we allow null for file source only for PSI-only "sources", currently - java ones - val definedSource = fileSource ?: source ?: throw AssertionError("Cannot record lookup for \"$name\" without a source") + // finding file for a source only possible for PSI, here it means + // that we allow null for file source only for PSI-only "sources", currently - java ones, ignoring the other cases + // TODO: although there are valid use cases for missing fileSource, the ignore may hide some possible bugs; consider stricter implementation + val definedSource = fileSource ?: (source as? KtPsiSourceElement) ?: return val path = sourceToFilePathsCache.getOrPut(definedSource) { sourceToFilePath(definedSource) ?: // TODO: the lookup by non-file source mostly doesn't work for the LT, so we cannot afford null file sources here diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/FirSupertypesResolution.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/FirSupertypesResolution.kt index 896ef46c1b9..fcb1e5ecd9a 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/FirSupertypesResolution.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/FirSupertypesResolution.kt @@ -348,8 +348,11 @@ open class FirSupertypeResolverVisitor( val transformer = FirSpecificTypeResolverTransformer(session, supertypeSupplier = supertypeComputationSession.supertypesSupplier) - @OptIn(PrivateForInline::class) - val resolvedTypesRefs = transformer.withFile(useSiteFile) { + val newUseSiteFile = + if (classLikeDeclaration.isLocalClassOrAnonymousObject()) @OptIn(PrivateForInline::class) useSiteFile + else session.firProvider.getFirClassifierContainerFileIfAny(classLikeDeclaration.symbol) + + val resolvedTypesRefs = transformer.withFile(newUseSiteFile) { resolveSuperTypeRefs( transformer, ScopeClassDeclaration(scopes, classDeclarationsStack, containerDeclaration = classLikeDeclaration), diff --git a/compiler/testData/diagnostics/tests/typealias/privateInFile.fir.kt b/compiler/testData/diagnostics/tests/typealias/privateInFile.fir.kt index cd07b3b6c0e..28c60a6d60e 100644 --- a/compiler/testData/diagnostics/tests/typealias/privateInFile.fir.kt +++ b/compiler/testData/diagnostics/tests/typealias/privateInFile.fir.kt @@ -3,7 +3,7 @@ private class C { companion object } -private typealias TA = C +private typealias TA = C private val test1: C = C() private val test1co: C.Companion = C diff --git a/jps/jps-plugin/jps-tests/test/org/jetbrains/kotlin/jps/GenerateJpsPluginTests.kt b/jps/jps-plugin/jps-tests/test/org/jetbrains/kotlin/jps/GenerateJpsPluginTests.kt index b6e317b7201..71fbcde005e 100644 --- a/jps/jps-plugin/jps-tests/test/org/jetbrains/kotlin/jps/GenerateJpsPluginTests.kt +++ b/jps/jps-plugin/jps-tests/test/org/jetbrains/kotlin/jps/GenerateJpsPluginTests.kt @@ -57,19 +57,19 @@ fun main(args: Array) { testClass( init = incrementalJvmTestData( TargetBackend.JVM_IR, - excludePattern = "(^.*Expect.*)|(^classMovedIntoOtherClass)|(^companionConstantChanged)" + excludePattern = "(^.*Expect.*)" ) ) testClass( init = incrementalJvmTestData( TargetBackend.JVM_IR, - excludePattern = "(^.*Expect.*)|(^classMovedIntoOtherClass)|(^companionConstantChanged)" + excludePattern = "(^.*Expect.*)" ) ) testClass( init = incrementalJvmTestData( TargetBackend.JVM_IR, - excludePattern = "(^.*Expect.*)|(^classMovedIntoOtherClass)|(^companionConstantChanged)" + excludePattern = "(^.*Expect.*)" ) ) diff --git a/jps/jps-plugin/jps-tests/test/org/jetbrains/kotlin/jps/build/IncrementalK2FirICLightTreeJvmJpsTestGenerated.java b/jps/jps-plugin/jps-tests/test/org/jetbrains/kotlin/jps/build/IncrementalK2FirICLightTreeJvmJpsTestGenerated.java index 1cca77706b0..f5d3d17f37e 100644 --- a/jps/jps-plugin/jps-tests/test/org/jetbrains/kotlin/jps/build/IncrementalK2FirICLightTreeJvmJpsTestGenerated.java +++ b/jps/jps-plugin/jps-tests/test/org/jetbrains/kotlin/jps/build/IncrementalK2FirICLightTreeJvmJpsTestGenerated.java @@ -64,7 +64,7 @@ public class IncrementalK2FirICLightTreeJvmJpsTestGenerated extends AbstractIncr } public void testAllFilesPresentInPureKotlin() throws Exception { - KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("jps/jps-plugin/testData/incremental/pureKotlin"), Pattern.compile("^([^\\.]+)$"), Pattern.compile("(^.*Expect.*)|(^classMovedIntoOtherClass)|(^companionConstantChanged)"), TargetBackend.JVM_IR, false); + KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("jps/jps-plugin/testData/incremental/pureKotlin"), Pattern.compile("^([^\\.]+)$"), Pattern.compile("(^.*Expect.*)"), TargetBackend.JVM_IR, false); } @TestMetadata("annotations") @@ -117,6 +117,11 @@ public class IncrementalK2FirICLightTreeJvmJpsTestGenerated extends AbstractIncr runTest("jps/jps-plugin/testData/incremental/pureKotlin/classSignatureUnchanged/"); } + @TestMetadata("companionConstantChanged") + public void testCompanionConstantChanged() throws Exception { + runTest("jps/jps-plugin/testData/incremental/pureKotlin/companionConstantChanged/"); + } + @TestMetadata("compilationErrorThenFixedOtherPackage") public void testCompilationErrorThenFixedOtherPackage() throws Exception { runTest("jps/jps-plugin/testData/incremental/pureKotlin/compilationErrorThenFixedOtherPackage/"); @@ -692,7 +697,7 @@ public class IncrementalK2FirICLightTreeJvmJpsTestGenerated extends AbstractIncr } public void testAllFilesPresentInClassHierarchyAffected() throws Exception { - KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("jps/jps-plugin/testData/incremental/classHierarchyAffected"), Pattern.compile("^([^\\.]+)$"), Pattern.compile("(^.*Expect.*)|(^classMovedIntoOtherClass)|(^companionConstantChanged)"), TargetBackend.JVM_IR, false); + KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("jps/jps-plugin/testData/incremental/classHierarchyAffected"), Pattern.compile("^([^\\.]+)$"), Pattern.compile("(^.*Expect.*)"), TargetBackend.JVM_IR, false); } @TestMetadata("annotationFlagRemoved") @@ -725,6 +730,11 @@ public class IncrementalK2FirICLightTreeJvmJpsTestGenerated extends AbstractIncr runTest("jps/jps-plugin/testData/incremental/classHierarchyAffected/classBecamePrivate/"); } + @TestMetadata("classMovedIntoOtherClass") + public void testClassMovedIntoOtherClass() throws Exception { + runTest("jps/jps-plugin/testData/incremental/classHierarchyAffected/classMovedIntoOtherClass/"); + } + @TestMetadata("classRemoved") public void testClassRemoved() throws Exception { runTest("jps/jps-plugin/testData/incremental/classHierarchyAffected/classRemoved/"); diff --git a/jps/jps-plugin/jps-tests/test/org/jetbrains/kotlin/jps/build/IncrementalK2JvmJpsTestGenerated.java b/jps/jps-plugin/jps-tests/test/org/jetbrains/kotlin/jps/build/IncrementalK2JvmJpsTestGenerated.java index 25cf55e4e2d..843c40d888e 100644 --- a/jps/jps-plugin/jps-tests/test/org/jetbrains/kotlin/jps/build/IncrementalK2JvmJpsTestGenerated.java +++ b/jps/jps-plugin/jps-tests/test/org/jetbrains/kotlin/jps/build/IncrementalK2JvmJpsTestGenerated.java @@ -64,7 +64,7 @@ public class IncrementalK2JvmJpsTestGenerated extends AbstractIncrementalK2JvmJp } public void testAllFilesPresentInPureKotlin() throws Exception { - KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("jps/jps-plugin/testData/incremental/pureKotlin"), Pattern.compile("^([^\\.]+)$"), Pattern.compile("(^.*Expect.*)|(^classMovedIntoOtherClass)|(^companionConstantChanged)"), TargetBackend.JVM_IR, false); + KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("jps/jps-plugin/testData/incremental/pureKotlin"), Pattern.compile("^([^\\.]+)$"), Pattern.compile("(^.*Expect.*)"), TargetBackend.JVM_IR, false); } @TestMetadata("annotations") @@ -117,6 +117,11 @@ public class IncrementalK2JvmJpsTestGenerated extends AbstractIncrementalK2JvmJp runTest("jps/jps-plugin/testData/incremental/pureKotlin/classSignatureUnchanged/"); } + @TestMetadata("companionConstantChanged") + public void testCompanionConstantChanged() throws Exception { + runTest("jps/jps-plugin/testData/incremental/pureKotlin/companionConstantChanged/"); + } + @TestMetadata("compilationErrorThenFixedOtherPackage") public void testCompilationErrorThenFixedOtherPackage() throws Exception { runTest("jps/jps-plugin/testData/incremental/pureKotlin/compilationErrorThenFixedOtherPackage/"); @@ -692,7 +697,7 @@ public class IncrementalK2JvmJpsTestGenerated extends AbstractIncrementalK2JvmJp } public void testAllFilesPresentInClassHierarchyAffected() throws Exception { - KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("jps/jps-plugin/testData/incremental/classHierarchyAffected"), Pattern.compile("^([^\\.]+)$"), Pattern.compile("(^.*Expect.*)|(^classMovedIntoOtherClass)|(^companionConstantChanged)"), TargetBackend.JVM_IR, false); + KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("jps/jps-plugin/testData/incremental/classHierarchyAffected"), Pattern.compile("^([^\\.]+)$"), Pattern.compile("(^.*Expect.*)"), TargetBackend.JVM_IR, false); } @TestMetadata("annotationFlagRemoved") @@ -725,6 +730,11 @@ public class IncrementalK2JvmJpsTestGenerated extends AbstractIncrementalK2JvmJp runTest("jps/jps-plugin/testData/incremental/classHierarchyAffected/classBecamePrivate/"); } + @TestMetadata("classMovedIntoOtherClass") + public void testClassMovedIntoOtherClass() throws Exception { + runTest("jps/jps-plugin/testData/incremental/classHierarchyAffected/classMovedIntoOtherClass/"); + } + @TestMetadata("classRemoved") public void testClassRemoved() throws Exception { runTest("jps/jps-plugin/testData/incremental/classHierarchyAffected/classRemoved/"); diff --git a/jps/jps-plugin/jps-tests/test/org/jetbrains/kotlin/jps/build/IncrementalK2LightTreeJvmJpsTestGenerated.java b/jps/jps-plugin/jps-tests/test/org/jetbrains/kotlin/jps/build/IncrementalK2LightTreeJvmJpsTestGenerated.java index 3fc5cc350de..0d2d81be0e6 100644 --- a/jps/jps-plugin/jps-tests/test/org/jetbrains/kotlin/jps/build/IncrementalK2LightTreeJvmJpsTestGenerated.java +++ b/jps/jps-plugin/jps-tests/test/org/jetbrains/kotlin/jps/build/IncrementalK2LightTreeJvmJpsTestGenerated.java @@ -64,7 +64,7 @@ public class IncrementalK2LightTreeJvmJpsTestGenerated extends AbstractIncrement } public void testAllFilesPresentInPureKotlin() throws Exception { - KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("jps/jps-plugin/testData/incremental/pureKotlin"), Pattern.compile("^([^\\.]+)$"), Pattern.compile("(^.*Expect.*)|(^classMovedIntoOtherClass)|(^companionConstantChanged)"), TargetBackend.JVM_IR, false); + KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("jps/jps-plugin/testData/incremental/pureKotlin"), Pattern.compile("^([^\\.]+)$"), Pattern.compile("(^.*Expect.*)"), TargetBackend.JVM_IR, false); } @TestMetadata("annotations") @@ -117,6 +117,11 @@ public class IncrementalK2LightTreeJvmJpsTestGenerated extends AbstractIncrement runTest("jps/jps-plugin/testData/incremental/pureKotlin/classSignatureUnchanged/"); } + @TestMetadata("companionConstantChanged") + public void testCompanionConstantChanged() throws Exception { + runTest("jps/jps-plugin/testData/incremental/pureKotlin/companionConstantChanged/"); + } + @TestMetadata("compilationErrorThenFixedOtherPackage") public void testCompilationErrorThenFixedOtherPackage() throws Exception { runTest("jps/jps-plugin/testData/incremental/pureKotlin/compilationErrorThenFixedOtherPackage/"); @@ -692,7 +697,7 @@ public class IncrementalK2LightTreeJvmJpsTestGenerated extends AbstractIncrement } public void testAllFilesPresentInClassHierarchyAffected() throws Exception { - KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("jps/jps-plugin/testData/incremental/classHierarchyAffected"), Pattern.compile("^([^\\.]+)$"), Pattern.compile("(^.*Expect.*)|(^classMovedIntoOtherClass)|(^companionConstantChanged)"), TargetBackend.JVM_IR, false); + KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("jps/jps-plugin/testData/incremental/classHierarchyAffected"), Pattern.compile("^([^\\.]+)$"), Pattern.compile("(^.*Expect.*)"), TargetBackend.JVM_IR, false); } @TestMetadata("annotationFlagRemoved") @@ -725,6 +730,11 @@ public class IncrementalK2LightTreeJvmJpsTestGenerated extends AbstractIncrement runTest("jps/jps-plugin/testData/incremental/classHierarchyAffected/classBecamePrivate/"); } + @TestMetadata("classMovedIntoOtherClass") + public void testClassMovedIntoOtherClass() throws Exception { + runTest("jps/jps-plugin/testData/incremental/classHierarchyAffected/classMovedIntoOtherClass/"); + } + @TestMetadata("classRemoved") public void testClassRemoved() throws Exception { runTest("jps/jps-plugin/testData/incremental/classHierarchyAffected/classRemoved/");