Skip to content

Cast rhs to LHSTYPE in JRE_HANDLE_OVERFLOW_COMPOUND_ASSIGN. - #2817

Open
copybara-service[bot] wants to merge 1 commit into
masterfrom
test_952958170
Open

Cast rhs to LHSTYPE in JRE_HANDLE_OVERFLOW_COMPOUND_ASSIGN.#2817
copybara-service[bot] wants to merge 1 commit into
masterfrom
test_952958170

Conversation

@copybara-service

Copy link
Copy Markdown

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.

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant