Extracted 'deparenthesizeArgument', used it where necessary

#KT-6176 Fixed
This commit is contained in:
Svetlana Isakova
2014-11-10 17:05:25 +03:00
parent e2826a47e1
commit 8ad017c071
12 changed files with 574 additions and 33 deletions
@@ -139,6 +139,15 @@ public class ArgumentTypeResolver {
@Nullable
private static JetFunctionLiteralExpression getFunctionLiteralArgumentIfAny(@NotNull JetExpression expression) {
JetExpression deparenthesizedExpression = deparenthesizeArgument(expression);
if (deparenthesizedExpression instanceof JetFunctionLiteralExpression) {
return (JetFunctionLiteralExpression) deparenthesizedExpression;
}
return null;
}
@Nullable
public static JetExpression deparenthesizeArgument(@Nullable JetExpression expression) {
JetExpression deparenthesizedExpression = JetPsiUtil.deparenthesize(expression, false);
if (deparenthesizedExpression instanceof JetBlockExpression) {
// todo
@@ -147,13 +156,10 @@ public class ArgumentTypeResolver {
// (no arguments and no receiver) and therefore analyze them straight away (not in the 'complete' phase).
JetElement lastStatementInABlock = JetPsiUtil.getLastStatementInABlock((JetBlockExpression) deparenthesizedExpression);
if (lastStatementInABlock instanceof JetExpression) {
deparenthesizedExpression = JetPsiUtil.deparenthesize((JetExpression) lastStatementInABlock, false);
return deparenthesizeArgument((JetExpression) lastStatementInABlock);
}
}
if (deparenthesizedExpression instanceof JetFunctionLiteralExpression) {
return (JetFunctionLiteralExpression) deparenthesizedExpression;
}
return null;
return deparenthesizedExpression;
}
@NotNull
@@ -263,7 +269,7 @@ public class ArgumentTypeResolver {
return type;
}
public static <D extends CallableDescriptor> void updateNumberType(
public static void updateNumberType(
@NotNull JetType numberType,
@Nullable JetExpression expression,
@NotNull BindingTrace trace
@@ -272,16 +278,10 @@ public class ArgumentTypeResolver {
BindingContextUtils.updateRecordedType(numberType, expression, trace, false);
if (!(expression instanceof JetConstantExpression)) {
JetExpression deparenthesized = JetPsiUtil.deparenthesize(expression, false);
JetExpression deparenthesized = deparenthesizeArgument(expression);
if (deparenthesized != expression) {
updateNumberType(numberType, deparenthesized, trace);
}
if (deparenthesized instanceof JetBlockExpression) {
JetElement lastStatement = JetPsiUtil.getLastStatementInABlock((JetBlockExpression) deparenthesized);
if (lastStatement instanceof JetExpression) {
updateNumberType(numberType, (JetExpression) lastStatement, trace);
}
}
return;
}
@@ -229,12 +229,13 @@ public class CallCompleter(
if (valueArgument.isExternal()) return
val expression = valueArgument.getArgumentExpression()
if (expression == null) return
val deparenthesized = ArgumentTypeResolver.deparenthesizeArgument(expression)
if (deparenthesized == null) return
val recordedType = context.trace[BindingContext.EXPRESSION_TYPE, expression]
var updatedType: JetType? = recordedType
val results = completeCallForArgument(expression, context)
val results = completeCallForArgument(deparenthesized, context)
if (results != null && results.isSingleResult()) {
val resolvedCall = results.getResultingCall()
updatedType = if (resolvedCall.hasInferredReturnType()) resolvedCall.getResultingDescriptor()?.getReturnType() else null
@@ -256,14 +257,16 @@ public class CallCompleter(
context as CallResolutionContext<*>, RESOLVE_FUNCTION_ARGUMENTS)
}
DataFlowUtils.checkType(updatedType, expression, context as ResolutionContext<*>)
DataFlowUtils.checkType(updatedType, deparenthesized, context as ResolutionContext<*>)
}
private fun completeCallForArgument(
expression: JetExpression,
context: BasicCallResolutionContext
): OverloadResolutionResultsImpl<*>? {
val argumentCall = getCallForArgument(expression, context.trace.getBindingContext())
if (!ExpressionTypingUtils.dependsOnExpectedType(expression)) return null
val argumentCall = expression.getCall(context.trace.getBindingContext())
if (argumentCall == null) return null
val cachedDataForCall = context.resolutionResultsCache[argumentCall]
@@ -278,17 +281,6 @@ public class CallCompleter(
return completeCall(contextForArgument, cachedResults, tracing)
}
private fun getCallForArgument(argument: JetExpression?, bindingContext: BindingContext): Call? {
if (!ExpressionTypingUtils.dependsOnExpectedType(argument)) {
return null
}
if (argument is JetBlockExpression) {
val lastStatement = JetPsiUtil.getLastStatementInABlock(argument)
return getCallForArgument(lastStatement as? JetExpression, bindingContext)
}
return argument?.getCall(bindingContext)
}
private fun updateRecordedTypeForArgument(
updatedType: JetType?,
recordedType: JetType?,
@@ -52,6 +52,7 @@ import java.util.*;
import static org.jetbrains.jet.lang.diagnostics.Errors.PROJECTION_ON_NON_CLASS_TYPE_ARGUMENT;
import static org.jetbrains.jet.lang.diagnostics.Errors.SUPER_IS_NOT_AN_EXPRESSION;
import static org.jetbrains.jet.lang.resolve.calls.ArgumentTypeResolver.deparenthesizeArgument;
import static org.jetbrains.jet.lang.resolve.calls.CallResolverUtil.ResolveArgumentsMode.RESOLVE_FUNCTION_ARGUMENTS;
import static org.jetbrains.jet.lang.resolve.calls.CallResolverUtil.ResolveArgumentsMode.SHAPE_FUNCTION_ARGUMENTS;
import static org.jetbrains.jet.lang.resolve.calls.CallTransformer.CallForImplicitInvoke;
@@ -396,10 +397,10 @@ public class CandidateResolver {
@NotNull DataFlowInfo dataFlowInfoForArgument,
@NotNull BindingTrace trace
) {
if (argumentExpression == null || type == null) return type;
JetExpression deparenthesizedArgument = deparenthesizeArgument(argumentExpression);
if (deparenthesizedArgument == null || type == null) return type;
DataFlowValue dataFlowValue = DataFlowValueFactory.createDataFlowValue(
argumentExpression, type, trace.getBindingContext());
DataFlowValue dataFlowValue = DataFlowValueFactory.createDataFlowValue(deparenthesizedArgument, type, trace.getBindingContext());
if (!dataFlowValue.isStableIdentifier()) return type;
Set<JetType> possibleTypes = dataFlowInfoForArgument.getPossibleTypes(dataFlowValue);