[FIR] Consider stability of receiver for DFA variables

^KT-57425 Fixed
This commit is contained in:
Dmitriy Novozhilov
2023-04-28 16:37:41 +03:00
committed by Space Team
parent d579798169
commit 1936658e40
9 changed files with 252 additions and 6 deletions
@@ -30884,6 +30884,12 @@ public class DiagnosticCompilerTestFE10TestdataTestGenerated extends AbstractDia
@TestMetadata("compiler/testData/diagnostics/tests/smartCasts/publicVals")
@TestDataPath("$PROJECT_ROOT")
public class PublicVals {
@Test
@TestMetadata("accessThrowOtherModule.kt")
public void testAccessThrowOtherModule() throws Exception {
runTest("compiler/testData/diagnostics/tests/smartCasts/publicVals/accessThrowOtherModule.kt");
}
@Test
public void testAllFilesPresentInPublicVals() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/diagnostics/tests/smartCasts/publicVals"), Pattern.compile("^(.+)\\.kt$"), Pattern.compile("^(.+)\\.(reversed|fir|ll)\\.kts?$"), true, "multiplatform");
@@ -31317,6 +31323,12 @@ public class DiagnosticCompilerTestFE10TestdataTestGenerated extends AbstractDia
runTest("compiler/testData/diagnostics/tests/smartCasts/variables/reassignedDependency.kt");
}
@Test
@TestMetadata("reassignedDependency_unstable.kt")
public void testReassignedDependency_unstable() throws Exception {
runTest("compiler/testData/diagnostics/tests/smartCasts/variables/reassignedDependency_unstable.kt");
}
@Test
@TestMetadata("reassignedInRhs.kt")
public void testReassignedInRhs() throws Exception {
@@ -30884,6 +30884,12 @@ public class LLFirPreresolvedReversedDiagnosticCompilerFE10TestDataTestGenerated
@TestMetadata("compiler/testData/diagnostics/tests/smartCasts/publicVals")
@TestDataPath("$PROJECT_ROOT")
public class PublicVals {
@Test
@TestMetadata("accessThrowOtherModule.kt")
public void testAccessThrowOtherModule() throws Exception {
runTest("compiler/testData/diagnostics/tests/smartCasts/publicVals/accessThrowOtherModule.kt");
}
@Test
public void testAllFilesPresentInPublicVals() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/diagnostics/tests/smartCasts/publicVals"), Pattern.compile("^(.+)\\.kt$"), Pattern.compile("^(.+)\\.(reversed|fir|ll)\\.kts?$"), true, "multiplatform");
@@ -31317,6 +31323,12 @@ public class LLFirPreresolvedReversedDiagnosticCompilerFE10TestDataTestGenerated
runTest("compiler/testData/diagnostics/tests/smartCasts/variables/reassignedDependency.kt");
}
@Test
@TestMetadata("reassignedDependency_unstable.kt")
public void testReassignedDependency_unstable() throws Exception {
runTest("compiler/testData/diagnostics/tests/smartCasts/variables/reassignedDependency_unstable.kt");
}
@Test
@TestMetadata("reassignedInRhs.kt")
public void testReassignedInRhs() throws Exception {
@@ -30884,6 +30884,12 @@ public class FirLightTreeOldFrontendDiagnosticsTestGenerated extends AbstractFir
@TestMetadata("compiler/testData/diagnostics/tests/smartCasts/publicVals")
@TestDataPath("$PROJECT_ROOT")
public class PublicVals {
@Test
@TestMetadata("accessThrowOtherModule.kt")
public void testAccessThrowOtherModule() throws Exception {
runTest("compiler/testData/diagnostics/tests/smartCasts/publicVals/accessThrowOtherModule.kt");
}
@Test
public void testAllFilesPresentInPublicVals() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/diagnostics/tests/smartCasts/publicVals"), Pattern.compile("^(.+)\\.kt$"), Pattern.compile("^(.+)\\.(reversed|fir|ll)\\.kts?$"), true, "multiplatform");
@@ -31317,6 +31323,12 @@ public class FirLightTreeOldFrontendDiagnosticsTestGenerated extends AbstractFir
runTest("compiler/testData/diagnostics/tests/smartCasts/variables/reassignedDependency.kt");
}
@Test
@TestMetadata("reassignedDependency_unstable.kt")
public void testReassignedDependency_unstable() throws Exception {
runTest("compiler/testData/diagnostics/tests/smartCasts/variables/reassignedDependency_unstable.kt");
}
@Test
@TestMetadata("reassignedInRhs.kt")
public void testReassignedInRhs() throws Exception {
@@ -30980,6 +30980,12 @@ public class FirPsiOldFrontendDiagnosticsTestGenerated extends AbstractFirPsiDia
@TestMetadata("compiler/testData/diagnostics/tests/smartCasts/publicVals")
@TestDataPath("$PROJECT_ROOT")
public class PublicVals {
@Test
@TestMetadata("accessThrowOtherModule.kt")
public void testAccessThrowOtherModule() throws Exception {
runTest("compiler/testData/diagnostics/tests/smartCasts/publicVals/accessThrowOtherModule.kt");
}
@Test
public void testAllFilesPresentInPublicVals() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/diagnostics/tests/smartCasts/publicVals"), Pattern.compile("^(.*)\\.kts?$"), Pattern.compile("^(.+)\\.(reversed|fir|ll)\\.kts?$"), true, "multiplatform");
@@ -31413,6 +31419,12 @@ public class FirPsiOldFrontendDiagnosticsTestGenerated extends AbstractFirPsiDia
runTest("compiler/testData/diagnostics/tests/smartCasts/variables/reassignedDependency.kt");
}
@Test
@TestMetadata("reassignedDependency_unstable.kt")
public void testReassignedDependency_unstable() throws Exception {
runTest("compiler/testData/diagnostics/tests/smartCasts/variables/reassignedDependency_unstable.kt");
}
@Test
@TestMetadata("reassignedInRhs.kt")
public void testReassignedInRhs() throws Exception {
@@ -9,7 +9,6 @@ import org.jetbrains.kotlin.fir.FirElement
import org.jetbrains.kotlin.fir.expressions.FirResolvedQualifier
import org.jetbrains.kotlin.fir.symbols.FirBasedSymbol
import org.jetbrains.kotlin.fir.symbols.impl.FirCallableSymbol
import org.jetbrains.kotlin.fir.types.ConeKotlinType
import org.jetbrains.kotlin.types.SmartcastStability
data class Identifier(
@@ -34,6 +33,9 @@ enum class PropertyStability(val impliedSmartcastStability: SmartcastStability?)
// Smartcast is definitely safe regardless of usage.
STABLE_VALUE(SmartcastStability.STABLE_VALUE),
// Smartcast may or may not be safe, depending on whether there are concurrent writes to this local variable.
LOCAL_VAR(null),
// Open or custom getter.
// Smartcast is always unsafe regardless of usage.
PROPERTY_WITH_GETTER(SmartcastStability.PROPERTY_WITH_GETTER),
@@ -42,16 +44,24 @@ enum class PropertyStability(val impliedSmartcastStability: SmartcastStability?)
// Smartcast is always unsafe regardless of usage.
ALIEN_PUBLIC_PROPERTY(SmartcastStability.ALIEN_PUBLIC_PROPERTY),
// Smartcast may or may not be safe, depending on whether there are concurrent writes to this local variable.
LOCAL_VAR(null),
// Mutable member property of a class or object.
// Smartcast is always unsafe regardless of usage.
MUTABLE_PROPERTY(SmartcastStability.MUTABLE_PROPERTY),
// Delegated property of a class or object.
// Smartcast is always unsafe regardless of usage.
DELEGATED_PROPERTY(SmartcastStability.DELEGATED_PROPERTY),
DELEGATED_PROPERTY(SmartcastStability.DELEGATED_PROPERTY);
fun combineWithReceiverStability(receiverStability: PropertyStability?): PropertyStability {
if (receiverStability == null) return this
if (this == LOCAL_VAR) {
require(receiverStability == STABLE_VALUE || receiverStability == LOCAL_VAR) {
"LOCAL_VAR can have only stable or local receiver, but got $receiverStability"
}
return this
}
return maxOf(this, receiverStability)
}
}
class RealVariable(
@@ -59,10 +69,12 @@ class RealVariable(
val isThisReference: Boolean,
val explicitReceiverVariable: DataFlowVariable?,
variableIndexForDebug: Int,
val stability: PropertyStability,
stability: PropertyStability,
) : DataFlowVariable(variableIndexForDebug) {
val dependentVariables = mutableSetOf<RealVariable>()
val stability: PropertyStability = stability.combineWithReceiverStability((explicitReceiverVariable as? RealVariable)?.stability)
override fun equals(other: Any?): Boolean {
return this === other
}
@@ -0,0 +1,14 @@
// FIR_IDENTICAL
// ISSUE: KT-57425
// WITH_STDLIB
data class Data(val x: Int?)
fun test(pair: Pair<String?, Data>) {
if (pair.second.x != null) {
<!SMARTCAST_IMPOSSIBLE!>pair.second.x<!>.inc() // should be an error
}
if (pair.first != null) {
<!SMARTCAST_IMPOSSIBLE!>pair.first<!>.length // should be an error
}
}
@@ -0,0 +1,80 @@
// SKIP_TXT
class C {
val x: String?
get() = null
}
fun test1() {
var c = C()
val x = c.x
if (x == null) return
x.length // smartcast
c.x<!UNSAFE_CALL!>.<!>length // no smartcast
c = C()
x.length // smartcast
c.x<!UNSAFE_CALL!>.<!>length // no smartcast
}
fun test2() {
var c = C()
val x = c.x
if (x == null) return
while (true) {
x.length // smartcast
c.x<!UNSAFE_CALL!>.<!>length // no smartcast
c = C()
x.length // smartcast
c.x<!UNSAFE_CALL!>.<!>length // no smartcast
}
}
fun test3(p: Boolean) {
var c = C()
val x = c.x
if (x == null) return
x.length // smartcast
c.x<!UNSAFE_CALL!>.<!>length // no smartcast
if (p) {
c = C()
}
x.length // smartcast
c.x<!UNSAFE_CALL!>.<!>length // no smartcast
}
fun test4(p: Boolean, q: Boolean) {
var c = C()
val x = c.x
if (x == null) return
x.length // smartcast
c.x<!UNSAFE_CALL!>.<!>length // no smartcast
if (p) {
if (q) {
c = C()
} else {
c = C()
}
} else {
if (q) {
c = C()
} else {
c = C()
}
}
x.length // smartcast
c.x<!UNSAFE_CALL!>.<!>length // no smartcast
}
fun test5() {
var c = C()
val d = c
val x = d.x
if (x == null) return
x.length // smartcast
c.x<!UNSAFE_CALL!>.<!>length // no smartcast
d.x<!UNSAFE_CALL!>.<!>length // no smartcast
c = C()
x.length // smartcast
c.x<!UNSAFE_CALL!>.<!>length // no smartcast
d.x<!UNSAFE_CALL!>.<!>length // no smartcast
}
@@ -0,0 +1,80 @@
// SKIP_TXT
class C {
val x: String?
get() = null
}
fun test1() {
var c = C()
val x = c.x
if (x == null) return
<!DEBUG_INFO_SMARTCAST!>x<!>.length // smartcast
c.x<!UNSAFE_CALL!>.<!>length // no smartcast
c = C()
<!DEBUG_INFO_SMARTCAST!>x<!>.length // smartcast
c.x<!UNSAFE_CALL!>.<!>length // no smartcast
}
fun test2() {
var c = C()
val x = c.x
if (x == null) return
while (true) {
<!DEBUG_INFO_SMARTCAST!>x<!>.length // smartcast
c.x<!UNSAFE_CALL!>.<!>length // no smartcast
c = C()
<!DEBUG_INFO_SMARTCAST!>x<!>.length // smartcast
c.x<!UNSAFE_CALL!>.<!>length // no smartcast
}
}
fun test3(p: Boolean) {
var c = C()
val x = c.x
if (x == null) return
<!DEBUG_INFO_SMARTCAST!>x<!>.length // smartcast
c.x<!UNSAFE_CALL!>.<!>length // no smartcast
if (p) {
c = C()
}
<!DEBUG_INFO_SMARTCAST!>x<!>.length // smartcast
c.x<!UNSAFE_CALL!>.<!>length // no smartcast
}
fun test4(p: Boolean, q: Boolean) {
var c = C()
val x = c.x
if (x == null) return
<!DEBUG_INFO_SMARTCAST!>x<!>.length // smartcast
c.x<!UNSAFE_CALL!>.<!>length // no smartcast
if (p) {
if (q) {
c = C()
} else {
c = C()
}
} else {
if (q) {
c = C()
} else {
c = C()
}
}
<!DEBUG_INFO_SMARTCAST!>x<!>.length // smartcast
c.x<!UNSAFE_CALL!>.<!>length // no smartcast
}
fun test5() {
var c = C()
val d = c
val x = d.x
if (x == null) return
<!DEBUG_INFO_SMARTCAST!>x<!>.length // smartcast
c.x<!UNSAFE_CALL!>.<!>length // no smartcast
<!SMARTCAST_IMPOSSIBLE!>d.x<!>.length // no smartcast
c = C()
<!DEBUG_INFO_SMARTCAST!>x<!>.length // smartcast
c.x<!UNSAFE_CALL!>.<!>length // no smartcast
<!SMARTCAST_IMPOSSIBLE!>d.x<!>.length // no smartcast
}
@@ -31752,6 +31752,12 @@ public class DiagnosticTestGenerated extends AbstractDiagnosticTest {
@TestMetadata("compiler/testData/diagnostics/tests/smartCasts/publicVals")
@TestDataPath("$PROJECT_ROOT")
public class PublicVals {
@Test
@TestMetadata("accessThrowOtherModule.kt")
public void testAccessThrowOtherModule() throws Exception {
runTest("compiler/testData/diagnostics/tests/smartCasts/publicVals/accessThrowOtherModule.kt");
}
@Test
public void testAllFilesPresentInPublicVals() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/diagnostics/tests/smartCasts/publicVals"), Pattern.compile("^(.*)\\.kts?$"), Pattern.compile("^(.+)\\.(reversed|fir|ll)\\.kts?$"), true);
@@ -32185,6 +32191,12 @@ public class DiagnosticTestGenerated extends AbstractDiagnosticTest {
runTest("compiler/testData/diagnostics/tests/smartCasts/variables/reassignedDependency.kt");
}
@Test
@TestMetadata("reassignedDependency_unstable.kt")
public void testReassignedDependency_unstable() throws Exception {
runTest("compiler/testData/diagnostics/tests/smartCasts/variables/reassignedDependency_unstable.kt");
}
@Test
@TestMetadata("reassignedInRhs.kt")
public void testReassignedInRhs() throws Exception {