refactoring: extracted method 'getTypeOfLastExpressionInBlock'
This commit is contained in:
+44
-34
@@ -259,40 +259,9 @@ public class ExpressionTypingServices {
|
|||||||
trace.record(STATEMENT, statement);
|
trace.record(STATEMENT, statement);
|
||||||
JetExpression statementExpression = (JetExpression) statement;
|
JetExpression statementExpression = (JetExpression) statement;
|
||||||
if (!iterator.hasNext()) {
|
if (!iterator.hasNext()) {
|
||||||
if (noExpectedType(context.expectedType) && context.expectedType != UNIT_EXPECTED_TYPE) {
|
result = getTypeOfLastExpressionInBlock(
|
||||||
result = blockLevelVisitor.getTypeInfo(statementExpression, newContext, true);
|
statementExpression, newContext.replaceExpectedType(context.expectedType), coercionStrategyForLastExpression,
|
||||||
if (coercionStrategyForLastExpression == COERCION_TO_UNIT) {
|
blockLevelVisitor);
|
||||||
boolean mightBeUnit = false;
|
|
||||||
if (statementExpression instanceof JetDeclaration) {
|
|
||||||
mightBeUnit = true;
|
|
||||||
}
|
|
||||||
if (statementExpression instanceof JetBinaryExpression) {
|
|
||||||
JetBinaryExpression binaryExpression = (JetBinaryExpression) statementExpression;
|
|
||||||
IElementType operationType = binaryExpression.getOperationToken();
|
|
||||||
//noinspection SuspiciousMethodCalls
|
|
||||||
if (operationType == JetTokens.EQ || OperatorConventions.ASSIGNMENT_OPERATIONS.containsKey(operationType)) {
|
|
||||||
mightBeUnit = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (mightBeUnit) {
|
|
||||||
// ExpressionTypingVisitorForStatements should return only null or Unit for declarations and assignments
|
|
||||||
assert result.getType() == null || KotlinBuiltIns.getInstance().isUnit(result.getType());
|
|
||||||
result = JetTypeInfo.create(KotlinBuiltIns.getInstance().getUnitType(), newContext.dataFlowInfo);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
JetType expectedType;
|
|
||||||
if (context.expectedType == UNIT_EXPECTED_TYPE || (coercionStrategyForLastExpression == COERCION_TO_UNIT
|
|
||||||
&& KotlinBuiltIns.getInstance().isUnit(context.expectedType))) {
|
|
||||||
expectedType = UNIT_EXPECTED_TYPE;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
expectedType = context.expectedType;
|
|
||||||
}
|
|
||||||
|
|
||||||
result = blockLevelVisitor.getTypeInfo(statementExpression, newContext.replaceExpectedType(expectedType), true);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
result = blockLevelVisitor.getTypeInfo(statementExpression, newContext.replaceContextDependency(ContextDependency.INDEPENDENT), true);
|
result = blockLevelVisitor.getTypeInfo(statementExpression, newContext.replaceContextDependency(ContextDependency.INDEPENDENT), true);
|
||||||
@@ -307,6 +276,47 @@ public class ExpressionTypingServices {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private JetTypeInfo getTypeOfLastExpressionInBlock(
|
||||||
|
@NotNull JetExpression statementExpression,
|
||||||
|
@NotNull ExpressionTypingContext context,
|
||||||
|
@NotNull CoercionStrategy coercionStrategyForLastExpression,
|
||||||
|
@NotNull ExpressionTypingInternals blockLevelVisitor
|
||||||
|
) {
|
||||||
|
if (!noExpectedType(context.expectedType) || context.expectedType == UNIT_EXPECTED_TYPE) {
|
||||||
|
JetType expectedType;
|
||||||
|
if (context.expectedType == UNIT_EXPECTED_TYPE ||//the first check is necessary to avoid invocation 'isUnit(UNIT_EXPECTED_TYPE)'
|
||||||
|
(coercionStrategyForLastExpression == COERCION_TO_UNIT && KotlinBuiltIns.getInstance().isUnit(context.expectedType))) {
|
||||||
|
expectedType = UNIT_EXPECTED_TYPE;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
expectedType = context.expectedType;
|
||||||
|
}
|
||||||
|
|
||||||
|
return blockLevelVisitor.getTypeInfo(statementExpression, context.replaceExpectedType(expectedType), true);
|
||||||
|
}
|
||||||
|
JetTypeInfo result = blockLevelVisitor.getTypeInfo(statementExpression, context, true);
|
||||||
|
if (coercionStrategyForLastExpression == COERCION_TO_UNIT) {
|
||||||
|
boolean mightBeUnit = false;
|
||||||
|
if (statementExpression instanceof JetDeclaration) {
|
||||||
|
mightBeUnit = true;
|
||||||
|
}
|
||||||
|
if (statementExpression instanceof JetBinaryExpression) {
|
||||||
|
JetBinaryExpression binaryExpression = (JetBinaryExpression) statementExpression;
|
||||||
|
IElementType operationType = binaryExpression.getOperationToken();
|
||||||
|
//noinspection SuspiciousMethodCalls
|
||||||
|
if (operationType == JetTokens.EQ || OperatorConventions.ASSIGNMENT_OPERATIONS.containsKey(operationType)) {
|
||||||
|
mightBeUnit = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (mightBeUnit) {
|
||||||
|
// ExpressionTypingVisitorForStatements should return only null or Unit for declarations and assignments
|
||||||
|
assert result.getType() == null || KotlinBuiltIns.getInstance().isUnit(result.getType());
|
||||||
|
result = JetTypeInfo.create(KotlinBuiltIns.getInstance().getUnitType(), context.dataFlowInfo);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
private ExpressionTypingContext createContext(ExpressionTypingContext oldContext, BindingTrace trace, WritableScope scope, DataFlowInfo dataFlowInfo, JetType expectedType) {
|
private ExpressionTypingContext createContext(ExpressionTypingContext oldContext, BindingTrace trace, WritableScope scope, DataFlowInfo dataFlowInfo, JetType expectedType) {
|
||||||
return ExpressionTypingContext.newContext(this, oldContext.labelResolver, trace, scope, dataFlowInfo, expectedType,
|
return ExpressionTypingContext.newContext(this, oldContext.labelResolver, trace, scope, dataFlowInfo, expectedType,
|
||||||
oldContext.expressionPosition, oldContext.contextDependency, oldContext.resolutionResultsCache);
|
oldContext.expressionPosition, oldContext.contextDependency, oldContext.resolutionResultsCache);
|
||||||
|
|||||||
Reference in New Issue
Block a user