File tree 5 files changed +45
-32
lines changed
compiler/rustc_hir_typeck/src
5 files changed +45
-32
lines changed Original file line number Diff line number Diff line change @@ -539,25 +539,19 @@ impl<'a, 'tcx> CastCheck<'tcx> {
539
539
match self . expr_ty . kind ( ) {
540
540
ty:: Ref ( _, _, mt) => {
541
541
let mtstr = mt. prefix_str ( ) ;
542
- if self . cast_ty . is_trait ( ) {
543
- match fcx. tcx . sess . source_map ( ) . span_to_snippet ( self . cast_span ) {
544
- Ok ( s) => {
545
- err. span_suggestion (
546
- self . cast_span ,
547
- "try casting to a reference instead" ,
548
- format ! ( "&{mtstr}{s}" ) ,
549
- Applicability :: MachineApplicable ,
550
- ) ;
551
- }
552
- Err ( _) => {
553
- let msg = format ! ( "did you mean `&{mtstr}{tstr}`?" ) ;
554
- err. span_help ( self . cast_span , msg) ;
555
- }
542
+ match fcx. tcx . sess . source_map ( ) . span_to_snippet ( self . cast_span ) {
543
+ Ok ( s) => {
544
+ err. span_suggestion (
545
+ self . cast_span ,
546
+ "try casting to a reference instead" ,
547
+ format ! ( "&{mtstr}{s}" ) ,
548
+ Applicability :: MachineApplicable ,
549
+ ) ;
550
+ }
551
+ Err ( _) => {
552
+ let msg = format ! ( "did you mean `&{mtstr}{tstr}`?" ) ;
553
+ err. span_help ( self . cast_span , msg) ;
556
554
}
557
- } else {
558
- let msg =
559
- format ! ( "consider using an implicit coercion to `&{mtstr}{tstr}` instead" ) ;
560
- err. span_help ( self . span , msg) ;
561
555
}
562
556
}
563
557
ty:: Adt ( def, ..) if def. is_box ( ) => {
Original file line number Diff line number Diff line change
1
+ fn main ( ) {
2
+ "example" . as_bytes ( ) as [ char ] ;
3
+ //~^ ERROR cast to unsized type
4
+
5
+ let arr: & [ u8 ] = & [ 0 , 2 , 3 ] ;
6
+ arr as [ char ] ;
7
+ //~^ ERROR cast to unsized type
8
+ }
Original file line number Diff line number Diff line change
1
+ error[E0620]: cast to unsized type: `&[u8]` as `[char]`
2
+ --> $DIR/cast-to-slice.rs:2:5
3
+ |
4
+ LL | "example".as_bytes() as [char];
5
+ | ^^^^^^^^^^^^^^^^^^^^^^^^------
6
+ | |
7
+ | help: try casting to a reference instead: `&[char]`
8
+
9
+ error[E0620]: cast to unsized type: `&[u8]` as `[char]`
10
+ --> $DIR/cast-to-slice.rs:6:5
11
+ |
12
+ LL | arr as [char];
13
+ | ^^^^^^^------
14
+ | |
15
+ | help: try casting to a reference instead: `&[char]`
16
+
17
+ error: aborting due to 2 previous errors
18
+
19
+ For more information about this error, try `rustc --explain E0620`.
Original file line number Diff line number Diff line change @@ -2,13 +2,9 @@ error[E0620]: cast to unsized type: `&[usize; 2]` as `[usize]`
2
2
--> $DIR/E0620.rs:2:16
3
3
|
4
4
LL | let _foo = &[1_usize, 2] as [usize];
5
- | ^^^^^^^^^^^^^^^^^^^^^^^^
6
- |
7
- help: consider using an implicit coercion to `&[usize]` instead
8
- --> $DIR/E0620.rs:2:16
9
- |
10
- LL | let _foo = &[1_usize, 2] as [usize];
11
- | ^^^^^^^^^^^^^^^^^^^^^^^^
5
+ | ^^^^^^^^^^^^^^^^^-------
6
+ | |
7
+ | help: try casting to a reference instead: `&[usize]`
12
8
13
9
error: aborting due to 1 previous error
14
10
Original file line number Diff line number Diff line change @@ -2,13 +2,9 @@ error[E0620]: cast to unsized type: `&[usize; 2]` as `[usize]`
2
2
--> $DIR/issue-17441.rs:2:16
3
3
|
4
4
LL | let _foo = &[1_usize, 2] as [usize];
5
- | ^^^^^^^^^^^^^^^^^^^^^^^^
6
- |
7
- help: consider using an implicit coercion to `&[usize]` instead
8
- --> $DIR/issue-17441.rs:2:16
9
- |
10
- LL | let _foo = &[1_usize, 2] as [usize];
11
- | ^^^^^^^^^^^^^^^^^^^^^^^^
5
+ | ^^^^^^^^^^^^^^^^^-------
6
+ | |
7
+ | help: try casting to a reference instead: `&[usize]`
12
8
13
9
error[E0620]: cast to unsized type: `Box<usize>` as `dyn Debug`
14
10
--> $DIR/issue-17441.rs:5:16
You can’t perform that action at this time.
0 commit comments