From 33363ba1fce179cc74111914557896c372921bec Mon Sep 17 00:00:00 2001 From: pyos Date: Mon, 7 Nov 2022 13:10:17 +0100 Subject: [PATCH] Add more variable aliasing tests --- .../boundSmartcastsInBranches.dot | 118 ++++++++++++++++++ .../boundSmartcastsInBranches.fir.txt | 19 +++ .../boundSmartcastsInBranches.kt | 13 ++ .../smartCasts/variables/aliasing.fir.kt | 19 +++ .../tests/smartCasts/variables/aliasing.kt | 19 +++ .../tests/smartCasts/variables/aliasing.txt | 4 - .../variables/reassignedDependency.fir.kt | 14 +++ .../variables/reassignedDependency.kt | 14 +++ 8 files changed, 216 insertions(+), 4 deletions(-) delete mode 100644 compiler/testData/diagnostics/tests/smartCasts/variables/aliasing.txt diff --git a/compiler/fir/analysis-tests/testData/resolve/smartcasts/boundSmartcasts/boundSmartcastsInBranches.dot b/compiler/fir/analysis-tests/testData/resolve/smartcasts/boundSmartcasts/boundSmartcastsInBranches.dot index 0cce71a1bd9..03c239a99d7 100644 --- a/compiler/fir/analysis-tests/testData/resolve/smartcasts/boundSmartcasts/boundSmartcastsInBranches.dot +++ b/compiler/fir/analysis-tests/testData/resolve/smartcasts/boundSmartcasts/boundSmartcastsInBranches.dot @@ -1239,4 +1239,122 @@ digraph boundSmartcastsInBranches_kt { 477 -> {478}; 478 -> {479}; + subgraph cluster_90 { + color=red + 480 [label="Enter function test_9" style="filled" fillcolor=red]; + subgraph cluster_91 { + color=blue + 481 [label="Enter block"]; + 482 [label="Const: Null(null)"]; + 483 [label="Variable declaration: lvar a: R|kotlin/String?|"]; + 484 [label="Variable declaration: lval b: R|kotlin/String?|"]; + subgraph cluster_92 { + color=blue + 485 [label="Enter when"]; + subgraph cluster_93 { + color=blue + 486 [label="Enter when branch condition "]; + 487 [label="Access variable R|/a|"]; + 488 [label="Const: Null(null)"]; + 489 [label="Equality operator !="]; + 490 [label="Exit when branch condition"]; + } + subgraph cluster_94 { + color=blue + 491 [label="Enter when branch condition else"]; + 492 [label="Exit when branch condition"]; + } + 493 [label="Enter when branch result"]; + subgraph cluster_95 { + color=blue + 494 [label="Enter block"]; + 495 [label="Access variable R|/a|"]; + 496 [label="Smart cast: R|/a|"]; + 497 [label="Assignment: R|/b|"]; + 498 [label="Exit block"]; + } + 499 [label="Exit when branch result"]; + 500 [label="Enter when branch result"]; + subgraph cluster_96 { + color=blue + 501 [label="Enter block"]; + 502 [label="Access variable R|/a|"]; + 503 [label="Smart cast: R|/a|"]; + 504 [label="Assignment: R|/b|"]; + 505 [label="Exit block"]; + } + 506 [label="Exit when branch result"]; + 507 [label="Exit when"]; + } + subgraph cluster_97 { + color=blue + 508 [label="Enter when"]; + subgraph cluster_98 { + color=blue + 509 [label="Enter when branch condition "]; + 510 [label="Access variable R|/a|"]; + 511 [label="Const: Null(null)"]; + 512 [label="Equality operator !="]; + 513 [label="Exit when branch condition"]; + } + 514 [label="Synthetic else branch"]; + 515 [label="Enter when branch result"]; + subgraph cluster_99 { + color=blue + 516 [label="Enter block"]; + 517 [label="Access variable R|/b|"]; + 518 [label="Access variable #"]; + 519 [label="Exit block"]; + } + 520 [label="Exit when branch result"]; + 521 [label="Exit when"]; + } + 522 [label="Exit block"]; + } + 523 [label="Exit function test_9" style="filled" fillcolor=red]; + } + 480 -> {481}; + 481 -> {482}; + 482 -> {483}; + 483 -> {484}; + 484 -> {485}; + 485 -> {486}; + 486 -> {487}; + 487 -> {488}; + 488 -> {489}; + 489 -> {490}; + 490 -> {500 491}; + 491 -> {492}; + 492 -> {493}; + 493 -> {494}; + 494 -> {495}; + 495 -> {496}; + 496 -> {497}; + 497 -> {498}; + 498 -> {499}; + 499 -> {507}; + 500 -> {501}; + 501 -> {502}; + 502 -> {503}; + 503 -> {504}; + 504 -> {505}; + 505 -> {506}; + 506 -> {507}; + 507 -> {508}; + 508 -> {509}; + 509 -> {510}; + 510 -> {511}; + 511 -> {512}; + 512 -> {513}; + 513 -> {515 514}; + 514 -> {521}; + 515 -> {516}; + 516 -> {517}; + 517 -> {518}; + 518 -> {519}; + 519 -> {520}; + 520 -> {521}; + 521 -> {522}; + 522 -> {523}; + } diff --git a/compiler/fir/analysis-tests/testData/resolve/smartcasts/boundSmartcasts/boundSmartcastsInBranches.fir.txt b/compiler/fir/analysis-tests/testData/resolve/smartcasts/boundSmartcasts/boundSmartcastsInBranches.fir.txt index b35e59e68ac..d3b005cfbf5 100644 --- a/compiler/fir/analysis-tests/testData/resolve/smartcasts/boundSmartcasts/boundSmartcastsInBranches.fir.txt +++ b/compiler/fir/analysis-tests/testData/resolve/smartcasts/boundSmartcasts/boundSmartcastsInBranches.fir.txt @@ -211,3 +211,22 @@ FILE: boundSmartcastsInBranches.kt } } + public final fun test_9(): R|kotlin/Unit| { + lvar a: R|kotlin/String?| = Null(null) + lval b: R|kotlin/String?| + when () { + !=(R|/a|, Null(null)) -> { + R|/b| = R|/a| + } + else -> { + R|/b| = R|/a| + } + } + + when () { + !=(R|/a|, Null(null)) -> { + R|/b|.# + } + } + + } diff --git a/compiler/fir/analysis-tests/testData/resolve/smartcasts/boundSmartcasts/boundSmartcastsInBranches.kt b/compiler/fir/analysis-tests/testData/resolve/smartcasts/boundSmartcasts/boundSmartcastsInBranches.kt index 982a3305896..69e62cac433 100644 --- a/compiler/fir/analysis-tests/testData/resolve/smartcasts/boundSmartcasts/boundSmartcastsInBranches.kt +++ b/compiler/fir/analysis-tests/testData/resolve/smartcasts/boundSmartcasts/boundSmartcastsInBranches.kt @@ -161,3 +161,16 @@ fun test_8() { z.length // OK } } + +fun test_9() { + var a: String? = null + val b: String? + if (a != null) { + b = a + } else { + b = a + } + if (a != null) { + b.length // ok + } +} diff --git a/compiler/testData/diagnostics/tests/smartCasts/variables/aliasing.fir.kt b/compiler/testData/diagnostics/tests/smartCasts/variables/aliasing.fir.kt index 3dc0b517806..6dd4c24ac4d 100644 --- a/compiler/testData/diagnostics/tests/smartCasts/variables/aliasing.fir.kt +++ b/compiler/testData/diagnostics/tests/smartCasts/variables/aliasing.fir.kt @@ -1,3 +1,5 @@ +// SKIP_TXT + fun test() { var a: Any? = null var b = a @@ -63,3 +65,20 @@ fun test2() { b.length // OK b.unaryPlus() // error } + +fun test3() { + var a: Any? = null + val b = a + val c = a + if (a is String) { + a.length // ok + b.length // ok + c.length // ok + } + a = null // b and c are still aliases to the same old value + if (b is String) { + a.length // error + b.length // ok + c.length // ok + } +} diff --git a/compiler/testData/diagnostics/tests/smartCasts/variables/aliasing.kt b/compiler/testData/diagnostics/tests/smartCasts/variables/aliasing.kt index 6b13dcf26dd..e16f3acaa6b 100644 --- a/compiler/testData/diagnostics/tests/smartCasts/variables/aliasing.kt +++ b/compiler/testData/diagnostics/tests/smartCasts/variables/aliasing.kt @@ -1,3 +1,5 @@ +// SKIP_TXT + fun test() { var a: Any? = null var b = a @@ -63,3 +65,20 @@ fun test2() { b.length // OK b.unaryPlus() // error } + +fun test3() { + var a: Any? = null + val b = a + val c = a + if (a is String) { + a.length // ok + b.length // ok + c.length // ok + } + a = null // b and c are still aliases to the same old value + if (b is String) { + a.length // error + b.length // ok + c.length // ok + } +} diff --git a/compiler/testData/diagnostics/tests/smartCasts/variables/aliasing.txt b/compiler/testData/diagnostics/tests/smartCasts/variables/aliasing.txt deleted file mode 100644 index 6309e83aef4..00000000000 --- a/compiler/testData/diagnostics/tests/smartCasts/variables/aliasing.txt +++ /dev/null @@ -1,4 +0,0 @@ -package - -public fun test(): kotlin.Unit -public fun test2(): kotlin.Unit diff --git a/compiler/testData/diagnostics/tests/smartCasts/variables/reassignedDependency.fir.kt b/compiler/testData/diagnostics/tests/smartCasts/variables/reassignedDependency.fir.kt index 33dbb8a5bd6..c18b27b805f 100644 --- a/compiler/testData/diagnostics/tests/smartCasts/variables/reassignedDependency.fir.kt +++ b/compiler/testData/diagnostics/tests/smartCasts/variables/reassignedDependency.fir.kt @@ -61,3 +61,17 @@ fun test4(p: Boolean, q: Boolean) { x.length // ok c.x.length // bad } + +fun test5() { + var c = C("...") + val d = c + val x = d.x + if (x == null) return + x.length // ok + c.x.length // ok + d.x.length // ok + c = C(null) + x.length // ok + c.x.length // bad + d.x.length // ok +} diff --git a/compiler/testData/diagnostics/tests/smartCasts/variables/reassignedDependency.kt b/compiler/testData/diagnostics/tests/smartCasts/variables/reassignedDependency.kt index 897da652f3f..ea659791116 100644 --- a/compiler/testData/diagnostics/tests/smartCasts/variables/reassignedDependency.kt +++ b/compiler/testData/diagnostics/tests/smartCasts/variables/reassignedDependency.kt @@ -61,3 +61,17 @@ fun test4(p: Boolean, q: Boolean) { x.length // ok c.x.length // bad } + +fun test5() { + var c = C("...") + val d = c + val x = d.x + if (x == null) return + x.length // ok + c.x.length // ok + d.x.length // ok + c = C(null) + x.length // ok + c.x.length // bad + d.x.length // ok +}