}
CastError::NonScalar => {
struct_span_err!(fcx.tcx.sess, self.span, E0605,
- "non-scalar cast: `{}` as `{}`",
+ "non-primitive cast: `{}` as `{}`",
self.expr_ty,
fcx.ty_to_string(self.cast_ty))
.note("an `as` expression can only be used to convert between \
"##,
E0604: r##"
-A cast to `char` was attempted on another type than `u8`.
+A cast to `char` was attempted on a type other than `u8`.
Erroneous code example:
0u32 as char; // error: only `u8` can be cast as `char`, not `u32`
```
-As the error message indicates, only `u8` can be casted into `char`. Example:
+As the error message indicates, only `u8` can be cast into `char`. Example:
```
let c = 86u8 as char; // ok!
-assert!(c, 'V');
+assert_eq!(c, 'V');
```
"##,
```compile_fail,E0605
let x = 0u8;
-x as Vec<u8>; // error: non-scalar cast: `u8` as `std::vec::Vec<u8>`
+x as Vec<u8>; // error: non-primitive 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`
+v as &u8; // error: non-primitive cast: `*const u8` as `&u8`
```
-Only primitive types cast be casted into each others. Examples:
+Only primitive types can be cast into each other. Examples:
```
let x = 0u8;
let y: u32 = x as u32; // error: casting `&u8` as `u32` is invalid
```
-When casting, keep in mind that only primitive types cast be casted into each
-others. Example:
+When casting, keep in mind that only primitive types can be cast into each
+other. Example:
```
let x = &0u8;
First: what are thin and fat pointers?
-Thin pointers are "simple" pointers that simply reference a memory address.
+Thin pointers are "simple" pointers: they are purely a reference to a memory
+address.
Fat pointers are pointers referencing Dynamically Sized Types (also called DST).
-They don't have a statically known size, therefore they can only exist behind
+DST don't have a statically known size, therefore they can only exist behind
some kind of pointers that contain additional information. Slices and trait
-objects are DSTs.
+objects are DSTs. In the case of slices, the additional information the fat
+pointer holds is their size.
-So in order to fix this error, don't try to cast directly between thin and fat
-pointers.
+To fix this error, don't try to cast directly between thin and fat pointers.
"##,
E0609: r##"
// option. This file may not be copied, modified, or distributed
// except according to those terms.
-// error-pattern: non-scalar cast: `()` as `u32`
+// error-pattern: non-primitive cast: `()` as `u32`
fn main() { let u = (assert!(true) as u32); }
fn main() {
let v: u64 = 5;
let x = foo as extern "C" fn() -> isize;
- //~^ ERROR non-scalar cast
+ //~^ ERROR non-primitive cast
let y = v as extern "Rust" fn(isize) -> (isize, isize);
- //~^ ERROR non-scalar cast
+ //~^ ERROR non-primitive cast
y(x());
}
// option. This file may not be copied, modified, or distributed
// except according to those terms.
-// error-pattern: non-scalar cast: `u32` as `()`
+// error-pattern: non-primitive cast: `u32` as `()`
fn main() { let u = 0u32 as (); }
fn main() {
let b = 0u8;
let baz: fn() -> u8 = (|| { b }) as fn() -> u8;
- //~^ ERROR non-scalar cast
+ //~^ ERROR non-primitive cast
}
}
fn cast_b() {
- let y = 22 as !; //~ ERROR non-scalar cast
+ let y = 22 as !; //~ ERROR non-primitive cast
}
fn main() { }
a as isize; //~ ERROR casting
a as i16; //~ ERROR casting `&[i32]` as `i16` is invalid
a as u32; //~ ERROR casting `&[i32]` as `u32` is invalid
- b as usize; //~ ERROR non-scalar cast
+ b as usize; //~ ERROR non-primitive cast
p as usize;
//~^ ERROR casting
//~^^ HELP cast through a thin pointer
fn main() {
let nil = ();
- let _t = nil as usize; //~ ERROR: non-scalar cast: `()` as `usize`
+ let _t = nil as usize; //~ ERROR: non-primitive cast: `()` as `usize`
}
// except according to those terms.
fn main() {
- 0 as &std::any::Any; //~ ERROR non-scalar cast
+ 0 as &std::any::Any; //~ ERROR non-primitive cast
}
}
let i = y * self.columns() + x;
let indexer = &(*self as &Index<usize, Output = <Self as Index<usize>>::Output>);
- //~^ERROR non-scalar cast
+ //~^ERROR non-primitive cast
Some(indexer.index(i))
}
}
// except according to those terms.
fn bad (p: *const isize) {
- let _q: &isize = p as &isize; //~ ERROR non-scalar cast
+ let _q: &isize = p as &isize; //~ ERROR non-primitive cast
}
fn main() { }
// option. This file may not be copied, modified, or distributed
// except according to those terms.
-// error-pattern:non-scalar cast
-
#[derive(Debug)]
struct foo {
x: isize
}
fn main() {
- println!("{}", foo{ x: 1 } as isize);
+ println!("{}", foo{ x: 1 } as isize); //~ non-primitive cast: `foo` as `isize` [E0605]
}
// option. This file may not be copied, modified, or distributed
// except according to those terms.
-//error-pattern: non-scalar cast
-
enum non_nullary {
nullary,
other(isize),
fn main() {
let v = non_nullary::nullary;
- let val = v as isize;
+ let val = v as isize; //~ ERROR non-primitive cast: `non_nullary` as `isize` [E0605]
}
enum E {}
fn f(e: E) {
- println!("{}", (e as isize).to_string()); //~ ERROR non-scalar cast
+ println!("{}", (e as isize).to_string()); //~ ERROR non-primitive cast
}
fn main() {}
75 | let _ = main.f as *const u32;
| ^
-error[E0605]: non-scalar cast: `*const u8` as `&u8`
+error[E0605]: non-primitive cast: `*const u8` as `&u8`
--> $DIR/cast-rfc0401.rs:39:13
|
39 | let _ = v as &u8;
|
= note: an `as` expression can only be used to convert between primitive types. Consider using the `From` trait
-error[E0605]: non-scalar cast: `*const u8` as `E`
+error[E0605]: non-primitive cast: `*const u8` as `E`
--> $DIR/cast-rfc0401.rs:40:13
|
40 | let _ = v as E;
|
= note: an `as` expression can only be used to convert between primitive types. Consider using the `From` trait
-error[E0605]: non-scalar cast: `*const u8` as `fn()`
+error[E0605]: non-primitive cast: `*const u8` as `fn()`
--> $DIR/cast-rfc0401.rs:41:13
|
41 | let _ = v as fn();
|
= note: an `as` expression can only be used to convert between primitive types. Consider using the `From` trait
-error[E0605]: non-scalar cast: `*const u8` as `(u32,)`
+error[E0605]: non-primitive cast: `*const u8` as `(u32,)`
--> $DIR/cast-rfc0401.rs:42:13
|
42 | let _ = v as (u32,);
|
= note: an `as` expression can only be used to convert between primitive types. Consider using the `From` trait
-error[E0605]: non-scalar cast: `std::option::Option<&*const u8>` as `*const u8`
+error[E0605]: non-primitive cast: `std::option::Option<&*const u8>` as `*const u8`
--> $DIR/cast-rfc0401.rs:43:13
|
43 | let _ = Some(&v) as *const u8;
37 | write!(hello);
| -------------- in this macro invocation
-error[E0605]: non-scalar cast: `{integer}` as `()`
+error[E0605]: non-primitive cast: `{integer}` as `()`
--> $DIR/issue-26480.rs:32:19
|
32 | ($x:expr) => ($x as ())