Avoid throwing unchecked exceptions

Make `ResolvedCall.getValueArgumentsByIndex` return null instead of
throwing an InvalidStateException when resolving invalid arguments.
Invalid arguments are a user error (the user specified the invalid
arguments in their code), not a program error.
This commit is contained in:
Steven Allen
2014-02-27 15:53:38 -05:00
parent 162ed64706
commit 7e1e697a5b
10 changed files with 35 additions and 11 deletions
@@ -1927,7 +1927,11 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> implem
ResolvedCall<? extends CallableDescriptor> resolvedCall,
JavaClassDescriptor samInterface
) {
ResolvedValueArgument argument = resolvedCall.getValueArgumentsByIndex().get(0);
List<ResolvedValueArgument> arguments = resolvedCall.getValueArgumentsByIndex();
if (arguments == null) {
throw new IllegalStateException("Failed to arrange value arguments by index");
}
ResolvedValueArgument argument = arguments.get(0);
if (!(argument instanceof ExpressionValueArgument)) {
throw new IllegalStateException(
"argument of SAM constructor is " + argument.getClass().getName() + " " + expression.getText());
@@ -2321,6 +2325,9 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> implem
private int pushMethodArguments(@NotNull ResolvedCall resolvedCall, List<Type> valueParameterTypes, boolean skipLast, @NotNull CallGenerator callGenerator) {
@SuppressWarnings("unchecked")
List<ResolvedValueArgument> valueArguments = resolvedCall.getValueArgumentsByIndex();
if (valueArguments == null) {
throw new IllegalStateException("Failed to arrange value arguments by index");
}
CallableDescriptor fd = resolvedCall.getResultingDescriptor();
List<ValueParameterDescriptor> valueParameters = fd.getValueParameters();
@@ -69,7 +69,11 @@ public class TailRecursionCodegen {
assert fd instanceof FunctionDescriptor : "the resolved call is not refer to the function descriptor so why do we use generateTailRecursion for something strange?";
CallableMethod callable = (CallableMethod) codegen.resolveToCallable((FunctionDescriptor) fd, false);
assignParameterValues(fd, callable, resolvedCall.getValueArgumentsByIndex());
List<ResolvedValueArgument> arguments = resolvedCall.getValueArgumentsByIndex();
if (arguments == null) {
throw new IllegalStateException("Failed to arrange value arguments by index");
}
assignParameterValues(fd, callable, arguments);
if (callable.getReceiverClass() != null) {
if (resolvedCall.getReceiverArgument() != fd.getReceiverParameter().getValue()) {
StackValue expression = context.getReceiverExpression(codegen.typeMapper);
@@ -421,6 +421,9 @@ class CodegenAnnotatingVisitor extends JetVisitorVoid {
return;
}
List<ResolvedValueArgument> valueArguments = call.getValueArgumentsByIndex();
if (valueArguments == null) {
throw new IllegalStateException("Failed to arrange value arguments by index");
}
for (ValueParameterDescriptor valueParameter : original.getValueParameters()) {
JavaClassDescriptor samInterface = getInterfaceIfSamType(valueParameter.getType());
if (samInterface == null) {