From 4a9507b3abdfc199dfcc404e9167fb0a53a544be Mon Sep 17 00:00:00 2001 From: Dmitry Petrov Date: Mon, 23 May 2016 14:41:07 +0300 Subject: [PATCH] KT-11588 Type aliases Type alias companion object --- .../resolve/QualifiedExpressionResolver.kt | 1 + .../kotlin/resolve/calls/tower/TowerLevels.kt | 13 ++++-- .../tests/typealias/genericTypeAliasObject.kt | 25 +++++++++++ .../typealias/genericTypeAliasObject.txt | 37 ++++++++++++++++ .../tests/typealias/typeAliasObject.kt | 32 ++++++++++++++ .../tests/typealias/typeAliasObject.txt | 42 +++++++++++++++++++ .../checkers/DiagnosticsTestGenerated.java | 12 ++++++ 7 files changed, 159 insertions(+), 3 deletions(-) create mode 100644 compiler/testData/diagnostics/tests/typealias/genericTypeAliasObject.kt create mode 100644 compiler/testData/diagnostics/tests/typealias/genericTypeAliasObject.txt create mode 100644 compiler/testData/diagnostics/tests/typealias/typeAliasObject.kt create mode 100644 compiler/testData/diagnostics/tests/typealias/typeAliasObject.txt diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/QualifiedExpressionResolver.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/QualifiedExpressionResolver.kt index 76adcfe218e..98234dd6887 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/QualifiedExpressionResolver.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/QualifiedExpressionResolver.kt @@ -638,6 +638,7 @@ class QualifiedExpressionResolver(val symbolUsageValidator: SymbolUsageValidator is PackageViewDescriptor -> PackageQualifier(referenceExpression, descriptor) is ClassDescriptor -> ClassQualifier(referenceExpression, descriptor) is TypeParameterDescriptor -> TypeParameterQualifier(referenceExpression, descriptor) + is TypeAliasDescriptor -> ClassQualifier(referenceExpression, descriptor.classDescriptor ?: return null) else -> return null } 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 2752cef6a94..6380aea714f 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 @@ -243,9 +243,16 @@ private fun ResolutionScope.getContributedVariablesAndObjects(name: Name, locati private fun getFakeDescriptorForObject(classifier: ClassifierDescriptor?): FakeCallableDescriptorForObject? { - if (classifier !is ClassDescriptor || !classifier.hasClassValueDescriptor) return null // todo - - return FakeCallableDescriptorForObject(classifier) + return when (classifier) { + is TypeAliasDescriptor -> + getFakeDescriptorForObject(classifier.classDescriptor) + is ClassDescriptor -> + if (classifier.hasClassValueDescriptor) + FakeCallableDescriptorForObject(classifier) + else + null + else -> null + } } private fun getClassWithConstructors(classifier: ClassifierDescriptor?): ClassDescriptor? { diff --git a/compiler/testData/diagnostics/tests/typealias/genericTypeAliasObject.kt b/compiler/testData/diagnostics/tests/typealias/genericTypeAliasObject.kt new file mode 100644 index 00000000000..fe4d61cd67c --- /dev/null +++ b/compiler/testData/diagnostics/tests/typealias/genericTypeAliasObject.kt @@ -0,0 +1,25 @@ +object AnObject { + val ok = "OK" + fun foo() = "OK" +} + +typealias GenericTestObject = AnObject + +val test11: AnObject = GenericTestObject +val test12: GenericTestObject<*> = GenericTestObject +val test13: String = GenericTestObject.ok +val test14: String = GenericTestObject.foo() + +class GenericClassWithCompanion { + companion object { + val ok = "OK" + fun foo() = "OK" + } +} + +typealias TestGCWC = GenericClassWithCompanion + +val test25: GenericClassWithCompanion.Companion = TestGCWC +val test26 = TestGCWC +val test27: String = TestGCWC.ok +val test28: String = TestGCWC.foo() diff --git a/compiler/testData/diagnostics/tests/typealias/genericTypeAliasObject.txt b/compiler/testData/diagnostics/tests/typealias/genericTypeAliasObject.txt new file mode 100644 index 00000000000..3f60329f87b --- /dev/null +++ b/compiler/testData/diagnostics/tests/typealias/genericTypeAliasObject.txt @@ -0,0 +1,37 @@ +package + +public typealias GenericTestObject = AnObject +public typealias TestGCWC = GenericClassWithCompanion +public val test11: AnObject +public val test12: GenericTestObject<*> [= AnObject] +public val test13: kotlin.String = "OK" +public val test14: kotlin.String +public val test25: GenericClassWithCompanion.Companion +public val test26: GenericClassWithCompanion.Companion +public val test27: kotlin.String = "OK" +public val test28: kotlin.String + +public object AnObject { + private constructor AnObject() + public final val ok: kotlin.String = "OK" + public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean + public final fun foo(): kotlin.String + public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int + public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String +} + +public final class GenericClassWithCompanion { + public constructor GenericClassWithCompanion() + 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 companion object Companion { + private constructor Companion() + public final val ok: kotlin.String = "OK" + public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean + public final fun foo(): kotlin.String + public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int + public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String + } +} diff --git a/compiler/testData/diagnostics/tests/typealias/typeAliasObject.kt b/compiler/testData/diagnostics/tests/typealias/typeAliasObject.kt new file mode 100644 index 00000000000..db2a3b2a800 --- /dev/null +++ b/compiler/testData/diagnostics/tests/typealias/typeAliasObject.kt @@ -0,0 +1,32 @@ +object AnObject { + val ok = "OK" + fun foo() = "OK" +} + +typealias TestObject = AnObject + +val test11: AnObject = TestObject +val test12: TestObject = TestObject +val test13: String = TestObject.ok +val test14: String = TestObject.foo() + +typealias TestObject2 = TestObject + +val test21: AnObject = TestObject2 +val test22: TestObject2 = TestObject2 +val test23: String = TestObject2.ok +val test24: String = TestObject2.foo() + +class ClassWithCompanion { + companion object { + val ok = "OK" + fun foo() = "OK" + } +} + +typealias TestCWC = ClassWithCompanion + +val test35: ClassWithCompanion.Companion = TestCWC +val test36 = TestCWC +val test37: String = TestCWC.ok +val test38: String = TestCWC.foo() diff --git a/compiler/testData/diagnostics/tests/typealias/typeAliasObject.txt b/compiler/testData/diagnostics/tests/typealias/typeAliasObject.txt new file mode 100644 index 00000000000..cba213d4aaf --- /dev/null +++ b/compiler/testData/diagnostics/tests/typealias/typeAliasObject.txt @@ -0,0 +1,42 @@ +package + +public typealias TestCWC = ClassWithCompanion +public typealias TestObject = AnObject +public typealias TestObject2 = TestObject +public val test11: AnObject +public val test12: TestObject [= AnObject] +public val test13: kotlin.String = "OK" +public val test14: kotlin.String +public val test21: AnObject +public val test22: TestObject2 [= AnObject] +public val test23: kotlin.String = "OK" +public val test24: kotlin.String +public val test35: ClassWithCompanion.Companion +public val test36: ClassWithCompanion.Companion +public val test37: kotlin.String = "OK" +public val test38: kotlin.String + +public object AnObject { + private constructor AnObject() + public final val ok: kotlin.String = "OK" + public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean + public final fun foo(): kotlin.String + public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int + public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String +} + +public final class ClassWithCompanion { + public constructor ClassWithCompanion() + 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 companion object Companion { + private constructor Companion() + public final val ok: kotlin.String = "OK" + public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean + public final fun foo(): kotlin.String + public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int + public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String + } +} diff --git a/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java index 0245d0bc272..f4bc3bb8c1d 100644 --- a/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java @@ -19131,6 +19131,12 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTest { KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/diagnostics/tests/typealias"), Pattern.compile("^(.+)\\.kt$"), true); } + @TestMetadata("genericTypeAliasObject.kt") + public void testGenericTypeAliasObject() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/typealias/genericTypeAliasObject.kt"); + doTest(fileName); + } + @TestMetadata("inSupertypesList.kt") public void testInSupertypesList() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/typealias/inSupertypesList.kt"); @@ -19166,6 +19172,12 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTest { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/typealias/substitutionVariance.kt"); doTest(fileName); } + + @TestMetadata("typeAliasObject.kt") + public void testTypeAliasObject() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/typealias/typeAliasObject.kt"); + doTest(fileName); + } } @TestMetadata("compiler/testData/diagnostics/tests/unit")