Skip to content

Commit 0e4b8ff

Browse files
Add E0605
1 parent d5977df commit 0e4b8ff

File tree

5 files changed

+70
-13
lines changed

5 files changed

+70
-13
lines changed

src/librustc_typeck/check/cast.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -209,13 +209,13 @@ impl<'a, 'gcx, 'tcx> CastCheck<'tcx> {
209209
"only `u8` can be cast as `char`, not `{}`", self.expr_ty).emit();
210210
}
211211
CastError::NonScalar => {
212-
fcx.type_error_message(self.span,
213-
|actual| {
214-
format!("non-scalar cast: `{}` as `{}`",
215-
actual,
216-
fcx.ty_to_string(self.cast_ty))
217-
},
218-
self.expr_ty);
212+
struct_span_err!(fcx.tcx.sess, self.span, E0605,
213+
"non-scalar cast: `{}` as `{}`",
214+
self.expr_ty,
215+
fcx.ty_to_string(self.cast_ty))
216+
.note("an `as` expression can only be used to convert between \
217+
primitive types. Consider using the `From` trait")
218+
.emit();
219219
}
220220
CastError::IllegalCast => {
221221
fcx.type_error_message(self.span,

src/librustc_typeck/diagnostics.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4225,6 +4225,32 @@ assert!(c, 'V');
42254225
```
42264226
"##,
42274227

4228+
E0605: r##"
4229+
An invalid cast was attempted.
4230+
4231+
Erroneous code examples:
4232+
4233+
```compile_fail,E0605
4234+
let x = 0u8;
4235+
x as Vec<u8>; // error: non-scalar cast: `u8` as `std::vec::Vec<u8>`
4236+
4237+
// Another example
4238+
4239+
let v = 0 as *const u8; // So here, `v` is a `*const u8`.
4240+
v as &u8; // error: non-scalar cast: `*const u8` as `&u8`
4241+
```
4242+
4243+
Only primitive types cast be casted into each others. Examples:
4244+
4245+
```
4246+
let x = 0u8;
4247+
x as u32; // ok!
4248+
4249+
let v = 0 as *const u8;
4250+
v as *const i8; // ok!
4251+
```
4252+
"##,
4253+
42284254
E0609: r##"
42294255
Attempted to access a non-existent field in a struct.
42304256

src/test/compile-fail/E0605.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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+
let x = 0u8;
13+
x as Vec<u8>; //~ ERROR E0605
14+
//~| NOTE an `as` expression can only be used to convert between primitive types
15+
16+
let v = 0 as *const u8;
17+
v as &u8; //~ ERROR E0605
18+
//~| NOTE an `as` expression can only be used to convert between primitive types
19+
}

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

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,35 +20,45 @@ error[E0609]: no field `f` on type `fn() {main}`
2020
75 | let _ = main.f as *const u32;
2121
| ^
2222

23-
error: non-scalar cast: `*const u8` as `&u8`
23+
error[E0605]: non-scalar cast: `*const u8` as `&u8`
2424
--> $DIR/cast-rfc0401.rs:39:13
2525
|
2626
39 | let _ = v as &u8;
2727
| ^^^^^^^^
28+
|
29+
= note: an `as` expression can only be used to convert between primitive types. Consider using the `From` trait
2830

29-
error: non-scalar cast: `*const u8` as `E`
31+
error[E0605]: non-scalar cast: `*const u8` as `E`
3032
--> $DIR/cast-rfc0401.rs:40:13
3133
|
3234
40 | let _ = v as E;
3335
| ^^^^^^
36+
|
37+
= note: an `as` expression can only be used to convert between primitive types. Consider using the `From` trait
3438

35-
error: non-scalar cast: `*const u8` as `fn()`
39+
error[E0605]: non-scalar cast: `*const u8` as `fn()`
3640
--> $DIR/cast-rfc0401.rs:41:13
3741
|
3842
41 | let _ = v as fn();
3943
| ^^^^^^^^^
44+
|
45+
= note: an `as` expression can only be used to convert between primitive types. Consider using the `From` trait
4046

41-
error: non-scalar cast: `*const u8` as `(u32,)`
47+
error[E0605]: non-scalar cast: `*const u8` as `(u32,)`
4248
--> $DIR/cast-rfc0401.rs:42:13
4349
|
4450
42 | let _ = v as (u32,);
4551
| ^^^^^^^^^^^
52+
|
53+
= note: an `as` expression can only be used to convert between primitive types. Consider using the `From` trait
4654

47-
error: non-scalar cast: `std::option::Option<&*const u8>` as `*const u8`
55+
error[E0605]: non-scalar cast: `std::option::Option<&*const u8>` as `*const u8`
4856
--> $DIR/cast-rfc0401.rs:43:13
4957
|
5058
43 | let _ = Some(&v) as *const u8;
5159
| ^^^^^^^^^^^^^^^^^^^^^
60+
|
61+
= note: an `as` expression can only be used to convert between primitive types. Consider using the `From` trait
5262

5363
error: casting `*const u8` as `f32` is invalid
5464
--> $DIR/cast-rfc0401.rs:45:13

src/test/ui/mismatched_types/issue-26480.stderr

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,16 @@ error[E0308]: mismatched types
77
37 | write!(hello);
88
| -------------- in this macro invocation
99

10-
error: non-scalar cast: `{integer}` as `()`
10+
error[E0605]: non-scalar cast: `{integer}` as `()`
1111
--> $DIR/issue-26480.rs:32:19
1212
|
1313
32 | ($x:expr) => ($x as ())
1414
| ^^^^^^^^
1515
...
1616
38 | cast!(2);
1717
| --------- in this macro invocation
18+
|
19+
= note: an `as` expression can only be used to convert between primitive types. Consider using the `From` trait
1820

1921
error: aborting due to previous error(s)
2022

0 commit comments

Comments
 (0)