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 commonDataFlowInfo = null;
|
||||||
DataFlowInfo elseDataFlowInfo = context.dataFlowInfo;
|
DataFlowInfo elseDataFlowInfo = context.dataFlowInfo;
|
||||||
for (JetWhenEntry whenEntry : expression.getEntries()) {
|
for (JetWhenEntry whenEntry : expression.getEntries()) {
|
||||||
JetWhenCondition[] conditions = whenEntry.getConditions();
|
DataFlowInfos infosForCondition = getDataFlowInfosForEntryCondition(
|
||||||
DataFlowInfos infos = null;
|
whenEntry, context, subjectExpression, subjectType, subjectDataFlowValue);
|
||||||
if (whenEntry.isElse()) {
|
DataFlowInfo dataFlowInfoForEntryBody;
|
||||||
}
|
if (infosForCondition == null) {
|
||||||
else if (conditions.length == 1) {
|
dataFlowInfoForEntryBody = elseDataFlowInfo;
|
||||||
JetWhenCondition condition = conditions[0];
|
|
||||||
if (condition != null) {
|
|
||||||
infos = checkWhenCondition(
|
|
||||||
subjectExpression, subjectExpression == null,
|
|
||||||
subjectType, condition,
|
|
||||||
context, subjectDataFlowValue);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
infos = null;
|
dataFlowInfoForEntryBody = infosForCondition.thenInfo;
|
||||||
for (JetWhenCondition condition : conditions) {
|
elseDataFlowInfo = elseDataFlowInfo.and(infosForCondition.elseInfo);
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
JetExpression bodyExpression = whenEntry.getExpression();
|
JetExpression bodyExpression = whenEntry.getExpression();
|
||||||
if (bodyExpression != null) {
|
if (bodyExpression != null) {
|
||||||
WritableScope scopeToExtend = newWritableScopeImpl(context, "Scope extended in when entry");
|
WritableScope scopeToExtend = newWritableScopeImpl(context, "Scope extended in when entry");
|
||||||
ExpressionTypingContext newContext = contextWithExpectedType
|
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;
|
CoercionStrategy coercionStrategy = isStatement ? CoercionStrategy.COERCION_TO_UNIT : CoercionStrategy.NO_COERCION;
|
||||||
JetTypeInfo typeInfo = components.expressionTypingServices.getBlockReturnedTypeWithWritableScope(
|
JetTypeInfo typeInfo = components.expressionTypingServices.getBlockReturnedTypeWithWritableScope(
|
||||||
scopeToExtend, Collections.singletonList(bodyExpression), coercionStrategy, newContext, context.trace);
|
scopeToExtend, Collections.singletonList(bodyExpression), coercionStrategy, newContext, context.trace);
|
||||||
@@ -165,6 +141,33 @@ public class PatternMatchingTypingVisitor extends ExpressionTypingVisitor {
|
|||||||
return JetTypeInfo.create(null, commonDataFlowInfo);
|
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(
|
private DataFlowInfos checkWhenCondition(
|
||||||
@Nullable final JetExpression subjectExpression,
|
@Nullable final JetExpression subjectExpression,
|
||||||
final boolean expectedCondition,
|
final boolean expectedCondition,
|
||||||
|
|||||||
Reference in New Issue
Block a user