Welcome! Please see the About page for a little more info on how this works.

0 votes
ago in Java Interop by

Clojure 1.12.7, JDK 21 (same on master).

A compiled call that adapts a Clojure function to a functional interface with 3 or more arguments and a primitive numeric return (int, short, byte, float, or long) silently truncates out-of-range return values.

Expected: throw ArithmeticException (for int) or IllegalArgumentException (for short, byte, float, long), matching 0 to 2 argument adapters and reflective calls.

(definterface ^{java.lang.FunctionalInterface true} SAM2I (^int f [a b]))
(definterface ^{java.lang.FunctionalInterface true} SAM3I (^int f [a b c]))
(definterface ^{java.lang.FunctionalInterface true} SAM3S (^short f [a b c]))
(definterface ^{java.lang.FunctionalInterface true} SAM3B (^byte f [a b c]))
(definterface ^{java.lang.FunctionalInterface true} SAM3F (^float f [a b c]))
(definterface ^{java.lang.FunctionalInterface true} SAM3L (^long f [a b c]))
(definterface Taker
  (^int t2i [^user.SAM2I s])
  (^int t3i [^user.SAM3I s])
  (^short t3s [^user.SAM3S s])
  (^byte t3b [^user.SAM3B s])
  (^float t3f [^user.SAM3F s])
  (^long t3l [^user.SAM3L s]))
(def tk (reify user.Taker
  (t2i [_ s] (.f s 1 2))
  (t3i [_ s] (.f s 1 2 3))
  (t3s [_ s] (.f s 1 2 3))
  (t3b [_ s] (.f s 1 2 3))
  (t3f [_ s] (.f s 1 2 3))
  (t3l [_ s] (.f s 1 2 3))))

(.t2i ^user.Taker tk (fn [a b] 4294967297))
;; => ArithmeticException: integer overflow
(.t3i (identity tk) (fn [a b c] 4294967297))
;; => ArithmeticException: integer overflow

(.t3i ^user.Taker tk (fn [a b c] 4294967297))
;; Actual: 1
;; Expected: ArithmeticException: integer overflow
(.t3s ^user.Taker tk (fn [a b c] 70000))
;; Actual: 4464
;; Expected: IllegalArgumentException
(.t3b ^user.Taker tk (fn [a b c] 300))
;; Actual: 44
;; Expected: IllegalArgumentException
(.t3f ^user.Taker tk (fn [a b c] 1e100))
;; Actual: ##Inf
;; Expected: IllegalArgumentException
(.t3l ^user.Taker tk (fn [a b c] 100000000000000000000N))
;; Actual: 7766279631452241920
;; Expected: IllegalArgumentException

Cause: for 3 to 10 parameters, Compiler.maybeEmitFIAdapter picks an Object-returning method from FnInvokers, so LambdaMetafactory unboxes without RT.*Cast range checks. Reflector.coerceAdapterReturn always runs those checks. Adding primitive-returning FnInvokers methods for 3 to 10 Object parameters would make all arities and both call paths consistent.

This bug is reported at https://s.conao3.com/blog/2026/e4f733

Please log in or register to answer this question.

...