diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java index a45f7466f9eae..088dd2b0e0305 100644 --- a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java @@ -4000,7 +4000,10 @@ public void visitAssignop(JCAssignOp tree) { 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); diff --git a/test/langtools/tools/javac/lint/AssignShift64Bits.java b/test/langtools/tools/javac/lint/AssignShift64Bits.java new file mode 100644 index 0000000000000..b761b0aeea195 --- /dev/null +++ b/test/langtools/tools/javac/lint/AssignShift64Bits.java @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2025, 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; + } +} diff --git a/test/langtools/tools/javac/lint/ShiftOutOfRange.out b/test/langtools/tools/javac/lint/ShiftOutOfRange.out index 7aee0b014af44..6942e7e6809e4 100644 --- a/test/langtools/tools/javac/lint/ShiftOutOfRange.out +++ b/test/langtools/tools/javac/lint/ShiftOutOfRange.out @@ -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