[FE] Support reporting INFERRED_TYPE_VARIABLE_INTO_EMPTY_INTERSECTION for empty intersection types with type parameters

This commit is contained in:
Victor Petukhov
2022-02-11 14:20:28 +03:00
committed by teamcity
parent c16ae81a48
commit b96708c3e2
26 changed files with 307 additions and 19 deletions
@@ -1976,6 +1976,16 @@ internal val KT_DIAGNOSTIC_CONVERTER = KtDiagnosticConverterBuilder.buildConvert
token,
)
}
add(FirErrors.INFERRED_TYPE_VARIABLE_INTO_EMPTY_INTERSECTION) { firDiagnostic ->
InferredTypeVariableIntoEmptyIntersectionImpl(
firDiagnostic.a,
firDiagnostic.b.map { coneKotlinType ->
firSymbolBuilder.typeBuilder.buildKtType(coneKotlinType)
},
firDiagnostic as KtPsiDiagnostic,
token,
)
}
add(FirErrors.EXTENSION_IN_CLASS_REFERENCE_NOT_ALLOWED) { firDiagnostic ->
ExtensionInClassReferenceNotAllowedImpl(
firSymbolBuilder.callableBuilder.buildCallableSymbol(firDiagnostic.a),
@@ -1405,6 +1405,12 @@ sealed class KtFirDiagnostic<PSI : PsiElement> : KtDiagnosticWithPsi<PSI> {
abstract val kotlinClass: FqName
}
abstract class InferredTypeVariableIntoEmptyIntersection : KtFirDiagnostic<PsiElement>() {
override val diagnosticClass get() = InferredTypeVariableIntoEmptyIntersection::class
abstract val typeVariableDescription: String
abstract val incompatibleTypes: List<KtType>
}
abstract class ExtensionInClassReferenceNotAllowed : KtFirDiagnostic<KtExpression>() {
override val diagnosticClass get() = ExtensionInClassReferenceNotAllowed::class
abstract val referencedDeclaration: KtCallableSymbol
@@ -1687,6 +1687,13 @@ internal class PlatformClassMappedToKotlinImpl(
override val token: ValidityToken,
) : KtFirDiagnostic.PlatformClassMappedToKotlin(), KtAbstractFirDiagnostic<PsiElement>
internal class InferredTypeVariableIntoEmptyIntersectionImpl(
override val typeVariableDescription: String,
override val incompatibleTypes: List<KtType>,
override val firDiagnostic: KtPsiDiagnostic,
override val token: ValidityToken,
) : KtFirDiagnostic.InferredTypeVariableIntoEmptyIntersection(), KtAbstractFirDiagnostic<PsiElement>
internal class ExtensionInClassReferenceNotAllowedImpl(
override val referencedDeclaration: KtCallableSymbol,
override val firDiagnostic: KtPsiDiagnostic,
@@ -13840,6 +13840,30 @@ public class DiagnosisCompilerTestFE10TestdataTestGenerated extends AbstractDiag
runTest("compiler/testData/diagnostics/tests/inference/kt45461.kt");
}
@Test
@TestMetadata("kt45461_2.kt")
public void testKt45461_2() throws Exception {
runTest("compiler/testData/diagnostics/tests/inference/kt45461_2.kt");
}
@Test
@TestMetadata("kt45461_3.kt")
public void testKt45461_3() throws Exception {
runTest("compiler/testData/diagnostics/tests/inference/kt45461_3.kt");
}
@Test
@TestMetadata("kt45461_4.kt")
public void testKt45461_4() throws Exception {
runTest("compiler/testData/diagnostics/tests/inference/kt45461_4.kt");
}
@Test
@TestMetadata("kt45461_5.kt")
public void testKt45461_5() throws Exception {
runTest("compiler/testData/diagnostics/tests/inference/kt45461_5.kt");
}
@Test
@TestMetadata("kt46515.kt")
public void testKt46515() throws Exception {
@@ -13840,6 +13840,30 @@ public class FirOldFrontendDiagnosticsTestGenerated extends AbstractFirDiagnosti
runTest("compiler/testData/diagnostics/tests/inference/kt45461.kt");
}
@Test
@TestMetadata("kt45461_2.kt")
public void testKt45461_2() throws Exception {
runTest("compiler/testData/diagnostics/tests/inference/kt45461_2.kt");
}
@Test
@TestMetadata("kt45461_3.kt")
public void testKt45461_3() throws Exception {
runTest("compiler/testData/diagnostics/tests/inference/kt45461_3.kt");
}
@Test
@TestMetadata("kt45461_4.kt")
public void testKt45461_4() throws Exception {
runTest("compiler/testData/diagnostics/tests/inference/kt45461_4.kt");
}
@Test
@TestMetadata("kt45461_5.kt")
public void testKt45461_5() throws Exception {
runTest("compiler/testData/diagnostics/tests/inference/kt45461_5.kt");
}
@Test
@TestMetadata("kt46515.kt")
public void testKt46515() throws Exception {
@@ -13840,6 +13840,30 @@ public class FirOldFrontendDiagnosticsWithLightTreeTestGenerated extends Abstrac
runTest("compiler/testData/diagnostics/tests/inference/kt45461.kt");
}
@Test
@TestMetadata("kt45461_2.kt")
public void testKt45461_2() throws Exception {
runTest("compiler/testData/diagnostics/tests/inference/kt45461_2.kt");
}
@Test
@TestMetadata("kt45461_3.kt")
public void testKt45461_3() throws Exception {
runTest("compiler/testData/diagnostics/tests/inference/kt45461_3.kt");
}
@Test
@TestMetadata("kt45461_4.kt")
public void testKt45461_4() throws Exception {
runTest("compiler/testData/diagnostics/tests/inference/kt45461_4.kt");
}
@Test
@TestMetadata("kt45461_5.kt")
public void testKt45461_5() throws Exception {
runTest("compiler/testData/diagnostics/tests/inference/kt45461_5.kt");
}
@Test
@TestMetadata("kt46515.kt")
public void testKt46515() throws Exception {
@@ -111,6 +111,8 @@ abstract class ConstraintSystemCompletionContext : VariableFixationFinder.Contex
fun List<Constraint>.extractUpperTypes(): List<KotlinTypeMarker> =
filter { constraint ->
constraint.kind == ConstraintKind.UPPER && !constraint.type.contains { !it.typeConstructor().isClassTypeConstructor() }
constraint.kind == ConstraintKind.UPPER && !constraint.type.contains {
!it.typeConstructor().isClassTypeConstructor() && !it.typeConstructor().isTypeParameterTypeConstructor()
}
}.map { it.type }
}
@@ -1,12 +1,12 @@
/selectFromCovariantAndContravariantTypes.kt:12:22: warning: parameter 'y' is never used
/selectFromCovariantAndContravariantTypes.kt:11:22: warning: parameter 'y' is never used
fun <K> select(x: K, y: K): K = x
^
/selectFromCovariantAndContravariantTypes.kt:13:19: warning: parameter 'x' is never used
/selectFromCovariantAndContravariantTypes.kt:12:19: warning: parameter 'x' is never used
fun <V> genericIn(x: In<V>) {}
^
/selectFromCovariantAndContravariantTypes.kt:14:20: warning: parameter 'x' is never used
/selectFromCovariantAndContravariantTypes.kt:13:20: warning: parameter 'x' is never used
fun <V> genericOut(x: Out<V>) {}
^
/selectFromCovariantAndContravariantTypes.kt:17:5: warning: type argument for a type parameter V can't be inferred because it's upper bounded by incompatible types: A, B. This will become an error in Kotlin 1.8
/selectFromCovariantAndContravariantTypes.kt:16:5: warning: type argument for a type parameter V can't be inferred because it's upper bounded by incompatible types: A, B. This will become an error in Kotlin 1.8
genericIn(select(a, b))
^
@@ -1,6 +1,6 @@
/kt45461.kt:6:25: warning: parameter 'foo' is never used
/kt45461.kt:5:25: warning: parameter 'foo' is never used
fun <S : T> takeFoo(foo: Foo<in S>) {}
^
/kt45461.kt:11:19: warning: type argument for a type parameter S can't be inferred because it's upper bounded by incompatible types: String, Int. This will become an error in Kotlin 1.8
/kt45461.kt:10:19: warning: type argument for a type parameter S can't be inferred because it's upper bounded by incompatible types: String, Int. This will become an error in Kotlin 1.8
Bar<String>().takeFoo(foo) // error in 1.3.72, no error in 1.4.31
^
@@ -0,0 +1,10 @@
/kt45461_2.kt:5:25: warning: parameter 'foo' is never used
fun <S : T> takeFoo(foo: Foo<in S>) {}
^
/kt45461_2.kt:8:10: warning: 'Int' is a final type, and thus a value of the type parameter is predetermined
fun <K : Int> main() {
^
/kt45461_2.kt:10:19: warning: type argument for a type parameter S can't be inferred because it's upper bounded by incompatible types: String, K. This will become an error in Kotlin 1.8
Bar<String>().takeFoo(foo) // error in 1.3.72, no error in 1.4.31
^
@@ -0,0 +1,11 @@
// RENDER_DIAGNOSTICS_FULL_TEXT
class Foo<T>
class Bar<T> {
fun <S : T> takeFoo(foo: Foo<in S>) {}
}
fun <K : <!FINAL_UPPER_BOUND!>Int<!>> main() {
val foo = Foo<K>()
Bar<String>().<!INFERRED_TYPE_VARIABLE_INTO_EMPTY_INTERSECTION!>takeFoo<!>(foo) // error in 1.3.72, no error in 1.4.31
}
@@ -0,0 +1,11 @@
// RENDER_DIAGNOSTICS_FULL_TEXT
class Foo<T>
class Bar<T> {
fun <S : T> takeFoo(foo: Foo<in S>) {}
}
fun <K : <!FINAL_UPPER_BOUND!>Int<!>> main() {
val foo = Foo<K>()
Bar<String>().<!INFERRED_TYPE_VARIABLE_INTO_EMPTY_INTERSECTION_WARNING!>takeFoo<!>(foo) // error in 1.3.72, no error in 1.4.31
}
@@ -0,0 +1,18 @@
package
public fun </*0*/ K : kotlin.Int> main(): kotlin.Unit
public final class Bar</*0*/ T> {
public constructor Bar</*0*/ T>()
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 final fun </*0*/ S : T> takeFoo(/*0*/ foo: Foo<in S>): kotlin.Unit
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
public final class Foo</*0*/ T> {
public constructor Foo</*0*/ T>()
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,11 @@
// FIR_IDENTICAL
class Foo<T>
class Bar<T> {
fun <S : T> takeFoo(foo: Foo<in S>) {}
}
fun <K : <!FINAL_UPPER_BOUND!>String<!>> main() {
val foo = Foo<K>()
Bar<String>().takeFoo(foo) // error in 1.3.72, no error in 1.4.31
}
@@ -0,0 +1,18 @@
package
public fun </*0*/ K : kotlin.String> main(): kotlin.Unit
public final class Bar</*0*/ T> {
public constructor Bar</*0*/ T>()
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 final fun </*0*/ S : T> takeFoo(/*0*/ foo: Foo<in S>): kotlin.Unit
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
public final class Foo</*0*/ T> {
public constructor Foo</*0*/ T>()
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,11 @@
// FIR_IDENTICAL
class Foo<T>
class Bar<T> {
fun <S : T> takeFoo(foo: Foo<in S>) {}
}
fun <K : CharSequence> main() {
val foo = Foo<K>()
Bar<String>().takeFoo(foo) // error in 1.3.72, no error in 1.4.31
}
@@ -0,0 +1,18 @@
package
public fun </*0*/ K : kotlin.CharSequence> main(): kotlin.Unit
public final class Bar</*0*/ T> {
public constructor Bar</*0*/ T>()
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 final fun </*0*/ S : T> takeFoo(/*0*/ foo: Foo<in S>): kotlin.Unit
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
public final class Foo</*0*/ T> {
public constructor Foo</*0*/ T>()
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,9 @@
/kt45461_5.kt:5:25: warning: parameter 'foo' is never used
fun <S : T> takeFoo(foo: Foo<in S>) {}
^
/kt45461_5.kt:8:23: warning: 'Int' is a final type, and thus a value of the type parameter is predetermined
fun <K : L, L : N, N: Int> main() {
^
/kt45461_5.kt:10:19: warning: type argument for a type parameter S can't be inferred because it's upper bounded by incompatible types: String, K. This will become an error in Kotlin 1.8
Bar<String>().takeFoo(foo) // error in 1.3.72, no error in 1.4.31
^
@@ -0,0 +1,11 @@
// RENDER_DIAGNOSTICS_FULL_TEXT
class Foo<T>
class Bar<T> {
fun <S : T> takeFoo(foo: Foo<in S>) {}
}
fun <K : L, L : N, N: <!FINAL_UPPER_BOUND!>Int<!>> main() {
val foo = Foo<K>()
Bar<String>().<!INFERRED_TYPE_VARIABLE_INTO_EMPTY_INTERSECTION!>takeFoo<!>(foo) // error in 1.3.72, no error in 1.4.31
}
@@ -0,0 +1,11 @@
// RENDER_DIAGNOSTICS_FULL_TEXT
class Foo<T>
class Bar<T> {
fun <S : T> takeFoo(foo: Foo<in S>) {}
}
fun <K : L, L : N, N: <!FINAL_UPPER_BOUND!>Int<!>> main() {
val foo = Foo<K>()
Bar<String>().<!INFERRED_TYPE_VARIABLE_INTO_EMPTY_INTERSECTION_WARNING!>takeFoo<!>(foo) // error in 1.3.72, no error in 1.4.31
}
@@ -0,0 +1,18 @@
package
public fun </*0*/ K : L, /*1*/ L : N, /*2*/ N : kotlin.Int> main(): kotlin.Unit
public final class Bar</*0*/ T> {
public constructor Bar</*0*/ T>()
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 final fun </*0*/ S : T> takeFoo(/*0*/ foo: Foo<in S>): kotlin.Unit
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
public final class Foo</*0*/ T> {
public constructor Foo</*0*/ T>()
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,15 +1,15 @@
/kt48765.kt:5:44: warning: parameter 'x1' is never used
/kt48765.kt:4:44: warning: parameter 'x1' is never used
fun <T1: Number, T2: A<Float, T1>> foo(x1: T2, x2: T1) {}
^
/kt48765.kt:5:52: warning: parameter 'x2' is never used
/kt48765.kt:4:52: warning: parameter 'x2' is never used
fun <T1: Number, T2: A<Float, T1>> foo(x1: T2, x2: T1) {}
^
/kt48765.kt:9:13: warning: type argument for a type parameter T can't be inferred because it's upper bounded by incompatible types: String, Number. This will become an error in Kotlin 1.8
/kt48765.kt:8:13: warning: type argument for a type parameter T can't be inferred because it's upper bounded by incompatible types: String, Number. This will become an error in Kotlin 1.8
B().foo(x, foo())
^
/kt48765.kt:13:9: warning: 'String' is a final type, and thus a value of the type parameter is predetermined
/kt48765.kt:12:9: warning: 'String' is a final type, and thus a value of the type parameter is predetermined
fun <T: String> foo(): T {
^
/kt48765.kt:14:15: warning: unchecked cast: String to T
/kt48765.kt:13:15: warning: unchecked cast: String to T
return "" as T // this cast is safe because String is final.
^
@@ -1,7 +1,7 @@
/kt48935.kt:7:35: warning: parameter 'func' is never used
/kt48935.kt:6:35: warning: parameter 'func' is never used
fun <T, V> exampleGenericFunction(func: V) where T: Base, V: (T) -> Unit {
^
/kt48935.kt:13:5: warning: type argument for a type parameter T can't be inferred because it's upper bounded by incompatible types: Base, DoesNotImplementBase. This will become an error in Kotlin 1.8
/kt48935.kt:12:5: warning: type argument for a type parameter T can't be inferred because it's upper bounded by incompatible types: Base, DoesNotImplementBase. This will become an error in Kotlin 1.8
exampleGenericFunction(func) // expected this to be a compilation error as the T: Base constraint should not be satisfied
^
@@ -1,4 +1,4 @@
/kt49661.kt:11:5: warning: type argument for a type parameter T can't be inferred because it's upper bounded by incompatible types: Foo, Int. This will become an error in Kotlin 1.8
/kt49661.kt:10:5: warning: type argument for a type parameter T can't be inferred because it's upper bounded by incompatible types: Foo, Int. This will become an error in Kotlin 1.8
f<Int> { g() }
^
@@ -13846,6 +13846,30 @@ public class DiagnosticTestGenerated extends AbstractDiagnosticTest {
runTest("compiler/testData/diagnostics/tests/inference/kt45461.kt");
}
@Test
@TestMetadata("kt45461_2.kt")
public void testKt45461_2() throws Exception {
runTest("compiler/testData/diagnostics/tests/inference/kt45461_2.kt");
}
@Test
@TestMetadata("kt45461_3.kt")
public void testKt45461_3() throws Exception {
runTest("compiler/testData/diagnostics/tests/inference/kt45461_3.kt");
}
@Test
@TestMetadata("kt45461_4.kt")
public void testKt45461_4() throws Exception {
runTest("compiler/testData/diagnostics/tests/inference/kt45461_4.kt");
}
@Test
@TestMetadata("kt45461_5.kt")
public void testKt45461_5() throws Exception {
runTest("compiler/testData/diagnostics/tests/inference/kt45461_5.kt");
}
@Test
@TestMetadata("kt46515.kt")
public void testKt46515() throws Exception {
@@ -178,15 +178,25 @@ interface TypeSystemInferenceExtensionContext : TypeSystemContext, TypeSystemBui
fun KotlinTypeMarker.isFinal(): Boolean
fun Collection<KotlinTypeMarker>.isEmptyIntersection(): Boolean =
any { first ->
any { second ->
fun Collection<KotlinTypeMarker>.isEmptyIntersection(): Boolean {
val expandedTypes = buildSet {
for (type in this@isEmptyIntersection) {
val typeConstructor = type.typeConstructor()
when {
typeConstructor.isClassTypeConstructor() -> add(type)
typeConstructor.isTypeParameterTypeConstructor() -> addAll(typeConstructor.supertypes())
}
}
}.takeIf { it.isNotEmpty() } ?: return false
return expandedTypes.any { first ->
expandedTypes.any { second ->
first !== second &&
first.isFinal() &&
second.typeConstructor().isClassTypeConstructor() &&
!AbstractTypeChecker.isSubtypeOf(this@TypeSystemInferenceExtensionContext, first, second)
}
}
}
fun KotlinTypeMarker.removeAnnotations(): KotlinTypeMarker
fun KotlinTypeMarker.removeExactAnnotation(): KotlinTypeMarker