Introduce warning about implicitly inferred Nothing as a type parameter

^KT-20849 Fixed
This commit is contained in:
victor.petukhov
2019-03-13 16:28:51 +03:00
parent 1f98eaa27b
commit 70c35f4186
26 changed files with 188 additions and 24 deletions
@@ -13,13 +13,13 @@ fun doSomething() {}
fun test2() {
fun f(x: Any?) = x
f(null?.let { return })
f(null?.<!IMPLICIT_NOTHING_AS_TYPE_PARAMETER!>let<!> { return })
// false unreachable here
doSomething()
}
fun test3(x: Any?): Boolean =
x?.let {
x?.let {
return true
} ?: false
@@ -24,7 +24,7 @@ fun foo(x: MC<out Open>) {
x.addAll(<!NI;TYPE_MISMATCH, OI;TYPE_MISMATCH_DUE_TO_TYPE_PROJECTIONS!>mc<Derived>()<!>)
x.addAllMC(<!NI;TYPE_MISMATCH, OI;TYPE_MISMATCH_DUE_TO_TYPE_PROJECTIONS!>mc<Derived>()<!>)
x.addAll(c())
x.addAll(<!OI;IMPLICIT_NOTHING_AS_TYPE_PARAMETER!>c<!>())
x.addAll(c<Nothing>())
x.<!OI;MEMBER_PROJECTED_OUT!>addAllInv<!>(<!NI;TYPE_MISMATCH!>mc<Open>()<!>)
@@ -17,7 +17,7 @@ fun test(a: A<out CharSequence>, z: Out<CharSequence>) {
<!NI;TYPE_MISMATCH, NI;TYPE_MISMATCH, TYPE_MISMATCH!>""<!>
}
a.bar { <!NI;TYPE_MISMATCH, NI;TYPE_MISMATCH, NI;TYPE_MISMATCH, TYPE_MISMATCH!>Out<CharSequence>()<!> }
a.bar { Out() }
a.bar { <!OI;IMPLICIT_NOTHING_AS_TYPE_PARAMETER!>Out<!>() }
a.bar { <!NI;TYPE_MISMATCH, NI;TYPE_MISMATCH, TYPE_MISMATCH!>z.<!NI;TYPE_MISMATCH!>id()<!><!> }
a.foo {
@@ -16,7 +16,7 @@ fun foo2(a: A<out CharSequence>, b: A<in CharSequence>) {
a.<!OI;TYPE_INFERENCE_UPPER_BOUND_VIOLATED!>foo1<!>(<!NI;TYPE_MISMATCH, NI;TYPE_MISMATCH, NI;TYPE_MISMATCH!>Out<CharSequence>()<!>)
a.foo1<<!UPPER_BOUND_VIOLATED!>Out<CharSequence><!>>(<!NI;TYPE_MISMATCH!>Out()<!>)
a.foo1(Out())
a.foo1(<!OI;IMPLICIT_NOTHING_AS_TYPE_PARAMETER!>Out<!>())
a.foo1(Out<Nothing>())
a.<!OI;TYPE_INFERENCE_INCORPORATION_ERROR!>foo2<!>(<!NI;TYPE_MISMATCH!><!OI;TYPE_INFERENCE_NO_INFORMATION_FOR_PARAMETER!>Inv<!>()<!>)
@@ -7,6 +7,6 @@ fun foo1() {
<!OI;TYPE_INFERENCE_NO_INFORMATION_FOR_PARAMETER!>fooT22<!>()
}
val n : Nothing = null.sure()
val n : Nothing = null.<!OI;IMPLICIT_NOTHING_AS_TYPE_PARAMETER!>sure<!>()
fun <T : Any> T?.sure() : T = this!!
@@ -9,8 +9,8 @@ inline fun <reified T> foo2(f: (T) -> Unit): Foo<T> = Foo()
fun test1() {
val f1: Foo<out Int> = foo1 { it checkType { _<Int>() } }
val f2: Foo<in Nothing> = foo1 { it <!UNREACHABLE_CODE!>checkType { <!NI;DEBUG_INFO_UNRESOLVED_WITH_TARGET, NI;UNRESOLVED_REFERENCE_WRONG_RECEIVER!>_<!><Nothing>() }<!> }
val f2: Foo<in Nothing> = <!OI;IMPLICIT_NOTHING_AS_TYPE_PARAMETER!>foo1<!> { it <!UNREACHABLE_CODE!><!OI;IMPLICIT_NOTHING_AS_TYPE_PARAMETER!>checkType<!> { <!NI;DEBUG_INFO_UNRESOLVED_WITH_TARGET, NI;UNRESOLVED_REFERENCE_WRONG_RECEIVER!>_<!><Nothing>() }<!> }
val f3: Foo<out Int> = foo2 { it checkType { _<Int>() } }
val f4: Foo<in Nothing> = <!REIFIED_TYPE_FORBIDDEN_SUBSTITUTION!>foo2<!> { it <!UNREACHABLE_CODE!>checkType { <!NI;DEBUG_INFO_UNRESOLVED_WITH_TARGET, NI;UNRESOLVED_REFERENCE_WRONG_RECEIVER!>_<!><Nothing>() }<!> }
val f4: Foo<in Nothing> = <!OI;IMPLICIT_NOTHING_AS_TYPE_PARAMETER, REIFIED_TYPE_FORBIDDEN_SUBSTITUTION!>foo2<!> { it <!UNREACHABLE_CODE!><!OI;IMPLICIT_NOTHING_AS_TYPE_PARAMETER!>checkType<!> { <!NI;DEBUG_INFO_UNRESOLVED_WITH_TARGET, NI;UNRESOLVED_REFERENCE_WRONG_RECEIVER!>_<!><Nothing>() }<!> }
}
@@ -13,5 +13,5 @@ fun test(s: String): String {
a checkType { _<String>() }
<!UNREACHABLE_CODE!>val b =<!> TestClass { return s }
<!UNREACHABLE_CODE!>b checkType { <!NI;DEBUG_INFO_UNRESOLVED_WITH_TARGET, NI;UNRESOLVED_REFERENCE_WRONG_RECEIVER!>_<!><Nothing>() }<!>
<!UNREACHABLE_CODE!>b <!OI;IMPLICIT_NOTHING_AS_TYPE_PARAMETER!>checkType<!> { <!NI;DEBUG_INFO_UNRESOLVED_WITH_TARGET, NI;UNRESOLVED_REFERENCE_WRONG_RECEIVER!>_<!><Nothing>() }<!>
}
@@ -11,5 +11,5 @@ fun test(s: String): String {
a checkType { _<TestClass>() }
<!UNREACHABLE_CODE!>val b =<!> TestClass { return s }
<!UNREACHABLE_CODE!>b checkType { <!NI;DEBUG_INFO_UNRESOLVED_WITH_TARGET, NI;UNRESOLVED_REFERENCE_WRONG_RECEIVER!>_<!><Nothing>() }<!>
<!UNREACHABLE_CODE!>b <!OI;IMPLICIT_NOTHING_AS_TYPE_PARAMETER!>checkType<!> { <!NI;DEBUG_INFO_UNRESOLVED_WITH_TARGET, NI;UNRESOLVED_REFERENCE_WRONG_RECEIVER!>_<!><Nothing>() }<!>
}
@@ -8,7 +8,7 @@ class TestClass {
fun <T> test(value: T, test: TestClass): T {
<!UNREACHABLE_CODE!>val x =<!> test { return value }
<!UNREACHABLE_CODE!>x checkType { <!NI;DEBUG_INFO_UNRESOLVED_WITH_TARGET, NI;UNRESOLVED_REFERENCE_WRONG_RECEIVER!>_<!><Nothing>() }<!>
<!UNREACHABLE_CODE!>x <!OI;IMPLICIT_NOTHING_AS_TYPE_PARAMETER!>checkType<!> { <!NI;DEBUG_INFO_UNRESOLVED_WITH_TARGET, NI;UNRESOLVED_REFERENCE_WRONG_RECEIVER!>_<!><Nothing>() }<!>
<!UNREACHABLE_CODE!>return value<!>
}
+1 -1
View File
@@ -10,7 +10,7 @@ public class TestClass {
fun run() {
val testClass = TestClass()
// inferred as `set<Nothing>()`, return type is Nothing!
testClass.set("test", null)
testClass.<!IMPLICIT_NOTHING_AS_TYPE_PARAMETER!>set<!>("test", null)
// Should not be unreachable
run()
@@ -5,5 +5,5 @@ val <!IMPLICIT_NOTHING_PROPERTY_TYPE!>y<!> get() = null!!
fun foo() {
<!DEBUG_INFO_CONSTANT!>x<!> checkType { _<Nothing?>() }
y <!UNREACHABLE_CODE!>checkType { <!NI;DEBUG_INFO_UNRESOLVED_WITH_TARGET, NI;UNRESOLVED_REFERENCE_WRONG_RECEIVER!>_<!><Nothing>() }<!>
y <!UNREACHABLE_CODE!><!OI;IMPLICIT_NOTHING_AS_TYPE_PARAMETER!>checkType<!> { <!NI;DEBUG_INFO_UNRESOLVED_WITH_TARGET, NI;UNRESOLVED_REFERENCE_WRONG_RECEIVER!>_<!><Nothing>() }<!>
}
@@ -0,0 +1,43 @@
// !DIAGNOSTICS: -UNUSED_PARAMETER -UNCHECKED_CAST
// SKIP_TXT
// Issue: KT-20849
fun <T>test_1(x: T): T = null as T
fun <T>test_2(x: () -> T): T = null as T
fun case_1() {
null?.<!IMPLICIT_NOTHING_AS_TYPE_PARAMETER!>run<!> { return }
null!!.<!UNREACHABLE_CODE!><!IMPLICIT_NOTHING_AS_TYPE_PARAMETER!>run<!> { throw Exception() }<!>
}
fun case_2() {
test_1 { null!! }
test_2 { null!! }
}
fun case_3() {
test_1 { throw Exception() }
test_2 { throw Exception() }
}
fun case_6() {
null!!
}
fun case_7(x: Boolean?) {
<!DEBUG_INFO_IMPLICIT_EXHAUSTIVE!>when (x) {
true -> throw Exception()
false -> throw Exception()
null -> throw Exception()
}<!>
}
fun <T> something(): T = Any() as T
class Context<T>
fun <T> Any.decodeIn(typeFrom: Context<in T>): T = something()
fun <T> Any?.decodeOut(typeFrom: Context<out T>): T {
return this?.<!IMPLICIT_NOTHING_AS_TYPE_PARAMETER!>decodeIn<!>(typeFrom) ?: <!UNRESOLVED_REFERENCE!>error<!>("")
}
@@ -0,0 +1,49 @@
// !DIAGNOSTICS: -UNUSED_PARAMETER -UNCHECKED_CAST
// !LANGUAGE: +NewInference
// SKIP_TXT
// Issue: KT-20849
fun <T>test_1(x: T.() -> T): T = null as T
fun <T>test_2(x: (T) -> T): T = null as T
fun <T>test_3(x: T.(T) -> T): T = null as T
fun <T>test_4(x: T.(T) -> List<T>): T = null as T
fun <T>test_5(): List<T> = 10 <!CAST_NEVER_SUCCEEDS!>as<!> List<T>
fun <K, V>test_6(): Map<K, V> = 10 <!CAST_NEVER_SUCCEEDS!>as<!> Map<K, V>
fun case_1() {
<!IMPLICIT_NOTHING_AS_TYPE_PARAMETER!>test_5<!>()
<!IMPLICIT_NOTHING_AS_TYPE_PARAMETER!>test_6<!>()
val <!UNUSED_VARIABLE!>x<!> = <!IMPLICIT_NOTHING_AS_TYPE_PARAMETER!>test_6<!>()
}
fun case_2() {
test_1 { null!! }
}
fun case_3() {
test_2 { null!! }
}
fun case_4() {
test_3 { null!! }
}
fun case_5() {
<!IMPLICIT_NOTHING_AS_TYPE_PARAMETER!>test_4<!> { null!! }
}
fun case_6() {
test_1 { throw Exception() }
}
fun case_7() {
test_2 { throw Exception() }
}
fun case_8() {
test_3 { throw Exception() }
}
fun case_9() {
<!IMPLICIT_NOTHING_AS_TYPE_PARAMETER!>test_4<!> { throw Exception() }
}
@@ -13,4 +13,4 @@ fun <T> listOf(): List<T> = null!!
// since it has 'out' type projection in 'in' position.
val test1 = <!UNRESOLVED_REFERENCE!>BOutIn<!>(<!OI;TYPE_INFERENCE_NO_INFORMATION_FOR_PARAMETER!>listOf<!>(), null!!)
val test2 = <!OI;EXPANDED_TYPE_CANNOT_BE_CONSTRUCTED!>BInIn(listOf(), null!!)<!>
val test2 = <!OI;EXPANDED_TYPE_CANNOT_BE_CONSTRUCTED!><!OI;IMPLICIT_NOTHING_AS_TYPE_PARAMETER!>BInIn<!>(<!OI;IMPLICIT_NOTHING_AS_TYPE_PARAMETER!>listOf<!>(), null!!)<!>