Skip to content

Commit e36adff

Browse files
committed
add source type for invalid bool casts
1 parent 3ebb562 commit e36adff

File tree

8 files changed

+103
-14
lines changed

8 files changed

+103
-14
lines changed

Diff for: compiler/rustc_hir_typeck/src/cast.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -321,8 +321,13 @@ impl<'a, 'tcx> CastCheck<'tcx> {
321321
.emit();
322322
}
323323
CastError::CastToBool => {
324-
let mut err =
325-
struct_span_err!(fcx.tcx.sess, self.span, E0054, "cannot cast as `bool`");
324+
let mut err = struct_span_err!(
325+
fcx.tcx.sess,
326+
self.span,
327+
E0054,
328+
"cannot cast `{}` as `bool`",
329+
self.expr_ty
330+
);
326331

327332
if self.expr_ty.is_numeric() {
328333
match fcx.tcx.sess.source_map().span_to_snippet(self.expr_span) {

Diff for: tests/ui/cast/cast-as-bool.rs

+38-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,48 @@
11
fn main() {
2-
let u = 5 as bool; //~ ERROR cannot cast as `bool`
2+
let u = 5 as bool; //~ ERROR cannot cast `i32` as `bool`
33
//~| HELP compare with zero instead
44
//~| SUGGESTION 5 != 0
55

6-
let t = (1 + 2) as bool; //~ ERROR cannot cast as `bool`
6+
let t = (1 + 2) as bool; //~ ERROR cannot cast `i32` as `bool`
77
//~| HELP compare with zero instead
88
//~| SUGGESTION (1 + 2) != 0
99

10+
let _ = 5_u32 as bool; //~ ERROR cannot cast `u32` as `bool`
11+
//~| HELP compare with zero instead
12+
13+
let _ = 64.0_f64 as bool; //~ ERROR cannot cast `f64` as `bool`
14+
//~| HELP compare with zero instead
15+
16+
// Enums that can normally be cast to integers can't be cast to `bool`, just like integers.
17+
// Note that enums that cannot be cast to integers can't be cast to anything at *all*
18+
// so that's not tested here.
19+
enum IntEnum {
20+
Zero,
21+
One,
22+
Two
23+
}
24+
let _ = IntEnum::One as bool; //~ ERROR cannot cast `IntEnum` as `bool`
25+
26+
fn uwu(_: u8) -> String {
27+
todo!()
28+
}
29+
30+
unsafe fn owo() {}
31+
32+
// fn item to bool
33+
let _ = uwu as bool; //~ ERROR cannot cast `fn(u8) -> String {uwu}` as `bool`
34+
// unsafe fn item
35+
let _ = owo as bool; //~ ERROR cannot cast `unsafe fn() {owo}` as `bool`
36+
37+
// fn ptr to bool
38+
let _ = uwu as fn(u8) -> String as bool; //~ ERROR cannot cast `fn(u8) -> String` as `bool`
39+
40+
let _ = 'x' as bool; //~ ERROR cannot cast `char` as `bool`
41+
42+
let ptr = 1 as *const ();
43+
44+
let _ = ptr as bool; //~ ERROR cannot cast `*const ()` as `bool`
45+
1046
let v = "hello" as bool;
1147
//~^ ERROR casting `&'static str` as `bool` is invalid
1248
//~| HELP consider using the `is_empty` method on `&'static str` to determine if it contains anything

Diff for: tests/ui/cast/cast-as-bool.stderr

+52-4
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,66 @@
1-
error[E0054]: cannot cast as `bool`
1+
error[E0054]: cannot cast `i32` as `bool`
22
--> $DIR/cast-as-bool.rs:2:13
33
|
44
LL | let u = 5 as bool;
55
| ^^^^^^^^^ help: compare with zero instead: `5 != 0`
66

7-
error[E0054]: cannot cast as `bool`
7+
error[E0054]: cannot cast `i32` as `bool`
88
--> $DIR/cast-as-bool.rs:6:13
99
|
1010
LL | let t = (1 + 2) as bool;
1111
| ^^^^^^^^^^^^^^^ help: compare with zero instead: `(1 + 2) != 0`
1212

13-
error[E0606]: casting `&'static str` as `bool` is invalid
13+
error[E0054]: cannot cast `u32` as `bool`
1414
--> $DIR/cast-as-bool.rs:10:13
1515
|
16+
LL | let _ = 5_u32 as bool;
17+
| ^^^^^^^^^^^^^ help: compare with zero instead: `5_u32 != 0`
18+
19+
error[E0054]: cannot cast `f64` as `bool`
20+
--> $DIR/cast-as-bool.rs:13:13
21+
|
22+
LL | let _ = 64.0_f64 as bool;
23+
| ^^^^^^^^^^^^^^^^ help: compare with zero instead: `64.0_f64 != 0`
24+
25+
error[E0054]: cannot cast `IntEnum` as `bool`
26+
--> $DIR/cast-as-bool.rs:24:13
27+
|
28+
LL | let _ = IntEnum::One as bool;
29+
| ^^^^^^^^^^^^^^^^^^^^ unsupported cast
30+
31+
error[E0054]: cannot cast `fn(u8) -> String {uwu}` as `bool`
32+
--> $DIR/cast-as-bool.rs:33:13
33+
|
34+
LL | let _ = uwu as bool;
35+
| ^^^^^^^^^^^ unsupported cast
36+
37+
error[E0054]: cannot cast `unsafe fn() {owo}` as `bool`
38+
--> $DIR/cast-as-bool.rs:35:13
39+
|
40+
LL | let _ = owo as bool;
41+
| ^^^^^^^^^^^ unsupported cast
42+
43+
error[E0054]: cannot cast `fn(u8) -> String` as `bool`
44+
--> $DIR/cast-as-bool.rs:38:13
45+
|
46+
LL | let _ = uwu as fn(u8) -> String as bool;
47+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ unsupported cast
48+
49+
error[E0054]: cannot cast `char` as `bool`
50+
--> $DIR/cast-as-bool.rs:40:13
51+
|
52+
LL | let _ = 'x' as bool;
53+
| ^^^^^^^^^^^ unsupported cast
54+
55+
error[E0054]: cannot cast `*const ()` as `bool`
56+
--> $DIR/cast-as-bool.rs:44:13
57+
|
58+
LL | let _ = ptr as bool;
59+
| ^^^^^^^^^^^ unsupported cast
60+
61+
error[E0606]: casting `&'static str` as `bool` is invalid
62+
--> $DIR/cast-as-bool.rs:46:13
63+
|
1664
LL | let v = "hello" as bool;
1765
| ^^^^^^^^^^^^^^^
1866
|
@@ -21,7 +69,7 @@ help: consider using the `is_empty` method on `&'static str` to determine if it
2169
LL | let v = !"hello".is_empty();
2270
| + ~~~~~~~~~~~
2371

24-
error: aborting due to 3 previous errors
72+
error: aborting due to 11 previous errors
2573

2674
Some errors have detailed explanations: E0054, E0606.
2775
For more information about an error, try `rustc --explain E0054`.

Diff for: tests/ui/cast/cast-rfc0401-2.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@
44

55
fn main() {
66
let _ = 3 as bool;
7-
//~^ ERROR cannot cast as `bool`
7+
//~^ ERROR cannot cast `i32` as `bool`
88
}

Diff for: tests/ui/cast/cast-rfc0401-2.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0054]: cannot cast as `bool`
1+
error[E0054]: cannot cast `i32` as `bool`
22
--> $DIR/cast-rfc0401-2.rs:6:13
33
|
44
LL | let _ = 3 as bool;

Diff for: tests/ui/error-codes/E0054.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0054]: cannot cast as `bool`
1+
error[E0054]: cannot cast `i32` as `bool`
22
--> $DIR/E0054.rs:3:24
33
|
44
LL | let x_is_nonzero = x as bool;

Diff for: tests/ui/error-festival.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ error[E0605]: non-primitive cast: `u8` as `Vec<u8>`
5959
LL | x as Vec<u8>;
6060
| ^^^^^^^^^^^^ an `as` expression can only be used to convert between primitive types or to coerce to a specific trait object
6161

62-
error[E0054]: cannot cast as `bool`
62+
error[E0054]: cannot cast `{integer}` as `bool`
6363
--> $DIR/error-festival.rs:33:24
6464
|
6565
LL | let x_is_nonzero = x as bool;

Diff for: tests/ui/mismatched_types/cast-rfc0401.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -82,13 +82,13 @@ error[E0606]: casting `f32` as `*const u8` is invalid
8282
LL | let _ = f as *const u8;
8383
| ^^^^^^^^^^^^^^
8484

85-
error[E0054]: cannot cast as `bool`
85+
error[E0054]: cannot cast `i32` as `bool`
8686
--> $DIR/cast-rfc0401.rs:39:13
8787
|
8888
LL | let _ = 3_i32 as bool;
8989
| ^^^^^^^^^^^^^ help: compare with zero instead: `3_i32 != 0`
9090

91-
error[E0054]: cannot cast as `bool`
91+
error[E0054]: cannot cast `E` as `bool`
9292
--> $DIR/cast-rfc0401.rs:40:13
9393
|
9494
LL | let _ = E::A as bool;

0 commit comments

Comments
 (0)