@@ -105,7 +105,6 @@ enum CastError {
105
105
NeedViaPtr ,
106
106
NeedViaThinPtr ,
107
107
NeedViaInt ,
108
- NeedViaUsize ,
109
108
NonScalar ,
110
109
}
111
110
@@ -139,26 +138,39 @@ impl<'a, 'gcx, 'tcx> CastCheck<'tcx> {
139
138
140
139
fn report_cast_error ( & self , fcx : & FnCtxt < ' a , ' gcx , ' tcx > , e : CastError ) {
141
140
match e {
142
- CastError :: NeedViaPtr |
143
141
CastError :: NeedViaThinPtr |
144
- CastError :: NeedViaInt |
145
- CastError :: NeedViaUsize => {
142
+ CastError :: NeedViaPtr => {
143
+ let mut err = fcx. type_error_struct ( self . span ,
144
+ |actual| {
145
+ format ! ( "casting `{}` as `{}` is invalid" ,
146
+ actual,
147
+ fcx. ty_to_string( self . cast_ty) )
148
+ } ,
149
+ self . expr_ty ) ;
150
+ if self . cast_ty . is_uint ( ) {
151
+ err. help ( & format ! ( "cast through {} first" ,
152
+ match e {
153
+ CastError :: NeedViaPtr => "a raw pointer" ,
154
+ CastError :: NeedViaThinPtr => "a thin pointer" ,
155
+ _ => bug!( ) ,
156
+ } ) ) ;
157
+ }
158
+ err. emit ( ) ;
159
+ }
160
+ CastError :: NeedViaInt => {
146
161
fcx. type_error_struct ( self . span ,
147
- |actual| {
148
- format ! ( "casting `{}` as `{}` is invalid" ,
149
- actual,
150
- fcx. ty_to_string( self . cast_ty) )
151
- } ,
152
- self . expr_ty )
153
- . help ( & format ! ( "cast through {} first" ,
154
- match e {
155
- CastError :: NeedViaPtr => "a raw pointer" ,
156
- CastError :: NeedViaThinPtr => "a thin pointer" ,
157
- CastError :: NeedViaInt => "an integer" ,
158
- CastError :: NeedViaUsize => "a usize" ,
159
- _ => bug!( ) ,
160
- } ) )
161
- . emit ( ) ;
162
+ |actual| {
163
+ format ! ( "casting `{}` as `{}` is invalid" ,
164
+ actual,
165
+ fcx. ty_to_string( self . cast_ty) )
166
+ } ,
167
+ self . expr_ty )
168
+ . help ( & format ! ( "cast through {} first" ,
169
+ match e {
170
+ CastError :: NeedViaInt => "an integer" ,
171
+ _ => bug!( ) ,
172
+ } ) )
173
+ . emit ( ) ;
162
174
}
163
175
CastError :: CastToBool => {
164
176
struct_span_err ! ( fcx. tcx. sess, self . span, E0054 , "cannot cast as `bool`" )
@@ -366,21 +378,23 @@ impl<'a, 'gcx, 'tcx> CastCheck<'tcx> {
366
378
( Int ( Bool ) , Float ) |
367
379
( Int ( CEnum ) , Float ) |
368
380
( Int ( Char ) , Float ) => Err ( CastError :: NeedViaInt ) ,
381
+
369
382
( Int ( Bool ) , Ptr ( _) ) |
370
383
( Int ( CEnum ) , Ptr ( _) ) |
371
- ( Int ( Char ) , Ptr ( _) ) => Err ( CastError :: NeedViaUsize ) ,
384
+ ( Int ( Char ) , Ptr ( _) ) |
385
+ ( Ptr ( _) , Float ) |
386
+ ( FnPtr , Float ) |
387
+ ( Float , Ptr ( _) ) => Err ( CastError :: IllegalCast ) ,
372
388
373
389
// ptr -> *
374
390
( Ptr ( m_e) , Ptr ( m_c) ) => self . check_ptr_ptr_cast ( fcx, m_e, m_c) , // ptr-ptr-cast
375
391
( Ptr ( m_expr) , Int ( _) ) => self . check_ptr_addr_cast ( fcx, m_expr) , // ptr-addr-cast
376
- ( Ptr ( _) , Float ) | ( FnPtr , Float ) => Err ( CastError :: NeedViaUsize ) ,
377
392
( FnPtr , Int ( _) ) => Ok ( CastKind :: FnPtrAddrCast ) ,
378
393
( RPtr ( _) , Int ( _) ) |
379
394
( RPtr ( _) , Float ) => Err ( CastError :: NeedViaPtr ) ,
380
395
// * -> ptr
381
396
( Int ( _) , Ptr ( mt) ) => self . check_addr_ptr_cast ( fcx, mt) , // addr-ptr-cast
382
397
( FnPtr , Ptr ( mt) ) => self . check_fptr_ptr_cast ( fcx, mt) ,
383
- ( Float , Ptr ( _) ) => Err ( CastError :: NeedViaUsize ) ,
384
398
( RPtr ( rmt) , Ptr ( mt) ) => self . check_ref_cast ( fcx, rmt, mt) , // array-ptr-cast
385
399
386
400
// prim -> prim
@@ -391,7 +405,6 @@ impl<'a, 'gcx, 'tcx> CastCheck<'tcx> {
391
405
( Int ( _) , Int ( _) ) | ( Int ( _) , Float ) | ( Float , Int ( _) ) | ( Float , Float ) => {
392
406
Ok ( CastKind :: NumericCast )
393
407
}
394
-
395
408
}
396
409
}
397
410
0 commit comments