Catch correct exception type in MultiplicationNode (#1652)

Closes #1651
This commit is contained in:
Daniel Chao
2026-06-05 12:55:27 -07:00
committed by GitHub
parent 41e012a0f0
commit 01f8fcae7b
3 changed files with 43 additions and 33 deletions
@@ -1,5 +1,5 @@
/* /*
* Copyright © 2024 Apple Inc. and the Pkl project authors. All rights reserved. * Copyright © 2024-2026 Apple Inc. and the Pkl project authors. All rights reserved.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -31,7 +31,7 @@ public abstract class MultiplicationNode extends BinaryExpressionNode {
protected long eval(long left, long right) { protected long eval(long left, long right) {
try { try {
return StrictMath.multiplyExact(left, right); return StrictMath.multiplyExact(left, right);
} catch (VmException e) { } catch (ArithmeticException e) {
CompilerDirectives.transferToInterpreter(); CompilerDirectives.transferToInterpreter();
throw exceptionBuilder().evalError("integerOverflow").build(); throw exceptionBuilder().evalError("integerOverflow").build();
} }
@@ -48,14 +48,14 @@ examples {
42 42
00042 00042
123456789123456789 123456789123456789
-42 -42
-00042 -00042
-123456789123456789 -123456789123456789
-9223372036854775808 -9223372036854775808
9223372036854775807 9223372036854775807
1_000 1_000
1_000__000 1_000__000
1___ 1___
@@ -70,13 +70,13 @@ examples {
0x123456789ABCDEF 0x123456789ABCDEF
0x123456789aBcDeF 0x123456789aBcDeF
0x000123456789abcdef 0x000123456789abcdef
0x42 0x42
-0x123456789abcdef -0x123456789abcdef
-0x123456789ABCDEF -0x123456789ABCDEF
-0x123456789aBcDeF -0x123456789aBcDeF
-0x000123456789abcdef -0x000123456789abcdef
0x42_ab_AB_de_12 0x42_ab_AB_de_12
0x41__ 0x41__
0x59__9 0x59__9
@@ -86,10 +86,10 @@ examples {
["binary literal"] { ["binary literal"] {
0b101101 0b101101
0b000101101 0b000101101
-0b101101 -0b101101
-0b000101101 -0b000101101
0b1011_0110 0b1011_0110
0b01__10 0b01__10
0b1____0 0b1____0
@@ -107,7 +107,7 @@ examples {
0o45__67 0o45__67
0o7____0 0o7____0
-0o7_____ -0o7_____
0o644 0o644
0o755 0o755
} }
@@ -116,32 +116,32 @@ examples {
1 + 2 1 + 2
0x1 + 0b10 0x1 + 0b10
} }
["subtraction"] { ["subtraction"] {
2 - 3 2 - 3
0x2 - 0b11 0x2 - 0b11
} }
["multiplication"] { ["multiplication"] {
3 * 4 3 * 4
0x3 * 0b100 0x3 * 0b100
} }
["division"] { ["division"] {
4 / 3 4 / 3
0x4 / 0b11 0x4 / 0b11
} }
["integer division"] { ["integer division"] {
4 ~/ 3 4 ~/ 3
0x4 ~/ 0b11 0x4 ~/ 0b11
} }
["remainder"] { ["remainder"] {
5 % 6 5 % 6
0x5 % 0b110 0x5 % 0b110
} }
["negation"] { ["negation"] {
// pkl, js, dart, and kotlin use #1; their grammar has no negative numeric literals // pkl, js, dart, and kotlin use #1; their grammar has no negative numeric literals
// ruby and scala use #2 (scala switched from #1 around version 2.8) // ruby and scala use #2 (scala switched from #1 around version 2.8)
@@ -202,6 +202,17 @@ examples {
1 ** math.minInt 1 ** math.minInt
-1 ** math.minInt -1 ** math.minInt
math.maxInt ** 0
math.maxInt ** 1
4 ** 3 ** 2 == 4 ** (3 ** 2)
1 + 2 ** 3 == 1 + (2 ** 3)
2 * 2 ** 3 == 2 * (2 ** 3)
2 ** 3 + 1 == (2 ** 3) + 1
2 ** 3 * 2 == (2 ** 3) * 2
}
["integer overflow"] {
module.catch(() -> 2 ** math.maxInt8) module.catch(() -> 2 ** math.maxInt8)
module.catch(() -> 2 ** math.maxInt16) module.catch(() -> 2 ** math.maxInt16)
module.catch(() -> 2 ** math.maxInt32) module.catch(() -> 2 ** math.maxInt32)
@@ -210,15 +221,9 @@ examples {
module.catch(() -> -2 ** math.maxInt16) module.catch(() -> -2 ** math.maxInt16)
module.catch(() -> -2 ** math.maxInt32) module.catch(() -> -2 ** math.maxInt32)
module.catch(() -> -2 ** math.maxInt) module.catch(() -> -2 ** math.maxInt)
math.maxInt ** 0
math.maxInt ** 1
module.catch(() -> math.maxInt ** 2) module.catch(() -> math.maxInt ** 2)
module.catch(() -> math.maxInt * 2)
4 ** 3 ** 2 == 4 ** (3 ** 2) module.catch(() -> math.maxInt + 2)
1 + 2 ** 3 == 1 + (2 ** 3) module.catch(() -> math.minInt - 2)
2 * 2 ** 3 == 2 * (2 ** 3)
2 ** 3 + 1 == (2 ** 3) + 1
2 ** 3 * 2 == (2 ** 3) * 2
} }
} }
@@ -149,21 +149,26 @@ examples {
Infinity Infinity
1.0 1.0
1.0 1.0
"Integer overflow."
"Integer overflow."
"Integer overflow."
"Integer overflow."
"Integer overflow."
"Integer overflow."
"Integer overflow."
"Integer overflow."
1 1
9223372036854775807 9223372036854775807
"Integer overflow."
true true
true true
true true
true true
true true
} }
["integer overflow"] {
"Integer overflow."
"Integer overflow."
"Integer overflow."
"Integer overflow."
"Integer overflow."
"Integer overflow."
"Integer overflow."
"Integer overflow."
"Integer overflow."
"Integer overflow."
"Integer overflow."
"Integer overflow."
}
} }