Painless: Fix errors allowing void to be assigned to def. (#27460)

This commit is contained in:
Jack Conradson 2017-11-28 13:44:52 -08:00 committed by GitHub
parent 9e42b77f7e
commit 2d927fabab
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 30 additions and 15 deletions

View File

@ -20,7 +20,6 @@
package org.elasticsearch.painless.node;
import org.elasticsearch.painless.DefBootstrap;
import org.elasticsearch.painless.Definition;
import org.elasticsearch.painless.Definition.Cast;
import org.elasticsearch.painless.Definition.Type;
import org.elasticsearch.painless.Globals;
@ -213,6 +212,11 @@ public final class EAssignment extends AExpression {
// If the lhs node is a def optimized node we update the actual type to remove the need for a cast.
if (lhs.isDefOptimized()) {
rhs.analyze(locals);
if (rhs.actual.clazz == void.class) {
throw createError(new IllegalArgumentException("Right-hand side cannot be a [void] type for assignment."));
}
rhs.expected = rhs.actual;
lhs.updateActual(rhs.actual);
// Otherwise, we must adapt the rhs type to the lhs type with a cast.

View File

@ -19,9 +19,7 @@
package org.elasticsearch.painless.node;
import java.util.Collections;
import org.elasticsearch.painless.DefBootstrap;
import org.elasticsearch.painless.Definition;
import org.elasticsearch.painless.Globals;
import org.elasticsearch.painless.Locals;
import org.elasticsearch.painless.Location;
@ -29,6 +27,7 @@ import org.elasticsearch.painless.MethodWriter;
import org.objectweb.asm.Type;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.Set;
@ -76,6 +75,10 @@ final class PSubDefCall extends AExpression {
totalCaptures += lambda.getCaptureCount();
}
if (expression.actual.clazz == void.class) {
throw createError(new IllegalArgumentException("Argument(s) cannot be of [void] type when calling method [" + name + "]."));
}
expression.expected = expression.actual;
arguments.set(argument, expression.cast(locals));
}

View File

@ -20,7 +20,6 @@
package org.elasticsearch.painless.node;
import org.elasticsearch.painless.DefBootstrap;
import org.elasticsearch.painless.Definition;
import org.elasticsearch.painless.Definition.Type;
import org.elasticsearch.painless.Globals;
import org.elasticsearch.painless.Locals;

View File

@ -318,4 +318,13 @@ public class CastTests extends ScriptTestCase {
exec("def x = 5L; boolean y = (boolean) (x + x); return y");
});
}
public void testIllegalVoidCasts() {
expectScriptThrows(IllegalArgumentException.class, () -> {
exec("def map = ['a': 1,'b': 2,'c': 3]; map.c = Collections.sort(new ArrayList(map.keySet()));");
});
expectScriptThrows(IllegalArgumentException.class, () -> {
exec("Map map = ['a': 1,'b': 2,'c': 3]; def x = new HashMap(); x.put(1, map.clear());");
});
}
}