From 54cca882797de3d7861fcddae0214ad802937669 Mon Sep 17 00:00:00 2001 From: Roman Golyshev Date: Tue, 11 Jan 2022 17:08:41 +0300 Subject: [PATCH] KT-50728 Enforce receiver type resolve When stdlib is represented as a source dependency (which seems to be the case in the `kotlin` project), it will be resolved lazily for the IDE. Same thing can happen if someone decides to "extend" the stdlib by declaring their declaration in the `kotlin` package In both of those cases, we need to make sure that receiver type is fully resolved before trying to get a `coneType` from it N.B. To make resolve tests work, I've added a separate folder `withAllowedKotlinPackage` to the `testData`, because in the default test setup it is not allowed to extend the `kotlin` package by user's definitions ^KT-50728 Fixed --- ...nosisCompilerFirTestdataTestGenerated.java | 16 +++++++++++++ ...TouchedTilContractsPhaseTestGenerated.java | 18 ++++++++++++++ .../extensionFunctionAddedToStdlib.fir.txt | 24 +++++++++++++++++++ .../extensionFunctionAddedToStdlib.kt | 15 ++++++++++++ .../runners/FirDiagnosticTestGenerated.java | 16 +++++++++++++ ...DiagnosticsWithLightTreeTestGenerated.java | 16 +++++++++++++ .../fir/resolve/calls/tower/TowerLevels.kt | 2 +- .../test/runners/AbstractFirDiagnosticTest.kt | 7 ++++++ 8 files changed, 113 insertions(+), 1 deletion(-) create mode 100644 compiler/fir/analysis-tests/testData/resolve/withAllowedKotlinPackage/extensionFunctionAddedToStdlib.fir.txt create mode 100644 compiler/fir/analysis-tests/testData/resolve/withAllowedKotlinPackage/extensionFunctionAddedToStdlib.kt diff --git a/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/DiagnosisCompilerFirTestdataTestGenerated.java b/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/DiagnosisCompilerFirTestdataTestGenerated.java index f171c1340c8..e502a56ede2 100644 --- a/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/DiagnosisCompilerFirTestdataTestGenerated.java +++ b/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/DiagnosisCompilerFirTestdataTestGenerated.java @@ -4073,6 +4073,22 @@ public class DiagnosisCompilerFirTestdataTestGenerated extends AbstractDiagnosis runTest("compiler/fir/analysis-tests/testData/resolve/visibility/visibilityWithOverrides.kt"); } } + + @Nested + @TestMetadata("compiler/fir/analysis-tests/testData/resolve/withAllowedKotlinPackage") + @TestDataPath("$PROJECT_ROOT") + public class WithAllowedKotlinPackage { + @Test + public void testAllFilesPresentInWithAllowedKotlinPackage() throws Exception { + KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/fir/analysis-tests/testData/resolve/withAllowedKotlinPackage"), Pattern.compile("^([^.]+)\\.kt$"), null, true); + } + + @Test + @TestMetadata("extensionFunctionAddedToStdlib.kt") + public void testExtensionFunctionAddedToStdlib() throws Exception { + runTest("compiler/fir/analysis-tests/testData/resolve/withAllowedKotlinPackage/extensionFunctionAddedToStdlib.kt"); + } + } } @Nested diff --git a/compiler/fir/analysis-tests/legacy-fir-tests/tests-gen/org/jetbrains/kotlin/fir/LazyBodyIsNotTouchedTilContractsPhaseTestGenerated.java b/compiler/fir/analysis-tests/legacy-fir-tests/tests-gen/org/jetbrains/kotlin/fir/LazyBodyIsNotTouchedTilContractsPhaseTestGenerated.java index ad722c0ced1..4ec2d348fcc 100644 --- a/compiler/fir/analysis-tests/legacy-fir-tests/tests-gen/org/jetbrains/kotlin/fir/LazyBodyIsNotTouchedTilContractsPhaseTestGenerated.java +++ b/compiler/fir/analysis-tests/legacy-fir-tests/tests-gen/org/jetbrains/kotlin/fir/LazyBodyIsNotTouchedTilContractsPhaseTestGenerated.java @@ -3644,4 +3644,22 @@ public class LazyBodyIsNotTouchedTilContractsPhaseTestGenerated extends Abstract runTest("compiler/fir/analysis-tests/testData/resolve/visibility/visibilityWithOverrides.kt"); } } + + @TestMetadata("compiler/fir/analysis-tests/testData/resolve/withAllowedKotlinPackage") + @TestDataPath("$PROJECT_ROOT") + @RunWith(JUnit3RunnerWithInners.class) + public static class WithAllowedKotlinPackage extends AbstractLazyBodyIsNotTouchedTilContractsPhaseTest { + private void runTest(String testDataFilePath) throws Exception { + KotlinTestUtils.runTest(this::doTest, this, testDataFilePath); + } + + public void testAllFilesPresentInWithAllowedKotlinPackage() throws Exception { + KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/fir/analysis-tests/testData/resolve/withAllowedKotlinPackage"), Pattern.compile("^([^.]+)\\.kt$"), null, true); + } + + @TestMetadata("extensionFunctionAddedToStdlib.kt") + public void testExtensionFunctionAddedToStdlib() throws Exception { + runTest("compiler/fir/analysis-tests/testData/resolve/withAllowedKotlinPackage/extensionFunctionAddedToStdlib.kt"); + } + } } diff --git a/compiler/fir/analysis-tests/testData/resolve/withAllowedKotlinPackage/extensionFunctionAddedToStdlib.fir.txt b/compiler/fir/analysis-tests/testData/resolve/withAllowedKotlinPackage/extensionFunctionAddedToStdlib.fir.txt new file mode 100644 index 00000000000..4f258a85603 --- /dev/null +++ b/compiler/fir/analysis-tests/testData/resolve/withAllowedKotlinPackage/extensionFunctionAddedToStdlib.fir.txt @@ -0,0 +1,24 @@ +FILE: main.kt + package test + + public final class TestClass : R|kotlin/Any| { + public constructor(): R|test/TestClass| { + super() + } + + public final fun test(): R|kotlin/Unit| { + #() + } + + } +FILE: stdlibExt.kt + package kotlin + + public final class MyClass : R|kotlin/Any| { + public constructor(): R|kotlin/MyClass| { + super() + } + + } + public final fun R|kotlin/MyClass|.extensionFun(): R|kotlin/Unit| { + } diff --git a/compiler/fir/analysis-tests/testData/resolve/withAllowedKotlinPackage/extensionFunctionAddedToStdlib.kt b/compiler/fir/analysis-tests/testData/resolve/withAllowedKotlinPackage/extensionFunctionAddedToStdlib.kt new file mode 100644 index 00000000000..6526629a815 --- /dev/null +++ b/compiler/fir/analysis-tests/testData/resolve/withAllowedKotlinPackage/extensionFunctionAddedToStdlib.kt @@ -0,0 +1,15 @@ +// FILE: main.kt +package test + +class TestClass { + fun test() { + extensionFun() + } +} + +// FILE: stdlibExt.kt +package kotlin + +class MyClass + +fun MyClass.extensionFun() {} diff --git a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirDiagnosticTestGenerated.java b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirDiagnosticTestGenerated.java index 307cac60e79..f09a039f0b6 100644 --- a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirDiagnosticTestGenerated.java +++ b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirDiagnosticTestGenerated.java @@ -4073,6 +4073,22 @@ public class FirDiagnosticTestGenerated extends AbstractFirDiagnosticTest { runTest("compiler/fir/analysis-tests/testData/resolve/visibility/visibilityWithOverrides.kt"); } } + + @Nested + @TestMetadata("compiler/fir/analysis-tests/testData/resolve/withAllowedKotlinPackage") + @TestDataPath("$PROJECT_ROOT") + public class WithAllowedKotlinPackage { + @Test + public void testAllFilesPresentInWithAllowedKotlinPackage() throws Exception { + KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/fir/analysis-tests/testData/resolve/withAllowedKotlinPackage"), Pattern.compile("^([^.]+)\\.kt$"), null, true); + } + + @Test + @TestMetadata("extensionFunctionAddedToStdlib.kt") + public void testExtensionFunctionAddedToStdlib() throws Exception { + runTest("compiler/fir/analysis-tests/testData/resolve/withAllowedKotlinPackage/extensionFunctionAddedToStdlib.kt"); + } + } } @Nested diff --git a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirDiagnosticsWithLightTreeTestGenerated.java b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirDiagnosticsWithLightTreeTestGenerated.java index 2c4e61877fa..9408754e207 100644 --- a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirDiagnosticsWithLightTreeTestGenerated.java +++ b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirDiagnosticsWithLightTreeTestGenerated.java @@ -4073,6 +4073,22 @@ public class FirDiagnosticsWithLightTreeTestGenerated extends AbstractFirDiagnos runTest("compiler/fir/analysis-tests/testData/resolve/visibility/visibilityWithOverrides.kt"); } } + + @Nested + @TestMetadata("compiler/fir/analysis-tests/testData/resolve/withAllowedKotlinPackage") + @TestDataPath("$PROJECT_ROOT") + public class WithAllowedKotlinPackage { + @Test + public void testAllFilesPresentInWithAllowedKotlinPackage() throws Exception { + KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/fir/analysis-tests/testData/resolve/withAllowedKotlinPackage"), Pattern.compile("^([^.]+)\\.kt$"), null, true); + } + + @Test + @TestMetadata("extensionFunctionAddedToStdlib.kt") + public void testExtensionFunctionAddedToStdlib() throws Exception { + runTest("compiler/fir/analysis-tests/testData/resolve/withAllowedKotlinPackage/extensionFunctionAddedToStdlib.kt"); + } + } } @Nested diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/calls/tower/TowerLevels.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/calls/tower/TowerLevels.kt index 84224167707..0bb262aa38a 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/calls/tower/TowerLevels.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/calls/tower/TowerLevels.kt @@ -262,7 +262,7 @@ class ScopeTowerLevel( if (scope is FirDefaultStarImportingScope && extensionReceiver != null) { val extensionReceiverType = extensionReceiver.type if (extensionReceiverType is ConeClassLikeType) { - val declarationReceiverType = candidate.fir.receiverTypeRef?.coneType + val declarationReceiverType = candidate.resolvedReceiverTypeRef?.coneType if (declarationReceiverType is ConeClassLikeType) { if (!AbstractTypeChecker.isSubtypeOf( session.typeContext, diff --git a/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/runners/AbstractFirDiagnosticTest.kt b/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/runners/AbstractFirDiagnosticTest.kt index 7c636cb8f42..0083c6d57e3 100644 --- a/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/runners/AbstractFirDiagnosticTest.kt +++ b/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/runners/AbstractFirDiagnosticTest.kt @@ -19,6 +19,7 @@ import org.jetbrains.kotlin.test.directives.JvmEnvironmentConfigurationDirective import org.jetbrains.kotlin.test.directives.JvmEnvironmentConfigurationDirectives.WITH_REFLECT import org.jetbrains.kotlin.test.directives.ConfigurationDirectives.WITH_STDLIB import org.jetbrains.kotlin.test.directives.LanguageSettingsDirectives +import org.jetbrains.kotlin.test.directives.LanguageSettingsDirectives.ALLOW_KOTLIN_PACKAGE import org.jetbrains.kotlin.test.directives.LanguageSettingsDirectives.LANGUAGE import org.jetbrains.kotlin.test.frontend.fir.FirFailingTestSuppressor import org.jetbrains.kotlin.test.frontend.fir.FirFrontendFacade @@ -100,6 +101,12 @@ fun TestConfigurationBuilder.baseFirDiagnosticTestConfiguration( } } + forTestsMatching("compiler/fir/analysis-tests/testData/resolve/withAllowedKotlinPackage/*") { + defaultDirectives { + +ALLOW_KOTLIN_PACKAGE + } + } + forTestsMatching( "compiler/testData/diagnostics/testsWithStdLib/*" or "compiler/fir/analysis-tests/testData/resolveWithStdlib/*" or