8371162: Compiler warns about implicit cast from long to int in shift operation

Reviewed-by: vromero
This commit is contained in:
Archie Cobbs 2026-01-30 03:43:46 +00:00
parent 9a10cceeaf
commit 2953e0f445
3 changed files with 42 additions and 7 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 1999, 2025, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1999, 2026, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -4000,7 +4000,10 @@ public class Attr extends JCTree.Visitor {
chk.checkCastable(tree.rhs.pos(),
operator.type.getReturnType(),
owntype);
chk.checkLossOfPrecision(tree.rhs.pos(), operand, owntype);
switch (tree.getTag()) {
case SL_ASG, SR_ASG, USR_ASG -> { } // we only use (at most) the lower 6 bits, so any integral type is OK
default -> chk.checkLossOfPrecision(tree.rhs.pos(), operand, owntype);
}
chk.checkOutOfRangeShift(tree.rhs.pos(), operator, operand);
}
result = check(tree, owntype, KindSelector.VAL, resultInfo);

View File

@ -0,0 +1,36 @@
/*
* Copyright (c) 2026, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*
* @test
* @bug 8371162
* @summary Verify no lossy conversion warning for 64 bit shift amount
* @compile -Xlint:lossy-conversions -Werror AssignShift64Bits.java
*/
public class AssignShift64Bits {
void m() {
int a = 1 << 1L;
a <<= 1L;
long b = 1 << 1L;
b <<= 1L;
}
}

View File

@ -1,17 +1,13 @@
ShiftOutOfRange.java:14:18: compiler.warn.bit.shift.out.of.range: int, -32, 0
ShiftOutOfRange.java:15:18: compiler.warn.bit.shift.out.of.range: int, -32, 0
ShiftOutOfRange.java:16:19: compiler.warn.bit.shift.out.of.range: int, -32, 0
ShiftOutOfRange.java:17:15: compiler.warn.possible.loss.of.precision: long, int
ShiftOutOfRange.java:17:15: compiler.warn.bit.shift.out.of.range: int, -32, 0
ShiftOutOfRange.java:18:15: compiler.warn.bit.shift.out.of.range: int, -32, 0
ShiftOutOfRange.java:19:16: compiler.warn.bit.shift.out.of.range: int, -32, 0
ShiftOutOfRange.java:25:15: compiler.warn.possible.loss.of.precision: long, int
ShiftOutOfRange.java:32:15: compiler.warn.possible.loss.of.precision: long, int
ShiftOutOfRange.java:39:18: compiler.warn.bit.shift.out.of.range: int, 32, 0
ShiftOutOfRange.java:40:18: compiler.warn.bit.shift.out.of.range: int, 32, 0
ShiftOutOfRange.java:41:19: compiler.warn.bit.shift.out.of.range: int, 32, 0
ShiftOutOfRange.java:42:15: compiler.warn.bit.shift.out.of.range: int, 32, 0
ShiftOutOfRange.java:43:15: compiler.warn.possible.loss.of.precision: long, int
ShiftOutOfRange.java:43:15: compiler.warn.bit.shift.out.of.range: int, 32, 0
ShiftOutOfRange.java:44:16: compiler.warn.bit.shift.out.of.range: int, 32, 0
ShiftOutOfRange.java:51:18: compiler.warn.bit.shift.out.of.range: long, -64, 0
@ -26,4 +22,4 @@ ShiftOutOfRange.java:78:19: compiler.warn.bit.shift.out.of.range: long, 64, 0
ShiftOutOfRange.java:79:15: compiler.warn.bit.shift.out.of.range: long, 64, 0
ShiftOutOfRange.java:80:15: compiler.warn.bit.shift.out.of.range: long, 64, 0
ShiftOutOfRange.java:81:16: compiler.warn.bit.shift.out.of.range: long, 64, 0
28 warnings
24 warnings