From 058f693b6f3f6f1e025827cdd67739aaadb2d152 Mon Sep 17 00:00:00 2001 From: J2ObjC Team Date: Thu, 23 Jul 2026 14:26:41 -0700 Subject: [PATCH] Cast rhs to LHSTYPE in JRE_HANDLE_OVERFLOW_COMPOUND_ASSIGN. The `JRE_HANDLE_OVERFLOW_COMPOUND_ASSIGN` macro generates helper functions for compound assignment operators (e.g., `+=`, `-=`, `*=`) that handle overflow. When `LHSTYPE` (e.g., `jint`) is smaller than `RHSTYPE` (e.g., `jlong`), calling `Jre##LHSTYPENAME##NAME` (which expects `LHSTYPE` arguments) with `rhs` (which is `RHSTYPE`) triggers an implicit conversion warning/error (`-Wshorten-64-to-32`). This fix explicitly casts `rhs` to `LHSTYPE` before passing it to the overflow helper function. This is semantically equivalent to Java's compound assignment behavior for +, -, and *, where the result is eventually cast back to the LHS type, and modulo arithmetic ensures the bit pattern is correct even if we cast the RHS beforehand. PiperOrigin-RevId: 952958170 --- jre_emul/Classes/J2ObjC_source.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/jre_emul/Classes/J2ObjC_source.h b/jre_emul/Classes/J2ObjC_source.h index a678cd06ab..d318086a24 100644 --- a/jre_emul/Classes/J2ObjC_source.h +++ b/jre_emul/Classes/J2ObjC_source.h @@ -462,12 +462,12 @@ JRE_HANDLE_OVERFLOW_INFIX_OPERATIONS(Times, *) #define JRE_HANDLE_OVERFLOW_COMPOUND_ASSIGN(NAME, LHSTYPENAME, RHSTYPENAME, LHSTYPE, RHSTYPE) \ __attribute__((always_inline)) inline LHSTYPE Jre##NAME##Assign##LHSTYPENAME##RHSTYPENAME( \ LHSTYPE *pLhs, RHSTYPE rhs) { \ - return *pLhs = Jre##LHSTYPENAME##NAME(*pLhs, rhs); \ + return *pLhs = Jre##LHSTYPENAME##NAME(*pLhs, (LHSTYPE)rhs); \ } \ __attribute__((always_inline)) inline LHSTYPE \ Jre##NAME##AssignVolatile##LHSTYPENAME##RHSTYPENAME(volatile_##LHSTYPE *pLhs, RHSTYPE rhs) { \ LHSTYPE lhs = __c11_atomic_load(pLhs, __ATOMIC_SEQ_CST); \ - LHSTYPE result = Jre##LHSTYPENAME##NAME(lhs, rhs); \ + LHSTYPE result = Jre##LHSTYPENAME##NAME(lhs, (LHSTYPE)rhs); \ __c11_atomic_store(pLhs, result, __ATOMIC_SEQ_CST); \ return result; \ }