5038439: Warning message for literal shift amounts outside the canonical domain

Reviewed-by: darcy, jlahoda
This commit is contained in:
Archie Cobbs
2025-11-14 23:53:31 +00:00
parent 7aff8e15ba
commit bc928c814b
10 changed files with 187 additions and 4 deletions

View File

@@ -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"),

View File

@@ -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);
}

View File

@@ -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.

View File

@@ -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

View File

@@ -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.

View File

@@ -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
* <tr><th scope="row">{@code finally} <td>{@code finally} clauses that do not terminate normally
* <tr><th scope="row">{@code identity} <td>use of a value-based class where an identity class is expected
* <tr><th scope="row">{@code lossy-conversions} <td>possible lossy conversions in compound assignment
* <tr><th scope="row">{@code lossy-conversions} <td>possible lossy conversions in compound assignments or bit shifts
* (more than \u00B131 bits for integers or \u00B163 bits for longs)
* <tr><th scope="row">{@code missing-explicit-ctor} <td>missing explicit constructors in public and protected classes
* in exported packages
* <tr><th scope="row">{@code module} <td>module system related issues

View File

@@ -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.

View File

@@ -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;
}
}

View File

@@ -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;
}
}

View File

@@ -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