From 5e86cbe004a43af32a4f0dcb4ca335f7446cf525 Mon Sep 17 00:00:00 2001 From: Evgeny Gerashchenko Date: Tue, 4 Dec 2012 16:01:34 +0400 Subject: [PATCH] Checking for return type in kotlin signature if have super methods. --- .../AlternativeMethodSignatureData.java | 24 ++++++++++++++----- .../return/CantMakeImmutableInSubclass.java | 21 ++++++++++++++++ .../return/CantMakeImmutableInSubclass.kt | 12 ++++++++++ .../return/CantMakeImmutableInSubclass.txt | 10 ++++++++ .../jvm/compiler/LoadJavaTestGenerated.java | 5 ++++ ...esolveNamespaceComparingTestGenerated.java | 5 ++++ 6 files changed, 71 insertions(+), 6 deletions(-) create mode 100644 compiler/testData/loadJava/kotlinSignature/propagation/return/CantMakeImmutableInSubclass.java create mode 100644 compiler/testData/loadJava/kotlinSignature/propagation/return/CantMakeImmutableInSubclass.kt create mode 100644 compiler/testData/loadJava/kotlinSignature/propagation/return/CantMakeImmutableInSubclass.txt diff --git a/compiler/frontend.java/src/org/jetbrains/jet/lang/resolve/java/kotlinSignature/AlternativeMethodSignatureData.java b/compiler/frontend.java/src/org/jetbrains/jet/lang/resolve/java/kotlinSignature/AlternativeMethodSignatureData.java index b7d952015ef..3c11d45344c 100644 --- a/compiler/frontend.java/src/org/jetbrains/jet/lang/resolve/java/kotlinSignature/AlternativeMethodSignatureData.java +++ b/compiler/frontend.java/src/org/jetbrains/jet/lang/resolve/java/kotlinSignature/AlternativeMethodSignatureData.java @@ -30,6 +30,7 @@ import org.jetbrains.jet.lang.resolve.java.JavaDescriptorResolver; import org.jetbrains.jet.lang.resolve.java.wrapper.PsiMethodWrapper; import org.jetbrains.jet.lang.resolve.name.Name; import org.jetbrains.jet.lang.types.*; +import org.jetbrains.jet.lang.types.checker.JetTypeChecker; import org.jetbrains.jet.lang.types.lang.KotlinBuiltIns; import java.util.ArrayList; @@ -76,22 +77,23 @@ public class AlternativeMethodSignatureData extends ElementAlternativeSignatureD computeTypeParameters(methodTypeParameters); computeValueParameters(valueParameterDescriptors); - if (hasSuperMethods) { - checkSameParameterTypes(valueParameterDescriptors, methodTypeParameters); - } - if (originalReturnType != null) { altReturnType = computeReturnType(originalReturnType, altFunDeclaration.getReturnTypeRef(), originalToAltTypeParameters); } + + if (hasSuperMethods) { + checkParameterAndReturnTypesForOverridingMethods(valueParameterDescriptors, methodTypeParameters, originalReturnType); + } } catch (AlternativeSignatureMismatchException e) { setError(e.getMessage()); } } - private void checkSameParameterTypes( + private void checkParameterAndReturnTypesForOverridingMethods( @NotNull JavaDescriptorResolver.ValueParameterDescriptors valueParameterDescriptors, - @NotNull List methodTypeParameters + @NotNull List methodTypeParameters, + @Nullable JetType returnType ) { TypeSubstitutor substitutor = SignaturesUtil.createSubstitutorForFunctionTypeParameters(originalToAltTypeParameters); @@ -123,6 +125,16 @@ public class AlternativeMethodSignatureData extends ElementAlternativeSignatureD + altTypeParameters.get(index).getUpperBoundsAsType() + ", was: " + parameter.getUpperBoundsAsType()); } } + + if (returnType != null) { + JetType substitutedReturnType = substitutor.substitute(returnType, Variance.INVARIANT); + assert substitutedReturnType != null; + + if (!JetTypeChecker.INSTANCE.isSubtypeOf(altReturnType, substitutedReturnType)) { + throw new AlternativeSignatureMismatchException( + "Return type is changed to not subtype for method which overrides another: " + altReturnType + ", was: " + returnType); + } + } } @NotNull diff --git a/compiler/testData/loadJava/kotlinSignature/propagation/return/CantMakeImmutableInSubclass.java b/compiler/testData/loadJava/kotlinSignature/propagation/return/CantMakeImmutableInSubclass.java new file mode 100644 index 00000000000..0509407a64f --- /dev/null +++ b/compiler/testData/loadJava/kotlinSignature/propagation/return/CantMakeImmutableInSubclass.java @@ -0,0 +1,21 @@ +package test; + +import org.jetbrains.annotations.NotNull; +import java.util.*; + +import jet.runtime.typeinfo.KotlinSignature; +import org.jetbrains.jet.jvm.compiler.annotation.ExpectLoadError; + +public interface CantMakeImmutableInSubclass { + + public interface Super { + @KotlinSignature("fun foo(): MutableCollection") + Collection foo(); + } + + public interface Sub extends Super { + @ExpectLoadError("Return type is changed to not subtype for method which overrides another: List, was: MutableList") + @KotlinSignature("fun foo(): List") + List foo(); + } +} diff --git a/compiler/testData/loadJava/kotlinSignature/propagation/return/CantMakeImmutableInSubclass.kt b/compiler/testData/loadJava/kotlinSignature/propagation/return/CantMakeImmutableInSubclass.kt new file mode 100644 index 00000000000..33d3753b6ef --- /dev/null +++ b/compiler/testData/loadJava/kotlinSignature/propagation/return/CantMakeImmutableInSubclass.kt @@ -0,0 +1,12 @@ +package test + +public trait CantMakeImmutableInSubclass: Object { + + public trait Super: Object { + public fun foo(): MutableCollection + } + + public trait Sub: Super { + override fun foo(): MutableList + } +} diff --git a/compiler/testData/loadJava/kotlinSignature/propagation/return/CantMakeImmutableInSubclass.txt b/compiler/testData/loadJava/kotlinSignature/propagation/return/CantMakeImmutableInSubclass.txt new file mode 100644 index 00000000000..2fe543bb85b --- /dev/null +++ b/compiler/testData/loadJava/kotlinSignature/propagation/return/CantMakeImmutableInSubclass.txt @@ -0,0 +1,10 @@ +namespace test + +public abstract trait test.CantMakeImmutableInSubclass : java.lang.Object { + public abstract trait test.CantMakeImmutableInSubclass.Sub : test.CantMakeImmutableInSubclass.Super { + public abstract override /*1*/ fun foo(): jet.MutableList + } + public abstract trait test.CantMakeImmutableInSubclass.Super : java.lang.Object { + public abstract fun foo(): jet.MutableCollection + } +} diff --git a/compiler/tests/org/jetbrains/jet/jvm/compiler/LoadJavaTestGenerated.java b/compiler/tests/org/jetbrains/jet/jvm/compiler/LoadJavaTestGenerated.java index 59a2c2a5dc0..30f37b6c0fc 100644 --- a/compiler/tests/org/jetbrains/jet/jvm/compiler/LoadJavaTestGenerated.java +++ b/compiler/tests/org/jetbrains/jet/jvm/compiler/LoadJavaTestGenerated.java @@ -599,6 +599,11 @@ public class LoadJavaTestGenerated extends AbstractLoadJavaTest { JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("compiler/testData/loadJava/kotlinSignature/propagation/return"), "java", true); } + @TestMetadata("CantMakeImmutableInSubclass.java") + public void testCantMakeImmutableInSubclass() throws Exception { + doTest("compiler/testData/loadJava/kotlinSignature/propagation/return/CantMakeImmutableInSubclass.java"); + } + @TestMetadata("HalfSubstitutedTypeParameters.java") public void testHalfSubstitutedTypeParameters() throws Exception { doTest("compiler/testData/loadJava/kotlinSignature/propagation/return/HalfSubstitutedTypeParameters.java"); diff --git a/compiler/tests/org/jetbrains/jet/lang/resolve/lazy/LazyResolveNamespaceComparingTestGenerated.java b/compiler/tests/org/jetbrains/jet/lang/resolve/lazy/LazyResolveNamespaceComparingTestGenerated.java index 3602c8ae9ad..1d185bb4e9c 100644 --- a/compiler/tests/org/jetbrains/jet/lang/resolve/lazy/LazyResolveNamespaceComparingTestGenerated.java +++ b/compiler/tests/org/jetbrains/jet/lang/resolve/lazy/LazyResolveNamespaceComparingTestGenerated.java @@ -1489,6 +1489,11 @@ public class LazyResolveNamespaceComparingTestGenerated extends AbstractLazyReso JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("compiler/testData/loadJava/kotlinSignature/propagation/return"), "kt", true); } + @TestMetadata("CantMakeImmutableInSubclass.kt") + public void testCantMakeImmutableInSubclass() throws Exception { + doTestSinglePackage("compiler/testData/loadJava/kotlinSignature/propagation/return/CantMakeImmutableInSubclass.kt"); + } + @TestMetadata("HalfSubstitutedTypeParameters.kt") public void testHalfSubstitutedTypeParameters() throws Exception { doTestSinglePackage("compiler/testData/loadJava/kotlinSignature/propagation/return/HalfSubstitutedTypeParameters.kt");