Skip to content

Commit a48a489

Browse files
authored
Painless: improve error message on non-constant (#68517)
As of #68088 painless can have methods where all parameters must be constant. This improves the error message when the parameter isn't. It's still not super great, but its better and its what we can easilly give at that point in the compiler.
1 parent 6e94e67 commit a48a489

File tree

3 files changed

+11
-6
lines changed

3 files changed

+11
-6
lines changed

modules/lang-painless/src/main/java/org/elasticsearch/painless/phase/DefaultConstantFoldingOptimizationPhase.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -841,12 +841,17 @@ private void replaceCallWithConstant(
841841
Object[] args = new Object[irInvokeCallMemberNode.getArgumentNodes().size()];
842842
for (int i = 0; i < irInvokeCallMemberNode.getArgumentNodes().size(); i++) {
843843
ExpressionNode argNode = irInvokeCallMemberNode.getArgumentNodes().get(i);
844-
if (argNode instanceof ConstantNode == false) {
844+
IRDConstant constantDecoration = argNode.getDecoration(IRDConstant.class);
845+
if (constantDecoration == null) {
845846
// TODO find a better string to output
846847
throw irInvokeCallMemberNode.getLocation()
847-
.createError(new IllegalArgumentException("all arguments must be constant but the [" + (i + 1) + "] argument isn't"));
848+
.createError(
849+
new IllegalArgumentException(
850+
"all arguments to [" + javaMethod.getName() + "] must be constant but the [" + (i + 1) + "] argument isn't"
851+
)
852+
);
848853
}
849-
args[i] = ((ConstantNode) argNode).getDecorationValue(IRDConstant.class);
854+
args[i] = constantDecoration.getValue();
850855
}
851856
Object result;
852857
try {

modules/lang-painless/src/test/java/org/elasticsearch/painless/BindingsTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ public void testClassMethodCompileTimeOnlyVariableParams() {
211211
IllegalArgumentException.class,
212212
() -> scriptEngine.compile(null, "def a = 2; classMul(2, a)", BindingsTestScript.CONTEXT, Collections.emptyMap())
213213
);
214-
assertThat(e.getMessage(), equalTo("all arguments must be constant but the [2] argument isn't"));
214+
assertThat(e.getMessage(), equalTo("all arguments to [classMul] must be constant but the [2] argument isn't"));
215215
}
216216

217217
public void testClassMethodCompileTimeOnlyThrows() {
@@ -240,7 +240,7 @@ public void testInstanceMethodCompileTimeOnlyVariableParams() {
240240
IllegalArgumentException.class,
241241
() -> scriptEngine.compile(null, "def a = 2; instanceMul(a, 2)", BindingsTestScript.CONTEXT, Collections.emptyMap())
242242
);
243-
assertThat(e.getMessage(), equalTo("all arguments must be constant but the [1] argument isn't"));
243+
assertThat(e.getMessage(), equalTo("all arguments to [instanceMul] must be constant but the [1] argument isn't"));
244244
}
245245

246246
public void testCompileTimeOnlyParameterFoldedToConstant() {

x-pack/plugin/src/test/resources/rest-api-spec/test/runtime_fields/250_grok.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ fetch:
8080
---
8181
mutable pattern:
8282
- do:
83-
catch: /all arguments must be constant but the \[1\] argument isn't/
83+
catch: /all arguments to \[grok\] must be constant but the \[1\] argument isn't/
8484
search:
8585
index: http_logs
8686
body:

0 commit comments

Comments
 (0)