KT-1865 Calls with default argument values are generated incorrectly

#KT-1865 Fixed
This commit is contained in:
pTalanov
2012-05-05 16:51:24 +04:00
parent 68680f702b
commit ea6076f638
4 changed files with 73 additions and 48 deletions
@@ -125,4 +125,8 @@ public final class MiscTest extends AbstractExpressionTest {
public void testExtensionLiteralCalledInsideExtensionFunction() throws Exception { public void testExtensionLiteralCalledInsideExtensionFunction() throws Exception {
checkFooBoxIsTrue("extensionLiteralCalledInsideExtensionFunction.kt"); checkFooBoxIsTrue("extensionLiteralCalledInsideExtensionFunction.kt");
} }
public void testKt1865() throws Exception {
checkFooBoxIsTrue("KT-1865.kt");
}
} }
@@ -57,7 +57,7 @@ public abstract class AbstractCallExpressionTranslator extends AbstractTranslato
this.expression = expression; this.expression = expression;
this.resolvedCall = getResolvedCallForCallExpression(bindingContext(), expression); this.resolvedCall = getResolvedCallForCallExpression(bindingContext(), expression);
this.receiver = receiver; this.receiver = receiver;
callType = type; this.callType = type;
} }
abstract public boolean shouldWrapVarargInArray(); abstract public boolean shouldWrapVarargInArray();
@@ -57,7 +57,7 @@ public final class BindingUtils {
assert descriptorClass.isInstance(descriptor) assert descriptorClass.isInstance(descriptor)
: expression.toString() + " expected to have of type" + descriptorClass.toString(); : expression.toString() + " expected to have of type" + descriptorClass.toString();
//noinspection unchecked //noinspection unchecked
return (D)descriptor; return (D) descriptor;
} }
@NotNull @NotNull
@@ -102,7 +102,7 @@ public final class BindingUtils {
@NotNull SimpleFunctionDescriptor descriptor) { @NotNull SimpleFunctionDescriptor descriptor) {
PsiElement result = BindingContextUtils.callableDescriptorToDeclaration(context, descriptor); PsiElement result = BindingContextUtils.callableDescriptorToDeclaration(context, descriptor);
assert result instanceof JetFunction : "SimpleFunctionDescriptor should have declaration of type JetFunction"; assert result instanceof JetFunction : "SimpleFunctionDescriptor should have declaration of type JetFunction";
return (JetFunction)result; return (JetFunction) result;
} }
@NotNull @NotNull
@@ -130,7 +130,7 @@ public final class BindingUtils {
return null; return null;
} }
assert result instanceof JetDeclaration : "Descriptor should correspond to an element."; assert result instanceof JetDeclaration : "Descriptor should correspond to an element.";
return (JetDeclaration)result; return (JetDeclaration) result;
} }
@NotNull @NotNull
@@ -138,7 +138,7 @@ public final class BindingUtils {
@NotNull ValueParameterDescriptor descriptor) { @NotNull ValueParameterDescriptor descriptor) {
PsiElement result = BindingContextUtils.descriptorToDeclaration(context, descriptor); PsiElement result = BindingContextUtils.descriptorToDeclaration(context, descriptor);
assert result instanceof JetParameter : "ValueParameterDescriptor should have corresponding JetParameter."; assert result instanceof JetParameter : "ValueParameterDescriptor should have corresponding JetParameter.";
return (JetParameter)result; return (JetParameter) result;
} }
public static boolean hasAncestorClass(@NotNull BindingContext context, @NotNull JetClassOrObject classDeclaration) { public static boolean hasAncestorClass(@NotNull BindingContext context, @NotNull JetClassOrObject classDeclaration) {
@@ -180,7 +180,7 @@ public final class BindingUtils {
if (!(result instanceof JetProperty)) { if (!(result instanceof JetProperty)) {
return null; return null;
} }
return (JetProperty)result; return (JetProperty) result;
} }
@NotNull @NotNull
@@ -216,7 +216,7 @@ public final class BindingUtils {
ResolvedCall<? extends CallableDescriptor> resolvedCall = context.get(BindingContext.RESOLVED_CALL, expression); ResolvedCall<? extends CallableDescriptor> resolvedCall = context.get(BindingContext.RESOLVED_CALL, expression);
assert resolvedCall != null : "Must resolve to a call."; assert resolvedCall != null : "Must resolve to a call.";
if (resolvedCall instanceof VariableAsFunctionResolvedCall) { if (resolvedCall instanceof VariableAsFunctionResolvedCall) {
return ((VariableAsFunctionResolvedCall)resolvedCall).getVariableCall(); return ((VariableAsFunctionResolvedCall) resolvedCall).getVariableCall();
} }
return resolvedCall; return resolvedCall;
} }
@@ -245,7 +245,7 @@ public final class BindingUtils {
assert descriptorForReferenceExpression instanceof FunctionDescriptor assert descriptorForReferenceExpression instanceof FunctionDescriptor
: "Operation should resolve to function descriptor."; : "Operation should resolve to function descriptor.";
return (FunctionDescriptor)descriptorForReferenceExpression; return (FunctionDescriptor) descriptorForReferenceExpression;
} }
@NotNull @NotNull
@@ -268,13 +268,23 @@ public final class BindingUtils {
@NotNull @NotNull
public static JetExpression getDefaultArgument(@NotNull BindingContext context, public static JetExpression getDefaultArgument(@NotNull BindingContext context,
@NotNull ValueParameterDescriptor parameterDescriptor) { @NotNull ValueParameterDescriptor parameterDescriptor) {
assert parameterDescriptor.hasDefaultValue() : "Unsupplied parameter must have default value."; ValueParameterDescriptor descriptorWhichDeclaresDefaultValue = getOriginalDescriptorWhichDeclaresDefaultValue(parameterDescriptor);
JetParameter psiParameter = getParameterForDescriptor(context, parameterDescriptor); JetParameter psiParameter = getParameterForDescriptor(context, descriptorWhichDeclaresDefaultValue);
JetExpression defaultValue = psiParameter.getDefaultValue(); JetExpression defaultValue = psiParameter.getDefaultValue();
assert defaultValue != null : "No default value found in PSI."; assert defaultValue != null : "No default value found in PSI.";
return defaultValue; return defaultValue;
} }
private static ValueParameterDescriptor getOriginalDescriptorWhichDeclaresDefaultValue(
@NotNull ValueParameterDescriptor parameterDescriptor) {
ValueParameterDescriptor result = parameterDescriptor;
assert result.hasDefaultValue() : "Unsupplied parameter must have default value.";
while (!result.declaresDefaultValue()) {
result = result.getOverriddenDescriptors().iterator().next();
}
return result;
}
@NotNull @NotNull
public static FunctionDescriptor getIteratorFunction(@NotNull BindingContext context, public static FunctionDescriptor getIteratorFunction(@NotNull BindingContext context,
@NotNull JetExpression rangeExpression) { @NotNull JetExpression rangeExpression) {
@@ -0,0 +1,11 @@
package foo
open class A {
open fun foo(a : Int = 1) = a
}
class B : A() {
override fun foo(a : Int) = a + 1
}
fun box() = (B().foo() == 2)