++wrap if branches in blocks

This commit is contained in:
Svetlana Isakova
2013-08-29 18:18:35 +04:00
parent a493125a75
commit 58158ab30b
4 changed files with 23 additions and 13 deletions
@@ -563,6 +563,21 @@ public class JetPsiUtil {
return statements.isEmpty() ? null : statements.get(statements.size() - 1);
}
@NotNull
public static JetExpression unwrapFromBlock(@NotNull JetExpression expression) {
//used for 'if' branches that are wrapped in a block
if (expression instanceof JetBlockExpression) {
List<JetElement> statements = ((JetBlockExpression) expression).getStatements();
if (statements.size() == 1) {
JetElement lastStatement = getLastStatementInABlock((JetBlockExpression) expression);
if (lastStatement instanceof JetExpression) {
return (JetExpression) lastStatement;
}
}
}
return expression;
}
public static boolean isLocalClass(@NotNull JetClassOrObject classOrObject) {
return getOutermostClassOrObject(classOrObject) == null;
}
@@ -698,7 +698,8 @@ public class CandidateResolver {
) {
if (argumentExpression == null || type == null) return type;
DataFlowValue dataFlowValue = DataFlowValueFactory.INSTANCE.createDataFlowValue(argumentExpression, type, trace.getBindingContext());
DataFlowValue dataFlowValue = DataFlowValueFactory.INSTANCE.createDataFlowValue(
JetPsiUtil.unwrapFromBlock(argumentExpression), type, trace.getBindingContext());
Set<JetType> possibleTypes = dataFlowInfoForArgument.getPossibleTypes(dataFlowValue);
if (possibleTypes.isEmpty()) return type;
@@ -819,8 +820,9 @@ public class CandidateResolver {
@NotNull JetType actualType,
@NotNull ResolutionContext<?> context
) {
ExpressionReceiver receiverToCast = new ExpressionReceiver(expression, actualType);
List<ReceiverValue> variants = AutoCastUtils.getAutoCastVariants(context.trace.getBindingContext(), context.dataFlowInfo, receiverToCast);
ExpressionReceiver receiverToCast = new ExpressionReceiver(JetPsiUtil.unwrapFromBlock(expression), actualType);
List<ReceiverValue> variants =
AutoCastUtils.getAutoCastVariants(context.trace.getBindingContext(), context.dataFlowInfo, receiverToCast);
for (ReceiverValue receiverValue : variants) {
JetType possibleType = receiverValue.getType();
if (JetTypeChecker.INSTANCE.isSubtypeOf(possibleType, expectedType)) {
@@ -160,15 +160,6 @@ public class DataFlowValueFactory {
return getIdForStableIdentifier(innerExpression, bindingContext);
}
else if (expression instanceof JetBlockExpression) {
List<JetElement> statements = ((JetBlockExpression) expression).getStatements();
if (statements.size() == 1) {
JetElement lastStatement = JetPsiUtil.getLastStatementInABlock((JetBlockExpression) expression);
if (lastStatement instanceof JetExpression) {
return getIdForStableIdentifier((JetExpression) lastStatement, bindingContext);
}
}
}
else if (expression instanceof JetQualifiedExpression) {
JetQualifiedExpression qualifiedExpression = (JetQualifiedExpression) expression;
JetExpression receiverExpression = qualifiedExpression.getReceiverExpression();
@@ -145,9 +145,11 @@ public class DataFlowUtils {
}
@Nullable
public static JetType checkType(@Nullable JetType expressionType, @NotNull JetExpression expression,
public static JetType checkType(@Nullable JetType expressionType, @NotNull JetExpression possiblyWrappedInBlockExpression,
@NotNull JetType expectedType, @NotNull DataFlowInfo dataFlowInfo, @NotNull BindingTrace trace
) {
// non-block 'if' branches are wrapped in a block, but here genuine expressions (not wrappers) should be checked
JetExpression expression = JetPsiUtil.unwrapFromBlock(possiblyWrappedInBlockExpression);
if (!noExpectedType(expectedType)) {
trace.record(BindingContext.EXPECTED_EXPRESSION_TYPE, expression, expectedType);
}