Skip to content

Commit

Permalink
8328178: javac erroneously allows widening conversion followed by box…
Browse files Browse the repository at this point in the history
…ing to Short in assignment context
  • Loading branch information
biboudis committed Mar 18, 2024
1 parent dec68d7 commit aea4887
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2368,11 +2368,13 @@ public boolean isAssignable(Type t, Type s, Warner warn) {
return true;
break;
case CLASS:
switch (unboxedType(s).getTag()) {
case BYTE:
case CHAR:
case SHORT:
return isAssignable(t, unboxedType(s), warn);
Type unboxedType = unboxedType(s);
switch (unboxedType.getTag()) {
case BYTE:
case CHAR:
case SHORT:
return !t.getTag().isStrictSubRangeOf(unboxedType(s).getTag()) &&
isAssignable(t, unboxedType(s), warn);
}
break;
}
Expand Down
1 change: 0 additions & 1 deletion test/langtools/tools/javac/boxing/T6816548.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ public static void testShort() {
public static void testByte() {
final byte fb = 0;
Byte b = fb;
Short s = fb;
Character c = fb;
}

Expand Down
31 changes: 31 additions & 0 deletions test/langtools/tools/javac/patterns/T8328178.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* @test /nodynamiccopyright/
* @summary javac erroneously allows widening conversion followed by boxing to Short in assignment context
* @compile/fail/ref=T8328178.out -XDrawDiagnostics -XDshould-stop.at=FLOW T8328178.java
*/
public class T8328178 {

void test() {
Integer t = (byte) 0; // error, no widening p.c. from byte to int and boxing
Short s = (byte) 0; // error, no widening p.c. from byte to short and boxing

Byte B1 = (char)0; // ok, narrowing p.c. from char to byte and boxing when target is Byte
Byte B2 = (short)0; // ok, narrowing p.c. from short to byte and boxing when target is Byte
Character C1 = (short)0; // ok, narrowing p.c. from short to char and boxing when target is Character
Short S1 = (int)0; // ok, narrowing p.c. from int to char and boxing to Short
}

void testWithSwitches() {
Integer i = 42;
var s1 = switch (i) {
case (byte)0 -> true; // error
default -> false;
};

Short s = 42;
var s2 = switch (s) {
case (byte)0 -> true; // error
default -> false;
};
}
}
5 changes: 5 additions & 0 deletions test/langtools/tools/javac/patterns/T8328178.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
T8328178.java:9:21: compiler.err.prob.found.req: (compiler.misc.inconvertible.types: byte, java.lang.Integer)
T8328178.java:10:19: compiler.err.prob.found.req: (compiler.misc.inconvertible.types: byte, java.lang.Short)
T8328178.java:21:18: compiler.err.prob.found.req: (compiler.misc.inconvertible.types: byte, java.lang.Integer)
T8328178.java:27:18: compiler.err.prob.found.req: (compiler.misc.inconvertible.types: byte, java.lang.Short)
4 errors
2 changes: 2 additions & 0 deletions test/langtools/tools/javac/types/BoxingConversionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ Result[][] getResults(BoxingConversionTest harness) {
Result[][] results2 = {
//Byte, Short, Integer, Long, Float, Double, Chararacter, Boolean
/*byte*/ { T , F , F , F , F , F , F , F },
/*byte1*/ { T , F , F , F , F , F , A , F },
/*short*/ { F , T , F , F , F , F , F , F },
/*short1*/ { A , T , F , F , F , F , A , F },
/*short2*/ { F , T , F , F , F , F , A , F },
Expand Down Expand Up @@ -192,6 +193,7 @@ Result[][] getResults(BoxingConversionTest harness) {

types2 = new Type[] {
predef.byteType,
fac.Constant((byte)0x0001),
predef.shortType,
fac.Constant((short)0x0001),
fac.Constant((short)0x0100),
Expand Down

0 comments on commit aea4887

Please sign in to comment.