#91836: Clarify error on casting larger integers to char
This commit is contained in:
parent
06a6674a7d
commit
a15cb49362
2 changed files with 9 additions and 3 deletions
|
@ -6,11 +6,16 @@ Erroneous code example:
|
||||||
0u32 as char; // error: only `u8` can be cast as `char`, not `u32`
|
0u32 as char; // error: only `u8` can be cast as `char`, not `u32`
|
||||||
```
|
```
|
||||||
|
|
||||||
As the error message indicates, only `u8` can be cast into `char`. Example:
|
`char` is a Unicode Scalar Value, an integer value from 0 to 0xD7FF and
|
||||||
|
0xE000 to 0x10FFFF. (The gap is for surrogate pairs.) Only `u8` always fits in
|
||||||
|
those ranges so only `u8` may be cast to `char`.
|
||||||
|
|
||||||
|
To allow larger values, use `char::from_u32`, which checks the value is valid.
|
||||||
|
|
||||||
```
|
```
|
||||||
let c = 86u8 as char; // ok!
|
assert_eq!(86u8 as char, 'V'); // ok!
|
||||||
assert_eq!(c, 'V');
|
assert_eq!(char::from_u32(0x3B1), Some('α')); // ok!
|
||||||
|
assert_eq!(char::from_u32(0xD800), None); // not a USV.
|
||||||
```
|
```
|
||||||
|
|
||||||
For more information about casts, take a look at the Type cast section in
|
For more information about casts, take a look at the Type cast section in
|
||||||
|
|
|
@ -337,6 +337,7 @@ impl<'a, 'tcx> CastCheck<'tcx> {
|
||||||
self.expr_ty
|
self.expr_ty
|
||||||
)
|
)
|
||||||
.span_label(self.span, "invalid cast")
|
.span_label(self.span, "invalid cast")
|
||||||
|
.span_help(self.span, "try `char::from_u32` instead")
|
||||||
.emit();
|
.emit();
|
||||||
}
|
}
|
||||||
CastError::NonScalar => {
|
CastError::NonScalar => {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue