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();
} }
@@ -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."
}
} }