diff --git a/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/DiagnosticCompilerTestFE10TestdataTestGenerated.java b/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/DiagnosticCompilerTestFE10TestdataTestGenerated.java index 5695dae87ab..06c54703b23 100644 --- a/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/DiagnosticCompilerTestFE10TestdataTestGenerated.java +++ b/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/DiagnosticCompilerTestFE10TestdataTestGenerated.java @@ -35082,6 +35082,12 @@ public class DiagnosticCompilerTestFE10TestdataTestGenerated extends AbstractDia runTest("compiler/testData/diagnostics/tests/smartCasts/enumEntryMembers_before.kt"); } + @Test + @TestMetadata("equalitySmartcast.kt") + public void testEqualitySmartcast() throws Exception { + runTest("compiler/testData/diagnostics/tests/smartCasts/equalitySmartcast.kt"); + } + @Test @TestMetadata("equals.kt") public void testEquals() throws Exception { diff --git a/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/LLFirPreresolvedReversedDiagnosticCompilerFE10TestDataTestGenerated.java b/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/LLFirPreresolvedReversedDiagnosticCompilerFE10TestDataTestGenerated.java index 122263f7a85..9ae9cfdf357 100644 --- a/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/LLFirPreresolvedReversedDiagnosticCompilerFE10TestDataTestGenerated.java +++ b/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/LLFirPreresolvedReversedDiagnosticCompilerFE10TestDataTestGenerated.java @@ -35082,6 +35082,12 @@ public class LLFirPreresolvedReversedDiagnosticCompilerFE10TestDataTestGenerated runTest("compiler/testData/diagnostics/tests/smartCasts/enumEntryMembers_before.kt"); } + @Test + @TestMetadata("equalitySmartcast.kt") + public void testEqualitySmartcast() throws Exception { + runTest("compiler/testData/diagnostics/tests/smartCasts/equalitySmartcast.kt"); + } + @Test @TestMetadata("equals.kt") public void testEquals() throws Exception { diff --git a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirLightTreeOldFrontendDiagnosticsTestGenerated.java b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirLightTreeOldFrontendDiagnosticsTestGenerated.java index c53285e7a44..b0741c9a713 100644 --- a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirLightTreeOldFrontendDiagnosticsTestGenerated.java +++ b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirLightTreeOldFrontendDiagnosticsTestGenerated.java @@ -32726,6 +32726,12 @@ public class FirLightTreeOldFrontendDiagnosticsTestGenerated extends AbstractFir runTest("compiler/testData/diagnostics/tests/smartCasts/enumEntryMembers_before.kt"); } + @Test + @TestMetadata("equalitySmartcast.kt") + public void testEqualitySmartcast() throws Exception { + runTest("compiler/testData/diagnostics/tests/smartCasts/equalitySmartcast.kt"); + } + @Test @TestMetadata("equals.kt") public void testEquals() throws Exception { diff --git a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirPsiOldFrontendDiagnosticsTestGenerated.java b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirPsiOldFrontendDiagnosticsTestGenerated.java index fa5116a8acc..327bb019fda 100644 --- a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirPsiOldFrontendDiagnosticsTestGenerated.java +++ b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirPsiOldFrontendDiagnosticsTestGenerated.java @@ -32852,6 +32852,12 @@ public class FirPsiOldFrontendDiagnosticsTestGenerated extends AbstractFirPsiDia runTest("compiler/testData/diagnostics/tests/smartCasts/enumEntryMembers_before.kt"); } + @Test + @TestMetadata("equalitySmartcast.kt") + public void testEqualitySmartcast() throws Exception { + runTest("compiler/testData/diagnostics/tests/smartCasts/equalitySmartcast.kt"); + } + @Test @TestMetadata("equals.kt") public void testEquals() throws Exception { diff --git a/compiler/testData/diagnostics/tests/smartCasts/equalitySmartcast.fir.kt b/compiler/testData/diagnostics/tests/smartCasts/equalitySmartcast.fir.kt new file mode 100644 index 00000000000..5e995729b1b --- /dev/null +++ b/compiler/testData/diagnostics/tests/smartCasts/equalitySmartcast.fir.kt @@ -0,0 +1,99 @@ +// SKIP_TXT +// ISSUE: KT-57513, KT-58169 + +fun string(foo: Any) { + val string = "" + if ("" == foo) foo.length + if (string == foo) foo.length + if (foo == "") foo.length +} + +fun int(foo: Any) { + val int = 1 + if (1 == foo) foo.plus(1) + if (int == foo) foo.plus(1) + if (foo == 1) foo.plus(1) +} + +fun long(foo: Any) { + val long = 1L + if (1L == foo) foo.plus(1L) + if (long == foo) foo.plus(1L) + if (foo == 1L) foo.plus(1L) +} + +fun char(foo: Any) { + val char = 'a' + if ('a' == foo) foo.compareTo('a') + if (char == foo) foo.compareTo('a') + if (foo == 'a') foo.compareTo('a') +} + +class A { fun a() = Unit } +class B { fun b() = Unit; override fun equals(other: Any?): Boolean = this === other } +data class C(val x: Int) { fun c() = Unit } +open class D { fun d() = Unit } +enum class E { ONE; fun e() = Unit } + +fun testA(foo: A, bar: Any) { + if (foo == bar) bar.a() + if (bar == foo) bar.a() +} + +fun testNullableA(foo: A?, bar: Any?) { + if (foo != null && foo == bar) bar.a() +} + +fun testB(foo: B, bar: Any) { + if (foo == bar) bar.b() + if (bar == foo) bar.b() +} + +fun testNullableB(foo: B?, bar: B?) { + if (foo != null && foo == bar) bar.b() +} + +fun testC(foo: C, bar: Any) { + if (foo == bar) bar.c() + if (bar == foo) bar.c() +} + +fun testNullableC(foo: C?, bar: C?) { + if (foo != null && foo == bar) bar.c() +} + +fun testD(foo: D, bar: Any) { + if (foo == bar) bar.d() + if (bar == foo) bar.d() +} + +fun testNullableD(foo: D?, bar: D?) { + if (foo != null && foo == bar) bar.d() +} + +fun testE(foo: E, bar: Any) { + if (foo == bar) bar.e() + if (bar == foo) bar.e() +} + +fun testNullableE(foo: E?, bar: E?) { + if (foo != null && foo == bar) bar.e() +} + +fun testSmartcast(foo: Any, bar: Any) { + if (foo is A && foo == bar) { + bar.a() + } + if (foo is B && foo == bar) { + bar.b() + } + if (foo is C && foo == bar) { + bar.c() + } + if (foo is D && foo == bar) { + bar.d() + } + if (foo is E && foo == bar) { + bar.e() + } +} diff --git a/compiler/testData/diagnostics/tests/smartCasts/equalitySmartcast.kt b/compiler/testData/diagnostics/tests/smartCasts/equalitySmartcast.kt new file mode 100644 index 00000000000..e58edcc801d --- /dev/null +++ b/compiler/testData/diagnostics/tests/smartCasts/equalitySmartcast.kt @@ -0,0 +1,99 @@ +// SKIP_TXT +// ISSUE: KT-57513, KT-58169 + +fun string(foo: Any) { + val string = "" + if ("" == foo) foo.length + if (string == foo) foo.length + if (foo == "") foo.length +} + +fun int(foo: Any) { + val int = 1 + if (1 == foo) foo.plus(1) + if (int == foo) foo.plus(1) + if (foo == 1) foo.plus(1) +} + +fun long(foo: Any) { + val long = 1L + if (1L == foo) foo.plus(1L) + if (long == foo) foo.plus(1L) + if (foo == 1L) foo.plus(1L) +} + +fun char(foo: Any) { + val char = 'a' + if ('a' == foo) foo.compareTo('a') + if (char == foo) foo.compareTo('a') + if (foo == 'a') foo.compareTo('a') +} + +class A { fun a() = Unit } +class B { fun b() = Unit; override fun equals(other: Any?): Boolean = this === other } +data class C(val x: Int) { fun c() = Unit } +open class D { fun d() = Unit } +enum class E { ONE; fun e() = Unit } + +fun testA(foo: A, bar: Any) { + if (foo == bar) bar.a() + if (bar == foo) bar.a() +} + +fun testNullableA(foo: A?, bar: Any?) { + if (foo != null && foo == bar) bar.a() +} + +fun testB(foo: B, bar: Any) { + if (foo == bar) bar.b() + if (bar == foo) bar.b() +} + +fun testNullableB(foo: B?, bar: B?) { + if (foo != null && foo == bar) bar.b() +} + +fun testC(foo: C, bar: Any) { + if (foo == bar) bar.c() + if (bar == foo) bar.c() +} + +fun testNullableC(foo: C?, bar: C?) { + if (foo != null && foo == bar) bar.c() +} + +fun testD(foo: D, bar: Any) { + if (foo == bar) bar.d() + if (bar == foo) bar.d() +} + +fun testNullableD(foo: D?, bar: D?) { + if (foo != null && foo == bar) bar.d() +} + +fun testE(foo: E, bar: Any) { + if (foo == bar) bar.e() + if (bar == foo) bar.e() +} + +fun testNullableE(foo: E?, bar: E?) { + if (foo != null && foo == bar) bar.e() +} + +fun testSmartcast(foo: Any, bar: Any) { + if (foo is A && foo == bar) { + bar.a() + } + if (foo is B && foo == bar) { + bar.b() + } + if (foo is C && foo == bar) { + bar.c() + } + if (foo is D && foo == bar) { + bar.d() + } + if (foo is E && foo == bar) { + bar.e() + } +} diff --git a/compiler/testData/diagnostics/tests/smartCasts/equals.fir.kt b/compiler/testData/diagnostics/tests/smartCasts/equals.fir.kt index 18d903ce1e7..b469ddc1049 100644 --- a/compiler/testData/diagnostics/tests/smartCasts/equals.fir.kt +++ b/compiler/testData/diagnostics/tests/smartCasts/equals.fir.kt @@ -1,3 +1,5 @@ +// SKIP_TXT + fun foo(x: String?) = x class Test @@ -23,31 +25,3 @@ fun gav(i: TestWithEquals?, j: TestWithEquals?) { if (i == j) foo(i) } } - -fun string(foo: Any) { - val string = "" - if ("" == foo) foo.length - if (string == foo) foo.length - if (foo == "") foo.length -} - -fun int(foo: Any) { - val int = 1 - if (1 == foo) foo.plus(1) - if (int == foo) foo.plus(1) - if (foo == 1) foo.plus(1) -} - -fun long(foo: Any) { - val long = 1L - if (1L == foo) foo.plus(1L) - if (long == foo) foo.plus(1L) - if (foo == 1L) foo.plus(1L) -} - -fun char(foo: Any) { - val char = 'a' - if ('a' == foo) foo.compareTo('a') - if (char == foo) foo.compareTo('a') - if (foo == 'a') foo.compareTo('a') -} diff --git a/compiler/testData/diagnostics/tests/smartCasts/equals.kt b/compiler/testData/diagnostics/tests/smartCasts/equals.kt index db9450c121e..8ec0332d8f4 100644 --- a/compiler/testData/diagnostics/tests/smartCasts/equals.kt +++ b/compiler/testData/diagnostics/tests/smartCasts/equals.kt @@ -1,3 +1,5 @@ +// SKIP_TXT + fun foo(x: String?) = x class Test @@ -23,31 +25,3 @@ fun gav(i: TestWithEquals?, j: TestWithEquals?) { if (i == j) foo(i) } } - -fun string(foo: Any) { - val string = "" - if ("" == foo) foo.length - if (string == foo) foo.length - if (foo == "") foo.length -} - -fun int(foo: Any) { - val int = 1 - if (1 == foo) foo.plus(1) - if (int == foo) foo.plus(1) - if (foo == 1) foo.plus(1) -} - -fun long(foo: Any) { - val long = 1L - if (1L == foo) foo.plus(1L) - if (long == foo) foo.plus(1L) - if (foo == 1L) foo.plus(1L) -} - -fun char(foo: Any) { - val char = 'a' - if ('a' == foo) foo.compareTo('a') - if (char == foo) foo.compareTo('a') - if (foo == 'a') foo.compareTo('a') -} diff --git a/compiler/testData/diagnostics/tests/smartCasts/equals.txt b/compiler/testData/diagnostics/tests/smartCasts/equals.txt deleted file mode 100644 index 750d4f8dd15..00000000000 --- a/compiler/testData/diagnostics/tests/smartCasts/equals.txt +++ /dev/null @@ -1,24 +0,0 @@ -package - -public fun bar(/*0*/ i: Test?): kotlin.Unit -public fun bar(/*0*/ i: TestWithEquals?): kotlin.Unit -public fun char(/*0*/ foo: kotlin.Any): kotlin.Unit -public fun foo(/*0*/ x: kotlin.String?): kotlin.String? -public fun gav(/*0*/ i: TestWithEquals?, /*1*/ j: TestWithEquals?): kotlin.Unit -public fun int(/*0*/ foo: kotlin.Any): kotlin.Unit -public fun long(/*0*/ foo: kotlin.Any): kotlin.Unit -public fun string(/*0*/ foo: kotlin.Any): kotlin.Unit - -public final class Test { - public constructor Test() - 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 TestWithEquals { - public constructor TestWithEquals() - public open override /*1*/ 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 -} diff --git a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/DiagnosticTestGenerated.java b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/DiagnosticTestGenerated.java index 23ae7c38954..f09f87d6a51 100644 --- a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/DiagnosticTestGenerated.java +++ b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/DiagnosticTestGenerated.java @@ -35082,6 +35082,12 @@ public class DiagnosticTestGenerated extends AbstractDiagnosticTest { runTest("compiler/testData/diagnostics/tests/smartCasts/enumEntryMembers_before.kt"); } + @Test + @TestMetadata("equalitySmartcast.kt") + public void testEqualitySmartcast() throws Exception { + runTest("compiler/testData/diagnostics/tests/smartCasts/equalitySmartcast.kt"); + } + @Test @TestMetadata("equals.kt") public void testEquals() throws Exception {