Update typing rules for class literal expressions.
C::class : KClass<C> expr: T => expr::class : KClass<out T> NB: this means Obj::class : KClass<out Obj> for object Obj.
This commit is contained in:
+2
-1
@@ -92,7 +92,8 @@ class DoubleColonExpressionResolver(
|
||||
val type = result?.type
|
||||
if (type != null && !type.isError) {
|
||||
checkClassLiteral(c, expression, result)
|
||||
val kClassType = reflectionTypes.getKClassType(Annotations.EMPTY, type)
|
||||
val variance = if (result is DoubleColonLHS.Expression && !result.isObject) Variance.OUT_VARIANCE else Variance.INVARIANT
|
||||
val kClassType = reflectionTypes.getKClassType(Annotations.EMPTY, type, variance)
|
||||
val dataFlowInfo = (result as? DoubleColonLHS.Expression)?.dataFlowInfo ?: c.dataFlowInfo
|
||||
return dataFlowAnalyzer.checkType(createTypeInfo(kClassType, dataFlowInfo), expression, c)
|
||||
}
|
||||
|
||||
+3
-1
@@ -7,6 +7,8 @@ fun test(s: String) {
|
||||
val g: () -> String = s::toString
|
||||
val h: (Any?) -> Boolean = s::equals
|
||||
|
||||
val k: KClass<String> = s::class
|
||||
val k: KClass<out String> = s::class
|
||||
val l: KClass<*> = s::class
|
||||
val m: KClass<String> = String::class
|
||||
val n: KClass<Unit> = Unit::class
|
||||
}
|
||||
|
||||
+3
-3
@@ -21,9 +21,9 @@ package test {
|
||||
public final fun </*0*/ T> kotlin.collections.List<T>.testCallable2(): () -> kotlin.Unit
|
||||
public final fun </*0*/ T> kotlin.collections.List<T>.testCallable3(): () -> kotlin.Unit
|
||||
public final fun </*0*/ T> kotlin.collections.List<T>.testCallable4(): () -> kotlin.Unit
|
||||
public final fun </*0*/ T> kotlin.collections.List<T>.testClassLiteral1(): kotlin.reflect.KClass<kotlin.Int>
|
||||
public final fun </*0*/ T> kotlin.collections.List<T>.testClassLiteral2(): kotlin.reflect.KClass<kotlin.Int?>
|
||||
public final fun </*0*/ T> kotlin.collections.List<T>.testClassLiteral3(): kotlin.reflect.KClass<kotlin.Int?>
|
||||
public final fun </*0*/ T> kotlin.collections.List<T>.testClassLiteral1(): kotlin.reflect.KClass<out kotlin.Int>
|
||||
public final fun </*0*/ T> kotlin.collections.List<T>.testClassLiteral2(): kotlin.reflect.KClass<out kotlin.Int?>
|
||||
public final fun </*0*/ T> kotlin.collections.List<T>.testClassLiteral3(): kotlin.reflect.KClass<out kotlin.Int?>
|
||||
public final fun </*0*/ T> kotlin.collections.List<T>.testUnresolved1(): [ERROR : Error function type]
|
||||
public final fun </*0*/ T> kotlin.collections.List<T>.testUnresolved2(): kotlin.reflect.KFunction0<kotlin.Unit>
|
||||
public final fun </*0*/ T> kotlin.collections.List<T>.testUnresolved3(): kotlin.reflect.KFunction0<kotlin.Unit>
|
||||
|
||||
@@ -8,11 +8,11 @@ public fun f5(): kotlin.reflect.KClass<test.GenericSam<*>>
|
||||
public fun f6(): kotlin.reflect.KFunction2<test.GenericSam<*>, @kotlin.ParameterName(name = "t") kotlin.Nothing!, kotlin.Unit>
|
||||
public fun f7(): kotlin.reflect.KClass<test.GenericSam<*>>
|
||||
public fun f8(): kotlin.reflect.KFunction2<test.GenericSam<kotlin.String>, @kotlin.ParameterName(name = "t") kotlin.String!, kotlin.Unit>
|
||||
public fun g1(): kotlin.reflect.KClass<java.lang.Runnable>
|
||||
public fun g1(): kotlin.reflect.KClass<out java.lang.Runnable>
|
||||
public fun g2(): kotlin.reflect.KFunction0<kotlin.Unit>
|
||||
public fun g3(): kotlin.reflect.KClass<java.lang.Runnable>
|
||||
public fun g3(): kotlin.reflect.KClass<out java.lang.Runnable>
|
||||
public fun g4(): kotlin.reflect.KFunction0<kotlin.Unit>
|
||||
public fun g5(): kotlin.reflect.KClass<test.GenericSam<kotlin.String>>
|
||||
public fun g5(): kotlin.reflect.KClass<out test.GenericSam<kotlin.String>>
|
||||
public fun g6(): kotlin.reflect.KFunction1<@kotlin.ParameterName(name = "t") kotlin.String!, kotlin.Unit>
|
||||
public fun g7(): kotlin.reflect.KClass<test.GenericSam<kotlin.String>>
|
||||
public fun g7(): kotlin.reflect.KClass<out test.GenericSam<kotlin.String>>
|
||||
public fun g8(): kotlin.reflect.KFunction1<@kotlin.ParameterName(name = "t") kotlin.String!, kotlin.Unit>
|
||||
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
import kotlin.reflect.KClass
|
||||
|
||||
abstract class Base<T : Any>(val klass: KClass<out T>)
|
||||
|
||||
class DerivedClass : Base<DerivedClass>(DerivedClass::class)
|
||||
|
||||
object DerivedObject : Base<DerivedObject>(DerivedObject::class)
|
||||
|
||||
enum class TestEnum {
|
||||
TEST_ENTRY
|
||||
}
|
||||
|
||||
val test1: KClass<DerivedClass> = DerivedClass::class
|
||||
val test2: KClass<DerivedObject> = DerivedObject::class
|
||||
val test3: KClass<TestEnum> = TestEnum::class
|
||||
val test4: KClass<out TestEnum> = TestEnum.TEST_ENTRY::class
|
||||
+49
@@ -0,0 +1,49 @@
|
||||
package
|
||||
|
||||
public val test1: kotlin.reflect.KClass<DerivedClass>
|
||||
public val test2: kotlin.reflect.KClass<DerivedObject>
|
||||
public val test3: kotlin.reflect.KClass<TestEnum>
|
||||
public val test4: kotlin.reflect.KClass<out TestEnum>
|
||||
|
||||
public abstract class Base</*0*/ T : kotlin.Any> {
|
||||
public constructor Base</*0*/ T : kotlin.Any>(/*0*/ klass: kotlin.reflect.KClass<out T>)
|
||||
public final val klass: kotlin.reflect.KClass<out 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 DerivedClass : Base<DerivedClass> {
|
||||
public constructor DerivedClass()
|
||||
public final override /*1*/ /*fake_override*/ val klass: kotlin.reflect.KClass<out DerivedClass>
|
||||
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 object DerivedObject : Base<DerivedObject> {
|
||||
private constructor DerivedObject()
|
||||
public final override /*1*/ /*fake_override*/ val klass: kotlin.reflect.KClass<out DerivedObject>
|
||||
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 enum class TestEnum : kotlin.Enum<TestEnum> {
|
||||
enum entry TEST_ENTRY
|
||||
|
||||
private constructor TestEnum()
|
||||
public final override /*1*/ /*fake_override*/ val name: kotlin.String
|
||||
public final override /*1*/ /*fake_override*/ val ordinal: kotlin.Int
|
||||
protected final override /*1*/ /*fake_override*/ fun clone(): kotlin.Any
|
||||
public final override /*1*/ /*fake_override*/ fun compareTo(/*0*/ other: TestEnum): kotlin.Int
|
||||
public final override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
|
||||
protected/*protected and package*/ final override /*1*/ /*fake_override*/ /*isHiddenForResolutionEverywhereBesideSupercalls*/ fun finalize(): kotlin.Unit
|
||||
public final override /*1*/ /*fake_override*/ /*isHiddenForResolutionEverywhereBesideSupercalls*/ fun getDeclaringClass(): java.lang.Class<TestEnum!>!
|
||||
public final override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
|
||||
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
|
||||
|
||||
// Static members
|
||||
public final /*synthesized*/ fun valueOf(/*0*/ value: kotlin.String): TestEnum
|
||||
public final /*synthesized*/ fun values(): kotlin.Array<TestEnum>
|
||||
}
|
||||
@@ -3,7 +3,13 @@ import kotlin.reflect.KClass
|
||||
class A
|
||||
class B
|
||||
|
||||
val listOfString: List<String> = null!!
|
||||
val arrayOfString: Array<String> = null!!
|
||||
|
||||
val a1 : KClass<*> = A::class
|
||||
val a2 : KClass<A> = A::class
|
||||
val a3 : KClass<B> = <!TYPE_MISMATCH!>A::class<!>
|
||||
val a4 : B = <!TYPE_MISMATCH!>A::class<!>
|
||||
|
||||
val a5 : KClass<out List<String>> = listOfString::class
|
||||
val a6 : KClass<out Array<String>> = arrayOfString::class
|
||||
|
||||
@@ -4,6 +4,10 @@ public val a1: kotlin.reflect.KClass<*>
|
||||
public val a2: kotlin.reflect.KClass<A>
|
||||
public val a3: kotlin.reflect.KClass<B>
|
||||
public val a4: B
|
||||
public val a5: kotlin.reflect.KClass<out kotlin.collections.List<kotlin.String>>
|
||||
public val a6: kotlin.reflect.KClass<out kotlin.Array<kotlin.String>>
|
||||
public val arrayOfString: kotlin.Array<kotlin.String>
|
||||
public val listOfString: kotlin.collections.List<kotlin.String>
|
||||
|
||||
public final class A {
|
||||
public constructor A()
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
import kotlin.reflect.KClass
|
||||
|
||||
fun f(<!UNUSED_PARAMETER!>x<!>: KClass<Int>) {}
|
||||
fun f(<!UNUSED_PARAMETER!>x<!>: KClass<out Int>) {}
|
||||
|
||||
fun test() {
|
||||
f(42::class)
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package
|
||||
|
||||
public fun f(/*0*/ x: kotlin.reflect.KClass<kotlin.Int>): kotlin.Unit
|
||||
public fun f(/*0*/ x: kotlin.reflect.KClass<out kotlin.Int>): kotlin.Unit
|
||||
public fun test(): kotlin.Unit
|
||||
|
||||
+3
-3
@@ -1,9 +1,9 @@
|
||||
package
|
||||
|
||||
public val fail1: kotlin.reflect.KClass<kotlin.String>
|
||||
public val fail1: kotlin.reflect.KClass<out kotlin.String>
|
||||
public val fail2: kotlin.reflect.KClass<kotlin.String?>
|
||||
public val fail3: kotlin.reflect.KClass<C.Companion>
|
||||
public val fail4: kotlin.reflect.KClass<C.Companion>
|
||||
public val fail3: kotlin.reflect.KClass<out C.Companion>
|
||||
public val fail4: kotlin.reflect.KClass<out C.Companion>
|
||||
public val ok1: kotlin.reflect.KClass<C>
|
||||
public val ok2: kotlin.reflect.KClass<C.Companion>
|
||||
public val ok3: kotlin.reflect.KClass<O>
|
||||
|
||||
@@ -9,12 +9,12 @@ FILE /classReference.kt
|
||||
TYPE_OP origin=IMPLICIT_COERCION_TO_UNIT typeOperand=kotlin.Unit
|
||||
CLASS_REFERENCE 'A' type=kotlin.reflect.KClass<A>
|
||||
TYPE_OP origin=IMPLICIT_COERCION_TO_UNIT typeOperand=kotlin.Unit
|
||||
GET_CLASS type=kotlin.reflect.KClass<A>
|
||||
GET_CLASS type=kotlin.reflect.KClass<out A>
|
||||
CALL 'constructor A()' type=A origin=null
|
||||
TYPE_OP origin=IMPLICIT_COERCION_TO_UNIT typeOperand=kotlin.Unit
|
||||
CALL '<get-java>() on KClass<A>: Class<A>' type=java.lang.Class<A> origin=GET_PROPERTY
|
||||
$receiver: CLASS_REFERENCE 'A' type=kotlin.reflect.KClass<A>
|
||||
TYPE_OP origin=IMPLICIT_COERCION_TO_UNIT typeOperand=kotlin.Unit
|
||||
CALL '<get-java>() on KClass<A>: Class<A>' type=java.lang.Class<A> origin=GET_PROPERTY
|
||||
$receiver: GET_CLASS type=kotlin.reflect.KClass<A>
|
||||
CALL '<get-java>() on KClass<out A>: Class<out A>' type=java.lang.Class<out A> origin=GET_PROPERTY
|
||||
$receiver: GET_CLASS type=kotlin.reflect.KClass<out A>
|
||||
CALL 'constructor A()' type=A origin=null
|
||||
|
||||
@@ -24,15 +24,15 @@ FILE /reflectionLiterals.kt
|
||||
BLOCK_BODY
|
||||
RETURN type=kotlin.Nothing from='<get-test1>(): KClass<A>'
|
||||
GET_FIELD 'test1: KClass<A>' type=kotlin.reflect.KClass<A> origin=null
|
||||
PROPERTY public val test2: kotlin.reflect.KClass<kotlin.Int>
|
||||
FIELD PROPERTY_BACKING_FIELD public val test2: kotlin.reflect.KClass<kotlin.Int>
|
||||
PROPERTY public val test2: kotlin.reflect.KClass<out kotlin.Int>
|
||||
FIELD PROPERTY_BACKING_FIELD public val test2: kotlin.reflect.KClass<out kotlin.Int>
|
||||
EXPRESSION_BODY
|
||||
GET_CLASS type=kotlin.reflect.KClass<kotlin.Int>
|
||||
GET_CLASS type=kotlin.reflect.KClass<out kotlin.Int>
|
||||
CALL '<get-qux>(): Int' type=kotlin.Int origin=GET_PROPERTY
|
||||
FUN DEFAULT_PROPERTY_ACCESSOR public fun <get-test2>(): kotlin.reflect.KClass<kotlin.Int>
|
||||
FUN DEFAULT_PROPERTY_ACCESSOR public fun <get-test2>(): kotlin.reflect.KClass<out kotlin.Int>
|
||||
BLOCK_BODY
|
||||
RETURN type=kotlin.Nothing from='<get-test2>(): KClass<Int>'
|
||||
GET_FIELD 'test2: KClass<Int>' type=kotlin.reflect.KClass<kotlin.Int> origin=null
|
||||
RETURN type=kotlin.Nothing from='<get-test2>(): KClass<out Int>'
|
||||
GET_FIELD 'test2: KClass<out Int>' type=kotlin.reflect.KClass<out kotlin.Int> origin=null
|
||||
PROPERTY public val test3: kotlin.reflect.KFunction1<A, kotlin.Unit>
|
||||
FIELD PROPERTY_BACKING_FIELD public val test3: kotlin.reflect.KFunction1<A, kotlin.Unit>
|
||||
EXPRESSION_BODY
|
||||
|
||||
@@ -2998,6 +2998,12 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTest {
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("classAndObjectLiteralType.kt")
|
||||
public void testClassAndObjectLiteralType() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/classLiteral/classAndObjectLiteralType.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("classLiteralType.kt")
|
||||
public void testClassLiteralType() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/classLiteral/classLiteralType.kt");
|
||||
|
||||
@@ -59,13 +59,13 @@ class ReflectionTypes(module: ModuleDescriptor) {
|
||||
val kMutableProperty0: ClassDescriptor by ClassLookup
|
||||
val kMutableProperty1: ClassDescriptor by ClassLookup
|
||||
|
||||
fun getKClassType(annotations: Annotations, type: KotlinType): KotlinType {
|
||||
fun getKClassType(annotations: Annotations, type: KotlinType, variance: Variance): KotlinType {
|
||||
val descriptor = kClass
|
||||
if (ErrorUtils.isError(descriptor)) {
|
||||
return descriptor.defaultType
|
||||
}
|
||||
|
||||
val arguments = listOf(TypeProjectionImpl(Variance.INVARIANT, type))
|
||||
val arguments = listOf(TypeProjectionImpl(variance, type))
|
||||
return KotlinTypeFactory.simpleNotNullType(annotations, descriptor, arguments)
|
||||
}
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@ package foo
|
||||
|
||||
import kotlin.reflect.KClass
|
||||
|
||||
fun <A : Any, B : A> check(k: KClass<A>, instance: B, nonInstance: Any) {
|
||||
fun check(k: KClass<*>, instance: Any, nonInstance: Any) {
|
||||
assertTrue(k.isInstance(instance))
|
||||
assertFalse(k.isInstance(nonInstance))
|
||||
assertFalse(k.isInstance(null))
|
||||
|
||||
Reference in New Issue
Block a user