mirror of
https://github.com/rust-lang/rust.git
synced 2024-11-23 15:23:46 +00:00
wording improvement
This commit is contained in:
parent
7b8c6a2d30
commit
deb1eb6134
@ -217,7 +217,7 @@ impl<'a, 'gcx, 'tcx> CastCheck<'tcx> {
|
||||
}
|
||||
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 \
|
||||
|
@ -4209,7 +4209,7 @@ println!("{}", v[2]);
|
||||
"##,
|
||||
|
||||
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:
|
||||
|
||||
@ -4217,11 +4217,11 @@ 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');
|
||||
```
|
||||
"##,
|
||||
|
||||
@ -4232,15 +4232,15 @@ Erroneous code examples:
|
||||
|
||||
```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;
|
||||
@ -4261,8 +4261,8 @@ let x = &0u8; // Here, `x` is a `&u8`.
|
||||
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;
|
||||
@ -4282,15 +4282,16 @@ v as *const [u8];
|
||||
|
||||
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##"
|
||||
|
@ -8,5 +8,5 @@
|
||||
// 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); }
|
||||
|
@ -13,8 +13,8 @@ fn foo(_x: isize) { }
|
||||
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());
|
||||
}
|
||||
|
@ -8,5 +8,5 @@
|
||||
// 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 (); }
|
||||
|
@ -14,5 +14,5 @@
|
||||
fn main() {
|
||||
let b = 0u8;
|
||||
let baz: fn() -> u8 = (|| { b }) as fn() -> u8;
|
||||
//~^ ERROR non-scalar cast
|
||||
//~^ ERROR non-primitive cast
|
||||
}
|
||||
|
@ -17,7 +17,7 @@ fn cast_a() {
|
||||
}
|
||||
|
||||
fn cast_b() {
|
||||
let y = 22 as !; //~ ERROR non-scalar cast
|
||||
let y = 22 as !; //~ ERROR non-primitive cast
|
||||
}
|
||||
|
||||
fn main() { }
|
||||
|
@ -22,7 +22,7 @@ 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
|
||||
|
@ -10,5 +10,5 @@
|
||||
|
||||
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`
|
||||
}
|
||||
|
@ -9,5 +9,5 @@
|
||||
// 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
|
||||
}
|
||||
|
@ -19,7 +19,7 @@ pub trait Array2D: Index<usize> {
|
||||
}
|
||||
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))
|
||||
}
|
||||
}
|
||||
|
@ -9,7 +9,7 @@
|
||||
// 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() { }
|
||||
|
@ -8,13 +8,11 @@
|
||||
// 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]
|
||||
}
|
||||
|
@ -8,8 +8,6 @@
|
||||
// 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),
|
||||
@ -17,5 +15,5 @@ enum non_nullary {
|
||||
|
||||
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]
|
||||
}
|
||||
|
@ -11,7 +11,7 @@
|
||||
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() {}
|
||||
|
@ -20,7 +20,7 @@ error[E0609]: no field `f` on type `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;
|
||||
@ -28,7 +28,7 @@ error[E0605]: non-scalar cast: `*const u8` 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;
|
||||
@ -36,7 +36,7 @@ error[E0605]: non-scalar cast: `*const u8` 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();
|
||||
@ -44,7 +44,7 @@ error[E0605]: non-scalar cast: `*const u8` 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,);
|
||||
@ -52,7 +52,7 @@ error[E0605]: non-scalar cast: `*const u8` 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;
|
||||
|
@ -7,7 +7,7 @@ error[E0308]: mismatched types
|
||||
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 ())
|
||||
|
Loading…
Reference in New Issue
Block a user