Checking for return type in kotlin signature if have super methods.

This commit is contained in:
Evgeny Gerashchenko
2012-12-04 16:01:34 +04:00
parent 6975691e7a
commit 5e86cbe004
6 changed files with 71 additions and 6 deletions
@@ -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<TypeParameterDescriptor> methodTypeParameters
@NotNull List<TypeParameterDescriptor> 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
@@ -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<String>")
Collection<String> foo();
}
public interface Sub extends Super {
@ExpectLoadError("Return type is changed to not subtype for method which overrides another: List<String>, was: MutableList<String>")
@KotlinSignature("fun foo(): List<String>")
List<String> foo();
}
}
@@ -0,0 +1,12 @@
package test
public trait CantMakeImmutableInSubclass: Object {
public trait Super: Object {
public fun foo(): MutableCollection<String>
}
public trait Sub: Super {
override fun foo(): MutableList<String>
}
}
@@ -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<jet.String>
}
public abstract trait test.CantMakeImmutableInSubclass.Super : java.lang.Object {
public abstract fun foo(): jet.MutableCollection<jet.String>
}
}
@@ -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");
@@ -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");