From 34240e2ff922ab3f670cbe5c1a69412c3769d2a0 Mon Sep 17 00:00:00 2001 From: Dmitry Petrov Date: Fri, 16 Sep 2016 12:00:08 +0300 Subject: [PATCH] Create special descriptor for an object corresponding to type alias. --- .../kotlin/resolve/calls/CandidateResolver.kt | 2 +- .../kotlin/resolve/calls/tower/TowerLevels.kt | 8 ++++- .../util/FakeCallableDescriptorForObject.kt | 6 ++-- ...akeCallableDescriptorForTypeAliasObject.kt | 32 +++++++++++++++++++ .../typealias/typeAliasInvisibleObject.kt | 8 +++++ .../typealias/typeAliasInvisibleObject.txt | 19 +++++++++++ .../checkers/DiagnosticsTestGenerated.java | 6 ++++ .../js/translate/context/StaticContext.java | 2 +- 8 files changed, 78 insertions(+), 5 deletions(-) create mode 100644 compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/util/FakeCallableDescriptorForTypeAliasObject.kt create mode 100644 compiler/testData/diagnostics/tests/typealias/typeAliasInvisibleObject.kt create mode 100644 compiler/testData/diagnostics/tests/typealias/typeAliasInvisibleObject.txt diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/CandidateResolver.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/CandidateResolver.kt index f9402311eff..6e333965c58 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/CandidateResolver.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/CandidateResolver.kt @@ -246,7 +246,7 @@ class CandidateResolver( nestedClass = candidateDescriptor.containingDeclaration } else if (candidateDescriptor is FakeCallableDescriptorForObject) { - nestedClass = candidateDescriptor.getReferencedDescriptor() + nestedClass = candidateDescriptor.getReferencedObject() } if (nestedClass != null) { tracing.nestedClassAccessViaInstanceReference(trace, nestedClass, candidateCall.explicitReceiverKind) diff --git a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/tower/TowerLevels.kt b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/tower/TowerLevels.kt index 152dbec9212..a1b13721a8f 100644 --- a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/tower/TowerLevels.kt +++ b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/tower/TowerLevels.kt @@ -22,6 +22,7 @@ import org.jetbrains.kotlin.incremental.components.LookupLocation import org.jetbrains.kotlin.name.Name import org.jetbrains.kotlin.resolve.calls.smartcasts.getReceiverValueWithSmartCast import org.jetbrains.kotlin.resolve.calls.util.FakeCallableDescriptorForObject +import org.jetbrains.kotlin.resolve.calls.util.FakeCallableDescriptorForTypeAliasObject import org.jetbrains.kotlin.resolve.coroutine.CoroutineReceiverValue import org.jetbrains.kotlin.resolve.coroutine.createCoroutineSuspensionFunctionView import org.jetbrains.kotlin.resolve.descriptorUtil.HIDES_MEMBERS_NAME_LIST @@ -273,7 +274,12 @@ private fun ResolutionScope.getContributedObjectVariables(name: Name, location: fun getFakeDescriptorForObject(classifier: ClassifierDescriptor?): FakeCallableDescriptorForObject? = when (classifier) { is TypeAliasDescriptor -> - getFakeDescriptorForObject(classifier.classDescriptor) + classifier.classDescriptor?.let { classDescriptor -> + if (classDescriptor.hasClassValueDescriptor) + FakeCallableDescriptorForTypeAliasObject(classifier) + else + null + } is ClassDescriptor -> if (classifier.hasClassValueDescriptor) FakeCallableDescriptorForObject(classifier) diff --git a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/util/FakeCallableDescriptorForObject.kt b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/util/FakeCallableDescriptorForObject.kt index a53540cab7f..5f19caa5d3f 100644 --- a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/util/FakeCallableDescriptorForObject.kt +++ b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/util/FakeCallableDescriptorForObject.kt @@ -23,7 +23,7 @@ import org.jetbrains.kotlin.resolve.descriptorUtil.hasClassValueDescriptor import org.jetbrains.kotlin.types.KotlinType import java.util.* -class FakeCallableDescriptorForObject( +open class FakeCallableDescriptorForObject( val classDescriptor: ClassDescriptor ) : DeclarationDescriptorWithVisibility by classDescriptor.getClassObjectReferenceTarget(), VariableDescriptor { @@ -34,7 +34,9 @@ class FakeCallableDescriptorForObject( } - fun getReferencedDescriptor(): ClassDescriptor = classDescriptor.getClassObjectReferenceTarget() + open fun getReferencedDescriptor(): ClassifierDescriptorWithTypeParameters = classDescriptor.getClassObjectReferenceTarget() + + fun getReferencedObject(): ClassDescriptor = classDescriptor.getClassObjectReferenceTarget() override fun getExtensionReceiverParameter(): ReceiverParameterDescriptor? = null diff --git a/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/util/FakeCallableDescriptorForTypeAliasObject.kt b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/util/FakeCallableDescriptorForTypeAliasObject.kt new file mode 100644 index 00000000000..66da11a0b54 --- /dev/null +++ b/compiler/resolution/src/org/jetbrains/kotlin/resolve/calls/util/FakeCallableDescriptorForTypeAliasObject.kt @@ -0,0 +1,32 @@ +/* + * Copyright 2010-2016 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jetbrains.kotlin.resolve.calls.util + +import org.jetbrains.kotlin.descriptors.TypeAliasDescriptor + +class FakeCallableDescriptorForTypeAliasObject(val typeAliasDescriptor: TypeAliasDescriptor) : + FakeCallableDescriptorForObject(typeAliasDescriptor.classDescriptor!!) { + override fun getReferencedDescriptor() = + typeAliasDescriptor + + override fun equals(other: Any?): Boolean = + other is FakeCallableDescriptorForTypeAliasObject && + typeAliasDescriptor == other.typeAliasDescriptor + + override fun hashCode(): Int = + typeAliasDescriptor.hashCode() +} diff --git a/compiler/testData/diagnostics/tests/typealias/typeAliasInvisibleObject.kt b/compiler/testData/diagnostics/tests/typealias/typeAliasInvisibleObject.kt new file mode 100644 index 00000000000..068dcfd2de4 --- /dev/null +++ b/compiler/testData/diagnostics/tests/typealias/typeAliasInvisibleObject.kt @@ -0,0 +1,8 @@ +class C { + private companion object +} + +typealias CAlias = C + +val test1 = CAlias +val test1a = C \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/typealias/typeAliasInvisibleObject.txt b/compiler/testData/diagnostics/tests/typealias/typeAliasInvisibleObject.txt new file mode 100644 index 00000000000..0dc12c38d20 --- /dev/null +++ b/compiler/testData/diagnostics/tests/typealias/typeAliasInvisibleObject.txt @@ -0,0 +1,19 @@ +package + +public val test1: C.Companion +public val test1a: C.Companion + +public final class C { + public constructor C() + public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean + public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int + public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String + + private companion object Companion { + private constructor Companion() + public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean + public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int + public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String + } +} +public typealias CAlias = C diff --git a/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java index eeb363ccae6..73ef1db303e 100644 --- a/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java @@ -20397,6 +20397,12 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTest { doTest(fileName); } + @TestMetadata("typeAliasInvisibleObject.kt") + public void testTypeAliasInvisibleObject() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/typealias/typeAliasInvisibleObject.kt"); + doTest(fileName); + } + @TestMetadata("typeAliasNotNull.kt") public void testTypeAliasNotNull() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/typealias/typeAliasNotNull.kt"); diff --git a/js/js.translator/src/org/jetbrains/kotlin/js/translate/context/StaticContext.java b/js/js.translator/src/org/jetbrains/kotlin/js/translate/context/StaticContext.java index ae64bd56e91..c7295cc7f76 100644 --- a/js/js.translator/src/org/jetbrains/kotlin/js/translate/context/StaticContext.java +++ b/js/js.translator/src/org/jetbrains/kotlin/js/translate/context/StaticContext.java @@ -433,7 +433,7 @@ public final class StaticContext { } FakeCallableDescriptorForObject fakeCallableDescriptor = (FakeCallableDescriptorForObject) descriptor; - return getNameForDescriptor(fakeCallableDescriptor.getReferencedDescriptor()); + return getNameForDescriptor(fakeCallableDescriptor.getReferencedObject()); } };