diff options
| -rw-r--r-- | src/librustc_typeck/check/cast.rs | 29 |
1 files changed, 25 insertions, 4 deletions
diff --git a/src/librustc_typeck/check/cast.rs b/src/librustc_typeck/check/cast.rs index d2f759f5d99..d31a99dd171 100644 --- a/src/librustc_typeck/check/cast.rs +++ b/src/librustc_typeck/check/cast.rs @@ -281,10 +281,31 @@ impl<'a, 'gcx, 'tcx> CastCheck<'tcx> { .emit(); } CastError::SizedUnsizedCast => { - type_error_struct!(fcx.tcx.sess, self.span, self.expr_ty, E0607, - "cannot cast thin pointer `{}` to fat pointer `{}`", - self.expr_ty, - fcx.ty_to_string(self.cast_ty)).emit(); + let mut err = type_error_struct!( + fcx.tcx.sess, + self.span, + self.expr_ty, + E0607, + "cannot cast thin pointer `{}` to fat pointer `{}`", + self.expr_ty, + fcx.ty_to_string(self.cast_ty) + ); + if fcx.tcx.sess.opts.debugging_opts.explain { + err.note( + "Thin pointers are \"simple\" pointers: they are purely a reference to a \ + memory address.\n\n\ + Fat pointers are pointers referencing \"Dynamically Sized Types\" (also \ + called DST). DST don't have a statically known size, therefore they can \ + only exist behind some kind of pointers that contain additional \ + information. Slices and trait objects are DSTs. In the case of slices, \ + the additional information the fat pointer holds is their size."); + err.note("to fix this error, don't try to cast directly between thin and fat \ + pointers"); + err.help("for more information about casts, take a look at [The Book]\ + (https://doc.rust-lang.org/book/first-edition/\ + casting-between-types.html)"); + } + err.emit(); } CastError::UnknownCastPtrKind | CastError::UnknownExprPtrKind => { |
