From 81774926fa4a9f9ba7f8c961be6f50a855832092 Mon Sep 17 00:00:00 2001 From: Alexander Udalov Date: Mon, 20 Mar 2017 14:41:23 +0300 Subject: [PATCH] Discriminate header class in resolution from sources in AbstractLazyMemberScope Similarly to getFirstClassifierDiscriminateHeaders, we select the first non-header class if possible, otherwise we select just the first class. This makes sure that a reference will never be resolved to a header class if the corresponding impl class is present. Note that overall the issue may not be fixed yet, because there are other scopes where header classes are not yet discriminated (LazyImportScope, DeserializedMemberScope). However, at this point I cannot reproduce this problem with these other scopes #KT-15521 Fixed --- .../lazy/descriptors/AbstractLazyMemberScope.kt | 12 +++++++++--- .../createImplClassInPlatformModule/common.kt | 1 + .../createImplClassInPlatformModule/jvm.kt | 7 +++++++ .../createImplClassInPlatformModule/output.txt | 7 +++++++ .../MultiPlatformIntegrationTestGenerated.java | 6 ++++++ 5 files changed, 30 insertions(+), 3 deletions(-) create mode 100644 compiler/testData/multiplatform/createImplClassInPlatformModule/common.kt create mode 100644 compiler/testData/multiplatform/createImplClassInPlatformModule/jvm.kt create mode 100644 compiler/testData/multiplatform/createImplClassInPlatformModule/output.txt diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/lazy/descriptors/AbstractLazyMemberScope.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/lazy/descriptors/AbstractLazyMemberScope.kt index 10650f3da99..fcf786e8137 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/lazy/descriptors/AbstractLazyMemberScope.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/lazy/descriptors/AbstractLazyMemberScope.kt @@ -66,9 +66,15 @@ protected constructor( override fun getContributedClassifier(name: Name, location: LookupLocation): ClassifierDescriptor? { recordLookup(name, location) // NB we should resolve type alias descriptors even if a class descriptor with corresponding name is present - val firstClassDescriptor = classDescriptors(name).firstOrNull() - val firstTypeAliasDescriptor = typeAliasDescriptors(name).firstOrNull() - return firstClassDescriptor ?: firstTypeAliasDescriptor + val classes = classDescriptors(name) + val typeAliases = typeAliasDescriptors(name) + var resultingClass: ClassDescriptor? = null + for (klass in classes) { + // See getFirstClassifierDiscriminateHeaders() + if (!klass.isHeader) return klass + if (resultingClass == null) resultingClass = klass + } + return resultingClass ?: typeAliases.firstOrNull() } override fun getContributedFunctions(name: Name, location: LookupLocation): Collection { diff --git a/compiler/testData/multiplatform/createImplClassInPlatformModule/common.kt b/compiler/testData/multiplatform/createImplClassInPlatformModule/common.kt new file mode 100644 index 00000000000..b7b0f1b26c6 --- /dev/null +++ b/compiler/testData/multiplatform/createImplClassInPlatformModule/common.kt @@ -0,0 +1 @@ +header class Foo diff --git a/compiler/testData/multiplatform/createImplClassInPlatformModule/jvm.kt b/compiler/testData/multiplatform/createImplClassInPlatformModule/jvm.kt new file mode 100644 index 00000000000..5abb848dd57 --- /dev/null +++ b/compiler/testData/multiplatform/createImplClassInPlatformModule/jvm.kt @@ -0,0 +1,7 @@ +impl class Foo(x: Int) { + impl constructor() : this(0) + + val x: Int = x +} + +val y = Foo(42).x diff --git a/compiler/testData/multiplatform/createImplClassInPlatformModule/output.txt b/compiler/testData/multiplatform/createImplClassInPlatformModule/output.txt new file mode 100644 index 00000000000..b08950d503d --- /dev/null +++ b/compiler/testData/multiplatform/createImplClassInPlatformModule/output.txt @@ -0,0 +1,7 @@ +-- Common -- +Exit code: OK +Output: + +-- JVM -- +Exit code: OK +Output: diff --git a/compiler/tests/org/jetbrains/kotlin/multiplatform/MultiPlatformIntegrationTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/multiplatform/MultiPlatformIntegrationTestGenerated.java index c79106a88c0..1884cdd59bc 100644 --- a/compiler/tests/org/jetbrains/kotlin/multiplatform/MultiPlatformIntegrationTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/multiplatform/MultiPlatformIntegrationTestGenerated.java @@ -42,6 +42,12 @@ public class MultiPlatformIntegrationTestGenerated extends AbstractMultiPlatform doTest(fileName); } + @TestMetadata("createImplClassInPlatformModule") + public void testCreateImplClassInPlatformModule() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/multiplatform/createImplClassInPlatformModule/"); + doTest(fileName); + } + @TestMetadata("genericDeclarations") public void testGenericDeclarations() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/multiplatform/genericDeclarations/");