Report error on repeated upper bounds for type parameters

This commit is contained in:
Alexander Udalov
2015-11-20 23:06:24 +03:00
parent 21e64e02bd
commit 5fbb9bfe3b
16 changed files with 99 additions and 42 deletions
@@ -265,6 +265,7 @@ public interface Errors {
DiagnosticFactory0<KtTypeReference> DYNAMIC_UPPER_BOUND = DiagnosticFactory0.create(ERROR);
DiagnosticFactory0<KtTypeReference> UPPER_BOUND_IS_EXTENSION_FUNCTION_TYPE = DiagnosticFactory0.create(ERROR);
DiagnosticFactory0<KtTypeReference> ONLY_ONE_CLASS_BOUND_ALLOWED = DiagnosticFactory0.create(ERROR);
DiagnosticFactory0<KtTypeReference> REPEATED_BOUND = DiagnosticFactory0.create(ERROR);
DiagnosticFactory1<KtNamedDeclaration, TypeParameterDescriptor> CONFLICTING_UPPER_BOUNDS =
DiagnosticFactory1.create(ERROR, DECLARATION_NAME);
@@ -395,6 +395,7 @@ public class DefaultErrorMessages {
MAP.put(FINAL_UPPER_BOUND, "''{0}'' is a final type, and thus a value of the type parameter is predetermined", RENDER_TYPE);
MAP.put(UPPER_BOUND_IS_EXTENSION_FUNCTION_TYPE, "Extension function type can not be used as an upper bound");
MAP.put(ONLY_ONE_CLASS_BOUND_ALLOWED, "Only one of the upper bounds can be a class");
MAP.put(REPEATED_BOUND, "Type parameter already has this bound");
MAP.put(DYNAMIC_UPPER_BOUND, "Dynamic type can not be used as an upper bound");
MAP.put(USELESS_ELVIS, "Elvis operator (?:) always returns the left operand of non-nullable type {0}", RENDER_TYPE);
MAP.put(USELESS_ELVIS_ON_FUNCTION_LITERAL, "Left operand of elvis operator (?:) is function literal");
@@ -21,6 +21,7 @@ import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import com.intellij.psi.PsiElement;
import kotlin.CollectionsKt;
import kotlin.Pair;
import kotlin.SetsKt;
import kotlin.jvm.functions.Function0;
import kotlin.jvm.functions.Function1;
@@ -534,18 +535,25 @@ public class DescriptorResolver {
if (tasks.isEmpty()) return;
Set<Name> classBoundEncountered = new HashSet<Name>();
Set<Pair<Name, TypeConstructor>> allBounds = new HashSet<Pair<Name, TypeConstructor>>();
for (UpperBoundCheckerTask checkerTask : tasks) {
Name typeParameterName = checkerTask.typeParameterName;
KotlinType upperBound = checkerTask.upperBoundType;
KtTypeReference upperBoundElement = checkerTask.upperBound;
if (!upperBound.isError()) {
ClassDescriptor classDescriptor = TypeUtils.getClassDescriptor(upperBound);
if (classDescriptor != null) {
ClassKind kind = classDescriptor.getKind();
if (kind == ClassKind.CLASS || kind == ClassKind.ENUM_CLASS || kind == ClassKind.OBJECT) {
if (!classBoundEncountered.add(typeParameterName)) {
trace.report(ONLY_ONE_CLASS_BOUND_ALLOWED.on(upperBoundElement));
if (!allBounds.add(new Pair<Name, TypeConstructor>(typeParameterName, upperBound.getConstructor()))) {
trace.report(REPEATED_BOUND.on(upperBoundElement));
}
else {
ClassDescriptor classDescriptor = TypeUtils.getClassDescriptor(upperBound);
if (classDescriptor != null) {
ClassKind kind = classDescriptor.getKind();
if (kind == ClassKind.CLASS || kind == ClassKind.ENUM_CLASS || kind == ClassKind.OBJECT) {
if (!classBoundEncountered.add(typeParameterName)) {
trace.report(ONLY_ONE_CLASS_BOUND_ALLOWED.on(upperBoundElement));
}
}
}
}
@@ -6,15 +6,15 @@ interface B
interface D<T>
interface IncorrectF<<!MISPLACED_TYPE_PARAMETER_CONSTRAINTS, INCONSISTENT_TYPE_PARAMETER_BOUNDS!>T : D<A><!>> where T : D<B>
interface IncorrectF<<!MISPLACED_TYPE_PARAMETER_CONSTRAINTS, INCONSISTENT_TYPE_PARAMETER_BOUNDS!>T : D<A><!>> where T : <!REPEATED_BOUND!>D<B><!>
interface CorrectF<<!INCONSISTENT_TYPE_PARAMETER_BOUNDS!>T<!>> where T : D<A>, T : D<B>
interface CorrectF<<!INCONSISTENT_TYPE_PARAMETER_BOUNDS!>T<!>> where T : D<A>, T : <!REPEATED_BOUND!>D<B><!>
interface G<T>
interface IncorrectH<<!MISPLACED_TYPE_PARAMETER_CONSTRAINTS, INCONSISTENT_TYPE_PARAMETER_BOUNDS!>T : G<D<A>><!>> where T : G<D<T>>
interface IncorrectH<<!MISPLACED_TYPE_PARAMETER_CONSTRAINTS, INCONSISTENT_TYPE_PARAMETER_BOUNDS!>T : G<D<A>><!>> where T : <!REPEATED_BOUND!>G<D<T>><!>
interface CorrectH<<!INCONSISTENT_TYPE_PARAMETER_BOUNDS!>T<!>> where T : G<D<A>>, T : G<D<B>>
interface CorrectH<<!INCONSISTENT_TYPE_PARAMETER_BOUNDS!>T<!>> where T : G<D<A>>, T : <!REPEATED_BOUND!>G<D<B>><!>
interface I<T : G<D<T>>> {
fun <<!MISPLACED_TYPE_PARAMETER_CONSTRAINTS, INCONSISTENT_TYPE_PARAMETER_BOUNDS!>S : T?<!>> incorrectFoo() where S : G<D<S>>
@@ -22,8 +22,8 @@ interface I<T : G<D<T>>> {
fun <<!INCONSISTENT_TYPE_PARAMETER_BOUNDS!>S<!>> correctFoo() where S : T?, S : G<D<S>>
}
interface incorrectJ<<!MISPLACED_TYPE_PARAMETER_CONSTRAINTS, INCONSISTENT_TYPE_PARAMETER_BOUNDS!>T: G<D<T>><!>> where T : G<D<T?>>
interface incorrectJ<<!MISPLACED_TYPE_PARAMETER_CONSTRAINTS, INCONSISTENT_TYPE_PARAMETER_BOUNDS!>T: G<D<T>><!>> where T : <!REPEATED_BOUND!>G<D<T?>><!>
interface correctJ<<!INCONSISTENT_TYPE_PARAMETER_BOUNDS!>T<!>> where T : G<D<T>>, T : G<D<T?>>
interface correctJ<<!INCONSISTENT_TYPE_PARAMETER_BOUNDS!>T<!>> where T : G<D<T>>, T : <!REPEATED_BOUND!>G<D<T?>><!>
fun <<!INCONSISTENT_TYPE_PARAMETER_BOUNDS!>T<!>> bar() where T : D<A>, T : D<B> {}
fun <<!INCONSISTENT_TYPE_PARAMETER_BOUNDS!>T<!>> bar() where T : D<A>, T : <!REPEATED_BOUND!>D<B><!> {}
@@ -0,0 +1,7 @@
interface I1
class A1<T> where T : I1, T : <!REPEATED_BOUND!>I1<!>
class A2<T> where T : I1, T : <!REPEATED_BOUND!>I1?<!>
class A3<K, V> where K : V, K : <!REPEATED_BOUND!>V<!>
fun <T> f1() where T : I1, T : <!REPEATED_BOUND!>I1<!> {}
@@ -0,0 +1,30 @@
package
public fun </*0*/ T : I1> f1(): kotlin.Unit where T : I1
public final class A1</*0*/ T : I1> where T : I1 {
public constructor A1</*0*/ T : I1>() where T : 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 final class A2</*0*/ T : I1> where T : I1? {
public constructor A2</*0*/ T : I1>() where T : 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 final class A3</*0*/ K : V, /*1*/ V> where K : V {
public constructor A3</*0*/ K : V, /*1*/ V>() where K : V
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 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
}
@@ -1,6 +1,6 @@
fun <<!UPPER_BOUND_CANNOT_BE_ARRAY!>A : Array<Any><!>> f1() {}
fun <T, <!UPPER_BOUND_CANNOT_BE_ARRAY!>A : Array<out T><!>> f2() {}
fun <S, T : S, <!INCONSISTENT_TYPE_PARAMETER_BOUNDS, UPPER_BOUND_CANNOT_BE_ARRAY!>A<!>> f3() where A : Array<out S>, A : <!ONLY_ONE_CLASS_BOUND_ALLOWED!>Array<out T><!> {}
fun <S, T : S, <!INCONSISTENT_TYPE_PARAMETER_BOUNDS, UPPER_BOUND_CANNOT_BE_ARRAY!>A<!>> f3() where A : Array<out S>, A : <!REPEATED_BOUND!>Array<out T><!> {}
fun <<!UPPER_BOUND_CANNOT_BE_ARRAY!>T : <!FINAL_UPPER_BOUND!>IntArray<!><!>> f4() {}
@@ -9,7 +9,7 @@ fun <<!UPPER_BOUND_CANNOT_BE_ARRAY!>T<!>> f5() where T : Array<Any> {}
val <<!UPPER_BOUND_CANNOT_BE_ARRAY!>T : Array<Any?><!>> T.v: String get() = ""
class C2<T, <!UPPER_BOUND_CANNOT_BE_ARRAY!>A : Array<out T><!>>
interface C3<S, T : S, <!INCONSISTENT_TYPE_PARAMETER_BOUNDS, UPPER_BOUND_CANNOT_BE_ARRAY!>A<!>> where A : Array<out S>, A : <!ONLY_ONE_CLASS_BOUND_ALLOWED!>Array<out T><!>
interface C3<S, T : S, <!INCONSISTENT_TYPE_PARAMETER_BOUNDS, UPPER_BOUND_CANNOT_BE_ARRAY!>A<!>> where A : Array<out S>, A : <!REPEATED_BOUND!>Array<out T><!>
fun foo() {
class C1<<!UPPER_BOUND_CANNOT_BE_ARRAY!>A : Array<Any><!>> {
@@ -17022,6 +17022,12 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTest {
doTest(fileName);
}
@TestMetadata("repeatedBound.kt")
public void testRepeatedBound() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/typeParameters/repeatedBound.kt");
doTest(fileName);
}
@TestMetadata("upperBoundCannotBeArray.kt")
public void testUpperBoundCannotBeArray() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/typeParameters/upperBoundCannotBeArray.kt");
+2 -2
View File
@@ -6,6 +6,6 @@ interface B
interface D<T>
interface CorrectF<<error descr="[INCONSISTENT_TYPE_PARAMETER_BOUNDS] Type parameter T of 'D' has inconsistent bounds: A, B">T</error>> where T : D<A>, T : D<B>
interface CorrectF<<error descr="[INCONSISTENT_TYPE_PARAMETER_BOUNDS] Type parameter T of 'D' has inconsistent bounds: A, B">T</error>> where T : D<A>, T : <error descr="[REPEATED_BOUND] Type parameter already has this bound">D<B></error>
fun <<error descr="[INCONSISTENT_TYPE_PARAMETER_BOUNDS] Type parameter T of 'D' has inconsistent bounds: A, B">T</error>> bar() where T : D<A>, T : D<B> {}
fun <<error descr="[INCONSISTENT_TYPE_PARAMETER_BOUNDS] Type parameter T of 'D' has inconsistent bounds: A, B">T</error>> bar() where T : D<A>, T : <error descr="[REPEATED_BOUND] Type parameter already has this bound">D<B></error> {}
@@ -8,7 +8,7 @@ public final class SecondaryConstructors public constructor(x: kotlin.Boolean) {
private constructor(x: kotlin.Int) { /* compiled code */ }
public final inner class Inner<T : kotlin.String, G : kotlin.Int> where G : kotlin.Number {
public final inner class Inner<T : kotlin.String, G : kotlin.Int> where G : java.io.Serializable {
public constructor(x: T, g: G) { /* compiled code */ }
}
@@ -1,5 +1,7 @@
package test
import java.io.Serializable
class SecondaryConstructors(x: Boolean) {
init {
}
@@ -13,7 +15,7 @@ class SecondaryConstructors(x: Boolean) {
private constructor(x: Int) : this(x < 0) {
}
inner class Inner<T : String, G : Int> where G : Number {
inner class Inner<T : String, G : Int> where G : Serializable {
constructor(x: T, g: G) {
}
}
@@ -1,5 +1,7 @@
package test
import java.io.Serializable
class SecondaryConstructors(x: Boolean) {
init {
}
@@ -13,7 +15,7 @@ class SecondaryConstructors(x: Boolean) {
private constructor(x: Int) : this(x < 0) {
}
inner class Inner<T : String, G : Int> where G : Number {
inner class Inner<T : String, G : Int> where G : Serializable {
constructor(x: T, g: G) {
}
}
@@ -60,8 +60,10 @@ PsiJetFileStubImpl[package=test]
TYPE_REFERENCE:
USER_TYPE:[isAbsoluteInRootPackage=false]
USER_TYPE:[isAbsoluteInRootPackage=false]
REFERENCE_EXPRESSION:[referencedName=kotlin]
REFERENCE_EXPRESSION:[referencedName=Number]
USER_TYPE:[isAbsoluteInRootPackage=false]
REFERENCE_EXPRESSION:[referencedName=java]
REFERENCE_EXPRESSION:[referencedName=io]
REFERENCE_EXPRESSION:[referencedName=Serializable]
CLASS_BODY:
SECONDARY_CONSTRUCTOR:
MODIFIER_LIST:[public]
@@ -1,6 +1,8 @@
package test
class TypeParams<in T1 : Any, out T2, T3 : (Int) -> Int, T4, T5 : Any?, T6 : T5, T7 : Any> where T1 : Any?, T1 : Int?, T1 : Int, T2 : String, T7 : T6 {
import java.io.Serializable
class TypeParams<in T1 : Any, out T2, T3 : (Int) -> Int, T4, T5 : Any?, T6 : T5, T7 : Any> where T1 : Cloneable?, T1 : Serializable, T2 : String, T7 : T6 {
fun useParams(p1: T1, p2: (T2) -> Unit, p3: T3, p4: T4, P5: T5) {
}
@@ -15,7 +17,7 @@ class TypeParams<in T1 : Any, out T2, T3 : (Int) -> Int, T4, T5 : Any?, T6 : T5,
fun <G1, G2, G3> withOwnParams(p1: G1, p2: G2, p3: G3, p4: T1, p5: (T2) -> Unit) {
}
fun <G1 : Any?, G2 : G1, G3> withOwnParamsAndTypeConstraints(p1: G1, p2: G2, p3: G3, p4: T1, p5: (T2) -> Unit) where G3 : G1, G3 : String, G3 : String? {
fun <G1 : Any?, G2 : G1, G3> withOwnParamsAndTypeConstraints(p1: G1, p2: G2, p3: G3, p4: T1, p5: (T2) -> Unit) where G3 : G1, G3 : String, G3 : Serializable? {
}
fun <T1, T2, T3> withOwnParamsClashing(p1: T1, p2: T2, p3: T3, p4: T4, p5: T5) {
@@ -57,22 +57,16 @@ PsiJetFileStubImpl[package=test]
USER_TYPE:[isAbsoluteInRootPackage=false]
USER_TYPE:[isAbsoluteInRootPackage=false]
REFERENCE_EXPRESSION:[referencedName=kotlin]
REFERENCE_EXPRESSION:[referencedName=Any]
TYPE_CONSTRAINT:
REFERENCE_EXPRESSION:[referencedName=T1]
TYPE_REFERENCE:
NULLABLE_TYPE:
USER_TYPE:[isAbsoluteInRootPackage=false]
USER_TYPE:[isAbsoluteInRootPackage=false]
REFERENCE_EXPRESSION:[referencedName=kotlin]
REFERENCE_EXPRESSION:[referencedName=Int]
REFERENCE_EXPRESSION:[referencedName=Cloneable]
TYPE_CONSTRAINT:
REFERENCE_EXPRESSION:[referencedName=T1]
TYPE_REFERENCE:
USER_TYPE:[isAbsoluteInRootPackage=false]
USER_TYPE:[isAbsoluteInRootPackage=false]
REFERENCE_EXPRESSION:[referencedName=kotlin]
REFERENCE_EXPRESSION:[referencedName=Int]
USER_TYPE:[isAbsoluteInRootPackage=false]
REFERENCE_EXPRESSION:[referencedName=java]
REFERENCE_EXPRESSION:[referencedName=io]
REFERENCE_EXPRESSION:[referencedName=Serializable]
TYPE_CONSTRAINT:
REFERENCE_EXPRESSION:[referencedName=T7]
TYPE_REFERENCE:
@@ -371,8 +365,10 @@ PsiJetFileStubImpl[package=test]
NULLABLE_TYPE:
USER_TYPE:[isAbsoluteInRootPackage=false]
USER_TYPE:[isAbsoluteInRootPackage=false]
REFERENCE_EXPRESSION:[referencedName=kotlin]
REFERENCE_EXPRESSION:[referencedName=String]
USER_TYPE:[isAbsoluteInRootPackage=false]
REFERENCE_EXPRESSION:[referencedName=java]
REFERENCE_EXPRESSION:[referencedName=io]
REFERENCE_EXPRESSION:[referencedName=Serializable]
FUN:[fqName=test.TypeParams.withOwnParamsClashing, hasBlockBody=true, hasBody=true, hasTypeParameterListBeforeFunctionName=true, isExtension=false, isTopLevel=false, name=withOwnParamsClashing]
MODIFIER_LIST:[public final]
TYPE_PARAMETER_LIST:
@@ -22,13 +22,13 @@ import com.intellij.psi.PsiFile
import com.intellij.psi.PsiManager
import com.intellij.psi.PsiRecursiveElementVisitor
import com.intellij.testFramework.LightProjectDescriptor
import com.intellij.testFramework.UsefulTestCase
import org.jetbrains.kotlin.idea.test.JdkAndMockLibraryProjectDescriptor
import org.jetbrains.kotlin.idea.test.KotlinLightCodeInsightFixtureTestCase
import org.jetbrains.kotlin.idea.test.KotlinLightProjectDescriptor
import org.jetbrains.kotlin.idea.test.PluginTestCaseBase
import org.jetbrains.kotlin.psi.psiUtil.getElementTextWithContext
import kotlin.test.fail
import org.jetbrains.kotlin.test.KotlinTestUtils
import java.io.File
public abstract class AbstractDecompiledTextBaseTest(
baseDirectory: String,
@@ -44,9 +44,9 @@ public abstract class AbstractDecompiledTextBaseTest(
public fun doTest(path: String) {
val fileToDecompile = getFileToDecompile()
val psiFile = PsiManager.getInstance(getProject()).findFile(fileToDecompile)!!
val psiFile = PsiManager.getInstance(project).findFile(fileToDecompile)!!
checkPsiFile(psiFile)
UsefulTestCase.assertSameLinesWithFile(path.substring(0, path.length() - 1) + ".expected.kt", psiFile.getText())
KotlinTestUtils.assertEqualsToFile(File(path.substring(0, path.length - 1) + ".expected.kt"), psiFile.text)
checkThatFileWasParsedCorrectly(psiFile)
}
@@ -64,4 +64,4 @@ public abstract class AbstractDecompiledTextBaseTest(
}
})
}
}
}