Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve error message in painless when parameter must be a constant but isn't #82638

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

package org.elasticsearch.painless.phase;

import org.elasticsearch.core.Tuple;
import org.elasticsearch.painless.AnalyzerCaster;
import org.elasticsearch.painless.Operation;
import org.elasticsearch.painless.ir.BinaryImplNode;
Expand Down Expand Up @@ -66,10 +67,15 @@
import org.elasticsearch.painless.symbol.IRDecorations.IRDInstanceBinding;
import org.elasticsearch.painless.symbol.IRDecorations.IRDMethod;
import org.elasticsearch.painless.symbol.IRDecorations.IRDOperation;
import org.elasticsearch.painless.symbol.IRDecorations.IRDName;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.function.Consumer;
import java.util.stream.Collectors;

/**
* This optimization pass will perform the specified operation on two leafs nodes if they are both
Expand Down Expand Up @@ -1103,19 +1109,33 @@ private void replaceCallWithConstant(
Object receiver
) {
Object[] args = new Object[irInvokeCallMemberNode.getArgumentNodes().size()];

List<Tuple<Integer,String>> argumentsShouldBeConstants = new ArrayList<>();
for (int i = 0; i < irInvokeCallMemberNode.getArgumentNodes().size(); i++) {
ExpressionNode argNode = irInvokeCallMemberNode.getArgumentNodes().get(i);
IRDConstant constantDecoration = argNode.getDecoration(IRDConstant.class);
if (constantDecoration == null) {
// TODO find a better string to output
throw irInvokeCallMemberNode.getLocation()
.createError(
new IllegalArgumentException(
"all arguments to [" + javaMethod.getName() + "] must be constant but the [" + (i + 1) + "] argument isn't"
)
);
// offering the symbol name in error message (CastNode was evolved from ESymbol)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this handle cases when there are multiple arguments that should be constants?

Copy link
Contributor

@stu-elastic stu-elastic Jan 20, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CastNode was evolved from ESymbol

What does this mean?

String argumentName = argNode instanceof CastNode
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How does this handle the case of 2 + x as an argument?

Copy link
Contributor Author

@Kxrr Kxrr Jan 27, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It can't handle the 2 + x case yet, so if we need to handle this more complex case, I'm going to write a special method to build the original expression from Node.

Copy link
Contributor Author

@Kxrr Kxrr Jan 27, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see that the argNode has a location field with some information about the original expression, but it's missing the length. It would be much simpler to use this location field (no need to traverse the argNode's data structure)

? ((CastNode) argNode).getChildNode().getDecoration(IRDName.class).getValue()
Copy link
Contributor

@stu-elastic stu-elastic Jan 20, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we go with this code (which is not clear at the moment), getDecorationValue should be used to avoid the potential null dereference.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, I will use getDecorationValue.

: "";
argumentsShouldBeConstants.add(Tuple.tuple(i, argumentName));
} else {
args[i] = constantDecoration.getValue();
}
args[i] = constantDecoration.getValue();
}
if (argumentsShouldBeConstants.size() > 0 ){
throw irInvokeCallMemberNode
.getLocation()
.createError(new IllegalArgumentException(String.format(
Locale.ROOT,
"All arguments of the [%s] method must be constants, but the following arguments are not: %s",
javaMethod.getName(),
argumentsShouldBeConstants
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The stream and Tuple use is unnecessary, why not build the error directly?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My willingness is to print out all the errors at once. I will rollback this.

.stream()
.map(pair -> "argument [" + (pair.v1()+1) +"]"+ (pair.v2().isEmpty() ? "" : " named [" + pair.v2() + "]"))
.collect(Collectors.joining(", "))
)));
}
Object result;
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,9 +238,10 @@ public void testClassMethodCompileTimeOnly() {
public void testClassMethodCompileTimeOnlyVariableParams() {
Exception e = expectScriptThrows(
IllegalArgumentException.class,
() -> scriptEngine.compile(null, "def a = 2; classMul(2, a)", BindingsTestScript.CONTEXT, Collections.emptyMap())
() -> scriptEngine.compile(null, "def a = 2; def b = 3; classMul(a, b)", BindingsTestScript.CONTEXT, Collections.emptyMap())
);
assertThat(e.getMessage(), equalTo("all arguments to [classMul] must be constant but the [2] argument isn't"));
assertThat(e.getMessage(), equalTo("All arguments of the [classMul] method must be constants," +
" but the following arguments are not: argument [1] named [a], argument [2] named [b]"));
}

public void testClassMethodCompileTimeOnlyThrows() {
Expand Down Expand Up @@ -269,7 +270,8 @@ public void testInstanceMethodCompileTimeOnlyVariableParams() {
IllegalArgumentException.class,
() -> scriptEngine.compile(null, "def a = 2; instanceMul(a, 2)", BindingsTestScript.CONTEXT, Collections.emptyMap())
);
assertThat(e.getMessage(), equalTo("all arguments to [instanceMul] must be constant but the [1] argument isn't"));
assertThat(e.getMessage(), equalTo("All arguments of the [instanceMul] method must be constants," +
" but the following arguments are not: argument [1] named [a]"));
}

public void testCompileTimeOnlyParameterFoldedToConstant() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ fetch:
---
mutable pattern:
- do:
catch: /all arguments to \[grok\] must be constant but the \[1\] argument isn't/
catch: '/All arguments of the \[grok\] method must be constants, but the following arguments are not\: argument \[1\]/'
search:
index: http_logs
body:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ fetch:
---
mutable pattern:
- do:
catch: /all arguments to \[grok\] must be constant but the \[1\] argument isn't/
catch: '/All arguments of the \[grok\] method must be constants, but the following arguments are not\: argument \[1\]/'
search:
index: http_logs
body:
Expand Down