[FIR] Add smartcasts for reassigned vars

This commit is contained in:
Dmitriy Novozhilov
2019-11-13 14:23:01 +03:00
parent 2a74e37e28
commit 660bcce90a
10 changed files with 356 additions and 12 deletions
@@ -584,10 +584,22 @@ class FirDataFlowAnalyzer(private val components: FirAbstractBodyResolveTransfor
}
fun exitVariableAssignment(assignment: FirVariableAssignment) {
graphBuilder.exitVariableAssignment(assignment).mergeIncomingFlow()
val lhsVariable = variableStorage[assignment.resolvedSymbol ?: return] ?: return
val rhsVariable = variableStorage[assignment.rValue.resolvedSymbol ?: return]?.takeIf { !it.isSynthetic() } ?: return
variableStorage.rebindAliasVariable(lhsVariable, rhsVariable)
val node = graphBuilder.exitVariableAssignment(assignment).mergeIncomingFlow()
val lhsSymbol: AbstractFirBasedSymbol<*> = (assignment.lValue as? FirResolvedNamedReference)?.resolvedSymbol ?: return
val lhsVariable = getOrCreateRealVariable(lhsSymbol.fir) ?: return
val rhsSymbol = assignment.rValue.resolvedSymbol
val rhsVariable = rhsSymbol?.let { variableStorage[it]?.takeIf { !it.isSynthetic() } }
if (rhsVariable == null) {
val type = assignment.rValue.typeRef.coneTypeSafe<ConeKotlinType>() ?: return
logicSystem.addApprovedInfo(
node.flow,
lhsVariable,
FirDataFlowInfo(setOf(type), emptySet())
)
return
} else {
variableStorage.rebindAliasVariable(lhsVariable, rhsVariable)
}
}
fun exitThrowExceptionNode(throwExpression: FirThrowExpression) {
@@ -0,0 +1,107 @@
digraph delayedAssignment_kt {
graph [splines=ortho nodesep=3]
node [shape=box penwidth=2]
edge [penwidth=2]
subgraph cluster_0 {
color=red
0 [label="Enter function <init>" style="filled" fillcolor=red];
1 [label="Exit function <init>" style="filled" fillcolor=red];
}
0 -> {1};
subgraph cluster_1 {
color=red
2 [label="Enter function foo" style="filled" fillcolor=red];
subgraph cluster_2 {
color=blue
3 [label="Enter block"];
4 [label="Exit block"];
}
5 [label="Exit function foo" style="filled" fillcolor=red];
}
2 -> {3};
3 -> {4};
4 -> {5};
subgraph cluster_3 {
color=red
6 [label="Enter function test" style="filled" fillcolor=red];
subgraph cluster_4 {
color=blue
7 [label="Enter block"];
8 [label="Variable declaration: lval a: R|A?|"];
subgraph cluster_5 {
color=blue
9 [label="Enter when"];
subgraph cluster_6 {
color=blue
10 [label="Enter when branch condition "];
11 [label="Access variable R|<local>/b|"];
12 [label="Exit when branch condition"];
}
subgraph cluster_7 {
color=blue
13 [label="Enter when branch condition else"];
14 [label="Exit when branch condition"];
}
15 [label="Enter when branch result"];
subgraph cluster_8 {
color=blue
16 [label="Enter block"];
17 [label="Const: Null(null)"];
18 [label="Assignmenet: R|<local>/a|"];
19 [label="Exit block"];
}
20 [label="Exit when branch result"];
21 [label="Enter when branch result"];
subgraph cluster_9 {
color=blue
22 [label="Enter block"];
23 [label="Function call: R|/A.A|()"];
24 [label="Assignmenet: R|<local>/a|"];
25 [label="Access variable R|<local>/a|"];
26 [label="Function call: R|<local>/a|.R|/A.foo|()"];
27 [label="Exit block"];
}
28 [label="Exit when branch result"];
29 [label="Exit when"];
}
30 [label="Access variable R|<local>/a|"];
31 [label="Function call: R|<local>/a|.<Inapplicable(WRONG_RECEIVER): [/A.foo]>#()"];
32 [label="Exit block"];
}
33 [label="Exit function test" style="filled" fillcolor=red];
}
6 -> {7};
7 -> {8};
8 -> {9};
9 -> {10};
10 -> {11};
11 -> {12};
12 -> {21 13};
13 -> {14};
14 -> {15};
15 -> {16};
16 -> {17};
17 -> {18};
18 -> {19};
19 -> {20};
20 -> {29};
21 -> {22};
22 -> {23};
23 -> {24};
24 -> {25};
25 -> {26};
26 -> {27};
27 -> {28};
28 -> {29};
29 -> {30};
30 -> {31};
31 -> {32};
32 -> {33};
}
@@ -0,0 +1,14 @@
class A {
fun foo() {}
}
fun test(b: Boolean) {
val a: A?
if (b) {
a = A()
a.foo()
} else {
a = null
}
a.<!INAPPLICABLE_CANDIDATE!>foo<!>()
}
@@ -0,0 +1,24 @@
FILE: delayedAssignment.kt
public final class A : R|kotlin/Any| {
public constructor(): R|A| {
super<R|kotlin/Any|>()
}
public final fun foo(): R|kotlin/Unit| {
}
}
public final fun test(b: R|kotlin/Boolean|): R|kotlin/Unit| {
lval a: R|A?|
when () {
R|<local>/b| -> {
R|<local>/a| = R|/A.A|()
R|<local>/a|.R|/A.foo|()
}
else -> {
R|<local>/a| = Null(null)
}
}
R|<local>/a|.<Inapplicable(WRONG_RECEIVER): [/A.foo]>#()
}
@@ -0,0 +1,131 @@
digraph smartcastAfterReassignment_kt {
graph [splines=ortho nodesep=3]
node [shape=box penwidth=2]
edge [penwidth=2]
subgraph cluster_0 {
color=red
0 [label="Enter function test_1" style="filled" fillcolor=red];
subgraph cluster_1 {
color=blue
1 [label="Enter block"];
2 [label="Const: Int(1)"];
3 [label="Variable declaration: lvar x: R|kotlin/Any|"];
4 [label="Const: String()"];
5 [label="Assignmenet: R|<local>/x|"];
6 [label="Access variable R|<local>/x|"];
7 [label="Access variable R|kotlin/String.length|"];
8 [label="Exit block"];
}
9 [label="Exit function test_1" style="filled" fillcolor=red];
}
0 -> {1};
1 -> {2};
2 -> {3};
3 -> {4};
4 -> {5};
5 -> {6};
6 -> {7};
7 -> {8};
8 -> {9};
subgraph cluster_2 {
color=red
10 [label="Enter function test_2" style="filled" fillcolor=red];
subgraph cluster_3 {
color=blue
11 [label="Enter block"];
12 [label="Const: Null(null)"];
13 [label="Variable declaration: lvar x: R|kotlin/String?|"];
subgraph cluster_4 {
color=blue
14 [label="Enter when"];
subgraph cluster_5 {
color=blue
15 [label="Enter when branch condition "];
16 [label="Access variable R|<local>/x|"];
17 [label="Const: Null(null)"];
18 [label="Operator =="];
19 [label="Exit when branch condition"];
}
20 [label="Synthetic else branch"];
21 [label="Enter when branch result"];
subgraph cluster_6 {
color=blue
22 [label="Enter block"];
23 [label="Const: String()"];
24 [label="Assignmenet: R|<local>/x|"];
25 [label="Exit block"];
}
26 [label="Exit when branch result"];
27 [label="Exit when"];
}
28 [label="Access variable R|<local>/x|"];
29 [label="Access variable R|kotlin/String.length|"];
30 [label="Exit block"];
}
31 [label="Exit function test_2" style="filled" fillcolor=red];
}
10 -> {11};
11 -> {12};
12 -> {13};
13 -> {14};
14 -> {15};
15 -> {16};
16 -> {17};
17 -> {18};
18 -> {19};
19 -> {21 20};
20 -> {27};
21 -> {22};
22 -> {23};
23 -> {24};
24 -> {25};
25 -> {26};
26 -> {27};
27 -> {28};
28 -> {29};
29 -> {30};
30 -> {31};
subgraph cluster_7 {
color=red
32 [label="Enter function test_3" style="filled" fillcolor=red];
subgraph cluster_8 {
color=blue
33 [label="Enter block"];
34 [label="Const: Null(null)"];
35 [label="Variable declaration: lvar x: R|kotlin/String?|"];
36 [label="Const: String()"];
37 [label="Assignmenet: R|<local>/x|"];
38 [label="Access variable R|<local>/x|"];
39 [label="Access variable R|kotlin/String.length|"];
40 [label="Const: Null(null)"];
41 [label="Assignmenet: R|<local>/x|"];
42 [label="Access variable R|<local>/x|"];
43 [label="Stub" style="filled" fillcolor=gray];
44 [label="Access variable <Unresolved name: length>#" style="filled" fillcolor=gray];
45 [label="Exit block" style="filled" fillcolor=gray];
}
46 [label="Exit function test_3" style="filled" fillcolor=red];
}
32 -> {33};
33 -> {34};
34 -> {35};
35 -> {36};
36 -> {37};
37 -> {38};
38 -> {39};
39 -> {40};
40 -> {41};
41 -> {42};
42 -> {46};
42 -> {43} [style=dotted];
43 -> {44} [style=dotted];
44 -> {45} [style=dotted];
45 -> {46} [style=dotted];
}
@@ -0,0 +1,21 @@
fun test_1() {
var x: Any = 1
x = ""
x.length
}
fun test_2() {
var x: String? = null
if (x == null) {
x = ""
}
x.length
}
fun test_3() {
var x: String? = null
x = ""
x.length
x = null
x.<!UNRESOLVED_REFERENCE!>length<!>
}
@@ -0,0 +1,23 @@
FILE: smartcastAfterReassignment.kt
public final fun test_1(): R|kotlin/Unit| {
lvar x: R|kotlin/Any| = Int(1)
R|<local>/x| = String()
R|<local>/x|.R|kotlin/String.length|
}
public final fun test_2(): R|kotlin/Unit| {
lvar x: R|kotlin/String?| = Null(null)
when () {
==(R|<local>/x|, Null(null)) -> {
R|<local>/x| = String()
}
}
R|<local>/x|.R|kotlin/String.length|
}
public final fun test_3(): R|kotlin/Unit| {
lvar x: R|kotlin/String?| = Null(null)
R|<local>/x| = String()
R|<local>/x|.R|kotlin/String.length|
R|<local>/x| = Null(null)
R|<local>/x|.<Unresolved name: length>#
}
+1 -1
View File
@@ -18,7 +18,7 @@ FILE: hashSet.kt
public final fun foo(): R|kotlin/Unit| {
lvar c: R|kotlin/collections/MutableSet<kotlin/String>?| = Null(null)
R|<local>/c| = R|java/util/HashSet.HashSet|<R|kotlin/String|>()
when (lval <bangbang>: R|kotlin/collections/MutableSet<kotlin/String>?| = R|<local>/c|) {
when (lval <bangbang>: R|java/util/HashSet<kotlin/String>| = R|<local>/c|) {
==($subj$, Null(null)) -> {
throw R|kotlin/KotlinNullPointerException.KotlinNullPointerException|()
}
@@ -133,6 +133,11 @@ public class FirDiagnosticsWithCfgTestGenerated extends AbstractFirDiagnosticsWi
runTest("compiler/fir/resolve/testData/resolve/smartcasts/dataFlowInfoFromWhileCondition.kt");
}
@TestMetadata("delayedAssignment.kt")
public void testDelayedAssignment() throws Exception {
runTest("compiler/fir/resolve/testData/resolve/smartcasts/delayedAssignment.kt");
}
@TestMetadata("elvis.kt")
public void testElvis() throws Exception {
runTest("compiler/fir/resolve/testData/resolve/smartcasts/elvis.kt");
@@ -183,6 +188,11 @@ public class FirDiagnosticsWithCfgTestGenerated extends AbstractFirDiagnosticsWi
runTest("compiler/fir/resolve/testData/resolve/smartcasts/simpleIf.kt");
}
@TestMetadata("smartcastAfterReassignment.kt")
public void testSmartcastAfterReassignment() throws Exception {
runTest("compiler/fir/resolve/testData/resolve/smartcasts/smartcastAfterReassignment.kt");
}
@TestMetadata("when.kt")
public void testWhen() throws Exception {
runTest("compiler/fir/resolve/testData/resolve/smartcasts/when.kt");
@@ -9,11 +9,13 @@ FILE fqName:<root> fileName:/localDelegatedProperties.kt
VAR name:x type:IrErrorType [var]
SET_VAR 'var x: IrErrorType [var] declared in <root>.test2' type=IrErrorType origin=null
CONST Int type=kotlin.Int value=0
VAR IR_TEMPORARY_VARIABLE name:tmp_0 type:IrErrorType [val]
GET_VAR 'var x: IrErrorType [var] declared in <root>.test2' type=IrErrorType origin=null
VAR IR_TEMPORARY_VARIABLE name:tmp_0 type:kotlin.Int [val]
GET_VAR 'var x: IrErrorType [var] declared in <root>.test2' type=kotlin.Int origin=null
SET_VAR 'var x: IrErrorType [var] declared in <root>.test2' type=IrErrorType origin=null
ERROR_CALL 'Unresolved reference: <Ambiguity: inc, [kotlin/inc, kotlin/inc]>#' type=IrErrorType
GET_VAR 'val tmp_0: IrErrorType [val] declared in <root>.test2' type=IrErrorType origin=null
CALL 'public final fun plusAssign (element: T of <uninitialized parent>): kotlin.Unit [inline] declared in kotlin.collections' type=kotlin.Unit origin=null
$receiver: GET_VAR 'var x: IrErrorType [var] declared in <root>.test2' type=IrErrorType origin=null
element: CONST Int type=kotlin.Int value=1
CALL 'public final fun inc (): kotlin.Int declared in kotlin.Int' type=kotlin.Int origin=null
$this: GET_VAR 'val tmp_0: kotlin.Int [val] declared in <root>.test2' type=kotlin.Int origin=null
GET_VAR 'val tmp_0: kotlin.Int [val] declared in <root>.test2' type=kotlin.Int origin=null
SET_VAR 'var x: IrErrorType [var] declared in <root>.test2' type=IrErrorType origin=null
CALL 'public final fun plus (other: kotlin.Int): kotlin.Int declared in kotlin.Int' type=kotlin.Int origin=null
$this: GET_VAR 'var x: IrErrorType [var] declared in <root>.test2' type=kotlin.Int origin=null
other: CONST Int type=kotlin.Int value=1