Skip to content

Commit

Permalink
Assignment context does not reject widening p.c. and boxing
Browse files Browse the repository at this point in the history
  • Loading branch information
biboudis committed Mar 14, 2024
1 parent 481c866 commit d000889
Show file tree
Hide file tree
Showing 4 changed files with 43 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/AssignmentBug.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* @test /nodynamiccopyright/
* @summary Assignment context does not reject widening p.c. and boxing
* @compile/fail/ref=AssignmentBug.out -XDrawDiagnostics -XDshould-stop.at=FLOW AssignmentBug.java
*/
public class AssignmentBug {

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/AssignmentBug.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
AssignmentBug.java:9:21: compiler.err.prob.found.req: (compiler.misc.inconvertible.types: byte, java.lang.Integer)
AssignmentBug.java:10:19: compiler.err.prob.found.req: (compiler.misc.inconvertible.types: byte, java.lang.Short)
AssignmentBug.java:21:18: compiler.err.prob.found.req: (compiler.misc.inconvertible.types: byte, java.lang.Integer)
AssignmentBug.java:27:18: compiler.err.prob.found.req: (compiler.misc.inconvertible.types: byte, java.lang.Short)
4 errors

0 comments on commit d000889

Please sign in to comment.