Skip to content

Commit

Permalink
Fixes casting in constant folding in Painless (#61508)
Browse files Browse the repository at this point in the history
The expression type wasn't being set to the target type after a constant fold on a ir cast node. This changes fixes that requirement.
  • Loading branch information
jdconrad authored Aug 24, 2020
1 parent a4a5fb1 commit 0ca776e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -667,6 +667,7 @@ public void visitCast(CastNode irCastNode, Consumer<ExpressionNode> scope) {
ConstantNode irConstantNode = (ConstantNode)irCastNode.getChildNode();
irConstantNode.setConstant(
AnalyzerCaster.constCast(irCastNode.getLocation(), irConstantNode.getConstant(), irCastNode.getCast()));
irConstantNode.setExpressionType(irCastNode.getExpressionType());
scope.accept(irConstantNode);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,7 @@ public void testdefToFloatImplicit() {
expectScriptThrows(ClassCastException.class, () -> exec("def d = Double.valueOf(0); Float b = d;"));
expectScriptThrows(ClassCastException.class, () -> exec("def d = new ArrayList(); Float b = d;"));
}

public void testdefToDoubleImplicit() {
expectScriptThrows(ClassCastException.class, () -> exec("def d = 'string'; Double b = d;"));
expectScriptThrows(ClassCastException.class, () -> exec("def d = true; Double b = d;"));
Expand Down Expand Up @@ -684,6 +684,19 @@ public void testdefToStringExplicit() {
assertEquals("s", exec("def d = (char)'s'; String b = (String)d; b"));
}

public void testConstFoldingDefCast() {
assertFalse((boolean)exec("def chr = 10; return (chr == (char)'x');"));
assertFalse((boolean)exec("def chr = 10; return (chr >= (char)'x');"));
assertTrue((boolean)exec("def chr = (char)10; return (chr <= (char)'x');"));
assertTrue((boolean)exec("def chr = 10; return (chr < (char)'x');"));
assertFalse((boolean)exec("def chr = (char)10; return (chr > (char)'x');"));
assertFalse((boolean)exec("def chr = 10L; return (chr > (char)'x');"));
assertFalse((boolean)exec("def chr = 10F; return (chr > (char)'x');"));
assertFalse((boolean)exec("def chr = 10D; return (chr > (char)'x');"));
assertFalse((boolean)exec("def chr = (char)10L; return (chr > (byte)10);"));
assertFalse((boolean)exec("def chr = (char)10L; return (chr > (double)(byte)(char)10);"));
}

// TODO: remove this when the transition from Joda to Java datetimes is completed
public void testdefToZonedDateTime() {
assertEquals(0L, exec(
Expand Down

0 comments on commit 0ca776e

Please sign in to comment.