Skip to content

Commit fdfb9a2

Browse files
committed
Add explanation for E0607
1 parent 4e68e2a commit fdfb9a2

File tree

1 file changed

+25
-4
lines changed

1 file changed

+25
-4
lines changed

src/librustc_typeck/check/cast.rs

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -281,10 +281,31 @@ impl<'a, 'gcx, 'tcx> CastCheck<'tcx> {
281281
.emit();
282282
}
283283
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();
288309
}
289310
CastError::UnknownCastPtrKind |
290311
CastError::UnknownExprPtrKind => {

0 commit comments

Comments
 (0)