Disconnect loops between upper bounds of type parameters

#KT-9759 Fixed
This commit is contained in:
Denis Zharkov
2015-10-27 16:06:32 +03:00
parent 9b4ad1466a
commit b6f724cf58
25 changed files with 279 additions and 51 deletions
@@ -416,7 +416,7 @@ public class DefaultErrorMessages {
MAP.put(TYPE_MISMATCH_IN_RANGE, "Type mismatch: incompatible types of range and element checked in it");
MAP.put(CYCLIC_INHERITANCE_HIERARCHY, "There's a cycle in the inheritance hierarchy for this type");
MAP.put(CYCLIC_GENERIC_UPPER_BOUND, "Type parameter has itself as an upper bound");
MAP.put(CYCLIC_GENERIC_UPPER_BOUND, "Type parameter has cyclic upper bounds");
MAP.put(MANY_CLASSES_IN_SUPERTYPE_LIST, "Only one class may appear in a supertype list");
MAP.put(SUPERTYPE_NOT_A_CLASS_OR_INTERFACE, "Only classes and interfaces may serve as supertypes");
@@ -23,6 +23,7 @@ import com.intellij.psi.PsiElement;
import kotlin.CollectionsKt;
import kotlin.SetsKt;
import kotlin.jvm.functions.Function0;
import kotlin.jvm.functions.Function1;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.kotlin.builtins.KotlinBuiltIns;
@@ -410,9 +411,9 @@ public class DescriptorResolver {
DeclarationDescriptor containingDescriptor,
LexicalWritableScope extensibleScope,
LexicalScope scopeForAnnotationsResolve,
KtTypeParameter typeParameter,
final KtTypeParameter typeParameter,
int index,
BindingTrace trace
final BindingTrace trace
) {
if (typeParameter.getVariance() != Variance.INVARIANT) {
assert !(containingDescriptor instanceof ClassifierDescriptor) : "This method is intended for functions/properties";
@@ -429,8 +430,16 @@ public class DescriptorResolver {
typeParameter.getVariance(),
KtPsiUtil.safeName(typeParameter.getName()),
index,
KotlinSourceElementKt.toSourceElement(typeParameter)
);
KotlinSourceElementKt.toSourceElement(typeParameter),
new Function1<KotlinType, Void>() {
@Override
public Void invoke(KotlinType type) {
trace.report(Errors.CYCLIC_GENERIC_UPPER_BOUND.on(typeParameter));
return null;
}
},
supertypeLoopsResolver
);
trace.record(BindingContext.TYPE_PARAMETER, typeParameter, typeParameterDescriptor);
extensibleScope.addClassifierDescriptor(typeParameterDescriptor);
return typeParameterDescriptor;
@@ -461,20 +470,6 @@ public class DescriptorResolver {
}
}
public KotlinType resolveTypeParameterExtendsBound(
@NotNull TypeParameterDescriptor typeParameterDescriptor,
@NotNull KtTypeReference extendsBound,
LexicalScope scope,
BindingTrace trace
) {
KotlinType type = typeResolver.resolveType(scope, extendsBound, trace, false);
if (type.getConstructor().equals(typeParameterDescriptor.getTypeConstructor())) {
trace.report(Errors.CYCLIC_GENERIC_UPPER_BOUND.on(extendsBound));
type = ErrorUtils.createErrorType("Cyclic upper bound: " + type);
}
return type;
}
public void resolveGenericBounds(
@NotNull KtTypeParameterListOwner declaration,
@NotNull DeclarationDescriptor descriptor,
@@ -494,7 +489,7 @@ public class DescriptorResolver {
KtTypeReference extendsBound = jetTypeParameter.getExtendsBound();
if (extendsBound != null) {
KotlinType type = resolveTypeParameterExtendsBound(typeParameterDescriptor, extendsBound, scope, trace);
KotlinType type = typeResolver.resolveType(scope, extendsBound, trace, false);
typeParameterDescriptor.addUpperBound(type);
deferredUpperBoundCheckerTasks.add(new UpperBoundCheckerTask(extendsBound, type));
}
@@ -523,9 +518,10 @@ public class DescriptorResolver {
for (TypeParameterDescriptorImpl parameter : parameters) {
parameter.addDefaultUpperBound();
parameter.setInitialized();
}
for (TypeParameterDescriptorImpl parameter : parameters) {
checkConflictingUpperBounds(trace, parameter, typeParameters.get(parameter.getIndex()));
}
@@ -18,7 +18,9 @@ package org.jetbrains.kotlin.resolve.lazy.descriptors;
import kotlin.CollectionsKt;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.kotlin.descriptors.SupertypeLoopChecker;
import org.jetbrains.kotlin.descriptors.impl.AbstractLazyTypeParameterDescriptor;
import org.jetbrains.kotlin.diagnostics.Errors;
import org.jetbrains.kotlin.lexer.KtTokens;
import org.jetbrains.kotlin.psi.*;
import org.jetbrains.kotlin.resolve.BindingContext;
@@ -58,6 +60,22 @@ public class LazyTypeParameterDescriptor extends AbstractLazyTypeParameterDescri
this.c.getTrace().record(BindingContext.TYPE_PARAMETER, typeParameter, this);
}
@NotNull
@Override
protected SupertypeLoopChecker getSupertypeLoopChecker() {
return c.getSupertypeLoopChecker();
}
@Override
protected void reportCycleError(@NotNull KotlinType type) {
for (KtTypeReference typeReference : getAllUpperBounds()) {
if (resolveBoundType(typeReference).getConstructor().equals(type.getConstructor())) {
c.getTrace().report(Errors.CYCLIC_GENERIC_UPPER_BOUND.on(typeReference));
return;
}
}
}
@NotNull
@Override
protected List<KotlinType> resolveUpperBounds() {
@@ -0,0 +1,7 @@
fun <<!CYCLIC_GENERIC_UPPER_BOUND!>T : F?<!>, F : T?> foo1() {}
fun <T : F?, <!CYCLIC_GENERIC_UPPER_BOUND!>F : E<!>, E : F?> foo2() {}
fun <<!CYCLIC_GENERIC_UPPER_BOUND!>T<!>, F> foo3() where T : F?, F : T {}
fun <T, <!CYCLIC_GENERIC_UPPER_BOUND!>F<!>, E> foo4() where T : F?, F : E, E : F? {}
@@ -0,0 +1,6 @@
package
public fun </*0*/ T : [ERROR : Cyclic upper bounds], /*1*/ F : T?> foo1(): kotlin.Unit
public fun </*0*/ T : F?, /*1*/ F : [ERROR : Cyclic upper bounds], /*2*/ E : F?> foo2(): kotlin.Unit
public fun </*0*/ T : [ERROR : Cyclic upper bounds], /*1*/ F : T> foo3(): kotlin.Unit
public fun </*0*/ T : F?, /*1*/ F : [ERROR : Cyclic upper bounds], /*2*/ E : F?> foo4(): kotlin.Unit
@@ -0,0 +1,7 @@
class A1<T : <!CYCLIC_GENERIC_UPPER_BOUND!>F?<!>, F : <!CYCLIC_GENERIC_UPPER_BOUND!>T?<!>>
class A2<T : F?, F : <!CYCLIC_GENERIC_UPPER_BOUND!>E<!>, E : <!CYCLIC_GENERIC_UPPER_BOUND!>F?<!>>
class A3<T, F> where T : <!CYCLIC_GENERIC_UPPER_BOUND!>F?<!>, F : <!CYCLIC_GENERIC_UPPER_BOUND!>T?<!>
class A4<T, F, E> where T : F?, F : <!CYCLIC_GENERIC_UPPER_BOUND!>E<!>, E : <!CYCLIC_GENERIC_UPPER_BOUND!>F<!>
@@ -0,0 +1,29 @@
package
public final class A1</*0*/ T : [ERROR : Cyclic upper bounds], /*1*/ F : [ERROR : Cyclic upper bounds]> {
public constructor A1</*0*/ T : [ERROR : Cyclic upper bounds], /*1*/ F : [ERROR : Cyclic upper bounds]>()
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 final class A2</*0*/ T : F?, /*1*/ F : [ERROR : Cyclic upper bounds], /*2*/ E : [ERROR : Cyclic upper bounds]> {
public constructor A2</*0*/ T : F?, /*1*/ F : [ERROR : Cyclic upper bounds], /*2*/ E : [ERROR : Cyclic upper bounds]>()
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 final class A3</*0*/ T : [ERROR : Cyclic upper bounds], /*1*/ F : [ERROR : Cyclic upper bounds]> {
public constructor A3</*0*/ T : [ERROR : Cyclic upper bounds], /*1*/ F : [ERROR : Cyclic upper bounds]>()
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 final class A4</*0*/ T : F?, /*1*/ F : [ERROR : Cyclic upper bounds], /*2*/ E : [ERROR : Cyclic upper bounds]> {
public constructor A4</*0*/ T : F?, /*1*/ F : [ERROR : Cyclic upper bounds], /*2*/ E : [ERROR : Cyclic upper bounds]>()
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
}
@@ -0,0 +1,8 @@
// FILE: XYZ.java
public interface XYZ<X extends X> {
XYZ foo() {}
}
// FILE: main.kt
fun main(xyz: XYZ<*>) = xyz.foo()
@@ -0,0 +1,11 @@
package
public /*synthesized*/ fun </*0*/ X : [ERROR : Cyclic upper bounds]> XYZ(/*0*/ function: () -> XYZ<(raw) [ERROR : Cyclic upper bounds]>!): XYZ<X>
public fun main(/*0*/ xyz: XYZ<*>): XYZ<(raw) [ERROR : Cyclic upper bounds]>!
public interface XYZ</*0*/ X : [ERROR : Cyclic upper bounds]> {
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public abstract fun foo(): XYZ<(raw) [ERROR : Cyclic upper bounds]>!
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
@@ -0,0 +1,8 @@
// FILE: XYZ.java
public interface XYZ<X extends Y, Y extends Z, Z extends Y> {
XYZ foo() {}
}
// FILE: main.kt
fun main(xyz: XYZ<*, *, *>) = xyz.foo()
@@ -0,0 +1,11 @@
package
public /*synthesized*/ fun </*0*/ X : Y!, /*1*/ Y : [ERROR : Cyclic upper bounds], /*2*/ Z : [ERROR : Cyclic upper bounds]> XYZ(/*0*/ function: () -> XYZ<(raw) [ERROR : Cyclic upper bounds], (raw) [ERROR : Cyclic upper bounds], (raw) [ERROR : Cyclic upper bounds]>!): XYZ<X, Y, Z>
public fun main(/*0*/ xyz: XYZ<*, *, *>): XYZ<(raw) [ERROR : Cyclic upper bounds], (raw) [ERROR : Cyclic upper bounds], (raw) [ERROR : Cyclic upper bounds]>!
public interface XYZ</*0*/ X : Y!, /*1*/ Y : [ERROR : Cyclic upper bounds], /*2*/ Z : [ERROR : Cyclic upper bounds]> {
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public abstract fun foo(): XYZ<(raw) [ERROR : Cyclic upper bounds], (raw) [ERROR : Cyclic upper bounds], (raw) [ERROR : Cyclic upper bounds]>!
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
@@ -1,3 +1,3 @@
// !DIAGNOSTICS: -MUST_BE_INITIALIZED -TYPE_PARAMETER_OF_PROPERTY_NOT_USED_IN_RECEIVER
fun <T: <!CYCLIC_GENERIC_UPPER_BOUND!>T?<!>> foo() {}
val <T: <!CYCLIC_GENERIC_UPPER_BOUND!>T?<!>> prop
fun <<!CYCLIC_GENERIC_UPPER_BOUND!>T: T?<!>> foo() {}
val <<!CYCLIC_GENERIC_UPPER_BOUND!>T: T?<!>> prop
@@ -1,4 +1,4 @@
package
public val </*0*/ T : [ERROR : Cyclic upper bound: T?]> prop: [ERROR : No type, no body]
public fun </*0*/ T : [ERROR : Cyclic upper bound: T?]> foo(): kotlin.Unit
public val </*0*/ T : [ERROR : Cyclic upper bounds]> prop: [ERROR : No type, no body]
public fun </*0*/ T : [ERROR : Cyclic upper bounds]> foo(): kotlin.Unit
@@ -1,7 +1,7 @@
package
public final class MyClass</*0*/ T : [ERROR : Cyclic upper bound: T?]> {
public constructor MyClass</*0*/ T : [ERROR : Cyclic upper bound: T?]>()
public final class MyClass</*0*/ T : [ERROR : Cyclic upper bounds]> {
public constructor MyClass</*0*/ T : [ERROR : Cyclic upper bounds]>()
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
@@ -1,7 +1,7 @@
package
public final class MyClass</*0*/ T : [ERROR : Cyclic upper bound: T]> {
public constructor MyClass</*0*/ T : [ERROR : Cyclic upper bound: T]>()
public final class MyClass</*0*/ T : [ERROR : Cyclic upper bounds]> {
public constructor MyClass</*0*/ T : [ERROR : Cyclic upper bounds]>()
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
@@ -1,4 +1,4 @@
fun bar() {
fun <T: <!CYCLIC_GENERIC_UPPER_BOUND!>T?<!>> foo() {}
fun <<!CYCLIC_GENERIC_UPPER_BOUND!>T: T?<!>> foo() {}
<!TYPE_INFERENCE_NO_INFORMATION_FOR_PARAMETER!>foo<!>()
}
@@ -1,5 +1,5 @@
// !DIAGNOSTICS: -MUST_BE_INITIALIZED_OR_BE_ABSTRACT -TYPE_PARAMETER_OF_PROPERTY_NOT_USED_IN_RECEIVER
class My {
fun <T: <!CYCLIC_GENERIC_UPPER_BOUND!>T?<!>> foo() {}
val <T: <!CYCLIC_GENERIC_UPPER_BOUND!>T?<!>> prop: T
fun <<!CYCLIC_GENERIC_UPPER_BOUND!>T: T?<!>> foo() {}
val <<!CYCLIC_GENERIC_UPPER_BOUND!>T: T?<!>> prop: T
}
@@ -2,9 +2,9 @@ package
public final class My {
public constructor My()
public final val </*0*/ T : [ERROR : Cyclic upper bound: T?]> prop: [ERROR : ?]
public final val </*0*/ T : [ERROR : Cyclic upper bounds]> prop: [ERROR : ?]
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public final fun </*0*/ T : [ERROR : Cyclic upper bound: T?]> foo(): kotlin.Unit
public final fun </*0*/ T : [ERROR : Cyclic upper bounds]> foo(): kotlin.Unit
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
@@ -1,3 +1,3 @@
// !DIAGNOSTICS: -MUST_BE_INITIALIZED -TYPE_PARAMETER_OF_PROPERTY_NOT_USED_IN_RECEIVER
fun <T: <!CYCLIC_GENERIC_UPPER_BOUND!>T<!>> foo() {}
val <T: <!CYCLIC_GENERIC_UPPER_BOUND!>T?<!>> prop: T
fun <<!CYCLIC_GENERIC_UPPER_BOUND!>T: T<!>> foo() {}
val <<!CYCLIC_GENERIC_UPPER_BOUND!>T: T?<!>> prop: T
@@ -1,4 +1,4 @@
package
public val </*0*/ T : [ERROR : Cyclic upper bound: T?]> prop: [ERROR : ?]
public fun </*0*/ T : [ERROR : Cyclic upper bound: T]> foo(): kotlin.Unit
public val </*0*/ T : [ERROR : Cyclic upper bounds]> prop: [ERROR : ?]
public fun </*0*/ T : [ERROR : Cyclic upper bounds]> foo(): kotlin.Unit
@@ -6618,6 +6618,27 @@ public class JetDiagnosticsTestGenerated extends AbstractJetDiagnosticsTest {
doTest(fileName);
}
@TestMetadata("compiler/testData/diagnostics/tests/generics/cyclicBounds")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class CyclicBounds extends AbstractJetDiagnosticsTest {
public void testAllFilesPresentInCyclicBounds() throws Exception {
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/diagnostics/tests/generics/cyclicBounds"), Pattern.compile("^(.+)\\.kt$"), true);
}
@TestMetadata("functions.kt")
public void testFunctions() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/generics/cyclicBounds/functions.kt");
doTest(fileName);
}
@TestMetadata("inClass.kt")
public void testInClass() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/generics/cyclicBounds/inClass.kt");
doTest(fileName);
}
}
@TestMetadata("compiler/testData/diagnostics/tests/generics/nullability")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
@@ -9252,6 +9273,18 @@ public class JetDiagnosticsTestGenerated extends AbstractJetDiagnosticsTest {
doTest(fileName);
}
@TestMetadata("recursiveRawUpperBound2.kt")
public void testRecursiveRawUpperBound2() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/j+k/recursiveRawUpperBound2.kt");
doTest(fileName);
}
@TestMetadata("recursiveRawUpperBound3.kt")
public void testRecursiveRawUpperBound3() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/j+k/recursiveRawUpperBound3.kt");
doTest(fileName);
}
@TestMetadata("samInConstructorWithGenerics.kt")
public void testSamInConstructorWithGenerics() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/j+k/samInConstructorWithGenerics.kt");