RFR: 8371162: Compiler warns about implicit cast from long to int in shift operation [v2]
Archie Cobbs
acobbs at openjdk.org
Mon Nov 10 20:59:08 UTC 2025
> Consider code like this:
>
> int x = 1;
> x <<= 1L;
>
> The compiler currently emits this warning:
>
> warning: [lossy-conversions] implicit cast from long to int in compound assignment is possibly lossy
> x <<= 1L;
> ^
>
> By definition, bit shift operations only use the bottom 5 or 6 bits of the specified shift amount (in this example, `1L`), and the JLS does not require the shift amount to be any specific integral type, only that that it be some integral type. So as long all but the bottom 5 or 6 bits are zero, there is no loss of information and the warning is inappropriate.
>
> The case where the bottom 5 or 6 bits are _not_ all zero is addressed separately in [JDK-5038439](https://bugs.openjdk.org/browse/JDK-5038439).
Archie Cobbs has updated the pull request incrementally with one additional commit since the last revision:
Use cleaner switch statement syntax.
-------------
Changes:
- all: https://git.openjdk.org/jdk/pull/28180/files
- new: https://git.openjdk.org/jdk/pull/28180/files/43c91117..e09ba44f
Webrevs:
- full: https://webrevs.openjdk.org/?repo=jdk&pr=28180&range=01
- incr: https://webrevs.openjdk.org/?repo=jdk&pr=28180&range=00-01
Stats: 6 lines in 1 file changed: 0 ins; 4 del; 2 mod
Patch: https://git.openjdk.org/jdk/pull/28180.diff
Fetch: git fetch https://git.openjdk.org/jdk.git pull/28180/head:pull/28180
PR: https://git.openjdk.org/jdk/pull/28180
More information about the compiler-dev
mailing list