From 43c91117c9a0afdb0ccc4e6e3fcd303850a558ec Mon Sep 17 00:00:00 2001 From: "Archie L. Cobbs" Date: Thu, 6 Nov 2025 10:54:59 -0600 Subject: [PATCH 1/2] Avoid lossy conversion warnings for 64 bit shift amounts. --- .../com/sun/tools/javac/comp/Attr.java | 9 ++++- .../tools/javac/lint/AssignShift64Bits.java | 36 +++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 test/langtools/tools/javac/lint/AssignShift64Bits.java 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 617d8ca7077d1..666973d051ff2 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 @@ -3989,7 +3989,14 @@ public void visitAssignop(JCAssignOp tree) { chk.checkCastable(tree.rhs.pos(), operator.type.getReturnType(), owntype); - chk.checkLossOfPrecision(tree.rhs.pos(), operand, owntype); + switch (tree.getTag()) { + default -> { + chk.checkLossOfPrecision(tree.rhs.pos(), operand, owntype); + } + case SL_ASG, SR_ASG, USR_ASG -> { // don't warn about shift amounts: we only use (at most) the lower 6 bits + break; + } + } } 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; + } +} From e09ba44f760e060dad55eb1684aacdc3118a5b64 Mon Sep 17 00:00:00 2001 From: "Archie L. Cobbs" Date: Mon, 10 Nov 2025 14:52:57 -0600 Subject: [PATCH 2/2] Use cleaner switch statement syntax. --- .../share/classes/com/sun/tools/javac/comp/Attr.java | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) 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 666973d051ff2..d2d704d165c72 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 @@ -3990,12 +3990,8 @@ public void visitAssignop(JCAssignOp tree) { operator.type.getReturnType(), owntype); switch (tree.getTag()) { - default -> { - chk.checkLossOfPrecision(tree.rhs.pos(), operand, owntype); - } - case SL_ASG, SR_ASG, USR_ASG -> { // don't warn about shift amounts: we only use (at most) the lower 6 bits - break; - } + 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); } } result = check(tree, owntype, KindSelector.VAL, resultInfo);