[FIR] Add UNCHECKED_CAST

Regenerate diagnostics code
This commit is contained in:
Ivan Kochurkin
2021-09-16 23:24:58 +03:00
committed by TeamCityServer
parent 1fbccff1bd
commit d0a4ca199d
70 changed files with 118 additions and 933 deletions
+1 -1
View File
@@ -43,7 +43,7 @@ typealias ArrayOfEnumEntry = Array<Color.<!ENUM_ENTRY_AS_TYPE!>RED<!>>
typealias ArrayOfEnumEntryAlias = Array<RedAlias>
fun <T> bar(a: Any): T = a as T
fun <T> bar(a: Any): T = a <!UNCHECKED_CAST!>as T<!>
fun <T> foo() {
foo<Color.<!ENUM_ENTRY_AS_TYPE!>RED<!>>()
@@ -26,7 +26,7 @@ fun test() {
c
val d = id(Scope.Nested<*>::keyT as Scope.Nested<Number>)
val d = id(Scope.Nested<*>::keyT <!UNCHECKED_CAST!>as Scope.Nested<Number><!>)
d
@@ -40,4 +40,4 @@ fun justResolve() {
val b = Scope.Nested<String>::keyT
val c = Scope.Nested<*>::keyT
val d = Scope.Nested<out Number?>::keyT
}
}
@@ -1 +0,0 @@
fun f(x: Any) = x as Array<String>
+1
View File
@@ -1 +1,2 @@
// FIR_IDENTICAL
fun f(x: Any) = x <!UNCHECKED_CAST!>as Array<String><!>
@@ -1,2 +0,0 @@
fun ff(c: MutableCollection<String>) = c as MutableList<Int>
@@ -1,2 +1,3 @@
// FIR_IDENTICAL
fun ff(c: MutableCollection<String>) = c <!UNCHECKED_CAST!>as MutableList<Int><!>
@@ -1,2 +0,0 @@
fun ff(a: Any) = a as MutableList<String>
@@ -1,2 +1,3 @@
// FIR_IDENTICAL
fun ff(a: Any) = a <!UNCHECKED_CAST!>as MutableList<String><!>
@@ -1,34 +0,0 @@
// See also: KT-6611 (cast can never succeed: Class<T> -> Class<Any>)
class Class<T>(val name: String, val instance: T)
fun <T> test(clazz: Class<T>) {
println((clazz as Class<Any>).name)
}
fun use() {
test(Class("String", ""))
}
fun checkArrays(): Array<Any> {
val someArray = arrayOfNulls<Any>(5)
someArray as Array<Int>
return someArray as Array<Any>
}
class Wrapper<T>(val x: T)
fun checkArrays2(): Array<Wrapper<String>> {
val someArray = arrayOf(Wrapper(1), Wrapper(2))
return someArray as Array<Wrapper<String>>
}
fun checkArrays3() {
val someArray = arrayOfNulls<String>(1)
someArray as Array<Any>
val intArray = arrayOfNulls<Int>(1)
intArray as Array<Any>
}
fun println(s: String) = s
@@ -1,3 +1,4 @@
// FIR_IDENTICAL
// See also: KT-6611 (cast can never succeed: Class<T> -> Class<Any>)
class Class<T>(val name: String, val instance: T)
@@ -10,9 +10,9 @@ fun <T, S : T> test(x: T?, y: S, z: T) {
<!USELESS_IS_CHECK!>z is T<!>
<!USELESS_IS_CHECK!>z is T?<!>
null as T
null <!UNCHECKED_CAST!>as T<!>
null as T?
null as S
null <!UNCHECKED_CAST!>as S<!>
}
inline fun <reified T> test(x: T?) {
@@ -25,7 +25,7 @@ class MyProperty<R, T> {
operator fun getValue(thisRef: R, desc: KProperty<*>): T {
println("get $thisRef ${desc.name}")
return null as T
return null <!UNCHECKED_CAST!>as T<!>
}
operator fun setValue(thisRef: R, desc: KProperty<*>, value: T) {
@@ -22,7 +22,7 @@ fun <E> foo(x: Any, y: Any) : Any {
return y
}
y as Outer<*>.Inner
y <!UNCHECKED_CAST!>as Outer<*>.Inner<!>
return C()
}
@@ -39,7 +39,7 @@ fun main1() {
// --- from Kotlin --- //
public class Bar<T> {
var value: T = null as T
var value: T = null <!UNCHECKED_CAST!>as T<!>
}
fun takeStarBar(x: Bar<*>) {
@@ -115,4 +115,4 @@ fun main6() {
bar.value = 1
takeStarBar3(bar)
println(bar.value) // CCE: String cannot be cast to Number
}
}
@@ -39,7 +39,7 @@ fun main1() {
// --- from Kotlin --- //
public class Bar<T> {
var value: T = null as T
var value: T = null <!UNCHECKED_CAST!>as T<!>
}
fun takeStarBar(x: Bar<*>) {
@@ -2,7 +2,7 @@
fun foo() = 1
fun <T> foo() = foo() as T
fun <T> foo() = foo() <!UNCHECKED_CAST!>as T<!>
fun <T> foo2(): T = TODO()
@@ -12,7 +12,7 @@ object WriterAppender {
class Builder1<B : Builder1<B>> {
fun asBuilder(): B {
return this as B
return this <!UNCHECKED_CAST!>as B<!>
}
}
@@ -23,4 +23,4 @@ object WriterAppender {
fun <B> intersectTwoSelfTypes(): B where B : Builder1<B>, B: Builder2<B> {
return Builder1<B>().asBuilder()
}
}
}
@@ -37,7 +37,7 @@ object WriterAppender {
class Builder1<B : Builder1<B>> {
fun asBuilder(): B {
return this as B
return this <!UNCHECKED_CAST!>as B<!>
}
}
@@ -48,4 +48,4 @@ object WriterAppender {
fun <B> intersectTwoSelfTypes(): B where B : Builder1<B>, B: Builder2<B> {
return Builder1<B>().asBuilder()
}
}
}
@@ -11,7 +11,7 @@ class B<T> {
fun <Y> foo(c: A<Y>): Y = TODO()
fun <E> main(a: A<E>) {
a as A<B<*>>
a <!UNCHECKED_CAST!>as A<B<*>><!>
foo(a).b()
}
@@ -21,7 +21,7 @@ class AOut<out X>
fun <Y> foo(c: AOut<Y>): Y = TODO()
fun <E> mainOut(a: AOut<E>) {
a as AOut<B<*>>
a <!UNCHECKED_CAST!>as AOut<B<*>><!>
foo(a).b()
}
@@ -1,14 +0,0 @@
// !LANGUAGE: +NewInference
// !DIAGNOSTICS: -UNUSED_PARAMETER -UNUSED_VARIABLE
class Inv<T>
fun <T> materializeInv() = Inv<T>()
fun <X> foo(x: Inv<X>, y: X) = materializeInv<X>()
fun <X> foo(x: Inv<X>, y: () -> X) = materializeInv<X>()
fun <R> main(fn: () -> R) {
fun bar(): R = null as R
val x1 = foo<R>(materializeInv()) { fn() } // OVERLOAD_RESOLUTION_AMBIGUITY only in NI
val x2 = foo<R>(materializeInv(), fn) // OK
val x3 = foo<R>(materializeInv(), ::bar) // OK
}
@@ -1,3 +1,4 @@
// FIR_IDENTICAL
// !LANGUAGE: +NewInference
// !DIAGNOSTICS: -UNUSED_PARAMETER -UNUSED_VARIABLE
@@ -1,20 +0,0 @@
//KT-2856 Fix the getOrElse signature to be able to return any supertype of V
package d
import java.util.HashMap
public inline fun <K,V1, V: V1> Map<K,V>.getOrElse1(key: K, defaultValue: ()-> V1) : V1 {
if (this.containsKey(key)) {
return this.get(key) as V
} else {
return defaultValue()
}
}
fun main() {
val map = HashMap<Int, Int>()
println(map.getOrElse1(2, { null })) // Error
}
//from standard library
fun println(message : Any?) {}
@@ -1,3 +1,4 @@
// FIR_IDENTICAL
//KT-2856 Fix the getOrElse signature to be able to return any supertype of V
package d
@@ -26,7 +26,7 @@ fun <T> testMutableCollection(c: MutableCollection<T>, t: T) {
val mutableIterator: MutableIterator<T> = c.iterator()
c.add(t)
c.remove(1 as T)
c.remove(1 <!UNCHECKED_CAST!>as T<!>)
c.addAll(c)
c.removeAll(c)
c.retainAll(c)
@@ -84,7 +84,7 @@ fun <T> testMutableSet(s: MutableSet<T>, t: T) {
val mutableIterator: MutableIterator<T> = s.iterator()
s.add(t)
s.remove(1 as T)
s.remove(1 <!UNCHECKED_CAST!>as T<!>)
s.addAll(s)
s.removeAll(s)
s.retainAll(s)
@@ -1,48 +0,0 @@
// !DIAGNOSTICS: -UNUSED_PARAMETER, -SENSELESS_COMPARISON, -DEBUG_INFO_SMARTCAST
fun <T: Any?> test1(t: Any?): Any {
return t as T ?: ""
}
fun <T: Any> test2(t: Any?): Any {
return t as T <!USELESS_ELVIS!>?: ""<!>
}
fun <T: Any?> test3(t: Any?): Any {
if (t != null) {
return t <!USELESS_ELVIS!>?: ""<!>
}
return 1
}
fun takeNotNull(s: String) {}
fun <T> notNull(): T = TODO()
fun <T> nullable(): T? = null
fun <T> dependOn(x: T) = x
fun test() {
takeNotNull(notNull() ?: "")
takeNotNull(nullable() ?: "")
val x: String? = null
takeNotNull(dependOn(x) ?: "")
takeNotNull(dependOn(dependOn(x)) ?: "")
takeNotNull(dependOn(dependOn(x as String)) <!USELESS_ELVIS!>?: ""<!>)
if (x != null) {
takeNotNull(dependOn(x) <!USELESS_ELVIS!>?: ""<!>)
takeNotNull(dependOn(dependOn(x)) <!USELESS_ELVIS!>?: ""<!>)
takeNotNull(dependOn(dependOn(x) as? String) ?: "")
}
takeNotNull(bar()!!)
}
inline fun <reified T : Any> reifiedNull(): T? = null
fun testFrom13648() {
takeNotNull(reifiedNull() ?: "")
}
fun bar() = <!UNRESOLVED_REFERENCE!>unresolved<!>
@@ -1,3 +1,4 @@
// FIR_IDENTICAL
// !DIAGNOSTICS: -UNUSED_PARAMETER, -SENSELESS_COMPARISON, -DEBUG_INFO_SMARTCAST
fun <T: Any?> test1(t: Any?): Any {
@@ -1,67 +0,0 @@
// !DIAGNOSTICS: -UNUSED_VARIABLE
// FILE: A.java
import java.util.*;
public class A {
void foo(List<Object> x) {}
void foo(Iterable<Object> x) {}
void foo(Iterator<Object> x) {}
void foo(Set<Object> x) {}
void foo(Map<Object, Object> x) {}
void foo(Map.Entry<Object, Object> x) {}
void foo1(List<List<Object>> x) {}
}
// FILE: main.kt
fun main(
a: A,
ml: MutableList<String>, l: List<String>,
ms: MutableSet<String>, s: Set<String>,
mm: MutableMap<Any, String>, m: Map<Any, String>,
mme: MutableMap.MutableEntry<Any, String>, me: Map.Entry<Any, String>,
mll: MutableList<MutableList<String>>, ll: List<List<String>>
) {
// Lists
a.foo(<!JAVA_TYPE_MISMATCH!>ml<!>)
a.foo(l)
a.foo(ml as MutableList<Any>)
a.foo(l as List<Any>)
// Iterables
val mit: MutableIterable<String> = ml
val it: Iterable<String> = ml
a.foo(mit)
a.foo(it)
// Iterators
a.foo(ml.iterator())
a.foo(l.iterator())
// Sets
a.foo(<!JAVA_TYPE_MISMATCH!>ms<!>)
a.foo(s)
a.foo(ms as MutableSet<Any>)
a.foo(s as Set<Any>)
// Maps
a.foo(<!JAVA_TYPE_MISMATCH!>mm<!>)
a.foo(m)
a.foo(mm as MutableMap<Any, Any>)
a.foo(m as Map<Any, Any>)
// Map entries
a.foo(<!JAVA_TYPE_MISMATCH!>mme<!>)
a.foo(me)
a.foo(mme as MutableMap.MutableEntry<Any, Any>)
a.foo(me as Map.Entry<Any, Any>)
// Lists of lists
a.foo1(<!JAVA_TYPE_MISMATCH!>mll<!>)
a.foo1(ll)
a.foo1(mll as MutableList<MutableList<Any>>)
a.foo1(ll as List<List<Any>>)
}
@@ -1,3 +1,4 @@
// FIR_IDENTICAL
// !DIAGNOSTICS: -UNUSED_VARIABLE
// FILE: A.java
@@ -11,6 +11,6 @@ public class A {
fun main(a: A, ml: Any) {
if (ml is <!CANNOT_CHECK_FOR_ERASED!>MutableList<String><!>) {
a.foo(<!JAVA_TYPE_MISMATCH!>ml<!>)
a.foo(ml as List<Any>)
a.foo(ml <!UNCHECKED_CAST!>as List<Any><!>)
}
}
@@ -1,11 +0,0 @@
// KT-307 Unresolved reference
open class AL {
fun get(i : Int) : Any? = i
}
interface ALE<T> : <!INTERFACE_WITH_SUPERCLASS!>AL<!> {
fun getOrNull(index: Int, value: T) : T {
return get(index) as? T ?: value
}
}
@@ -1,3 +1,4 @@
// FIR_IDENTICAL
// KT-307 Unresolved reference
open class AL {
@@ -4,5 +4,5 @@
class IdUnavailableException() : Exception() {}
fun <T : Any> T.getJavaClass() : Class<T> {
return ((this as Object).getClass()) as Class<T> // Some error here, because of Exception() used above. ?!!!
}
return ((this as Object).getClass()) <!UNCHECKED_CAST!>as Class<T><!> // Some error here, because of Exception() used above. ?!!!
}
@@ -6,7 +6,7 @@ fun <T> typeinfo() : TypeInfo<T> = null <!CAST_NEVER_SUCCEEDS!>as<!> TypeInfo<T>
fun <T> TypeInfo<T>.getJavaClass() : java.lang.Class<T> {
val t : java.lang.Object = this as java.lang.Object
return t.getClass() as java.lang.Class<T> // inferred type is Object but Serializable was expected
return t.getClass() <!UNCHECKED_CAST!>as java.lang.Class<T><!> // inferred type is Object but Serializable was expected
}
fun <T> getJavaClass() = typeinfo<T>().getJavaClass()
@@ -9,12 +9,12 @@ fun test2() = J { x: String -> x }
fun test3() = H.bar { x: String -> x }
fun test4(a: Any) {
a as J<String>
a <!UNCHECKED_CAST!>as J<String><!>
H.bar(a)
}
fun test5(a: Any) {
a as (String) -> String
a <!UNCHECKED_CAST!>as (String) -> String<!>
H.bar(a)
}
@@ -23,7 +23,7 @@ fun <T> test6(a: (T) -> T) {
}
fun <T> test7(a: Any) {
a as (T) -> T
a <!UNCHECKED_CAST!>as (T) -> T<!>
H.bar(a)
}
@@ -55,4 +55,4 @@ public interface J2X<T3> extends J2<String, T3> {
public class H {
public static <X> void bar(J<X> j) {}
public static <Y> void bar2x(J2X<Y> j2x) {}
}
}
@@ -37,18 +37,18 @@ fun test5(a: Any) {
fun test5x(a: Any) {
if (a is Runnable) {
a as () -> Unit
a <!UNCHECKED_CAST!>as () -> Unit<!>
J().run1(a)
}
}
fun test6(a: Any) {
a as () -> Unit
a <!UNCHECKED_CAST!>as () -> Unit<!>
J().run1(a)
}
fun test7(a: (Int) -> Int) {
a as () -> Unit
a <!UNCHECKED_CAST!>as () -> Unit<!>
J().run1(a)
}
@@ -69,4 +69,4 @@ public class J {
public void run2(Runnable r1, Runnable r2) {}
public static <T> T id(T x) { return x; }
}
}
@@ -3,12 +3,12 @@
class Qwe<T : Any>(val a: T?) {
fun test1(obj: Any) {
obj as Qwe<T>
obj <!UNCHECKED_CAST!>as Qwe<T><!>
check(obj.a)
}
fun test1(obj: Qwe<*>) {
obj as Qwe<T>
obj <!UNCHECKED_CAST!>as Qwe<T><!>
check(obj.a)
}
@@ -1,8 +0,0 @@
// Works already in M11
fun test(c : Class<*>) {
val sc = c as Class<String>
// No ambiguous overload
c.getAnnotations();
sc.getAnnotations();
}
@@ -1,3 +1,4 @@
// FIR_IDENTICAL
// Works already in M11
fun test(c : Class<*>) {
@@ -1,8 +0,0 @@
package h
public class MyClass<S, T>(param: MyClass<S, T>) {
fun test() {
val result: MyClass<Any, Any>? = null
MyClass<S, Any>(result as MyClass<S, Any>)
}
}
@@ -1,3 +1,4 @@
// FIR_IDENTICAL
package h
public class MyClass<S, T>(param: MyClass<S, T>) {