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
This commit is contained in:
Roman Golyshev
2022-01-11 17:08:41 +03:00
committed by Space
parent 3ac472ceb3
commit 54cca88279
8 changed files with 113 additions and 1 deletions
@@ -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
@@ -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");
}
}
}
@@ -0,0 +1,24 @@
FILE: main.kt
package test
public final class TestClass : R|kotlin/Any| {
public constructor(): R|test/TestClass| {
super<R|kotlin/Any|>()
}
public final fun test(): R|kotlin/Unit| {
<Unresolved name: extensionFun>#()
}
}
FILE: stdlibExt.kt
package kotlin
public final class MyClass : R|kotlin/Any| {
public constructor(): R|kotlin/MyClass| {
super<R|kotlin/Any|>()
}
}
public final fun R|kotlin/MyClass|.extensionFun(): R|kotlin/Unit| {
}
@@ -0,0 +1,15 @@
// FILE: main.kt
package test
class TestClass {
fun test() {
<!UNRESOLVED_REFERENCE!>extensionFun<!>()
}
}
// FILE: stdlibExt.kt
package kotlin
class MyClass
fun MyClass.extensionFun() {}
@@ -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
@@ -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
@@ -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,
@@ -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