RFR: 8371162: Compiler warns about implicit cast from long to int in shift operation [v4]

Maurizio Cimadamore mcimadamore at openjdk.org
Tue Feb 3 17:05:23 UTC 2026


On Sun, 4 Jan 2026 18:40:25 GMT, Archie Cobbs <acobbs at openjdk.org> wrote:

>> 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 with a new target base due to a merge or a rebase. The pull request now contains five commits:
> 
>  - Update copyrights to 2026.
>  - Merge branch 'master' into JDK-8371162
>  - Merge branch 'master' into JDK-8371162 to fix conflict.
>  - Use cleaner switch statement syntax.
>  - Avoid lossy conversion warnings for 64 bit shift amounts.

src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java line 4004:

> 4002:                               owntype);
> 4003:             switch (tree.getTag()) {
> 4004:             case SL_ASG, SR_ASG, USR_ASG -> { }     // we only use (at most) the lower 6 bits, so any integral type is OK

It's true that there's no loss of precision. One possible thing we might want to do (low priority) is to issue a lint warning if we see a constant value that exceeds the max shift amount.

-------------

PR Review Comment: https://git.openjdk.org/jdk/pull/28180#discussion_r2760084504


More information about the compiler-dev mailing list