Note ref expr being cast when encounter NonScalar cast error

Signed-off-by: xizheyin <xizheyin@smail.nju.edu.cn>
This commit is contained in:
xizheyin
2025-05-09 15:30:16 +08:00
parent 31ee840000
commit 17352e6937
3 changed files with 14 additions and 0 deletions
+10
View File
@@ -408,6 +408,16 @@ fn report_cast_error(&self, fcx: &FnCtxt<'a, 'tcx>, e: CastError<'tcx>) {
self.expr_ty,
fcx.ty_to_string(self.cast_ty)
);
if let Ok(snippet) = fcx.tcx.sess.source_map().span_to_snippet(self.expr_span)
&& matches!(self.expr.kind, ExprKind::AddrOf(..))
{
err.note(format!(
"casting reference expression `{}` because `&` binds tighter than `as`",
snippet
));
}
let mut sugg = None;
let mut sugg_mutref = false;
if let ty::Ref(reg, cast_ty, mutbl) = *self.cast_ty.kind() {