Use 'Any?' instead of throwing NPE when calculating star projection type
It's a quite hacky solution, but the choice seems to be conservative, so these types can be refined later #KT-9597 Fixed
This commit is contained in:
@@ -0,0 +1,26 @@
|
||||
// !CHECK_TYPE
|
||||
// !DIAGNOSTICS: -UNUSED_PARAMETER
|
||||
// FILE: main.kt
|
||||
class Inv<T>(val x: T)
|
||||
|
||||
class A<T : Inv<in T>> {
|
||||
fun foo(): T = null!!
|
||||
}
|
||||
|
||||
class Inv2<<!FINITE_BOUNDS_VIOLATION!>T : Inv2<in T><!>>(val x: T)
|
||||
|
||||
fun main(a: A<*>, j: JavaClass<*>, i2: Inv2<*>) {
|
||||
// Probably it's too restrictive to suppose star projection type here as Any?,
|
||||
// but looks like we can refine it later
|
||||
a.foo() checkType { _<Any?>() }
|
||||
j.foo() checkType { _<Any?>() }
|
||||
i2.x checkType { _<Any?>() }
|
||||
|
||||
j.<!MEMBER_PROJECTED_OUT!>bar<!>(1, 2, Any())
|
||||
}
|
||||
|
||||
// FILE: JavaClass.java
|
||||
public class JavaClass<T extends JavaClass<? super T>> {
|
||||
public void bar(T... x) {}
|
||||
public T foo() {}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package
|
||||
|
||||
public fun main(/*0*/ a: A<*>, /*1*/ j: JavaClass<*>, /*2*/ i2: Inv2<*>): kotlin.Unit
|
||||
|
||||
public final class A</*0*/ T : Inv<in T>> {
|
||||
public constructor A</*0*/ T : Inv<in T>>()
|
||||
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
|
||||
public final fun foo(): T
|
||||
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
|
||||
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
|
||||
}
|
||||
|
||||
public final class Inv</*0*/ T> {
|
||||
public constructor Inv</*0*/ T>(/*0*/ x: T)
|
||||
public final val x: 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
|
||||
}
|
||||
|
||||
public final class Inv2</*0*/ T : Inv2<in T>> {
|
||||
public constructor Inv2</*0*/ T : Inv2<in T>>(/*0*/ x: T)
|
||||
public final val x: 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
|
||||
}
|
||||
|
||||
public open class JavaClass</*0*/ T : JavaClass<in T!>!> {
|
||||
public constructor JavaClass</*0*/ T : JavaClass<in T!>!>()
|
||||
public open fun bar(/*0*/ vararg x: T! /*kotlin.Array<(out) T!>!*/): kotlin.Unit
|
||||
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
|
||||
public open fun foo(): T!
|
||||
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
|
||||
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
|
||||
}
|
||||
@@ -8104,6 +8104,12 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTest {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/generics/starProjections/inheritedFromKotlin.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("invalid.kt")
|
||||
public void testInvalid() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/generics/starProjections/invalid.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
}
|
||||
|
||||
@TestMetadata("compiler/testData/diagnostics/tests/generics/tpAsReified")
|
||||
|
||||
@@ -18,6 +18,7 @@ package org.jetbrains.kotlin.types
|
||||
|
||||
import org.jetbrains.kotlin.descriptors.ClassDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.TypeParameterDescriptor
|
||||
import org.jetbrains.kotlin.resolve.descriptorUtil.builtIns
|
||||
|
||||
class StarProjectionImpl(
|
||||
private val typeParameter: TypeParameterDescriptor
|
||||
@@ -45,7 +46,7 @@ fun TypeParameterDescriptor.starProjectionType(): KotlinType {
|
||||
else null
|
||||
|
||||
}
|
||||
).substitute(this.upperBounds.first(), Variance.OUT_VARIANCE)!!
|
||||
).substitute(this.upperBounds.first(), Variance.OUT_VARIANCE) ?: builtIns.defaultBound
|
||||
}
|
||||
|
||||
class TypeBasedStarProjectionImpl(
|
||||
@@ -56,4 +57,4 @@ class TypeBasedStarProjectionImpl(
|
||||
override fun getProjectionKind() = Variance.OUT_VARIANCE
|
||||
|
||||
override fun getType() = _type
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user