[FIR] Propagate smartcasts in safe calls chain

This commit is contained in:
Dmitriy Novozhilov
2019-11-14 10:57:03 +03:00
parent 833648d8e0
commit ee20f88837
17 changed files with 1265 additions and 611 deletions
@@ -69,6 +69,7 @@ class FirCallResolver(
qualifiedResolver.reset()
@Suppress("NAME_SHADOWING")
val functionCall = functionCall.transformExplicitReceiver(transformer, ResolutionMode.ContextIndependent)
.also { dataFlowAnalyzer.enterQualifiedAccessExpression(functionCall) }
.transformArguments(transformer, ResolutionMode.ContextDependent)
val name = functionCall.calleeReference.name
@@ -22,13 +22,11 @@ import org.jetbrains.kotlin.fir.resolve.dfa.cfg.*
import org.jetbrains.kotlin.fir.resolve.dfa.contracts.buildContractFir
import org.jetbrains.kotlin.fir.resolve.dfa.contracts.createArgumentsMapping
import org.jetbrains.kotlin.fir.resolve.transformers.body.resolve.FirAbstractBodyResolveTransformer
import org.jetbrains.kotlin.fir.resolve.withNullability
import org.jetbrains.kotlin.fir.symbols.AbstractFirBasedSymbol
import org.jetbrains.kotlin.fir.symbols.CallableId
import org.jetbrains.kotlin.fir.symbols.impl.FirNamedFunctionSymbol
import org.jetbrains.kotlin.fir.types.ConeKotlinType
import org.jetbrains.kotlin.fir.types.coneTypeSafe
import org.jetbrains.kotlin.fir.types.coneTypeUnsafe
import org.jetbrains.kotlin.fir.types.isMarkedNullable
import org.jetbrains.kotlin.fir.types.*
import org.jetbrains.kotlin.fir.visitors.transformSingle
import org.jetbrains.kotlin.ir.expressions.IrConstKind
import org.jetbrains.kotlin.name.FqName
@@ -469,21 +467,63 @@ class FirDataFlowAnalyzer(private val components: FirAbstractBodyResolveTransfor
// ----------------------------------- Resolvable call -----------------------------------
fun exitQualifiedAccessExpression(qualifiedAccessExpression: FirQualifiedAccessExpression) {
graphBuilder.exitQualifiedAccessExpression(qualifiedAccessExpression).mergeIncomingFlow()
private fun enterSafeCall(qualifiedAccess: FirQualifiedAccess) {
if (!qualifiedAccess.safe) return
val node = graphBuilder.enterSafeCall(qualifiedAccess).mergeIncomingFlow()
val previousNode = node.alivePreviousNodes.first()
val shouldFork: Boolean
var flow= if (previousNode is ExitSafeCallNode) {
shouldFork = false
previousNode.alivePreviousNodes.getOrNull(1)?.flow ?: node.flow
} else {
shouldFork = true
node.flow
}
qualifiedAccess.explicitReceiver?.let {
val type = it.typeRef.coneTypeSafe<ConeKotlinType>()
?.takeIf { it.isMarkedNullable }
?.withNullability(ConeNullability.NOT_NULL)
?: return@let
when (val variable = getOrCreateVariable(it)) {
is RealDataFlowVariable -> {
if (shouldFork) {
flow = logicSystem.forkFlow(flow)
}
logicSystem.addApprovedInfo(flow, variable, FirDataFlowInfo(setOf(type), emptySet()))
}
is SyntheticDataFlowVariable -> {
flow = logicSystem.approveFactsInsideFlow(variable, NotEqNull, flow, shouldFork, true)
}
}
}
node.flow = flow
}
fun enterFunctionCall(functionCall: FirFunctionCall) {
// TODO: add processing in-place lambdas
private fun exitSafeCall(qualifiedAccess: FirQualifiedAccess) {
if (!qualifiedAccess.safe) return
graphBuilder.exitSafeCall(qualifiedAccess).mergeIncomingFlow()
}
fun enterQualifiedAccessExpression(qualifiedAccessExpression: FirQualifiedAccessExpression) {
enterSafeCall(qualifiedAccessExpression)
}
fun exitQualifiedAccessExpression(qualifiedAccessExpression: FirQualifiedAccessExpression) {
graphBuilder.exitQualifiedAccessExpression(qualifiedAccessExpression).mergeIncomingFlow()
exitSafeCall(qualifiedAccessExpression)
}
fun exitFunctionCall(functionCall: FirFunctionCall) {
val node = graphBuilder.exitFunctionCall(functionCall).mergeIncomingFlow()
if (functionCall.isBooleanNot()) {
exitBooleanNot(functionCall, node)
return
}
processConditionalContract(functionCall)
if (functionCall.safe) {
exitSafeCall(functionCall)
}
}
private fun processConditionalContract(functionCall: FirFunctionCall) {
@@ -159,6 +159,9 @@ class VariableAssignmentNode(owner: ControlFlowGraph, override val fir: FirVaria
class EnterContractNode(owner: ControlFlowGraph, override val fir: FirFunctionCall, level: Int) : CFGNode<FirFunctionCall>(owner, level), EnterNode
class ExitContractNode(owner: ControlFlowGraph, override val fir: FirFunctionCall, level: Int) : CFGNode<FirFunctionCall>(owner, level), ExitNode
class EnterSafeCallNode(owner: ControlFlowGraph, override val fir: FirQualifiedAccess, level: Int) : CFGNode<FirQualifiedAccess>(owner, level)
class ExitSafeCallNode(owner: ControlFlowGraph, override val fir: FirQualifiedAccess, level: Int) : CFGNode<FirQualifiedAccess>(owner, level)
// ----------------------------------- Other -----------------------------------
class AnnotationEnterNode(owner: ControlFlowGraph, override val fir: FirAnnotationCall, level: Int) : CFGNode<FirAnnotationCall>(owner, level), EnterNode
@@ -43,6 +43,8 @@ class ControlFlowGraphBuilder {
private val initBlockExitNodes: Stack<InitBlockExitNode> = stackOf()
private val exitSafeCallNodes: Stack<ExitSafeCallNode> = stackOf()
var levelCounter: Int = 0
private set
@@ -572,6 +574,25 @@ class ControlFlowGraphBuilder {
}
}
// ----------------------------------- Safe calls -----------------------------------
fun enterSafeCall(qualifiedAccess: FirQualifiedAccess): EnterSafeCallNode {
val lastNode = lastNodes.pop()
val enterNode = createEnterSafeCallNode(qualifiedAccess)
lastNodes.push(enterNode)
val exitNode = createExitSafeCallNode(qualifiedAccess)
exitSafeCallNodes.push(exitNode)
addEdge(lastNode, enterNode)
addEdge(lastNode, exitNode)
return enterNode
}
fun exitSafeCall(qualifiedAccess: FirQualifiedAccess): ExitSafeCallNode {
return exitSafeCallNodes.pop().also {
addNewSimpleNode(it)
}
}
// -------------------------------------------------------------------------------------------------------------------------
private fun CFGNode<*>.markAsDeadIfNecessary() {
@@ -170,4 +170,10 @@ fun ControlFlowGraphBuilder.createBinaryAndEnterRightOperandNode(fir: FirBinaryL
BinaryAndEnterRightOperandNode(graph, fir, levelCounter)
fun ControlFlowGraphBuilder.createBinaryOrEnterRightOperandNode(fir: FirBinaryLogicExpression): BinaryOrEnterRightOperandNode =
BinaryOrEnterRightOperandNode(graph, fir, levelCounter)
BinaryOrEnterRightOperandNode(graph, fir, levelCounter)
fun ControlFlowGraphBuilder.createExitSafeCallNode(fir: FirQualifiedAccess): ExitSafeCallNode =
ExitSafeCallNode(graph, fir, levelCounter)
fun ControlFlowGraphBuilder.createEnterSafeCallNode(fir: FirQualifiedAccess): EnterSafeCallNode =
EnterSafeCallNode(graph, fir, levelCounter)
@@ -149,6 +149,9 @@ fun CFGNode<*>.render(): String =
is EnterContractNode -> "Enter contract"
is ExitContractNode -> "Exit contract"
is EnterSafeCallNode -> "Enter safe call"
is ExitSafeCallNode -> "Exit safe call"
else -> TODO(this@render.toString())
}
)
@@ -12,7 +12,6 @@ import org.jetbrains.kotlin.fir.diagnostics.DiagnosticKind
import org.jetbrains.kotlin.fir.diagnostics.FirSimpleDiagnostic
import org.jetbrains.kotlin.fir.expressions.*
import org.jetbrains.kotlin.fir.expressions.impl.FirErrorExpressionImpl
import org.jetbrains.kotlin.fir.expressions.impl.FirExpressionWithSmartcastImpl
import org.jetbrains.kotlin.fir.expressions.impl.FirFunctionCallImpl
import org.jetbrains.kotlin.fir.expressions.impl.FirVariableAssignmentImpl
import org.jetbrains.kotlin.fir.references.FirDelegateFieldReference
@@ -23,7 +22,6 @@ import org.jetbrains.kotlin.fir.references.impl.FirExplicitThisReference
import org.jetbrains.kotlin.fir.references.impl.FirSimpleNamedReference
import org.jetbrains.kotlin.fir.render
import org.jetbrains.kotlin.fir.resolve.*
import org.jetbrains.kotlin.fir.resolve.calls.ConeInferenceContext
import org.jetbrains.kotlin.fir.resolve.calls.candidate
import org.jetbrains.kotlin.fir.resolve.diagnostics.FirOperatorAmbiguityError
import org.jetbrains.kotlin.fir.resolve.diagnostics.FirVariableExpectedError
@@ -121,6 +119,7 @@ class FirExpressionsResolveTransformer(transformer: FirBodyResolveTransformer) :
}
}
if (result is FirQualifiedAccessExpression) {
dataFlowAnalyzer.enterQualifiedAccessExpression(result)
result = components.transformQualifiedAccessUsingSmartcastInfo(result)
dataFlowAnalyzer.exitQualifiedAccessExpression(result)
}
@@ -131,7 +130,6 @@ class FirExpressionsResolveTransformer(transformer: FirBodyResolveTransformer) :
if (functionCall.calleeReference is FirResolvedNamedReference && functionCall.resultType is FirImplicitTypeRef) {
storeTypeFromCallee(functionCall)
}
dataFlowAnalyzer.enterFunctionCall(functionCall)
if (functionCall.calleeReference !is FirSimpleNamedReference) return functionCall.compose()
functionCall.transform<FirFunctionCall, Nothing?>(InvocationKindTransformer, null)
functionCall.transformTypeArguments(transformer, ResolutionMode.ContextIndependent)
+112
View File
@@ -0,0 +1,112 @@
digraph safeCalls_kt {
graph [splines=ortho nodesep=3]
node [shape=box penwidth=2]
edge [penwidth=2]
subgraph cluster_0 {
color=red
0 [label="Enter function foo" style="filled" fillcolor=red];
1 [label="Exit function foo" style="filled" fillcolor=red];
}
0 -> {1};
subgraph cluster_1 {
color=red
2 [label="Enter function bar" style="filled" fillcolor=red];
3 [label="Exit function bar" style="filled" fillcolor=red];
}
2 -> {3};
subgraph cluster_2 {
color=red
4 [label="Enter function getter" style="filled" fillcolor=red];
5 [label="Exit function getter" style="filled" fillcolor=red];
}
4 -> {5};
subgraph cluster_3 {
color=red
6 [label="Enter property" style="filled" fillcolor=red];
7 [label="Exit property" style="filled" fillcolor=red];
}
6 -> {7};
subgraph cluster_4 {
color=red
8 [label="Enter function getter" style="filled" fillcolor=red];
9 [label="Exit function getter" style="filled" fillcolor=red];
}
8 -> {9};
subgraph cluster_5 {
color=red
10 [label="Enter property" style="filled" fillcolor=red];
11 [label="Exit property" style="filled" fillcolor=red];
}
10 -> {11};
subgraph cluster_6 {
color=red
12 [label="Enter function test_1" style="filled" fillcolor=red];
subgraph cluster_7 {
color=blue
13 [label="Enter block"];
14 [label="Access variable R|<local>/x|"];
15 [label="Enter safe call"];
16 [label="Function call: R|<local>/x|?.R|/A.foo|()"];
17 [label="Exit safe call"];
18 [label="Enter safe call"];
19 [label="Function call: R|<local>/x|?.R|/A.foo|()?.R|/A.bar|()"];
20 [label="Exit safe call"];
21 [label="Exit block"];
}
22 [label="Exit function test_1" style="filled" fillcolor=red];
}
12 -> {13};
13 -> {14};
14 -> {15 17};
15 -> {16};
16 -> {17};
17 -> {18 20};
18 -> {19};
19 -> {20};
20 -> {21};
21 -> {22};
subgraph cluster_8 {
color=red
23 [label="Enter function test_2" style="filled" fillcolor=red];
subgraph cluster_9 {
color=blue
24 [label="Enter block"];
25 [label="Access variable R|<local>/x|"];
26 [label="Enter safe call"];
27 [label="Access variable R|/B.foo|"];
28 [label="Exit safe call"];
29 [label="Enter safe call"];
30 [label="Access variable R|/B.bar|"];
31 [label="Exit safe call"];
32 [label="Exit block"];
}
33 [label="Exit function test_2" style="filled" fillcolor=red];
}
23 -> {24};
24 -> {25};
25 -> {26 28};
26 -> {27};
27 -> {28};
28 -> {29 31};
29 -> {30};
30 -> {31};
31 -> {32};
32 -> {33};
}
+17
View File
@@ -0,0 +1,17 @@
interface A {
fun foo(): A
fun bar(): A
}
interface B {
val foo: B
val bar: B
}
fun test_1(x: A?) {
x?.foo()?.bar()
}
fun test_2(x: B?) {
x?.foo?.bar
}
+21
View File
@@ -0,0 +1,21 @@
FILE: safeCalls.kt
public abstract interface A : R|kotlin/Any| {
public abstract fun foo(): R|A|
public abstract fun bar(): R|A|
}
public abstract interface B : R|kotlin/Any| {
public abstract val foo: R|B|
public get(): R|B|
public abstract val bar: R|B|
public get(): R|B|
}
public final fun test_1(x: R|A?|): R|kotlin/Unit| {
R|<local>/x|?.R|/A.foo|()?.R|/A.bar|()
}
public final fun test_2(x: R|B?|): R|kotlin/Unit| {
R|<local>/x|?.R|/B.foo|?.R|/B.bar|
}
+129 -125
View File
@@ -43,56 +43,58 @@ digraph elvis_kt {
color=blue
10 [label="Enter when"];
11 [label="Access variable R|<local>/x|"];
12 [label="Access variable R|/A.b|"];
13 [label="Variable declaration: lval <elvis>: R|kotlin/Boolean?|"];
12 [label="Enter safe call"];
13 [label="Access variable R|/A.b|"];
14 [label="Exit safe call"];
15 [label="Variable declaration: lval <elvis>: R|kotlin/Boolean?|"];
subgraph cluster_8 {
color=blue
14 [label="Enter when branch condition "];
15 [label="Const: Null(null)"];
16 [label="Operator =="];
17 [label="Exit when branch condition"];
16 [label="Enter when branch condition "];
17 [label="Const: Null(null)"];
18 [label="Operator =="];
19 [label="Exit when branch condition"];
}
subgraph cluster_9 {
color=blue
18 [label="Enter when branch condition else"];
19 [label="Exit when branch condition"];
20 [label="Enter when branch condition else"];
21 [label="Exit when branch condition"];
}
20 [label="Enter when branch result"];
22 [label="Enter when branch result"];
subgraph cluster_10 {
color=blue
21 [label="Enter block"];
22 [label="Access variable R|<local>/<elvis>|"];
23 [label="Exit block"];
23 [label="Enter block"];
24 [label="Access variable R|<local>/<elvis>|"];
25 [label="Exit block"];
}
24 [label="Exit when branch result"];
25 [label="Enter when branch result"];
26 [label="Exit when branch result"];
27 [label="Enter when branch result"];
subgraph cluster_11 {
color=blue
26 [label="Enter block"];
27 [label="Jump: ^test_1 Unit"];
28 [label="Stub" style="filled" fillcolor=gray];
29 [label="Exit block" style="filled" fillcolor=gray];
28 [label="Enter block"];
29 [label="Jump: ^test_1 Unit"];
30 [label="Stub" style="filled" fillcolor=gray];
31 [label="Exit block" style="filled" fillcolor=gray];
}
30 [label="Exit when branch result" style="filled" fillcolor=gray];
31 [label="Exit when"];
32 [label="Exit when branch result" style="filled" fillcolor=gray];
33 [label="Exit when"];
}
32 [label="Exit when branch condition"];
34 [label="Exit when branch condition"];
}
33 [label="Synthetic else branch"];
34 [label="Enter when branch result"];
35 [label="Synthetic else branch"];
36 [label="Enter when branch result"];
subgraph cluster_12 {
color=blue
35 [label="Enter block"];
36 [label="Access variable R|<local>/x|"];
37 [label="Function call: R|<local>/x|.R|/A.foo|()"];
38 [label="Exit block"];
37 [label="Enter block"];
38 [label="Access variable R|<local>/x|"];
39 [label="Function call: R|<local>/x|.R|/A.foo|()"];
40 [label="Exit block"];
}
39 [label="Exit when branch result"];
40 [label="Exit when"];
41 [label="Exit when branch result"];
42 [label="Exit when"];
}
41 [label="Exit block"];
43 [label="Exit block"];
}
42 [label="Exit function test_1" style="filled" fillcolor=red];
44 [label="Exit function test_1" style="filled" fillcolor=red];
}
6 -> {7};
@@ -100,127 +102,129 @@ digraph elvis_kt {
8 -> {9};
9 -> {10};
10 -> {11};
11 -> {12};
11 -> {12 14};
12 -> {13};
13 -> {14};
14 -> {15};
15 -> {16};
16 -> {17};
17 -> {25 18};
17 -> {18};
18 -> {19};
19 -> {20};
19 -> {27 20};
20 -> {21};
21 -> {22};
22 -> {23};
23 -> {24};
24 -> {31};
24 -> {25};
25 -> {26};
26 -> {27};
27 -> {42};
27 -> {28} [style=dotted];
28 -> {29} [style=dotted];
26 -> {33};
27 -> {28};
28 -> {29};
29 -> {44};
29 -> {30} [style=dotted];
30 -> {31} [style=dotted];
31 -> {32};
32 -> {34 33};
33 -> {40};
34 -> {35};
35 -> {36};
31 -> {32} [style=dotted];
32 -> {33} [style=dotted];
33 -> {34};
34 -> {36 35};
35 -> {42};
36 -> {37};
37 -> {38};
38 -> {39};
39 -> {40};
40 -> {41};
41 -> {42};
42 -> {43};
43 -> {44};
subgraph cluster_13 {
color=red
43 [label="Enter function test2" style="filled" fillcolor=red];
45 [label="Enter function test2" style="filled" fillcolor=red];
subgraph cluster_14 {
color=blue
44 [label="Enter block"];
46 [label="Enter block"];
subgraph cluster_15 {
color=blue
45 [label="Enter when"];
47 [label="Enter when"];
subgraph cluster_16 {
color=blue
46 [label="Enter when branch condition "];
47 [label="Access variable R|<local>/b|"];
48 [label="Type operator: b !is String"];
49 [label="Exit when branch condition"];
48 [label="Enter when branch condition "];
49 [label="Access variable R|<local>/b|"];
50 [label="Type operator: b !is String"];
51 [label="Exit when branch condition"];
}
50 [label="Synthetic else branch"];
51 [label="Enter when branch result"];
52 [label="Synthetic else branch"];
53 [label="Enter when branch result"];
subgraph cluster_17 {
color=blue
52 [label="Enter block"];
53 [label="Const: String()"];
54 [label="Jump: ^test2 String()"];
55 [label="Stub" style="filled" fillcolor=gray];
56 [label="Exit block" style="filled" fillcolor=gray];
54 [label="Enter block"];
55 [label="Const: String()"];
56 [label="Jump: ^test2 String()"];
57 [label="Stub" style="filled" fillcolor=gray];
58 [label="Exit block" style="filled" fillcolor=gray];
}
57 [label="Exit when branch result" style="filled" fillcolor=gray];
58 [label="Exit when"];
59 [label="Exit when branch result" style="filled" fillcolor=gray];
60 [label="Exit when"];
}
subgraph cluster_18 {
color=blue
59 [label="Enter when"];
61 [label="Enter when"];
subgraph cluster_19 {
color=blue
60 [label="Enter when branch condition "];
61 [label="Access variable R|<local>/a|"];
62 [label="Type operator: a !is String?"];
63 [label="Exit when branch condition"];
62 [label="Enter when branch condition "];
63 [label="Access variable R|<local>/a|"];
64 [label="Type operator: a !is String?"];
65 [label="Exit when branch condition"];
}
64 [label="Synthetic else branch"];
65 [label="Enter when branch result"];
66 [label="Synthetic else branch"];
67 [label="Enter when branch result"];
subgraph cluster_20 {
color=blue
66 [label="Enter block"];
67 [label="Const: String()"];
68 [label="Jump: ^test2 String()"];
69 [label="Stub" style="filled" fillcolor=gray];
70 [label="Exit block" style="filled" fillcolor=gray];
68 [label="Enter block"];
69 [label="Const: String()"];
70 [label="Jump: ^test2 String()"];
71 [label="Stub" style="filled" fillcolor=gray];
72 [label="Exit block" style="filled" fillcolor=gray];
}
71 [label="Exit when branch result" style="filled" fillcolor=gray];
72 [label="Exit when"];
73 [label="Exit when branch result" style="filled" fillcolor=gray];
74 [label="Exit when"];
}
subgraph cluster_21 {
color=blue
73 [label="Enter when"];
74 [label="Access variable R|<local>/a|"];
75 [label="Variable declaration: lval <elvis>: R|kotlin/String?|"];
75 [label="Enter when"];
76 [label="Access variable R|<local>/a|"];
77 [label="Variable declaration: lval <elvis>: R|kotlin/String?|"];
subgraph cluster_22 {
color=blue
76 [label="Enter when branch condition "];
77 [label="Const: Null(null)"];
78 [label="Operator =="];
79 [label="Exit when branch condition"];
78 [label="Enter when branch condition "];
79 [label="Const: Null(null)"];
80 [label="Operator =="];
81 [label="Exit when branch condition"];
}
subgraph cluster_23 {
color=blue
80 [label="Enter when branch condition else"];
81 [label="Exit when branch condition"];
82 [label="Enter when branch condition else"];
83 [label="Exit when branch condition"];
}
82 [label="Enter when branch result"];
84 [label="Enter when branch result"];
subgraph cluster_24 {
color=blue
83 [label="Enter block"];
84 [label="Access variable R|<local>/<elvis>|"];
85 [label="Exit block"];
85 [label="Enter block"];
86 [label="Access variable R|<local>/<elvis>|"];
87 [label="Exit block"];
}
86 [label="Exit when branch result"];
87 [label="Enter when branch result"];
88 [label="Exit when branch result"];
89 [label="Enter when branch result"];
subgraph cluster_25 {
color=blue
88 [label="Enter block"];
89 [label="Access variable R|<local>/b|"];
90 [label="Exit block"];
90 [label="Enter block"];
91 [label="Access variable R|<local>/b|"];
92 [label="Exit block"];
}
91 [label="Exit when branch result"];
92 [label="Exit when"];
93 [label="Exit when branch result"];
94 [label="Exit when"];
}
93 [label="Jump: ^test2 when (lval <elvis>: R|kotlin/String?| = R|<local>/a|) {
95 [label="Jump: ^test2 when (lval <elvis>: R|kotlin/String?| = R|<local>/a|) {
==($subj$, Null(null)) -> {
R|<local>/b|
}
@@ -229,67 +233,67 @@ digraph elvis_kt {
}
}
"];
94 [label="Stub" style="filled" fillcolor=gray];
95 [label="Exit block" style="filled" fillcolor=gray];
96 [label="Stub" style="filled" fillcolor=gray];
97 [label="Exit block" style="filled" fillcolor=gray];
}
96 [label="Exit function test2" style="filled" fillcolor=red];
98 [label="Exit function test2" style="filled" fillcolor=red];
}
43 -> {44};
44 -> {45};
45 -> {46};
46 -> {47};
47 -> {48};
48 -> {49};
49 -> {51 50};
50 -> {58};
51 -> {52};
52 -> {53};
49 -> {50};
50 -> {51};
51 -> {53 52};
52 -> {60};
53 -> {54};
54 -> {96};
54 -> {55} [style=dotted];
55 -> {56} [style=dotted];
54 -> {55};
55 -> {56};
56 -> {98};
56 -> {57} [style=dotted];
57 -> {58} [style=dotted];
58 -> {59};
59 -> {60};
58 -> {59} [style=dotted];
59 -> {60} [style=dotted];
60 -> {61};
61 -> {62};
62 -> {63};
63 -> {65 64};
64 -> {72};
65 -> {66};
66 -> {67};
63 -> {64};
64 -> {65};
65 -> {67 66};
66 -> {74};
67 -> {68};
68 -> {96};
68 -> {69} [style=dotted];
69 -> {70} [style=dotted];
68 -> {69};
69 -> {70};
70 -> {98};
70 -> {71} [style=dotted];
71 -> {72} [style=dotted];
72 -> {73};
73 -> {74};
72 -> {73} [style=dotted];
73 -> {74} [style=dotted];
74 -> {75};
75 -> {76};
76 -> {77};
77 -> {78};
78 -> {79};
79 -> {87 80};
79 -> {80};
80 -> {81};
81 -> {82};
81 -> {89 82};
82 -> {83};
83 -> {84};
84 -> {85};
85 -> {86};
86 -> {92};
86 -> {87};
87 -> {88};
88 -> {89};
88 -> {94};
89 -> {90};
90 -> {91};
91 -> {92};
92 -> {93};
93 -> {96};
93 -> {94} [style=dotted];
94 -> {95} [style=dotted];
93 -> {94};
94 -> {95};
95 -> {98};
95 -> {96} [style=dotted];
96 -> {97} [style=dotted];
97 -> {98} [style=dotted];
}
File diff suppressed because it is too large Load Diff
@@ -116,4 +116,4 @@ fun test_10(a: Int?, b: Int?) {
b.<!AMBIGUITY!>inc<!>()
}
b.<!AMBIGUITY!>inc<!>()
}
}
@@ -0,0 +1,301 @@
digraph safeCalls_kt {
graph [splines=ortho nodesep=3]
node [shape=box penwidth=2]
edge [penwidth=2]
subgraph cluster_0 {
color=red
0 [label="Enter function foo" style="filled" fillcolor=red];
subgraph cluster_1 {
color=blue
1 [label="Enter block"];
2 [label="Const: String()"];
3 [label="Jump: ^foo String()"];
4 [label="Stub" style="filled" fillcolor=gray];
5 [label="Exit block" style="filled" fillcolor=gray];
}
6 [label="Exit function foo" style="filled" fillcolor=red];
}
0 -> {1};
1 -> {2};
2 -> {3};
3 -> {6};
3 -> {4} [style=dotted];
4 -> {5} [style=dotted];
5 -> {6} [style=dotted];
subgraph cluster_2 {
color=red
7 [label="Enter function let" style="filled" fillcolor=red];
subgraph cluster_3 {
color=blue
8 [label="Enter block"];
9 [label="Exit block"];
}
10 [label="Exit function let" style="filled" fillcolor=red];
}
7 -> {8};
8 -> {9};
9 -> {10};
subgraph cluster_4 {
color=red
11 [label="Enter function test" style="filled" fillcolor=red];
subgraph cluster_5 {
color=blue
12 [label="Enter block"];
13 [label="Access variable R|<local>/x|"];
14 [label="Enter safe call"];
15 [label="Access variable R|<local>/x|"];
16 [label="Access variable R|kotlin/String.length|"];
17 [label="Const: Int(1)"];
18 [label="Operator =="];
19 [label="Function call: R|<local>/x|?.R|/foo|(==(R|<local>/x|.R|kotlin/String.length|, Int(1)))"];
20 [label="Exit safe call"];
21 [label="Access variable R|<local>/x|"];
22 [label="Access variable <Inapplicable(WRONG_RECEIVER): [kotlin/String.length]>#"];
23 [label="Exit block"];
}
24 [label="Exit function test" style="filled" fillcolor=red];
}
11 -> {12};
12 -> {13};
13 -> {14 20};
14 -> {15};
15 -> {16};
16 -> {17};
17 -> {18};
18 -> {19};
19 -> {20};
20 -> {21};
21 -> {22};
22 -> {23};
23 -> {24};
subgraph cluster_6 {
color=red
25 [label="Enter function bar" style="filled" fillcolor=red];
26 [label="Exit function bar" style="filled" fillcolor=red];
}
25 -> {26};
subgraph cluster_7 {
color=red
27 [label="Enter function bool" style="filled" fillcolor=red];
28 [label="Exit function bool" style="filled" fillcolor=red];
}
27 -> {28};
subgraph cluster_8 {
color=red
29 [label="Enter function id" style="filled" fillcolor=red];
30 [label="Exit function id" style="filled" fillcolor=red];
}
29 -> {30};
subgraph cluster_9 {
color=red
31 [label="Enter function test_2" style="filled" fillcolor=red];
subgraph cluster_10 {
color=blue
32 [label="Enter block"];
33 [label="Access variable R|<local>/x|"];
34 [label="Type operator: x as? A"];
35 [label="Enter safe call"];
36 [label="Access variable R|<local>/x|"];
37 [label="Function call: (R|<local>/x| as? R|A|)?.R|/A.bar|(R|<local>/x|)"];
38 [label="Exit safe call"];
39 [label="Exit block"];
}
40 [label="Exit function test_2" style="filled" fillcolor=red];
}
31 -> {32};
32 -> {33};
33 -> {34};
34 -> {35 38};
35 -> {36};
36 -> {37};
37 -> {38};
38 -> {39};
39 -> {40};
subgraph cluster_11 {
color=red
41 [label="Enter function test_3" style="filled" fillcolor=red];
subgraph cluster_12 {
color=blue
42 [label="Enter block"];
43 [label="Access variable R|<local>/x|"];
44 [label="Type operator: x as? A"];
45 [label="Enter safe call"];
46 [label="Access variable R|<local>/x|"];
47 [label="Function call: (R|<local>/x| as? R|A|)?.R|/A.bar|(R|<local>/x|)"];
48 [label="Exit safe call"];
49 [label="Enter safe call"];
50 [label="Access variable R|<local>/x|"];
51 [label="Function call: R|<local>/x|.R|/A.bool|()"];
52 [label="Function call: (R|<local>/x| as? R|A|)?.R|/A.bar|(R|<local>/x|)?.R|/foo|(R|<local>/x|.R|/A.bool|())"];
53 [label="Exit safe call"];
54 [label="Enter safe call"];
55 [label="Function call: (R|<local>/x| as? R|A|)?.R|/A.bar|(R|<local>/x|)?.R|/foo|(R|<local>/x|.R|/A.bool|())?.R|/let|(<L> = let@fun <anonymous>(): R|kotlin/Unit| {
R|<local>/x|.R|/A.bool|()
}
)"];
56 [label="Exit safe call"];
57 [label="Access variable R|<local>/x|"];
58 [label="Function call: R|<local>/x|.<Unresolved name: bool>#()"];
59 [label="Exit block"];
}
60 [label="Exit function test_3" style="filled" fillcolor=red];
}
41 -> {42};
42 -> {43};
43 -> {44};
44 -> {45 48};
45 -> {46};
46 -> {47};
47 -> {48};
48 -> {49 53};
49 -> {50};
50 -> {51};
51 -> {52};
52 -> {53};
53 -> {54 56};
54 -> {55};
55 -> {56};
56 -> {57};
57 -> {58};
58 -> {59};
59 -> {60};
subgraph cluster_13 {
color=red
61 [label="Enter function anonymousFunction" style="filled" fillcolor=red];
subgraph cluster_14 {
color=blue
62 [label="Enter block"];
63 [label="Access variable R|<local>/x|"];
64 [label="Function call: R|<local>/x|.R|/A.bool|()"];
65 [label="Exit block"];
}
66 [label="Exit function anonymousFunction" style="filled" fillcolor=red];
}
61 -> {62};
62 -> {63};
63 -> {64};
64 -> {65};
65 -> {66};
subgraph cluster_15 {
color=red
67 [label="Enter function test_4" style="filled" fillcolor=red];
subgraph cluster_16 {
color=blue
68 [label="Enter block"];
69 [label="Access variable R|<local>/x|"];
70 [label="Enter safe call"];
71 [label="Function call: R|<local>/x|?.R|/A.id|()"];
72 [label="Exit safe call"];
73 [label="Enter safe call"];
74 [label="Function call: R|<local>/x|?.R|/A.id|()?.R|/A.bool|()"];
75 [label="Exit safe call"];
76 [label="Access variable R|<local>/x|"];
77 [label="Function call: R|<local>/x|.<Inapplicable(WRONG_RECEIVER): [/A.id]>#()"];
78 [label="Exit block"];
}
79 [label="Exit function test_4" style="filled" fillcolor=red];
}
67 -> {68};
68 -> {69};
69 -> {70 72};
70 -> {71};
71 -> {72};
72 -> {73 75};
73 -> {74};
74 -> {75};
75 -> {76};
76 -> {77};
77 -> {78};
78 -> {79};
subgraph cluster_17 {
color=red
80 [label="Enter function boo" style="filled" fillcolor=red];
81 [label="Exit function boo" style="filled" fillcolor=red];
}
80 -> {81};
subgraph cluster_18 {
color=red
82 [label="Enter function test_5" style="filled" fillcolor=red];
subgraph cluster_19 {
color=blue
83 [label="Enter block"];
84 [label="Access variable R|<local>/x|"];
85 [label="Enter safe call"];
subgraph cluster_20 {
color=blue
86 [label="Enter function anonymousFunction"];
subgraph cluster_21 {
color=blue
87 [label="Enter block"];
88 [label="Jump: ^ Unit"];
89 [label="Stub" style="filled" fillcolor=gray];
90 [label="Exit block" style="filled" fillcolor=gray];
}
91 [label="Exit function anonymousFunction"];
}
92 [label="Function call: R|<local>/x|?.R|kotlin/let|<R|A|, R|kotlin/Nothing|>(<L> = let@fun <anonymous>(it: R|A|): R|kotlin/Unit| <kind=EXACTLY_ONCE> {
^ Unit
}
)"];
93 [label="Exit safe call"];
94 [label="Enter safe call"];
95 [label="Access variable R|<local>/x|"];
96 [label="Function call: R|<local>/x|.R|/A.bool|()"];
97 [label="Function call: R|<local>/x|?.R|kotlin/let|<R|A|, R|kotlin/Nothing|>(<L> = let@fun <anonymous>(it: R|A|): R|kotlin/Unit| <kind=EXACTLY_ONCE> {
^ Unit
}
)?.R|/boo|(R|<local>/x|.R|/A.bool|())"];
98 [label="Exit safe call"];
99 [label="Access variable R|<local>/x|"];
100 [label="Function call: R|<local>/x|.<Inapplicable(WRONG_RECEIVER): [/A.id]>#()"];
101 [label="Exit block"];
}
102 [label="Exit function test_5" style="filled" fillcolor=red];
}
82 -> {83};
83 -> {84};
84 -> {85 93};
85 -> {86};
86 -> {87};
87 -> {88};
88 -> {91};
88 -> {89} [style=dotted];
89 -> {90} [style=dotted];
90 -> {91} [style=dotted];
91 -> {92};
92 -> {93};
93 -> {94 98};
94 -> {95};
95 -> {96};
96 -> {97};
97 -> {98};
98 -> {99};
99 -> {100};
100 -> {101};
101 -> {102};
}
@@ -0,0 +1,37 @@
fun String.foo(b: Boolean): String = ""
fun String.let(block: () -> Unit) {}
fun test(x: String?) {
x?.foo(x.length == 1)
x.<!INAPPLICABLE_CANDIDATE!>length<!>
}
interface A {
fun bar(a: A): String
fun bool(): Boolean
fun id(): A
}
fun test_2(x: Any) {
(x as? A)?.bar(x)
}
fun test_3(x: Any) {
(x as? A)?.bar(x)?.foo(x.bool())?.let {
x.bool()
}
x.<!UNRESOLVED_REFERENCE!>bool<!>()
}
fun test_4(x: A?) {
x?.id()?.bool()
x.<!INAPPLICABLE_CANDIDATE!>id<!>()
}
fun Any?.boo(b: Boolean)
fun test_5(x: A?) {
x?.let { return }?.boo(x.bool())
x.<!INAPPLICABLE_CANDIDATE!>id<!>()
}
@@ -0,0 +1,40 @@
FILE: safeCalls.kt
public final fun R|kotlin/String|.foo(b: R|kotlin/Boolean|): R|kotlin/String| {
^foo String()
}
public final fun R|kotlin/String|.let(block: R|() -> kotlin/Unit|): R|kotlin/Unit| {
}
public final fun test(x: R|kotlin/String?|): R|kotlin/Unit| {
R|<local>/x|?.R|/foo|(==(R|<local>/x|.R|kotlin/String.length|, Int(1)))
R|<local>/x|.<Inapplicable(WRONG_RECEIVER): [kotlin/String.length]>#
}
public abstract interface A : R|kotlin/Any| {
public abstract fun bar(a: R|A|): R|kotlin/String|
public abstract fun bool(): R|kotlin/Boolean|
public abstract fun id(): R|A|
}
public final fun test_2(x: R|kotlin/Any|): R|kotlin/Unit| {
(R|<local>/x| as? R|A|)?.R|/A.bar|(R|<local>/x|)
}
public final fun test_3(x: R|kotlin/Any|): R|kotlin/Unit| {
(R|<local>/x| as? R|A|)?.R|/A.bar|(R|<local>/x|)?.R|/foo|(R|<local>/x|.R|/A.bool|())?.R|/let|(<L> = let@fun <anonymous>(): R|kotlin/Unit| {
R|<local>/x|.R|/A.bool|()
}
)
R|<local>/x|.<Unresolved name: bool>#()
}
public final fun test_4(x: R|A?|): R|kotlin/Unit| {
R|<local>/x|?.R|/A.id|()?.R|/A.bool|()
R|<local>/x|.<Inapplicable(WRONG_RECEIVER): [/A.id]>#()
}
public final fun R|kotlin/Any?|.boo(b: R|kotlin/Boolean|): R|kotlin/Unit|
public final fun test_5(x: R|A?|): R|kotlin/Unit| {
R|<local>/x|?.R|kotlin/let|<R|A|, R|kotlin/Nothing|>(<L> = let@fun <anonymous>(it: R|A|): R|kotlin/Unit| <kind=EXACTLY_ONCE> {
^ Unit
}
)?.R|/boo|(R|<local>/x|.R|/A.bool|())
R|<local>/x|.<Inapplicable(WRONG_RECEIVER): [/A.id]>#()
}
@@ -80,6 +80,11 @@ public class FirDiagnosticsWithCfgTestGenerated extends AbstractFirDiagnosticsWi
runTest("compiler/fir/resolve/testData/resolve/cfg/propertiesAndInitBlocks.kt");
}
@TestMetadata("safeCalls.kt")
public void testSafeCalls() throws Exception {
runTest("compiler/fir/resolve/testData/resolve/cfg/safeCalls.kt");
}
@TestMetadata("simple.kt")
public void testSimple() throws Exception {
runTest("compiler/fir/resolve/testData/resolve/cfg/simple.kt");
@@ -183,6 +188,11 @@ public class FirDiagnosticsWithCfgTestGenerated extends AbstractFirDiagnosticsWi
runTest("compiler/fir/resolve/testData/resolve/smartcasts/returns.kt");
}
@TestMetadata("safeCalls.kt")
public void testSafeCalls() throws Exception {
runTest("compiler/fir/resolve/testData/resolve/smartcasts/safeCalls.kt");
}
@TestMetadata("simpleIf.kt")
public void testSimpleIf() throws Exception {
runTest("compiler/fir/resolve/testData/resolve/smartcasts/simpleIf.kt");