diff --git a/make/langtools/tools/propertiesparser/parser/MessageType.java b/make/langtools/tools/propertiesparser/parser/MessageType.java
index a4ea0ddc3c0..4b7064e3872 100644
--- a/make/langtools/tools/propertiesparser/parser/MessageType.java
+++ b/make/langtools/tools/propertiesparser/parser/MessageType.java
@@ -84,6 +84,7 @@ public interface MessageType {
FILE_OBJECT("file object", "JavaFileObject", "javax.tools"),
PATH("path", "Path", "java.nio.file"),
NAME("name", "Name", "com.sun.tools.javac.util"),
+ LONG("long", "long", null),
NUMBER("number", "int", null),
OPTION_NAME("option name", "Option", "com.sun.tools.javac.main"),
PROFILE("profile", "Profile", "com.sun.tools.javac.jvm"),
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 4a8a8d4a324..a45f7466f9e 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
@@ -4001,6 +4001,7 @@ public class Attr extends JCTree.Visitor {
operator.type.getReturnType(),
owntype);
chk.checkLossOfPrecision(tree.rhs.pos(), operand, owntype);
+ chk.checkOutOfRangeShift(tree.rhs.pos(), operator, operand);
}
result = check(tree, owntype, KindSelector.VAL, resultInfo);
}
@@ -4091,6 +4092,7 @@ public class Attr extends JCTree.Visitor {
}
chk.checkDivZero(tree.rhs.pos(), operator, right);
+ chk.checkOutOfRangeShift(tree.rhs.pos(), operator, right);
}
result = check(tree, owntype, KindSelector.VAL, resultInfo);
}
diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Check.java b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Check.java
index da7db138604..9098568f42a 100644
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Check.java
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Check.java
@@ -4041,6 +4041,37 @@ public class Check {
}
}
+ /**
+ * Check for bit shifts using an out-of-range bit count.
+ * @param pos Position for error reporting.
+ * @param operator The operator for the expression
+ * @param operand The right hand operand for the expression
+ */
+ void checkOutOfRangeShift(final DiagnosticPosition pos, Symbol operator, Type operand) {
+ if (operand.constValue() instanceof Number shiftAmount) {
+ Type targetType;
+ int maximumShift;
+ switch (((OperatorSymbol)operator).opcode) {
+ case ByteCodes.ishl, ByteCodes.ishr, ByteCodes.iushr, ByteCodes.ishll, ByteCodes.ishrl, ByteCodes.iushrl -> {
+ targetType = syms.intType;
+ maximumShift = 0x1f;
+ }
+ case ByteCodes.lshl, ByteCodes.lshr, ByteCodes.lushr, ByteCodes.lshll, ByteCodes.lshrl, ByteCodes.lushrl -> {
+ targetType = syms.longType;
+ maximumShift = 0x3f;
+ }
+ default -> {
+ return;
+ }
+ }
+ long specifiedShift = shiftAmount.longValue();
+ if (specifiedShift > maximumShift || specifiedShift < -maximumShift) {
+ int actualShift = (int)specifiedShift & (maximumShift - 1);
+ log.warning(pos, LintWarnings.BitShiftOutOfRange(targetType, specifiedShift, actualShift));
+ }
+ }
+ }
+
/**
* Check for possible loss of precission
* @param pos Position for error reporting.
diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties b/src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties
index 6318476fca4..95a7546e2b3 100644
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties
@@ -2464,6 +2464,11 @@ compiler.err.no.zipfs.for.archive=\
compiler.warn.div.zero=\
division by zero
+# 0: type, 1: long, 2: number
+# lint: lossy-conversions
+compiler.warn.bit.shift.out.of.range=\
+ shifting {0} by {1} bits is equivalent to shifting by {2} bit(s)
+
# lint: empty
compiler.warn.empty.if=\
empty statement after if
diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/resources/javac.properties b/src/jdk.compiler/share/classes/com/sun/tools/javac/resources/javac.properties
index 6d4276c794b..d5ee9469d22 100644
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/resources/javac.properties
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/resources/javac.properties
@@ -228,7 +228,7 @@ javac.opt.Xlint.desc.incubating=\
Warn about use of incubating modules.
javac.opt.Xlint.desc.lossy-conversions=\
- Warn about possible lossy conversions in compound assignment.
+ Warn about possible lossy conversions in compound assignment and bit shift operations.
javac.opt.Xlint.desc.module=\
Warn about module system related issues.
diff --git a/src/jdk.compiler/share/classes/module-info.java b/src/jdk.compiler/share/classes/module-info.java
index 46ada3a12e7..4aa60a3dffe 100644
--- a/src/jdk.compiler/share/classes/module-info.java
+++ b/src/jdk.compiler/share/classes/module-info.java
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2014, 2024, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2014, 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
@@ -165,7 +165,8 @@ import javax.tools.StandardLocation;
* the next
*
| {@code finally} | {@code finally} clauses that do not terminate normally
* |
|---|
| {@code identity} | use of a value-based class where an identity class is expected
- * |
|---|
| {@code lossy-conversions} | possible lossy conversions in compound assignment
+ * |
|---|
| {@code lossy-conversions} | possible lossy conversions in compound assignments or bit shifts
+ * (more than \u00B131 bits for integers or \u00B163 bits for longs)
* |
|---|
| {@code missing-explicit-ctor} | missing explicit constructors in public and protected classes
* in exported packages
* |
|---|
| {@code module} | module system related issues
diff --git a/src/jdk.compiler/share/man/javac.md b/src/jdk.compiler/share/man/javac.md
index 1822931bf40..c749ca4da10 100644
--- a/src/jdk.compiler/share/man/javac.md
+++ b/src/jdk.compiler/share/man/javac.md
@@ -611,7 +611,7 @@ file system locations may be directories, JAR files or JMOD files.
- `incubating`: Warns about the use of incubating modules.
- `lossy-conversions`: Warns about possible lossy conversions
- in compound assignment.
+ in compound assignment and bit shift operations.
- `missing-explicit-ctor`: Warns about missing explicit constructors in
public and protected classes in exported packages.
diff --git a/test/langtools/tools/javac/diags/examples/BitShiftOutOfRange.java b/test/langtools/tools/javac/diags/examples/BitShiftOutOfRange.java
new file mode 100644
index 00000000000..2855c6f3023
--- /dev/null
+++ b/test/langtools/tools/javac/diags/examples/BitShiftOutOfRange.java
@@ -0,0 +1,31 @@
+/*
+ * 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.
+ */
+
+// key: compiler.warn.bit.shift.out.of.range
+// options: -Xlint:lossy-conversions
+
+class BitShiftOutOfRange {
+ int m(int a) {
+ return a << 32;
+ }
+}
diff --git a/test/langtools/tools/javac/lint/ShiftOutOfRange.java b/test/langtools/tools/javac/lint/ShiftOutOfRange.java
new file mode 100644
index 00000000000..e066c4469b9
--- /dev/null
+++ b/test/langtools/tools/javac/lint/ShiftOutOfRange.java
@@ -0,0 +1,83 @@
+/*
+ * @test /nodynamiccopyright/
+ * @bug 5038439
+ * @summary Verify warnings about bit shifts using out-of-range shift amounts
+ * @compile/ref=ShiftOutOfRange.out -XDrawDiagnostics -Xlint:lossy-conversions ShiftOutOfRange.java
+ */
+
+public class ShiftOutOfRange {
+
+ public void shiftInt() {
+ int a = 123;
+
+ // These should generate warnings
+ a = a << (byte)-32;
+ a = a >> (short)-32;
+ a = a >>> -32;
+ a <<= -32L;
+ a >>= (byte)-32;
+ a >>>= (short)-32;
+
+ // These should not generate warnings
+ a = a << (byte)-31;
+ a = a >> (short)-23;
+ a = a >>> -17;
+ a <<= -13L;
+ a >>= (byte)-1;
+ a >>>= (short)0;
+ a = a << (byte)0;
+ a = a >> (char)7;
+ a = a >>> (short)13;
+ a <<= 17;
+ a >>= (long)23;
+ a >>>= (byte)31;
+ a <<= hashCode();
+ a >>= hashCode();
+ a >>>= hashCode();
+
+ // These should generate warnings
+ a = a << (byte)32;
+ a = a >> (char)32;
+ a = a >>> (short)32;
+ a <<= 32;
+ a >>= (long)32;
+ a >>>= (byte)32;
+ }
+
+ public void shiftLong() {
+ long a = 123;
+
+ // These should generate warnings
+ a = a << (byte)-64;
+ a = a >> (short)-64;
+ a = a >>> -64;
+ a <<= -64L;
+ a >>= (byte)-64L;
+ a >>>= (short)-64;
+
+ // These should not generate warnings
+ a = a << (byte)-63;
+ a = a >> (short)-47;
+ a = a >>> -34;
+ a <<= -25L;
+ a >>= (byte)-15;
+ a >>>= (short)0;
+ a = a << (byte)0;
+ a = a >> (char)15;
+ a = a >>> (short)25;
+ a <<= 34;
+ a >>= (long)47;
+ a >>>= (byte)63;
+ a <<= hashCode();
+ a >>= hashCode();
+ a >>>= hashCode();
+
+ // These should generate warnings
+ a = a << (byte)64;
+ a = a >> (char)64;
+ a = a >>> (short)64;
+ a <<= 64;
+ a >>= (long)64;
+ a >>>= (byte)64;
+ }
+}
diff --git a/test/langtools/tools/javac/lint/ShiftOutOfRange.out b/test/langtools/tools/javac/lint/ShiftOutOfRange.out
new file mode 100644
index 00000000000..7aee0b014af
--- /dev/null
+++ b/test/langtools/tools/javac/lint/ShiftOutOfRange.out
@@ -0,0 +1,29 @@
+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
+ShiftOutOfRange.java:52:18: compiler.warn.bit.shift.out.of.range: long, -64, 0
+ShiftOutOfRange.java:53:19: compiler.warn.bit.shift.out.of.range: long, -64, 0
+ShiftOutOfRange.java:54:15: compiler.warn.bit.shift.out.of.range: long, -64, 0
+ShiftOutOfRange.java:55:15: compiler.warn.bit.shift.out.of.range: long, -64, 0
+ShiftOutOfRange.java:56:16: compiler.warn.bit.shift.out.of.range: long, -64, 0
+ShiftOutOfRange.java:76:18: compiler.warn.bit.shift.out.of.range: long, 64, 0
+ShiftOutOfRange.java:77:18: compiler.warn.bit.shift.out.of.range: long, 64, 0
+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
|
|---|