From 426bddb54e49452383f5641589e93a9f86c98368 Mon Sep 17 00:00:00 2001 From: Alexander Udalov Date: Thu, 22 Oct 2015 22:57:34 +0300 Subject: [PATCH] Move OverridingUtil#getUpperBound to OverloadUtil, add tests on overloads --- .../jetbrains/kotlin/resolve/OverloadUtil.kt | 22 ++++++++++---- .../overload/TypeParameterMultipleBounds.kt | 15 ++++++++++ .../overload/TypeParameterMultipleBounds.txt | 29 +++++++++++++++++++ .../typeParameterWithTwoBounds.kt | 8 +++++ .../typeParameterWithTwoBounds.txt | 17 +++++++++++ .../checkers/JetDiagnosticsTestGenerated.java | 12 ++++++++ .../kotlin/resolve/OverridingUtil.java | 12 -------- 7 files changed, 98 insertions(+), 17 deletions(-) create mode 100644 compiler/testData/diagnostics/tests/overload/TypeParameterMultipleBounds.kt create mode 100644 compiler/testData/diagnostics/tests/overload/TypeParameterMultipleBounds.txt create mode 100644 compiler/testData/diagnostics/tests/redeclarations/typeParameterWithTwoBounds.kt create mode 100644 compiler/testData/diagnostics/tests/redeclarations/typeParameterWithTwoBounds.txt diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/OverloadUtil.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/OverloadUtil.kt index 58183a6de37..5bd58d5f5fe 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/OverloadUtil.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/OverloadUtil.kt @@ -21,8 +21,9 @@ import org.jetbrains.kotlin.descriptors.* import org.jetbrains.kotlin.incremental.components.NoLookupLocation import org.jetbrains.kotlin.name.FqNameUnsafe import org.jetbrains.kotlin.name.Name -import org.jetbrains.kotlin.resolve.OverridingUtil.OverrideCompatibilityInfo.Result.* +import org.jetbrains.kotlin.resolve.OverridingUtil.OverrideCompatibilityInfo.Result.INCOMPATIBLE import org.jetbrains.kotlin.resolve.scopes.KtScope +import org.jetbrains.kotlin.types.KotlinType import org.jetbrains.kotlin.types.checker.KotlinTypeChecker import org.jetbrains.kotlin.types.oneMoreSpecificThanAnother @@ -47,11 +48,12 @@ object OverloadUtil { val aValueParameters = OverridingUtil.compiledValueParameters(a) val bValueParameters = OverridingUtil.compiledValueParameters(b) - for (i in aValueParameters.indices) { - val superValueParameterType = OverridingUtil.getUpperBound(aValueParameters[i]) - val subValueParameterType = OverridingUtil.getUpperBound(bValueParameters[i]) + for ((aType, bType) in aValueParameters.zip(bValueParameters)) { + // TODO: check type parameters, create a substitution and compare parameter types according to it, like in OverridingUtil + val superValueParameterType = aType.upperBound + val subValueParameterType = bType.upperBound if (!KotlinTypeChecker.DEFAULT.equalTypes(superValueParameterType, subValueParameterType) || - oneMoreSpecificThanAnother(subValueParameterType, superValueParameterType)) { + oneMoreSpecificThanAnother(subValueParameterType, superValueParameterType)) { return true } } @@ -67,6 +69,16 @@ object OverloadUtil { else -> throw IllegalStateException() } + private val KotlinType.upperBound: KotlinType + get() { + val classifier = constructor.declarationDescriptor + return when (classifier) { + is ClassDescriptor -> this + is TypeParameterDescriptor -> classifier.upperBoundsAsType + else -> error("Unknown type constructor: $this") + } + } + public @JvmStatic fun groupModulePackageMembersByFqName( c: BodiesResolveContext, constructorsInPackages: MultiMap diff --git a/compiler/testData/diagnostics/tests/overload/TypeParameterMultipleBounds.kt b/compiler/testData/diagnostics/tests/overload/TypeParameterMultipleBounds.kt new file mode 100644 index 00000000000..e6e1d985c79 --- /dev/null +++ b/compiler/testData/diagnostics/tests/overload/TypeParameterMultipleBounds.kt @@ -0,0 +1,15 @@ +import java.io.Serializable + +interface Test1 { + fun foo(t: T) where T : Cloneable, T : Serializable + fun foo(t: T) where T : Serializable, T : Cloneable +} + + +interface I1 +interface I2 : I1 + +interface Test2 { + fun foo(t: T) where T : I1, T : I2 + fun foo(t: T) where T : I2, T : I1 +} diff --git a/compiler/testData/diagnostics/tests/overload/TypeParameterMultipleBounds.txt b/compiler/testData/diagnostics/tests/overload/TypeParameterMultipleBounds.txt new file mode 100644 index 00000000000..f5152d3d120 --- /dev/null +++ b/compiler/testData/diagnostics/tests/overload/TypeParameterMultipleBounds.txt @@ -0,0 +1,29 @@ +package + +public interface I1 { + 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 interface I2 : I1 { + 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 interface Test1 { + public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean + public abstract fun foo(/*0*/ t: T): kotlin.Unit where T : java.io.Serializable + public abstract fun foo(/*0*/ t: T): kotlin.Unit where T : kotlin.Cloneable + public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int + public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String +} + +public interface Test2 { + public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean + public abstract fun foo(/*0*/ t: T): kotlin.Unit where T : I2 + public abstract fun foo(/*0*/ t: T): kotlin.Unit where T : I1 + 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/redeclarations/typeParameterWithTwoBounds.kt b/compiler/testData/diagnostics/tests/redeclarations/typeParameterWithTwoBounds.kt new file mode 100644 index 00000000000..3eba7faa832 --- /dev/null +++ b/compiler/testData/diagnostics/tests/redeclarations/typeParameterWithTwoBounds.kt @@ -0,0 +1,8 @@ +// !DIAGNOSTICS: -UNUSED_PARAMETER + +interface Foo +interface Bar + +fun foo(x: T): T where T: Foo, T: Bar {null!!} +fun foo(x: Foo): Foo {null!!} +fun foo(x: Bar): Bar {null!!} diff --git a/compiler/testData/diagnostics/tests/redeclarations/typeParameterWithTwoBounds.txt b/compiler/testData/diagnostics/tests/redeclarations/typeParameterWithTwoBounds.txt new file mode 100644 index 00000000000..da840e7e020 --- /dev/null +++ b/compiler/testData/diagnostics/tests/redeclarations/typeParameterWithTwoBounds.txt @@ -0,0 +1,17 @@ +package + +public fun foo(/*0*/ x: Bar): Bar +public fun foo(/*0*/ x: Foo): Foo +public fun foo(/*0*/ x: T): T where T : Bar + +public interface Bar { + 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 interface Foo { + 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/tests/org/jetbrains/kotlin/checkers/JetDiagnosticsTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/checkers/JetDiagnosticsTestGenerated.java index 81217a39da6..43e67460bce 100644 --- a/compiler/tests/org/jetbrains/kotlin/checkers/JetDiagnosticsTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/checkers/JetDiagnosticsTestGenerated.java @@ -10697,6 +10697,12 @@ public class JetDiagnosticsTestGenerated extends AbstractJetDiagnosticsTest { String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/overload/SyntheticAndNotSynthetic.kt"); doTest(fileName); } + + @TestMetadata("TypeParameterMultipleBounds.kt") + public void testTypeParameterMultipleBounds() throws Exception { + String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/overload/TypeParameterMultipleBounds.kt"); + doTest(fileName); + } } @TestMetadata("compiler/testData/diagnostics/tests/override") @@ -12017,6 +12023,12 @@ public class JetDiagnosticsTestGenerated extends AbstractJetDiagnosticsTest { String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/redeclarations/SingletonAndFunctionSameName.kt"); doTest(fileName); } + + @TestMetadata("typeParameterWithTwoBounds.kt") + public void testTypeParameterWithTwoBounds() throws Exception { + String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/redeclarations/typeParameterWithTwoBounds.kt"); + doTest(fileName); + } } @TestMetadata("compiler/testData/diagnostics/tests/reflection") diff --git a/core/descriptors/src/org/jetbrains/kotlin/resolve/OverridingUtil.java b/core/descriptors/src/org/jetbrains/kotlin/resolve/OverridingUtil.java index 4a4bd6a674f..2095209fba5 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/resolve/OverridingUtil.java +++ b/core/descriptors/src/org/jetbrains/kotlin/resolve/OverridingUtil.java @@ -237,18 +237,6 @@ public class OverridingUtil { return parameters; } - static KotlinType getUpperBound(KotlinType type) { - if (type.getConstructor().getDeclarationDescriptor() instanceof ClassDescriptor) { - return type; - } - else if (type.getConstructor().getDeclarationDescriptor() instanceof TypeParameterDescriptor) { - return ((TypeParameterDescriptor) type.getConstructor().getDeclarationDescriptor()).getUpperBoundsAsType(); - } - else { - throw new IllegalStateException("unknown type constructor: " + type.getConstructor().getClass().getName()); - } - } - public static void generateOverridesInFunctionGroup( @SuppressWarnings("UnusedParameters") @NotNull Name name, //DO NOT DELETE THIS PARAMETER: needed to make sure all descriptors have the same name