[FIR TEST] Add test with some problematic smart casts

This commit is contained in:
Mikhail Glukhikh
2020-02-03 13:07:47 +03:00
parent ca5fee77d4
commit b25d99c1e5
3 changed files with 153 additions and 0 deletions
@@ -0,0 +1,57 @@
fun <T> runHigherOrder(f: () -> T): T = f()
val String.ext: Int get() = length
fun foo(a: Any?) {
val s = a as? String
val length = s?.ext ?: return
runHigherOrder {
s.isNotEmpty()
}
}
fun bar(s: String?) {
if (s?.isNotEmpty() == true) {
s.<!INAPPLICABLE_CANDIDATE!>length<!>
}
}
fun baz(s: String?) {
when {
!s.isNullOrEmpty() -> s.<!INAPPLICABLE_CANDIDATE!>length<!>
}
}
fun bazz(s: String?) {
s?.let { use(it) }
}
class Wrapper(val s: String?) {
fun withThis() {
if (s != null) {
use(this.s)
}
if (this.s != null) {
use(s)
}
}
}
fun Any.withInvoke(f: String.() -> Unit) {
if (this is String) {
<!INAPPLICABLE_CANDIDATE!>f<!>()
}
}
fun String.withInvoke(f: String.() -> Unit) {
f()
}
fun <X> withBangBang(a: X) {
if (a is String?) {
<!INAPPLICABLE_CANDIDATE!>use<!>(a!!)
}
}
fun use(s: String) {}
@@ -0,0 +1,91 @@
FILE: complexSmartCasts.kt
public final fun <T> runHigherOrder(f: R|() -> T|): R|T| {
^runHigherOrder R|<local>/f|.R|FakeOverride<kotlin/Function0.invoke: R|T|>|()
}
public final val R|kotlin/String|.ext: R|kotlin/Int|
public get(): R|kotlin/Int| {
^ this@R|/ext|.R|kotlin/String.length|
}
public final fun foo(a: R|kotlin/Any?|): R|kotlin/Unit| {
lval s: R|kotlin/String?| = (R|<local>/a| as? R|kotlin/String|)
lval length: R|kotlin/Int| = when (lval <elvis>: R|kotlin/Int?| = R|<local>/s|?.R|/ext|) {
==($subj$, Null(null)) -> {
^foo Unit
}
else -> {
R|<local>/<elvis>|
}
}
R|/runHigherOrder|<R|kotlin/Boolean|>(<L> = runHigherOrder@fun <anonymous>(): R|kotlin/Boolean| {
R|<local>/s|.R|kotlin/text/isNotEmpty|()
}
)
}
public final fun bar(s: R|kotlin/String?|): R|kotlin/Unit| {
when () {
==(R|<local>/s|?.R|kotlin/text/isNotEmpty|(), Boolean(true)) -> {
R|<local>/s|.<Inapplicable(WRONG_RECEIVER): [kotlin/String.length]>#
}
}
}
public final fun baz(s: R|kotlin/String?|): R|kotlin/Unit| {
when () {
R|<local>/s|.R|kotlin/text/isNullOrEmpty|().R|kotlin/Boolean.not|() -> {
R|<local>/s|.<Inapplicable(WRONG_RECEIVER): [kotlin/String.length]>#
}
}
}
public final fun bazz(s: R|kotlin/String?|): R|kotlin/Unit| {
R|<local>/s|?.R|kotlin/let|<R|kotlin/String|, R|kotlin/Unit|>(<L> = let@fun <anonymous>(it: R|kotlin/String|): R|kotlin/Unit| <kind=EXACTLY_ONCE> {
R|/use|(R|<local>/it|)
}
)
}
public final class Wrapper : R|kotlin/Any| {
public constructor(s: R|kotlin/String?|): R|Wrapper| {
super<R|kotlin/Any|>()
}
public final val s: R|kotlin/String?| = R|<local>/s|
public get(): R|kotlin/String?|
public final fun withThis(): R|kotlin/Unit| {
when () {
!=(this@R|/Wrapper|.R|/Wrapper.s|, Null(null)) -> {
R|/use|(this@R|/Wrapper|.R|/Wrapper.s|)
}
}
when () {
!=(this@R|/Wrapper|.R|/Wrapper.s|, Null(null)) -> {
R|/use|(this@R|/Wrapper|.R|/Wrapper.s|)
}
}
}
}
public final fun R|kotlin/Any|.withInvoke(f: R|kotlin/String.() -> kotlin/Unit|): R|kotlin/Unit| {
when () {
(this@R|/withInvoke| is R|kotlin/String|) -> {
<Inapplicable(INAPPLICABLE): [kotlin/Function1.invoke]>#()
}
}
}
public final fun R|kotlin/String|.withInvoke(f: R|kotlin/String.() -> kotlin/Unit|): R|kotlin/Unit| {
R|<local>/f|.R|FakeOverride<kotlin/Function1.invoke: R|kotlin/Unit|>|(this@R|/withInvoke|)
}
public final fun <X> withBangBang(a: R|X|): R|kotlin/Unit| {
when () {
(R|<local>/a| is R|kotlin/String?|) -> {
<Inapplicable(INAPPLICABLE): [/use]>#(R|<local>/a|!!)
}
}
}
public final fun use(s: R|kotlin/String|): R|kotlin/Unit| {
}
@@ -627,6 +627,11 @@ public class FirDiagnosticsWithStdlibTestGenerated extends AbstractFirDiagnostic
runTest("compiler/fir/resolve/testData/resolve/stdlib/problems/cloneArray.kt");
}
@TestMetadata("complexSmartCasts.kt")
public void testComplexSmartCasts() throws Exception {
runTest("compiler/fir/resolve/testData/resolve/stdlib/problems/complexSmartCasts.kt");
}
@TestMetadata("delegateTypeMismatch.kt")
public void testDelegateTypeMismatch() throws Exception {
runTest("compiler/fir/resolve/testData/resolve/stdlib/problems/delegateTypeMismatch.kt");