diff --git a/compiler/fir/resolve/testData/resolve/expresssions/syntheticSmartCast.kt b/compiler/fir/resolve/testData/resolve/expresssions/syntheticSmartCast.kt new file mode 100644 index 00000000000..c36f2b2f402 --- /dev/null +++ b/compiler/fir/resolve/testData/resolve/expresssions/syntheticSmartCast.kt @@ -0,0 +1,57 @@ +// FILE: SomeClass.java +import org.jetbrains.annotations.Nullable; + +public class SomeClass { + @Nullable + public CharSequence getBar(); + + public int getFoo(); +} + +// FILE: test.kt + +class AnotherClass(val bar: CharSequence?, val foo: Int) { + fun baz(): Any = true +} + +fun test1(x: AnotherClass?) { + val bar = x?.bar ?: return + x.bar +} + +fun test2(x: SomeClass?) { + val bar = x?.bar ?: return + x.bar +} + +fun test3(x: AnotherClass?) { + val bar = x?.bar + if (bar != null) { + x.bar.length + } +} + +fun test4(x: SomeClass?) { + val bar = x?.bar + if (bar != null) { + x.bar.length + } +} + +fun test5(x: AnotherClass?) { + val bar = x?.bar as? String ?: return + x.foo +} + +fun test6(x: SomeClass?) { + val bar = x?.bar as? String ?: return + x.foo +} + +fun test7(x: AnotherClass?) { + val baz = x?.baz() as? Boolean ?: return + x.foo +} + + + diff --git a/compiler/fir/resolve/testData/resolve/expresssions/syntheticSmartCast.txt b/compiler/fir/resolve/testData/resolve/expresssions/syntheticSmartCast.txt new file mode 100644 index 00000000000..f7f955a1db9 --- /dev/null +++ b/compiler/fir/resolve/testData/resolve/expresssions/syntheticSmartCast.txt @@ -0,0 +1,95 @@ +FILE: test.kt + public final class AnotherClass : R|kotlin/Any| { + public constructor(bar: R|kotlin/CharSequence?|, foo: R|kotlin/Int|): R|AnotherClass| { + super() + } + + public final val bar: R|kotlin/CharSequence?| = R|/bar| + public get(): R|kotlin/CharSequence?| + + public final val foo: R|kotlin/Int| = R|/foo| + public get(): R|kotlin/Int| + + public final fun baz(): R|kotlin/Any| { + ^baz Boolean(true) + } + + } + public final fun test1(x: R|AnotherClass?|): R|kotlin/Unit| { + lval bar: R|kotlin/CharSequence| = when (lval : R|kotlin/CharSequence?| = R|/x|?.R|/AnotherClass.bar|) { + ==($subj$, Null(null)) -> { + ^test1 Unit + } + else -> { + R|/| + } + } + + R|/x|.R|/AnotherClass.bar| + } + public final fun test2(x: R|SomeClass?|): R|kotlin/Unit| { + lval bar: R|kotlin/CharSequence| = when (lval : R|kotlin/CharSequence?| = R|/x|?.R|/SomeClass.bar|) { + ==($subj$, Null(null)) -> { + ^test2 Unit + } + else -> { + R|/| + } + } + + R|/x|.R|/SomeClass.bar| + } + public final fun test3(x: R|AnotherClass?|): R|kotlin/Unit| { + lval bar: R|kotlin/CharSequence?| = R|/x|?.R|/AnotherClass.bar| + when () { + !=(R|/bar|, Null(null)) -> { + R|/x|.R|/AnotherClass.bar|.R|kotlin/CharSequence.length| + } + } + + } + public final fun test4(x: R|SomeClass?|): R|kotlin/Unit| { + lval bar: R|kotlin/CharSequence?| = R|/x|?.R|/SomeClass.bar| + when () { + !=(R|/bar|, Null(null)) -> { + R|/x|.R|/SomeClass.bar|.# + } + } + + } + public final fun test5(x: R|AnotherClass?|): R|kotlin/Unit| { + lval bar: R|kotlin/String| = when (lval : R|kotlin/String?| = (R|/x|?.R|/AnotherClass.bar| as? R|kotlin/String|)) { + ==($subj$, Null(null)) -> { + ^test5 Unit + } + else -> { + R|/| + } + } + + R|/x|.# + } + public final fun test6(x: R|SomeClass?|): R|kotlin/Unit| { + lval bar: R|kotlin/String| = when (lval : R|kotlin/String?| = (R|/x|?.R|/SomeClass.bar| as? R|kotlin/String|)) { + ==($subj$, Null(null)) -> { + ^test6 Unit + } + else -> { + R|/| + } + } + + R|/x|.# + } + public final fun test7(x: R|AnotherClass?|): R|kotlin/Unit| { + lval baz: R|kotlin/Boolean| = when (lval : R|kotlin/Boolean?| = (R|/x|?.R|/AnotherClass.baz|() as? R|kotlin/Boolean|)) { + ==($subj$, Null(null)) -> { + ^test7 Unit + } + else -> { + R|/| + } + } + + R|/x|.R|/AnotherClass.foo| + } diff --git a/compiler/fir/resolve/tests/org/jetbrains/kotlin/fir/FirDiagnosticsTestGenerated.java b/compiler/fir/resolve/tests/org/jetbrains/kotlin/fir/FirDiagnosticsTestGenerated.java index f27aa1724b8..bbc8dd8fc55 100644 --- a/compiler/fir/resolve/tests/org/jetbrains/kotlin/fir/FirDiagnosticsTestGenerated.java +++ b/compiler/fir/resolve/tests/org/jetbrains/kotlin/fir/FirDiagnosticsTestGenerated.java @@ -689,6 +689,11 @@ public class FirDiagnosticsTestGenerated extends AbstractFirDiagnosticsTest { runTest("compiler/fir/resolve/testData/resolve/expresssions/simple.kt"); } + @TestMetadata("syntheticSmartCast.kt") + public void testSyntheticSmartCast() throws Exception { + runTest("compiler/fir/resolve/testData/resolve/expresssions/syntheticSmartCast.kt"); + } + @TestMetadata("this.kt") public void testThis() throws Exception { runTest("compiler/fir/resolve/testData/resolve/expresssions/this.kt"); diff --git a/compiler/fir/resolve/tests/org/jetbrains/kotlin/fir/FirDiagnosticsWithLightTreeTestGenerated.java b/compiler/fir/resolve/tests/org/jetbrains/kotlin/fir/FirDiagnosticsWithLightTreeTestGenerated.java index 5f33268544f..fcdf429dc52 100644 --- a/compiler/fir/resolve/tests/org/jetbrains/kotlin/fir/FirDiagnosticsWithLightTreeTestGenerated.java +++ b/compiler/fir/resolve/tests/org/jetbrains/kotlin/fir/FirDiagnosticsWithLightTreeTestGenerated.java @@ -689,6 +689,11 @@ public class FirDiagnosticsWithLightTreeTestGenerated extends AbstractFirDiagnos runTest("compiler/fir/resolve/testData/resolve/expresssions/simple.kt"); } + @TestMetadata("syntheticSmartCast.kt") + public void testSyntheticSmartCast() throws Exception { + runTest("compiler/fir/resolve/testData/resolve/expresssions/syntheticSmartCast.kt"); + } + @TestMetadata("this.kt") public void testThis() throws Exception { runTest("compiler/fir/resolve/testData/resolve/expresssions/this.kt");