1
Fork 0

Rollup merge of #91939 - GKFX:feature-91866, r=cjgillot

Clarify error on casting larger integers to char

Closes #91836 with changes to E0604.md and a `span_help`.
This commit is contained in:
Matthias Krüger 2022-02-06 04:13:29 +01:00 committed by GitHub
commit 58bfe72f52
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 48 additions and 7 deletions

View file

@ -328,16 +328,28 @@ impl<'a, 'tcx> CastCheck<'tcx> {
err.emit();
}
CastError::CastToChar => {
type_error_struct!(
let mut err = type_error_struct!(
fcx.tcx.sess,
self.span,
self.expr_ty,
E0604,
"only `u8` can be cast as `char`, not `{}`",
self.expr_ty
)
.span_label(self.span, "invalid cast")
.emit();
);
err.span_label(self.span, "invalid cast");
if self.expr_ty.is_numeric() {
err.span_help(
self.span,
if self.expr_ty == fcx.tcx.types.i8 {
"try casting from `u8` instead"
} else if self.expr_ty == fcx.tcx.types.u32 {
"try `char::from_u32` instead"
} else {
"try `char::from_u32` instead (via a `u32`)"
},
);
}
err.emit();
}
CastError::NonScalar => {
let mut err = type_error_struct!(