Skip to content

Commit d5977df

Browse files
Add E0604
1 parent c9bb935 commit d5977df

File tree

4 files changed

+33
-7
lines changed

4 files changed

+33
-7
lines changed

src/librustc_typeck/check/cast.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -205,12 +205,8 @@ impl<'a, 'gcx, 'tcx> CastCheck<'tcx> {
205205
.emit();
206206
}
207207
CastError::CastToChar => {
208-
fcx.type_error_message(self.span,
209-
|actual| {
210-
format!("only `u8` can be cast as `char`, not `{}`",
211-
actual)
212-
},
213-
self.expr_ty);
208+
struct_span_err!(fcx.tcx.sess, self.span, E0604,
209+
"only `u8` can be cast as `char`, not `{}`", self.expr_ty).emit();
214210
}
215211
CastError::NonScalar => {
216212
fcx.type_error_message(self.span,

src/librustc_typeck/diagnostics.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4208,6 +4208,23 @@ println!("{}", v[2]);
42084208
```
42094209
"##,
42104210

4211+
E0604: r##"
4212+
A cast to `char` was attempted on another type than `u8`.
4213+
4214+
Erroneous code example:
4215+
4216+
```compile_fail,E0604
4217+
0u32 as char; // error: only `u8` can be cast as `char`, not `u32`
4218+
```
4219+
4220+
As the error message indicates, only `u8` can be casted into `char`. Example:
4221+
4222+
```
4223+
let c = 86u8 as char; // ok!
4224+
assert!(c, 'V');
4225+
```
4226+
"##,
4227+
42114228
E0609: r##"
42124229
Attempted to access a non-existent field in a struct.
42134230

src/test/compile-fail/E0604.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
fn main() {
12+
1u32 as char; //~ ERROR E0604
13+
}

src/test/ui/mismatched_types/cast-rfc0401.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ error[E0054]: cannot cast as `bool`
9292
|
9393
= help: compare with zero instead
9494

95-
error: only `u8` can be cast as `char`, not `u32`
95+
error[E0604]: only `u8` can be cast as `char`, not `u32`
9696
--> $DIR/cast-rfc0401.rs:51:13
9797
|
9898
51 | let _ = 0x61u32 as char;

0 commit comments

Comments
 (0)