@@ -281,10 +281,31 @@ impl<'a, 'gcx, 'tcx> CastCheck<'tcx> {
281
281
. emit ( ) ;
282
282
}
283
283
CastError :: SizedUnsizedCast => {
284
- type_error_struct ! ( fcx. tcx. sess, self . span, self . expr_ty, E0607 ,
285
- "cannot cast thin pointer `{}` to fat pointer `{}`" ,
286
- self . expr_ty,
287
- fcx. ty_to_string( self . cast_ty) ) . emit ( ) ;
284
+ let mut err = type_error_struct ! (
285
+ fcx. tcx. sess,
286
+ self . span,
287
+ self . expr_ty,
288
+ E0607 ,
289
+ "cannot cast thin pointer `{}` to fat pointer `{}`" ,
290
+ self . expr_ty,
291
+ fcx. ty_to_string( self . cast_ty)
292
+ ) ;
293
+ if fcx. tcx . sess . opts . debugging_opts . explain {
294
+ err. note (
295
+ "Thin pointers are \" simple\" pointers: they are purely a reference to a \
296
+ memory address.\n \n \
297
+ Fat pointers are pointers referencing \" Dynamically Sized Types\" (also \
298
+ called DST). DST don't have a statically known size, therefore they can \
299
+ only exist behind some kind of pointers that contain additional \
300
+ information. Slices and trait objects are DSTs. In the case of slices, \
301
+ the additional information the fat pointer holds is their size.") ;
302
+ err. note ( "to fix this error, don't try to cast directly between thin and fat \
303
+ pointers") ;
304
+ err. help ( "for more information about casts, take a look at [The Book]\
305
+ (https://doc.rust-lang.org/book/first-edition/\
306
+ casting-between-types.html)") ;
307
+ }
308
+ err. emit ( ) ;
288
309
}
289
310
CastError :: UnknownCastPtrKind |
290
311
CastError :: UnknownExprPtrKind => {
0 commit comments