From 01f8fcae7b5000d4a01635257ec35ce21eb71eaa Mon Sep 17 00:00:00 2001 From: Daniel Chao Date: Fri, 5 Jun 2026 12:55:27 -0700 Subject: [PATCH] Catch correct exception type in MultiplicationNode (#1652) Closes #1651 --- .../expression/binary/MultiplicationNode.java | 4 +- .../LanguageSnippetTests/input/basic/int.pkl | 49 ++++++++++--------- .../LanguageSnippetTests/output/basic/int.pcf | 23 +++++---- 3 files changed, 43 insertions(+), 33 deletions(-) diff --git a/pkl-core/src/main/java/org/pkl/core/ast/expression/binary/MultiplicationNode.java b/pkl-core/src/main/java/org/pkl/core/ast/expression/binary/MultiplicationNode.java index 892b699ac..95fdeac04 100644 --- a/pkl-core/src/main/java/org/pkl/core/ast/expression/binary/MultiplicationNode.java +++ b/pkl-core/src/main/java/org/pkl/core/ast/expression/binary/MultiplicationNode.java @@ -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"); * 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) { try { return StrictMath.multiplyExact(left, right); - } catch (VmException e) { + } catch (ArithmeticException e) { CompilerDirectives.transferToInterpreter(); throw exceptionBuilder().evalError("integerOverflow").build(); } diff --git a/pkl-core/src/test/files/LanguageSnippetTests/input/basic/int.pkl b/pkl-core/src/test/files/LanguageSnippetTests/input/basic/int.pkl index 3c4f8714d..9fb643de4 100644 --- a/pkl-core/src/test/files/LanguageSnippetTests/input/basic/int.pkl +++ b/pkl-core/src/test/files/LanguageSnippetTests/input/basic/int.pkl @@ -48,14 +48,14 @@ examples { 42 00042 123456789123456789 - + -42 -00042 -123456789123456789 -9223372036854775808 9223372036854775807 - + 1_000 1_000__000 1___ @@ -70,13 +70,13 @@ examples { 0x123456789ABCDEF 0x123456789aBcDeF 0x000123456789abcdef - + 0x42 -0x123456789abcdef -0x123456789ABCDEF -0x123456789aBcDeF -0x000123456789abcdef - + 0x42_ab_AB_de_12 0x41__ 0x59__9 @@ -86,10 +86,10 @@ examples { ["binary literal"] { 0b101101 0b000101101 - + -0b101101 -0b000101101 - + 0b1011_0110 0b01__10 0b1____0 @@ -107,7 +107,7 @@ examples { 0o45__67 0o7____0 -0o7_____ - + 0o644 0o755 } @@ -116,32 +116,32 @@ examples { 1 + 2 0x1 + 0b10 } - + ["subtraction"] { 2 - 3 0x2 - 0b11 } - + ["multiplication"] { 3 * 4 0x3 * 0b100 } - + ["division"] { 4 / 3 0x4 / 0b11 } - + ["integer division"] { 4 ~/ 3 0x4 ~/ 0b11 } - + ["remainder"] { 5 % 6 0x5 % 0b110 } - + ["negation"] { // 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) @@ -202,6 +202,17 @@ examples { 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.maxInt16) module.catch(() -> 2 ** math.maxInt32) @@ -210,15 +221,9 @@ examples { module.catch(() -> -2 ** math.maxInt16) module.catch(() -> -2 ** math.maxInt32) module.catch(() -> -2 ** math.maxInt) - - math.maxInt ** 0 - math.maxInt ** 1 module.catch(() -> math.maxInt ** 2) - - 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 + module.catch(() -> math.maxInt * 2) + module.catch(() -> math.maxInt + 2) + module.catch(() -> math.minInt - 2) } } diff --git a/pkl-core/src/test/files/LanguageSnippetTests/output/basic/int.pcf b/pkl-core/src/test/files/LanguageSnippetTests/output/basic/int.pcf index 85b4b672a..ec53b03f3 100644 --- a/pkl-core/src/test/files/LanguageSnippetTests/output/basic/int.pcf +++ b/pkl-core/src/test/files/LanguageSnippetTests/output/basic/int.pcf @@ -149,21 +149,26 @@ examples { Infinity 1.0 1.0 - "Integer overflow." - "Integer overflow." - "Integer overflow." - "Integer overflow." - "Integer overflow." - "Integer overflow." - "Integer overflow." - "Integer overflow." 1 9223372036854775807 - "Integer overflow." 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." + } }