-
Bug
-
Resolution: Unresolved
-
P4
-
None
-
None
javac currently accepts the case of assigning a representable constant value of type `byte` to a `Character`:
```
Character c = (byte) 0
```
This is subtly valid under the conversion chain but lacks explicit support in the JLS similarly to casting and testing contexts:
a) widening conversion from `byte` to `int`
b) a narrowing conversion from `int` to `char`, where the value of `int` is representable in `char`
c) boxing from `char` to `Character`
However, in the testing context things are different.
1) A widening and narrowing conversion is explicitly mentioned in the testing
context by name "a widening and narrowing primitive conversion (§5.1.4)".
2) A widening and narrowing conversion followed by boxing does not
explicitly appear anywhere.
The spec for assignment context currently disallows `byte` to `char` where the constant value of the former type can be represented into the latter type
(something that happens for the narrowing conversion).
In addition to that, the JLS disallows the boxing of that case.
These two spec oversights can be addressed with the following additions:
```
In addition, if the expression is a constant expression (§15.29) of type
byte, short, char, or int:
- ***Either a narrowing primitive conversion or a widening-and-narrowing
primitive conversion*** may be used if the variable is of type byte,
short, or char, and the value of the constant expression is
representable in the type of the variable.
```
and:
```
- ***Either a narrowing primitive conversion followed by a boxing
conversion, or a widening-and-narrowing primitive conversion followed by
a boxing conversion,*** may be used if the variable is of type Byte,
Short, or Character, and the value of the constant expression is
representable in the type byte, short, or char respectively.
```
```
Character c = (byte) 0
```
This is subtly valid under the conversion chain but lacks explicit support in the JLS similarly to casting and testing contexts:
a) widening conversion from `byte` to `int`
b) a narrowing conversion from `int` to `char`, where the value of `int` is representable in `char`
c) boxing from `char` to `Character`
However, in the testing context things are different.
1) A widening and narrowing conversion is explicitly mentioned in the testing
context by name "a widening and narrowing primitive conversion (§5.1.4)".
2) A widening and narrowing conversion followed by boxing does not
explicitly appear anywhere.
The spec for assignment context currently disallows `byte` to `char` where the constant value of the former type can be represented into the latter type
(something that happens for the narrowing conversion).
In addition to that, the JLS disallows the boxing of that case.
These two spec oversights can be addressed with the following additions:
```
In addition, if the expression is a constant expression (§15.29) of type
byte, short, char, or int:
- ***Either a narrowing primitive conversion or a widening-and-narrowing
primitive conversion*** may be used if the variable is of type byte,
short, or char, and the value of the constant expression is
representable in the type of the variable.
```
and:
```
- ***Either a narrowing primitive conversion followed by a boxing
conversion, or a widening-and-narrowing primitive conversion followed by
a boxing conversion,*** may be used if the variable is of type Byte,
Short, or Character, and the value of the constant expression is
representable in the type byte, short, or char respectively.
```