Skip to content

Commit

Permalink
Fixing signature when overriding with erased signature.
Browse files Browse the repository at this point in the history
EA-43482 - ISE: JavaFunctionResolver.checkFunctionsOverrideCorrectly
  • Loading branch information
Evgeny Gerashchenko authored and Evgeny Gerashchenko committed Feb 19, 2013
1 parent cd06bde commit 48113f0
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 0 deletions.
Expand Up @@ -62,6 +62,7 @@ public class SignaturesPropagationData {
private final List<String> signatureErrors = Lists.newArrayList();
private final List<FunctionDescriptor> superFunctions;
private final Map<TypeParameterDescriptor, TypeParameterDescriptorImpl> autoTypeParameterToModified;
private final ClassDescriptor containingClass;

public SignaturesPropagationData(
@NotNull ClassDescriptor containingClass,
Expand All @@ -71,6 +72,7 @@ public SignaturesPropagationData(
@NotNull PsiMethodWrapper method,
@NotNull BindingTrace trace
) {
this.containingClass = containingClass;
superFunctions = getSuperFunctionsForMethod(method, trace, containingClass);

autoTypeParameterToModified = SignaturesUtil.recreateTypeParametersAndReturnMapping(autoTypeParameters);
Expand Down Expand Up @@ -617,6 +619,37 @@ else if (someSupersNotCovariantReadOnly || someSupersCovariantReadOnly) {
}
}

// Weird workaround for weird case. The sample code below is compiled by javac.
// In this case, we try to replace "Any" parameter type with "T" to fix substitution principle.
//
// public interface Super<T> {
// void foo(T t);
// }
//
// public interface Sub<T> extends Super<T> {
// void foo(Object o);
// }

List<TypeParameterDescriptor> typeParameterClassifiersFromSuper = Lists.newArrayList();
for (TypeAndVariance typeFromSuper : typesFromSuper) {
ClassifierDescriptor classifierFromSuper = typeFromSuper.type.getConstructor().getDeclarationDescriptor();
if (classifierFromSuper instanceof TypeParameterDescriptor) {
typeParameterClassifiersFromSuper.add((TypeParameterDescriptor) classifierFromSuper);
}
}

if (!typeParameterClassifiersFromSuper.isEmpty() && typeParameterClassifiersFromSuper.size() == typesFromSuper.size()) {
for (TypeParameterDescriptor typeParameter : typeParameterClassifiersFromSuper) {
if (typeParameter.getContainingDeclaration() != containingClass) {
continue;
}

return autoTypeParameterToModified.containsKey(typeParameter) ? autoTypeParameterToModified
.get(typeParameter) : typeParameter;

}
}

return classifier;
}

Expand Down
@@ -0,0 +1,12 @@
package test;

public interface OverrideWithErasedParameter {

public interface Super<T> {
void foo(T t);
}

public interface Sub<T> extends Super<T> {
void foo(Object o);
}
}
@@ -0,0 +1,12 @@
package test

public trait OverrideWithErasedParameter: Object {

public trait Super<T>: Object {
public fun foo(p0: T?)
}

public trait Sub<T>: Super<T> {
override fun foo(p0: T?)
}
}
@@ -0,0 +1,12 @@
package test

public trait OverrideWithErasedParameter : java.lang.Object {

public trait Sub</*0*/ T> : test.OverrideWithErasedParameter.Super<T> {
public abstract override /*1*/ fun foo(/*0*/ p0 : T?) : Unit
}

public trait Super</*0*/ T> : java.lang.Object {
public abstract fun foo(/*0*/ p0 : T?) : Unit
}
}
Expand Up @@ -599,6 +599,11 @@ public void testNullableToNotNullKotlinSignature() throws Exception {
doTest("compiler/testData/loadJava/kotlinSignature/propagation/parameter/NullableToNotNullKotlinSignature.java");
}

@TestMetadata("OverrideWithErasedParameter.java")
public void testOverrideWithErasedParameter() throws Exception {
doTest("compiler/testData/loadJava/kotlinSignature/propagation/parameter/OverrideWithErasedParameter.java");
}

@TestMetadata("ReadOnlyToMutable.java")
public void testReadOnlyToMutable() throws Exception {
doTest("compiler/testData/loadJava/kotlinSignature/propagation/parameter/ReadOnlyToMutable.java");
Expand Down
Expand Up @@ -1568,6 +1568,11 @@ public void testNullableToNotNullKotlinSignature() throws Exception {
doTestNotCheckingPrimaryConstructors("compiler/testData/loadJava/kotlinSignature/propagation/parameter/NullableToNotNullKotlinSignature.kt");
}

@TestMetadata("OverrideWithErasedParameter.kt")
public void testOverrideWithErasedParameter() throws Exception {
doTestNotCheckingPrimaryConstructors("compiler/testData/loadJava/kotlinSignature/propagation/parameter/OverrideWithErasedParameter.kt");
}

@TestMetadata("ReadOnlyToMutable.kt")
public void testReadOnlyToMutable() throws Exception {
doTestNotCheckingPrimaryConstructors("compiler/testData/loadJava/kotlinSignature/propagation/parameter/ReadOnlyToMutable.kt");
Expand Down

0 comments on commit 48113f0

Please sign in to comment.