diff options
Diffstat (limited to 'tests/ui/cast')
-rw-r--r-- | tests/ui/cast/cast-as-bool.rs | 44 | ||||
-rw-r--r-- | tests/ui/cast/cast-as-bool.stderr | 80 | ||||
-rw-r--r-- | tests/ui/cast/cast-rfc0401-2.rs | 2 | ||||
-rw-r--r-- | tests/ui/cast/cast-rfc0401-2.stderr | 9 | ||||
-rw-r--r-- | tests/ui/cast/unsized-struct-cast.rs | 6 | ||||
-rw-r--r-- | tests/ui/cast/unsized-struct-cast.stderr | 9 |
6 files changed, 137 insertions, 13 deletions
diff --git a/tests/ui/cast/cast-as-bool.rs b/tests/ui/cast/cast-as-bool.rs index fbebc80d9..511a02718 100644 --- a/tests/ui/cast/cast-as-bool.rs +++ b/tests/ui/cast/cast-as-bool.rs @@ -1,11 +1,47 @@ fn main() { - let u = 5 as bool; //~ ERROR cannot cast as `bool` + let u = 5 as bool; //~ ERROR cannot cast `i32` as `bool` //~| HELP compare with zero instead - //~| SUGGESTION 5 != 0 + //~| SUGGESTION != 0 - let t = (1 + 2) as bool; //~ ERROR cannot cast as `bool` + let t = (1 + 2) as bool; //~ ERROR cannot cast `i32` as `bool` //~| HELP compare with zero instead - //~| SUGGESTION (1 + 2) != 0 + //~| SUGGESTION != 0 + + let _ = 5_u32 as bool; //~ ERROR cannot cast `u32` as `bool` + //~| HELP compare with zero instead + + let _ = 64.0_f64 as bool; //~ ERROR cannot cast `f64` as `bool` + //~| HELP compare with zero instead + + // Enums that can normally be cast to integers can't be cast to `bool`, just like integers. + // Note that enums that cannot be cast to integers can't be cast to anything at *all* + // so that's not tested here. + enum IntEnum { + Zero, + One, + Two + } + let _ = IntEnum::One as bool; //~ ERROR cannot cast `IntEnum` as `bool` + + fn uwu(_: u8) -> String { + todo!() + } + + unsafe fn owo() {} + + // fn item to bool + let _ = uwu as bool; //~ ERROR cannot cast `fn(u8) -> String {uwu}` as `bool` + // unsafe fn item + let _ = owo as bool; //~ ERROR cannot cast `unsafe fn() {owo}` as `bool` + + // fn ptr to bool + let _ = uwu as fn(u8) -> String as bool; //~ ERROR cannot cast `fn(u8) -> String` as `bool` + + let _ = 'x' as bool; //~ ERROR cannot cast `char` as `bool` + + let ptr = 1 as *const (); + + let _ = ptr as bool; //~ ERROR cannot cast `*const ()` as `bool` let v = "hello" as bool; //~^ ERROR casting `&'static str` as `bool` is invalid diff --git a/tests/ui/cast/cast-as-bool.stderr b/tests/ui/cast/cast-as-bool.stderr index 19ac8f10f..4ff56a95e 100644 --- a/tests/ui/cast/cast-as-bool.stderr +++ b/tests/ui/cast/cast-as-bool.stderr @@ -1,18 +1,86 @@ -error[E0054]: cannot cast as `bool` +error[E0054]: cannot cast `i32` as `bool` --> $DIR/cast-as-bool.rs:2:13 | LL | let u = 5 as bool; - | ^^^^^^^^^ help: compare with zero instead: `5 != 0` + | ^^^^^^^^^ + | +help: compare with zero instead + | +LL | let u = 5 != 0; + | ~~~~ -error[E0054]: cannot cast as `bool` +error[E0054]: cannot cast `i32` as `bool` --> $DIR/cast-as-bool.rs:6:13 | LL | let t = (1 + 2) as bool; - | ^^^^^^^^^^^^^^^ help: compare with zero instead: `(1 + 2) != 0` + | ^^^^^^^^^^^^^^^ + | +help: compare with zero instead + | +LL | let t = (1 + 2) != 0; + | ~~~~ -error[E0606]: casting `&'static str` as `bool` is invalid +error[E0054]: cannot cast `u32` as `bool` --> $DIR/cast-as-bool.rs:10:13 | +LL | let _ = 5_u32 as bool; + | ^^^^^^^^^^^^^ + | +help: compare with zero instead + | +LL | let _ = 5_u32 != 0; + | ~~~~ + +error[E0054]: cannot cast `f64` as `bool` + --> $DIR/cast-as-bool.rs:13:13 + | +LL | let _ = 64.0_f64 as bool; + | ^^^^^^^^^^^^^^^^ + | +help: compare with zero instead + | +LL | let _ = 64.0_f64 != 0; + | ~~~~ + +error[E0054]: cannot cast `IntEnum` as `bool` + --> $DIR/cast-as-bool.rs:24:13 + | +LL | let _ = IntEnum::One as bool; + | ^^^^^^^^^^^^^^^^^^^^ unsupported cast + +error[E0054]: cannot cast `fn(u8) -> String {uwu}` as `bool` + --> $DIR/cast-as-bool.rs:33:13 + | +LL | let _ = uwu as bool; + | ^^^^^^^^^^^ unsupported cast + +error[E0054]: cannot cast `unsafe fn() {owo}` as `bool` + --> $DIR/cast-as-bool.rs:35:13 + | +LL | let _ = owo as bool; + | ^^^^^^^^^^^ unsupported cast + +error[E0054]: cannot cast `fn(u8) -> String` as `bool` + --> $DIR/cast-as-bool.rs:38:13 + | +LL | let _ = uwu as fn(u8) -> String as bool; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ unsupported cast + +error[E0054]: cannot cast `char` as `bool` + --> $DIR/cast-as-bool.rs:40:13 + | +LL | let _ = 'x' as bool; + | ^^^^^^^^^^^ unsupported cast + +error[E0054]: cannot cast `*const ()` as `bool` + --> $DIR/cast-as-bool.rs:44:13 + | +LL | let _ = ptr as bool; + | ^^^^^^^^^^^ unsupported cast + +error[E0606]: casting `&'static str` as `bool` is invalid + --> $DIR/cast-as-bool.rs:46:13 + | LL | let v = "hello" as bool; | ^^^^^^^^^^^^^^^ | @@ -21,7 +89,7 @@ help: consider using the `is_empty` method on `&'static str` to determine if it LL | let v = !"hello".is_empty(); | + ~~~~~~~~~~~ -error: aborting due to 3 previous errors +error: aborting due to 11 previous errors Some errors have detailed explanations: E0054, E0606. For more information about an error, try `rustc --explain E0054`. diff --git a/tests/ui/cast/cast-rfc0401-2.rs b/tests/ui/cast/cast-rfc0401-2.rs index 7709aa341..70604a587 100644 --- a/tests/ui/cast/cast-rfc0401-2.rs +++ b/tests/ui/cast/cast-rfc0401-2.rs @@ -4,5 +4,5 @@ fn main() { let _ = 3 as bool; - //~^ ERROR cannot cast as `bool` + //~^ ERROR cannot cast `i32` as `bool` } diff --git a/tests/ui/cast/cast-rfc0401-2.stderr b/tests/ui/cast/cast-rfc0401-2.stderr index 52f6af78a..dd90c3a97 100644 --- a/tests/ui/cast/cast-rfc0401-2.stderr +++ b/tests/ui/cast/cast-rfc0401-2.stderr @@ -1,8 +1,13 @@ -error[E0054]: cannot cast as `bool` +error[E0054]: cannot cast `i32` as `bool` --> $DIR/cast-rfc0401-2.rs:6:13 | LL | let _ = 3 as bool; - | ^^^^^^^^^ help: compare with zero instead: `3 != 0` + | ^^^^^^^^^ + | +help: compare with zero instead + | +LL | let _ = 3 != 0; + | ~~~~ error: aborting due to previous error diff --git a/tests/ui/cast/unsized-struct-cast.rs b/tests/ui/cast/unsized-struct-cast.rs new file mode 100644 index 000000000..52bb6cedc --- /dev/null +++ b/tests/ui/cast/unsized-struct-cast.rs @@ -0,0 +1,6 @@ +pub struct Data([u8]); + +fn main(){ + const _: *const Data = &[] as *const Data; + //~^ ERROR: casting `&[_; 0]` as `*const Data` is invalid +} diff --git a/tests/ui/cast/unsized-struct-cast.stderr b/tests/ui/cast/unsized-struct-cast.stderr new file mode 100644 index 000000000..79b3d973c --- /dev/null +++ b/tests/ui/cast/unsized-struct-cast.stderr @@ -0,0 +1,9 @@ +error[E0606]: casting `&[_; 0]` as `*const Data` is invalid + --> $DIR/unsized-struct-cast.rs:4:28 + | +LL | const _: *const Data = &[] as *const Data; + | ^^^^^^^^^^^^^^^^^^ + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0606`. |