@@ -109,7 +109,7 @@ public KotlinTypeInfo visitIfExpression(KtIfExpression ifExpression, ExpressionT
109
109
if (elseBranch == null ) {
110
110
if (thenBranch != null ) {
111
111
KotlinTypeInfo result = getTypeInfoWhenOnlyOneBranchIsPresent (
112
- thenBranch , thenScope , thenInfo , elseInfo , contextWithExpectedType , ifExpression , isStatement );
112
+ thenBranch , thenScope , thenInfo , elseInfo , contextWithExpectedType , ifExpression );
113
113
// If jump was possible, take condition check info as the jump info
114
114
return result .getJumpOutPossible ()
115
115
? result .replaceJumpOutPossible (true ).replaceJumpFlowInfo (conditionDataFlowInfo )
@@ -119,7 +119,7 @@ public KotlinTypeInfo visitIfExpression(KtIfExpression ifExpression, ExpressionT
119
119
}
120
120
if (thenBranch == null ) {
121
121
return getTypeInfoWhenOnlyOneBranchIsPresent (
122
- elseBranch , elseScope , elseInfo , thenInfo , contextWithExpectedType , ifExpression , isStatement );
122
+ elseBranch , elseScope , elseInfo , thenInfo , contextWithExpectedType , ifExpression );
123
123
}
124
124
KtPsiFactory psiFactory = KtPsiFactoryKt .KtPsiFactory (ifExpression );
125
125
KtBlockExpression thenBlock = psiFactory .wrapInABlockWrapper (thenBranch );
@@ -135,42 +135,51 @@ public KotlinTypeInfo visitIfExpression(KtIfExpression ifExpression, ExpressionT
135
135
BindingContext bindingContext = context .trace .getBindingContext ();
136
136
KotlinTypeInfo thenTypeInfo = BindingContextUtils .getRecordedTypeInfo (thenBranch , bindingContext );
137
137
KotlinTypeInfo elseTypeInfo = BindingContextUtils .getRecordedTypeInfo (elseBranch , bindingContext );
138
- assert thenTypeInfo != null : "'Then' branch of if expression was not processed: " + ifExpression ;
139
- assert elseTypeInfo != null : "'Else' branch of if expression was not processed: " + ifExpression ;
138
+ assert thenTypeInfo != null || elseTypeInfo != null : "Both branches of if expression were not processed: " + ifExpression .getText ();
140
139
141
140
KotlinType resultType = resolvedCall .getResultingDescriptor ().getReturnType ();
142
- KotlinType thenType = thenTypeInfo .getType ();
143
- KotlinType elseType = elseTypeInfo .getType ();
144
- DataFlowInfo thenDataFlowInfo = thenTypeInfo .getDataFlowInfo ();
145
- DataFlowInfo elseDataFlowInfo = elseTypeInfo .getDataFlowInfo ();
146
- if (resultType != null && thenType != null && elseType != null ) {
147
- DataFlowValue resultValue = DataFlowValueFactory .createDataFlowValue (ifExpression , resultType , context );
148
- DataFlowValue thenValue = DataFlowValueFactory .createDataFlowValue (thenBranch , thenType , context );
149
- thenDataFlowInfo = thenDataFlowInfo .assign (resultValue , thenValue );
150
- DataFlowValue elseValue = DataFlowValueFactory .createDataFlowValue (elseBranch , elseType , context );
151
- elseDataFlowInfo = elseDataFlowInfo .assign (resultValue , elseValue );
152
- }
153
-
154
- boolean loopBreakContinuePossible = loopBreakContinuePossibleInCondition ||
155
- thenTypeInfo .getJumpOutPossible () || elseTypeInfo .getJumpOutPossible ();
156
-
157
- boolean jumpInThen = thenType != null && KotlinBuiltIns .isNothing (thenType );
158
- boolean jumpInElse = elseType != null && KotlinBuiltIns .isNothing (elseType );
159
-
141
+ boolean loopBreakContinuePossible = loopBreakContinuePossibleInCondition ;
160
142
DataFlowInfo resultDataFlowInfo ;
161
- if (thenType == null && elseType == null ) {
162
- resultDataFlowInfo = thenDataFlowInfo .or (elseDataFlowInfo );
163
- }
164
- else if (thenType == null || (jumpInThen && !jumpInElse )) {
165
- resultDataFlowInfo = elseDataFlowInfo ;
143
+
144
+ if (elseTypeInfo == null ) {
145
+ loopBreakContinuePossible |= thenTypeInfo .getJumpOutPossible ();
146
+ resultDataFlowInfo = thenTypeInfo .getDataFlowInfo ();
166
147
}
167
- else if (elseType == null || (jumpInElse && !jumpInThen )) {
168
- resultDataFlowInfo = thenDataFlowInfo ;
148
+ else if (thenTypeInfo == null ) {
149
+ loopBreakContinuePossible |= elseTypeInfo .getJumpOutPossible ();
150
+ resultDataFlowInfo = elseTypeInfo .getDataFlowInfo ();
169
151
}
170
152
else {
171
- resultDataFlowInfo = thenDataFlowInfo .or (elseDataFlowInfo );
172
- }
153
+ KotlinType thenType = thenTypeInfo .getType ();
154
+ KotlinType elseType = elseTypeInfo .getType ();
155
+ DataFlowInfo thenDataFlowInfo = thenTypeInfo .getDataFlowInfo ();
156
+ DataFlowInfo elseDataFlowInfo = elseTypeInfo .getDataFlowInfo ();
157
+ if (resultType != null && thenType != null && elseType != null ) {
158
+ DataFlowValue resultValue = DataFlowValueFactory .createDataFlowValue (ifExpression , resultType , context );
159
+ DataFlowValue thenValue = DataFlowValueFactory .createDataFlowValue (thenBranch , thenType , context );
160
+ thenDataFlowInfo = thenDataFlowInfo .assign (resultValue , thenValue );
161
+ DataFlowValue elseValue = DataFlowValueFactory .createDataFlowValue (elseBranch , elseType , context );
162
+ elseDataFlowInfo = elseDataFlowInfo .assign (resultValue , elseValue );
163
+ }
173
164
165
+ loopBreakContinuePossible |= thenTypeInfo .getJumpOutPossible () || elseTypeInfo .getJumpOutPossible ();
166
+
167
+ boolean jumpInThen = thenType != null && KotlinBuiltIns .isNothing (thenType );
168
+ boolean jumpInElse = elseType != null && KotlinBuiltIns .isNothing (elseType );
169
+
170
+ if (thenType == null && elseType == null ) {
171
+ resultDataFlowInfo = thenDataFlowInfo .or (elseDataFlowInfo );
172
+ }
173
+ else if (thenType == null || (jumpInThen && !jumpInElse )) {
174
+ resultDataFlowInfo = elseDataFlowInfo ;
175
+ }
176
+ else if (elseType == null || (jumpInElse && !jumpInThen )) {
177
+ resultDataFlowInfo = thenDataFlowInfo ;
178
+ }
179
+ else {
180
+ resultDataFlowInfo = thenDataFlowInfo .or (elseDataFlowInfo );
181
+ }
182
+ }
174
183
// If break or continue was possible, take condition check info as the jump info
175
184
return TypeInfoFactoryKt .createTypeInfo (resultType , resultDataFlowInfo , loopBreakContinuePossible ,
176
185
loopBreakContinuePossibleInCondition ? context .dataFlowInfo : conditionDataFlowInfo );
@@ -183,8 +192,7 @@ private KotlinTypeInfo getTypeInfoWhenOnlyOneBranchIsPresent(
183
192
@ NotNull DataFlowInfo presentInfo ,
184
193
@ NotNull DataFlowInfo otherInfo ,
185
194
@ NotNull ExpressionTypingContext context ,
186
- @ NotNull KtIfExpression ifExpression ,
187
- boolean isStatement
195
+ @ NotNull KtIfExpression ifExpression
188
196
) {
189
197
ExpressionTypingContext newContext = context .replaceDataFlowInfo (presentInfo ).replaceExpectedType (NO_EXPECTED_TYPE )
190
198
.replaceContextDependency (INDEPENDENT );
0 commit comments