fix expression type for null safe operator (#63367)

An invalid void expression type from a null safe operator caused ClassFormatError for the script Map 
x= ['0': 0]; x?.0 > 1. This change sets and propagates the correct expression type for the null safe 
operator to be written out.
This commit is contained in:
Jack Conradson 2020-10-06 15:58:23 -07:00
parent 1d0f46cc35
commit 2b838d1ea6
2 changed files with 4 additions and 1 deletions

View File

@ -436,15 +436,16 @@ public class DefaultUserTreeToIRTreePhase implements UserTreeVisitor<ScriptScope
if (irIndexNode != null) { if (irIndexNode != null) {
// this load/store requires an index // this load/store requires an index
BinaryImplNode binaryImplNode = new BinaryImplNode(location); BinaryImplNode binaryImplNode = new BinaryImplNode(location);
binaryImplNode.setExpressionType(void.class);
if (isNullSafe) { if (isNullSafe) {
// the null-safe structure is slightly different from the standard structure since // the null-safe structure is slightly different from the standard structure since
// both the index and expression are not written to the stack if the prefix is null // both the index and expression are not written to the stack if the prefix is null
binaryImplNode.setExpressionType(irExpressionNode.getExpressionType());
binaryImplNode.setLeftNode(irIndexNode); binaryImplNode.setLeftNode(irIndexNode);
binaryImplNode.setRightNode(irExpressionNode); binaryImplNode.setRightNode(irExpressionNode);
irExpressionNode = binaryImplNode; irExpressionNode = binaryImplNode;
} else { } else {
binaryImplNode.setExpressionType(void.class);
binaryImplNode.setLeftNode(irPrefixNode); binaryImplNode.setLeftNode(irPrefixNode);
binaryImplNode.setRightNode(irIndexNode); binaryImplNode.setRightNode(irIndexNode);
irPrefixNode = binaryImplNode; irPrefixNode = binaryImplNode;

View File

@ -75,6 +75,8 @@ public class ElvisTests extends ScriptTestCase {
} }
public void testWithNullSafeDereferences() { public void testWithNullSafeDereferences() {
assertEquals(false, exec("Map x = ['0': 0]; x?.0 > 5.0"));
assertEquals(false, exec("List x = [0]; x?.0 > 5.0"));
assertEquals(1, exec("return params.a?.b ?: 1")); assertEquals(1, exec("return params.a?.b ?: 1"));
assertEquals(1, exec("return params.a?.b ?: 2", singletonMap("a", singletonMap("b", 1)), true)); assertEquals(1, exec("return params.a?.b ?: 2", singletonMap("a", singletonMap("b", 1)), true));