Extract method

This commit is contained in:
Andrey Breslav
2012-08-21 19:26:04 +04:00
parent 2dd2a4aaf4
commit 22974391eb
@@ -250,7 +250,6 @@ public class ControlStructureTypingVisitor extends ExpressionTypingVisitor {
if (!isStatement) return DataFlowUtils.illegalStatementType(expression, contextWithExpectedType, facade); if (!isStatement) return DataFlowUtils.illegalStatementType(expression, contextWithExpectedType, facade);
ExpressionTypingContext context = contextWithExpectedType.replaceExpectedType(TypeUtils.NO_EXPECTED_TYPE); ExpressionTypingContext context = contextWithExpectedType.replaceExpectedType(TypeUtils.NO_EXPECTED_TYPE);
JetParameter loopParameter = expression.getLoopParameter();
JetExpression loopRange = expression.getLoopRange(); JetExpression loopRange = expression.getLoopRange();
JetType expectedParameterType = null; JetType expectedParameterType = null;
if (loopRange != null) { if (loopRange != null) {
@@ -262,14 +261,32 @@ public class ControlStructureTypingVisitor extends ExpressionTypingVisitor {
WritableScope loopScope = newWritableScopeImpl(context, "Scope with for-loop index"); WritableScope loopScope = newWritableScopeImpl(context, "Scope with for-loop index");
JetParameter loopParameter = expression.getLoopParameter();
if (loopParameter != null) { if (loopParameter != null) {
VariableDescriptor variableDescriptor = createLoopParameterDescriptor(loopParameter, expectedParameterType, context);
loopScope.addVariableDescriptor(variableDescriptor);
}
JetExpression body = expression.getBody();
if (body != null) {
context.expressionTypingServices.getBlockReturnedTypeWithWritableScope(loopScope, Collections.singletonList(body), CoercionStrategy.NO_COERCION, context, context.trace);
}
return DataFlowUtils.checkType(JetStandardClasses.getUnitType(), expression, contextWithExpectedType, context.dataFlowInfo);
}
private static VariableDescriptor createLoopParameterDescriptor(
JetParameter loopParameter,
JetType expectedParameterType,
ExpressionTypingContext context
) {
JetTypeReference typeReference = loopParameter.getTypeReference(); JetTypeReference typeReference = loopParameter.getTypeReference();
VariableDescriptor variableDescriptor; VariableDescriptor variableDescriptor;
if (typeReference != null) { if (typeReference != null) {
variableDescriptor = context.expressionTypingServices.getDescriptorResolver().resolveLocalVariableDescriptor(context.scope.getContainingDeclaration(), context.scope, loopParameter, context.trace); variableDescriptor = context.expressionTypingServices.getDescriptorResolver().resolveLocalVariableDescriptor(context.scope.getContainingDeclaration(), context.scope, loopParameter, context.trace);
JetType actualParameterType = variableDescriptor.getType(); JetType actualParameterType = variableDescriptor.getType();
if (expectedParameterType != null && if (expectedParameterType != null &&
actualParameterType != null &&
!JetTypeChecker.INSTANCE.isSubtypeOf(expectedParameterType, actualParameterType)) { !JetTypeChecker.INSTANCE.isSubtypeOf(expectedParameterType, actualParameterType)) {
context.trace.report(TYPE_MISMATCH_IN_FOR_LOOP.on(typeReference, expectedParameterType, actualParameterType)); context.trace.report(TYPE_MISMATCH_IN_FOR_LOOP.on(typeReference, expectedParameterType, actualParameterType));
} }
@@ -290,16 +307,7 @@ public class ControlStructureTypingVisitor extends ExpressionTypingVisitor {
context.trace.report(Errors.NAME_SHADOWING.on(declaration, variableDescriptor.getName().getName())); context.trace.report(Errors.NAME_SHADOWING.on(declaration, variableDescriptor.getName().getName()));
} }
} }
return variableDescriptor;
loopScope.addVariableDescriptor(variableDescriptor);
}
JetExpression body = expression.getBody();
if (body != null) {
context.expressionTypingServices.getBlockReturnedTypeWithWritableScope(loopScope, Collections.singletonList(body), CoercionStrategy.NO_COERCION, context, context.trace);
}
return DataFlowUtils.checkType(JetStandardClasses.getUnitType(), expression, contextWithExpectedType, context.dataFlowInfo);
} }
@Nullable @Nullable