From 9cf8ef287e23ff5d2d56b857be2a685abaede268 Mon Sep 17 00:00:00 2001 From: Dmitry Petrov Date: Wed, 25 May 2016 15:38:45 +0300 Subject: [PATCH] KT-11588 Type aliases Additional tests for generic type alias arguments. Small refactoring in TowerLevels. --- .../kotlin/resolve/calls/tower/TowerLevels.kt | 32 +++++++++---------- .../typeAliasArgumentsInCompanionObject.kt | 10 ++++++ .../typeAliasArgumentsInCompanionObject.txt | 20 ++++++++++++ .../typeAliasArgumentsInConstructor.kt | 8 ++++- .../typeAliasArgumentsInConstructor.txt | 5 +++ .../checkers/DiagnosticsTestGenerated.java | 6 ++++ 6 files changed, 63 insertions(+), 18 deletions(-) create mode 100644 compiler/testData/diagnostics/tests/typealias/typeAliasArgumentsInCompanionObject.kt create mode 100644 compiler/testData/diagnostics/tests/typealias/typeAliasArgumentsInCompanionObject.txt 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 6f68820ae5b..91064e2c005 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 @@ -241,34 +241,32 @@ private fun ResolutionScope.getContributedVariablesAndObjects(name: Name, locati return getContributedVariables(name, location) + listOfNotNull(objectDescriptor) } - -private fun getFakeDescriptorForObject(classifier: ClassifierDescriptor?): FakeCallableDescriptorForObject? { - return when (classifier) { - is TypeAliasDescriptor -> - getFakeDescriptorForObject(classifier.classDescriptor) - is ClassDescriptor -> - if (classifier.hasClassValueDescriptor) - FakeCallableDescriptorForObject(classifier) - else - null - else -> null - } -} +private fun getFakeDescriptorForObject(classifier: ClassifierDescriptor?): FakeCallableDescriptorForObject? = + when (classifier) { + is TypeAliasDescriptor -> + getFakeDescriptorForObject(classifier.classDescriptor) + is ClassDescriptor -> + if (classifier.hasClassValueDescriptor) + FakeCallableDescriptorForObject(classifier) + else + null + else -> null + } private fun getClassWithConstructors(classifier: ClassifierDescriptor?): ClassDescriptor? = - if (classifier !is ClassDescriptor || !classifier.canHaveCallableConstructors()) + if (classifier !is ClassDescriptor || !classifier.canHaveCallableConstructors) null else classifier -private fun ClassDescriptor.canHaveCallableConstructors() = - !ErrorUtils.isError(this) && !kind.isSingleton +private val ClassDescriptor.canHaveCallableConstructors: Boolean + get() = !ErrorUtils.isError(this) && !kind.isSingleton private fun ClassifierDescriptor.getTypeAliasConstructors(inner: Boolean): Collection { if (this !is TypeAliasDescriptor) return emptyList() val classDescriptor = this.classDescriptor ?: return emptyList() - if (!classDescriptor.canHaveCallableConstructors()) return emptyList() + if (!classDescriptor.canHaveCallableConstructors) return emptyList() val substitutor = this.getTypeSubstitutorForUnderlyingClass() ?: throw AssertionError("classDescriptor should be non-null for $this") diff --git a/compiler/testData/diagnostics/tests/typealias/typeAliasArgumentsInCompanionObject.kt b/compiler/testData/diagnostics/tests/typealias/typeAliasArgumentsInCompanionObject.kt new file mode 100644 index 00000000000..efad685544c --- /dev/null +++ b/compiler/testData/diagnostics/tests/typealias/typeAliasArgumentsInCompanionObject.kt @@ -0,0 +1,10 @@ +class C { + companion object { + val OK = "OK" + } +} + +typealias C2 = C + +val test1: String = C2.OK +val test2: String = C2.OK diff --git a/compiler/testData/diagnostics/tests/typealias/typeAliasArgumentsInCompanionObject.txt b/compiler/testData/diagnostics/tests/typealias/typeAliasArgumentsInCompanionObject.txt new file mode 100644 index 00000000000..5f03c7f6530 --- /dev/null +++ b/compiler/testData/diagnostics/tests/typealias/typeAliasArgumentsInCompanionObject.txt @@ -0,0 +1,20 @@ +package + +public typealias C2 = C +public val test1: kotlin.String +public val test2: kotlin.String = "OK" + +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 + + 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 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/typeAliasArgumentsInConstructor.kt b/compiler/testData/diagnostics/tests/typealias/typeAliasArgumentsInConstructor.kt index 7f32a3cbe36..8945b180940 100644 --- a/compiler/testData/diagnostics/tests/typealias/typeAliasArgumentsInConstructor.kt +++ b/compiler/testData/diagnostics/tests/typealias/typeAliasArgumentsInConstructor.kt @@ -2,4 +2,10 @@ class Pair(val x1: T1, val x2: T2) typealias P2 = Pair -val test1 = P2("", "") \ No newline at end of file +val test1: Pair = P2("", "") +val test1x1: String = test1.x1 +val test1x2: String = test1.x2 + +val test2: P2 = P2("", "") +val test2x1: String = test2.x1 +val test2x2: String = test2.x2 diff --git a/compiler/testData/diagnostics/tests/typealias/typeAliasArgumentsInConstructor.txt b/compiler/testData/diagnostics/tests/typealias/typeAliasArgumentsInConstructor.txt index 78be3c1e82d..be1e2236d95 100644 --- a/compiler/testData/diagnostics/tests/typealias/typeAliasArgumentsInConstructor.txt +++ b/compiler/testData/diagnostics/tests/typealias/typeAliasArgumentsInConstructor.txt @@ -2,6 +2,11 @@ package public typealias P2 = Pair public val test1: Pair +public val test1x1: kotlin.String +public val test1x2: kotlin.String +public val test2: P2 [= Pair] +public val test2x1: kotlin.String +public val test2x2: kotlin.String public final class Pair { public constructor Pair(/*0*/ x1: T1, /*1*/ x2: T2) diff --git a/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java index f4b166aaafb..306e793458c 100644 --- a/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/checkers/DiagnosticsTestGenerated.java @@ -19179,6 +19179,12 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTest { doTest(fileName); } + @TestMetadata("typeAliasArgumentsInCompanionObject.kt") + public void testTypeAliasArgumentsInCompanionObject() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/typealias/typeAliasArgumentsInCompanionObject.kt"); + doTest(fileName); + } + @TestMetadata("typeAliasArgumentsInConstructor.kt") public void testTypeAliasArgumentsInConstructor() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/typealias/typeAliasArgumentsInConstructor.kt");