Refactoring: extracted 'getDataFlowInfosForEntryCondition'
Removed duplicated code
This commit is contained in:
+35
-32
@@ -99,46 +99,22 @@ public class PatternMatchingTypingVisitor extends ExpressionTypingVisitor {
|
||||
DataFlowInfo commonDataFlowInfo = null;
|
||||
DataFlowInfo elseDataFlowInfo = context.dataFlowInfo;
|
||||
for (JetWhenEntry whenEntry : expression.getEntries()) {
|
||||
JetWhenCondition[] conditions = whenEntry.getConditions();
|
||||
DataFlowInfos infos = null;
|
||||
if (whenEntry.isElse()) {
|
||||
}
|
||||
else if (conditions.length == 1) {
|
||||
JetWhenCondition condition = conditions[0];
|
||||
if (condition != null) {
|
||||
infos = checkWhenCondition(
|
||||
subjectExpression, subjectExpression == null,
|
||||
subjectType, condition,
|
||||
context, subjectDataFlowValue);
|
||||
}
|
||||
DataFlowInfos infosForCondition = getDataFlowInfosForEntryCondition(
|
||||
whenEntry, context, subjectExpression, subjectType, subjectDataFlowValue);
|
||||
DataFlowInfo dataFlowInfoForEntryBody;
|
||||
if (infosForCondition == null) {
|
||||
dataFlowInfoForEntryBody = elseDataFlowInfo;
|
||||
}
|
||||
else {
|
||||
infos = null;
|
||||
for (JetWhenCondition condition : conditions) {
|
||||
DataFlowInfos conditionInfos = checkWhenCondition(subjectExpression, subjectExpression == null, subjectType, condition,
|
||||
context, subjectDataFlowValue);
|
||||
if (infos != null) {
|
||||
infos = new DataFlowInfos(infos.thenInfo.or(conditionInfos.thenInfo), infos.elseInfo.and(conditionInfos.elseInfo));
|
||||
}
|
||||
else {
|
||||
infos = conditionInfos;
|
||||
}
|
||||
}
|
||||
}
|
||||
DataFlowInfo newDataFlowInfo;
|
||||
if (infos == null) {
|
||||
newDataFlowInfo = elseDataFlowInfo;
|
||||
}
|
||||
else {
|
||||
newDataFlowInfo = infos.thenInfo;
|
||||
elseDataFlowInfo = elseDataFlowInfo.and(infos.elseInfo);
|
||||
dataFlowInfoForEntryBody = infosForCondition.thenInfo;
|
||||
elseDataFlowInfo = elseDataFlowInfo.and(infosForCondition.elseInfo);
|
||||
}
|
||||
|
||||
JetExpression bodyExpression = whenEntry.getExpression();
|
||||
if (bodyExpression != null) {
|
||||
WritableScope scopeToExtend = newWritableScopeImpl(context, "Scope extended in when entry");
|
||||
ExpressionTypingContext newContext = contextWithExpectedType
|
||||
.replaceScope(scopeToExtend).replaceDataFlowInfo(newDataFlowInfo).replaceContextDependency(INDEPENDENT);
|
||||
.replaceScope(scopeToExtend).replaceDataFlowInfo(dataFlowInfoForEntryBody).replaceContextDependency(INDEPENDENT);
|
||||
CoercionStrategy coercionStrategy = isStatement ? CoercionStrategy.COERCION_TO_UNIT : CoercionStrategy.NO_COERCION;
|
||||
JetTypeInfo typeInfo = components.expressionTypingServices.getBlockReturnedTypeWithWritableScope(
|
||||
scopeToExtend, Collections.singletonList(bodyExpression), coercionStrategy, newContext, context.trace);
|
||||
@@ -165,6 +141,33 @@ public class PatternMatchingTypingVisitor extends ExpressionTypingVisitor {
|
||||
return JetTypeInfo.create(null, commonDataFlowInfo);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private DataFlowInfos getDataFlowInfosForEntryCondition(
|
||||
@NotNull JetWhenEntry whenEntry,
|
||||
@NotNull ExpressionTypingContext context,
|
||||
@Nullable JetExpression subjectExpression,
|
||||
@NotNull JetType subjectType,
|
||||
@NotNull DataFlowValue subjectDataFlowValue
|
||||
) {
|
||||
JetWhenCondition[] conditions = whenEntry.getConditions();
|
||||
if (whenEntry.isElse()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
DataFlowInfos infos = null;
|
||||
for (JetWhenCondition condition : conditions) {
|
||||
DataFlowInfos conditionInfos = checkWhenCondition(subjectExpression, subjectExpression == null, subjectType, condition,
|
||||
context, subjectDataFlowValue);
|
||||
if (infos != null) {
|
||||
infos = new DataFlowInfos(infos.thenInfo.or(conditionInfos.thenInfo), infos.elseInfo.and(conditionInfos.elseInfo));
|
||||
}
|
||||
else {
|
||||
infos = conditionInfos;
|
||||
}
|
||||
}
|
||||
return infos;
|
||||
}
|
||||
|
||||
private DataFlowInfos checkWhenCondition(
|
||||
@Nullable final JetExpression subjectExpression,
|
||||
final boolean expectedCondition,
|
||||
|
||||
Reference in New Issue
Block a user