-
Bug
-
Resolution: Fixed
-
P4
-
8
The rules for unboxing conversions in assignments (5.2) and various other contexts (invocation, 5.3; casting, 5.5; numeric, 5.6) assert that an unboxing may be followed by a widening primitive conversion, but do not allow a widening reference conversion before the unboxing occurs.
Despite these rules, longstanding behavior of javac is to allow, e.g., type variables to be widened to a boxed primitive type.
<T extends Integer> void test(T arg) {
int i = arg;
int i2 = (int) arg;
double d = arg + 1.0;
Double d2 = Double.valueOf(arg);
double d3 = (double) arg;
}
This behavior is especially important for interoperability with capture: various existing programs expect to be able to treat the elements of a 'List<? extends Integer>' as if they were ints.
List<? extends Integer> li = null;
int i = li.get(0);
Propose to change JLS in these sections to allow a widening reference conversion before unboxing.
Despite these rules, longstanding behavior of javac is to allow, e.g., type variables to be widened to a boxed primitive type.
<T extends Integer> void test(T arg) {
int i = arg;
int i2 = (int) arg;
double d = arg + 1.0;
Double d2 = Double.valueOf(arg);
double d3 = (double) arg;
}
This behavior is especially important for interoperability with capture: various existing programs expect to be able to treat the elements of a 'List<? extends Integer>' as if they were ints.
List<? extends Integer> li = null;
int i = li.get(0);
Propose to change JLS in these sections to allow a widening reference conversion before unboxing.
- relates to
-
JDK-8166421 14.14.2: Bugs in desugaring of for-each
-
- Open
-
-
JDK-8166396 5.5: Clean up description of legal conversions in casts
-
- Closed
-
-
JDK-6558543 Widening reference conversion followed by unboxing isn't allowed
-
- Closed
-
-
JDK-8055778 4.3: Clarify status of unnamed types
-
- Open
-
-
JDK-8030017 Assignments allow widening before unboxing
-
- Closed
-