mirror of
https://github.com/rust-lang/rust.git
synced 2025-05-14 02:49:40 +00:00
Add E0605
This commit is contained in:
parent
d5977df1c1
commit
0e4b8ffccd
@ -209,13 +209,13 @@ impl<'a, 'gcx, 'tcx> CastCheck<'tcx> {
|
|||||||
"only `u8` can be cast as `char`, not `{}`", self.expr_ty).emit();
|
"only `u8` can be cast as `char`, not `{}`", self.expr_ty).emit();
|
||||||
}
|
}
|
||||||
CastError::NonScalar => {
|
CastError::NonScalar => {
|
||||||
fcx.type_error_message(self.span,
|
struct_span_err!(fcx.tcx.sess, self.span, E0605,
|
||||||
|actual| {
|
"non-scalar cast: `{}` as `{}`",
|
||||||
format!("non-scalar cast: `{}` as `{}`",
|
self.expr_ty,
|
||||||
actual,
|
|
||||||
fcx.ty_to_string(self.cast_ty))
|
fcx.ty_to_string(self.cast_ty))
|
||||||
},
|
.note("an `as` expression can only be used to convert between \
|
||||||
self.expr_ty);
|
primitive types. Consider using the `From` trait")
|
||||||
|
.emit();
|
||||||
}
|
}
|
||||||
CastError::IllegalCast => {
|
CastError::IllegalCast => {
|
||||||
fcx.type_error_message(self.span,
|
fcx.type_error_message(self.span,
|
||||||
|
@ -4225,6 +4225,32 @@ assert!(c, 'V');
|
|||||||
```
|
```
|
||||||
"##,
|
"##,
|
||||||
|
|
||||||
|
E0605: r##"
|
||||||
|
An invalid cast was attempted.
|
||||||
|
|
||||||
|
Erroneous code examples:
|
||||||
|
|
||||||
|
```compile_fail,E0605
|
||||||
|
let x = 0u8;
|
||||||
|
x as Vec<u8>; // error: non-scalar cast: `u8` as `std::vec::Vec<u8>`
|
||||||
|
|
||||||
|
// Another example
|
||||||
|
|
||||||
|
let v = 0 as *const u8; // So here, `v` is a `*const u8`.
|
||||||
|
v as &u8; // error: non-scalar cast: `*const u8` as `&u8`
|
||||||
|
```
|
||||||
|
|
||||||
|
Only primitive types cast be casted into each others. Examples:
|
||||||
|
|
||||||
|
```
|
||||||
|
let x = 0u8;
|
||||||
|
x as u32; // ok!
|
||||||
|
|
||||||
|
let v = 0 as *const u8;
|
||||||
|
v as *const i8; // ok!
|
||||||
|
```
|
||||||
|
"##,
|
||||||
|
|
||||||
E0609: r##"
|
E0609: r##"
|
||||||
Attempted to access a non-existent field in a struct.
|
Attempted to access a non-existent field in a struct.
|
||||||
|
|
||||||
|
19
src/test/compile-fail/E0605.rs
Normal file
19
src/test/compile-fail/E0605.rs
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
|
||||||
|
// file at the top-level directory of this distribution and at
|
||||||
|
// http://rust-lang.org/COPYRIGHT.
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||||
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||||
|
// option. This file may not be copied, modified, or distributed
|
||||||
|
// except according to those terms.
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let x = 0u8;
|
||||||
|
x as Vec<u8>; //~ ERROR E0605
|
||||||
|
//~| NOTE an `as` expression can only be used to convert between primitive types
|
||||||
|
|
||||||
|
let v = 0 as *const u8;
|
||||||
|
v as &u8; //~ ERROR E0605
|
||||||
|
//~| NOTE an `as` expression can only be used to convert between primitive types
|
||||||
|
}
|
@ -20,35 +20,45 @@ error[E0609]: no field `f` on type `fn() {main}`
|
|||||||
75 | let _ = main.f as *const u32;
|
75 | let _ = main.f as *const u32;
|
||||||
| ^
|
| ^
|
||||||
|
|
||||||
error: non-scalar cast: `*const u8` as `&u8`
|
error[E0605]: non-scalar cast: `*const u8` as `&u8`
|
||||||
--> $DIR/cast-rfc0401.rs:39:13
|
--> $DIR/cast-rfc0401.rs:39:13
|
||||||
|
|
|
|
||||||
39 | let _ = v as &u8;
|
39 | let _ = v as &u8;
|
||||||
| ^^^^^^^^
|
| ^^^^^^^^
|
||||||
|
|
|
||||||
|
= note: an `as` expression can only be used to convert between primitive types. Consider using the `From` trait
|
||||||
|
|
||||||
error: non-scalar cast: `*const u8` as `E`
|
error[E0605]: non-scalar cast: `*const u8` as `E`
|
||||||
--> $DIR/cast-rfc0401.rs:40:13
|
--> $DIR/cast-rfc0401.rs:40:13
|
||||||
|
|
|
|
||||||
40 | let _ = v as E;
|
40 | let _ = v as E;
|
||||||
| ^^^^^^
|
| ^^^^^^
|
||||||
|
|
|
||||||
|
= note: an `as` expression can only be used to convert between primitive types. Consider using the `From` trait
|
||||||
|
|
||||||
error: non-scalar cast: `*const u8` as `fn()`
|
error[E0605]: non-scalar cast: `*const u8` as `fn()`
|
||||||
--> $DIR/cast-rfc0401.rs:41:13
|
--> $DIR/cast-rfc0401.rs:41:13
|
||||||
|
|
|
|
||||||
41 | let _ = v as fn();
|
41 | let _ = v as fn();
|
||||||
| ^^^^^^^^^
|
| ^^^^^^^^^
|
||||||
|
|
|
||||||
|
= note: an `as` expression can only be used to convert between primitive types. Consider using the `From` trait
|
||||||
|
|
||||||
error: non-scalar cast: `*const u8` as `(u32,)`
|
error[E0605]: non-scalar cast: `*const u8` as `(u32,)`
|
||||||
--> $DIR/cast-rfc0401.rs:42:13
|
--> $DIR/cast-rfc0401.rs:42:13
|
||||||
|
|
|
|
||||||
42 | let _ = v as (u32,);
|
42 | let _ = v as (u32,);
|
||||||
| ^^^^^^^^^^^
|
| ^^^^^^^^^^^
|
||||||
|
|
|
||||||
|
= note: an `as` expression can only be used to convert between primitive types. Consider using the `From` trait
|
||||||
|
|
||||||
error: non-scalar cast: `std::option::Option<&*const u8>` as `*const u8`
|
error[E0605]: non-scalar cast: `std::option::Option<&*const u8>` as `*const u8`
|
||||||
--> $DIR/cast-rfc0401.rs:43:13
|
--> $DIR/cast-rfc0401.rs:43:13
|
||||||
|
|
|
|
||||||
43 | let _ = Some(&v) as *const u8;
|
43 | let _ = Some(&v) as *const u8;
|
||||||
| ^^^^^^^^^^^^^^^^^^^^^
|
| ^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
|
|
||||||
|
= note: an `as` expression can only be used to convert between primitive types. Consider using the `From` trait
|
||||||
|
|
||||||
error: casting `*const u8` as `f32` is invalid
|
error: casting `*const u8` as `f32` is invalid
|
||||||
--> $DIR/cast-rfc0401.rs:45:13
|
--> $DIR/cast-rfc0401.rs:45:13
|
||||||
|
@ -7,7 +7,7 @@ error[E0308]: mismatched types
|
|||||||
37 | write!(hello);
|
37 | write!(hello);
|
||||||
| -------------- in this macro invocation
|
| -------------- in this macro invocation
|
||||||
|
|
||||||
error: non-scalar cast: `{integer}` as `()`
|
error[E0605]: non-scalar cast: `{integer}` as `()`
|
||||||
--> $DIR/issue-26480.rs:32:19
|
--> $DIR/issue-26480.rs:32:19
|
||||||
|
|
|
|
||||||
32 | ($x:expr) => ($x as ())
|
32 | ($x:expr) => ($x as ())
|
||||||
@ -15,6 +15,8 @@ error: non-scalar cast: `{integer}` as `()`
|
|||||||
...
|
...
|
||||||
38 | cast!(2);
|
38 | cast!(2);
|
||||||
| --------- in this macro invocation
|
| --------- in this macro invocation
|
||||||
|
|
|
||||||
|
= note: an `as` expression can only be used to convert between primitive types. Consider using the `From` trait
|
||||||
|
|
||||||
error: aborting due to previous error(s)
|
error: aborting due to previous error(s)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user