File tree 9 files changed +87
-36
lines changed
compiler/rustc_hir_typeck
9 files changed +87
-36
lines changed Original file line number Diff line number Diff line change @@ -16,6 +16,11 @@ hir_typeck_candidate_trait_note = `{$trait_name}` defines an item `{$item_name}`
16
16
*[ other ] , perhaps you need to restrict type parameter `{ $action_or_ty } ` with it
17
17
}
18
18
19
+ hir_typeck_cannot_cast_to_bool = cannot cast `{ $expr_ty } ` as `bool`
20
+ .suggestion = compare with zero instead
21
+ .help = compare with zero instead
22
+ .label = unsupported cast
23
+
19
24
hir_typeck_const_select_must_be_const = this argument must be a `const fn`
20
25
.help = consult the documentation on `const_eval_select` for more information
21
26
Original file line number Diff line number Diff line change @@ -322,33 +322,16 @@ impl<'a, 'tcx> CastCheck<'tcx> {
322
322
. emit ( ) ;
323
323
}
324
324
CastError :: CastToBool => {
325
- let mut err = struct_span_err ! (
326
- fcx. tcx. sess,
327
- self . span,
328
- E0054 ,
329
- "cannot cast `{}` as `bool`" ,
330
- self . expr_ty
331
- ) ;
332
-
333
- if self . expr_ty . is_numeric ( ) {
334
- match fcx. tcx . sess . source_map ( ) . span_to_snippet ( self . expr_span ) {
335
- Ok ( snippet) => {
336
- err. span_suggestion (
337
- self . span ,
338
- "compare with zero instead" ,
339
- format ! ( "{snippet} != 0" ) ,
340
- Applicability :: MachineApplicable ,
341
- ) ;
342
- }
343
- Err ( _) => {
344
- err. span_help ( self . span , "compare with zero instead" ) ;
345
- }
346
- }
325
+ let help = if self . expr_ty . is_numeric ( ) {
326
+ errors:: CannotCastToBoolHelp :: Numeric ( self . expr_span . shrink_to_hi ( ) . with_hi ( self . span . hi ( ) ) )
347
327
} else {
348
- err. span_label ( self . span , "unsupported cast" ) ;
349
- }
350
-
351
- err. emit ( ) ;
328
+ errors:: CannotCastToBoolHelp :: Unsupported ( self . span )
329
+ } ;
330
+ fcx. tcx . sess . emit_err ( errors:: CannotCastToBool {
331
+ span : self . span ,
332
+ expr_ty : self . expr_ty ,
333
+ help,
334
+ } ) ;
352
335
}
353
336
CastError :: CastToChar => {
354
337
let mut err = type_error_struct ! (
Original file line number Diff line number Diff line change @@ -495,6 +495,29 @@ pub struct CandidateTraitNote {
495
495
pub action_or_ty : String ,
496
496
}
497
497
498
+ #[ derive( Diagnostic ) ]
499
+ #[ diag( hir_typeck_cannot_cast_to_bool, code = "E0054" ) ]
500
+ pub struct CannotCastToBool < ' tcx > {
501
+ #[ primary_span]
502
+ pub span : Span ,
503
+ pub expr_ty : Ty < ' tcx > ,
504
+ #[ subdiagnostic]
505
+ pub help : CannotCastToBoolHelp ,
506
+ }
507
+
508
+ #[ derive( Subdiagnostic ) ]
509
+ pub enum CannotCastToBoolHelp {
510
+ #[ suggestion(
511
+ hir_typeck_suggestion,
512
+ applicability = "machine-applicable" ,
513
+ code = " != 0" ,
514
+ style = "verbose"
515
+ ) ]
516
+ Numeric ( #[ primary_span] Span ) ,
517
+ #[ label( hir_typeck_label) ]
518
+ Unsupported ( #[ primary_span] Span ) ,
519
+ }
520
+
498
521
#[ derive( Diagnostic ) ]
499
522
#[ diag( hir_typeck_ctor_is_private, code = "E0603" ) ]
500
523
pub struct CtorIsPrivate {
Original file line number Diff line number Diff line change 1
1
fn main ( ) {
2
2
let u = 5 as bool ; //~ ERROR cannot cast `i32` as `bool`
3
3
//~| HELP compare with zero instead
4
- //~| SUGGESTION 5 != 0
4
+ //~| SUGGESTION != 0
5
5
6
6
let t = ( 1 + 2 ) as bool ; //~ ERROR cannot cast `i32` as `bool`
7
7
//~| HELP compare with zero instead
8
- //~| SUGGESTION (1 + 2) != 0
8
+ //~| SUGGESTION != 0
9
9
10
10
let _ = 5_u32 as bool ; //~ ERROR cannot cast `u32` as `bool`
11
11
//~| HELP compare with zero instead
Original file line number Diff line number Diff line change @@ -2,25 +2,45 @@ error[E0054]: cannot cast `i32` as `bool`
2
2
--> $DIR/cast-as-bool.rs:2:13
3
3
|
4
4
LL | let u = 5 as bool;
5
- | ^^^^^^^^^ help: compare with zero instead: `5 != 0`
5
+ | ^^^^^^^^^
6
+ |
7
+ help: compare with zero instead
8
+ |
9
+ LL | let u = 5 != 0;
10
+ | ~~~~
6
11
7
12
error[E0054]: cannot cast `i32` as `bool`
8
13
--> $DIR/cast-as-bool.rs:6:13
9
14
|
10
15
LL | let t = (1 + 2) as bool;
11
- | ^^^^^^^^^^^^^^^ help: compare with zero instead: `(1 + 2) != 0`
16
+ | ^^^^^^^^^^^^^^^
17
+ |
18
+ help: compare with zero instead
19
+ |
20
+ LL | let t = (1 + 2) != 0;
21
+ | ~~~~
12
22
13
23
error[E0054]: cannot cast `u32` as `bool`
14
24
--> $DIR/cast-as-bool.rs:10:13
15
25
|
16
26
LL | let _ = 5_u32 as bool;
17
- | ^^^^^^^^^^^^^ help: compare with zero instead: `5_u32 != 0`
27
+ | ^^^^^^^^^^^^^
28
+ |
29
+ help: compare with zero instead
30
+ |
31
+ LL | let _ = 5_u32 != 0;
32
+ | ~~~~
18
33
19
34
error[E0054]: cannot cast `f64` as `bool`
20
35
--> $DIR/cast-as-bool.rs:13:13
21
36
|
22
37
LL | let _ = 64.0_f64 as bool;
23
- | ^^^^^^^^^^^^^^^^ help: compare with zero instead: `64.0_f64 != 0`
38
+ | ^^^^^^^^^^^^^^^^
39
+ |
40
+ help: compare with zero instead
41
+ |
42
+ LL | let _ = 64.0_f64 != 0;
43
+ | ~~~~
24
44
25
45
error[E0054]: cannot cast `IntEnum` as `bool`
26
46
--> $DIR/cast-as-bool.rs:24:13
Original file line number Diff line number Diff line change @@ -2,7 +2,12 @@ error[E0054]: cannot cast `i32` as `bool`
2
2
--> $DIR/cast-rfc0401-2.rs:6:13
3
3
|
4
4
LL | let _ = 3 as bool;
5
- | ^^^^^^^^^ help: compare with zero instead: `3 != 0`
5
+ | ^^^^^^^^^
6
+ |
7
+ help: compare with zero instead
8
+ |
9
+ LL | let _ = 3 != 0;
10
+ | ~~~~
6
11
7
12
error: aborting due to previous error
8
13
Original file line number Diff line number Diff line change @@ -2,7 +2,12 @@ error[E0054]: cannot cast `i32` as `bool`
2
2
--> $DIR/E0054.rs:3:24
3
3
|
4
4
LL | let x_is_nonzero = x as bool;
5
- | ^^^^^^^^^ help: compare with zero instead: `x != 0`
5
+ | ^^^^^^^^^
6
+ |
7
+ help: compare with zero instead
8
+ |
9
+ LL | let x_is_nonzero = x != 0;
10
+ | ~~~~
6
11
7
12
error: aborting due to previous error
8
13
Original file line number Diff line number Diff line change @@ -63,7 +63,12 @@ error[E0054]: cannot cast `{integer}` as `bool`
63
63
--> $DIR/error-festival.rs:33:24
64
64
|
65
65
LL | let x_is_nonzero = x as bool;
66
- | ^^^^^^^^^ help: compare with zero instead: `x != 0`
66
+ | ^^^^^^^^^
67
+ |
68
+ help: compare with zero instead
69
+ |
70
+ LL | let x_is_nonzero = x != 0;
71
+ | ~~~~
67
72
68
73
error[E0606]: casting `&u8` as `u32` is invalid
69
74
--> $DIR/error-festival.rs:37:18
Original file line number Diff line number Diff line change @@ -86,7 +86,12 @@ error[E0054]: cannot cast `i32` as `bool`
86
86
--> $DIR/cast-rfc0401.rs:39:13
87
87
|
88
88
LL | let _ = 3_i32 as bool;
89
- | ^^^^^^^^^^^^^ help: compare with zero instead: `3_i32 != 0`
89
+ | ^^^^^^^^^^^^^
90
+ |
91
+ help: compare with zero instead
92
+ |
93
+ LL | let _ = 3_i32 != 0;
94
+ | ~~~~
90
95
91
96
error[E0054]: cannot cast `E` as `bool`
92
97
--> $DIR/cast-rfc0401.rs:40:13
You can’t perform that action at this time.
0 commit comments