File tree Expand file tree Collapse file tree 5 files changed +45
-32
lines changed
compiler/rustc_hir_typeck/src Expand file tree Collapse file tree 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> {
539539 match self . expr_ty . kind ( ) {
540540 ty:: Ref ( _, _, mt) => {
541541 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) ;
556554 }
557- } else {
558- let msg =
559- format ! ( "consider using an implicit coercion to `&{mtstr}{tstr}` instead" ) ;
560- err. span_help ( self . span , msg) ;
561555 }
562556 }
563557 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]`
22 --> $DIR/E0620.rs:2:16
33 |
44LL | 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]`
128
139error: aborting due to 1 previous error
1410
Original file line number Diff line number Diff line change @@ -2,13 +2,9 @@ error[E0620]: cast to unsized type: `&[usize; 2]` as `[usize]`
22 --> $DIR/issue-17441.rs:2:16
33 |
44LL | 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]`
128
139error[E0620]: cast to unsized type: `Box<usize>` as `dyn Debug`
1410 --> $DIR/issue-17441.rs:5:16
You can’t perform that action at this time.
0 commit comments