about summary refs log tree commit diff
path: root/compiler
diff options
context:
space:
mode:
authorMatthias Krüger <476013+matthiaskrgr@users.noreply.github.com>2025-05-31 18:51:47 +0200
committerGitHub <noreply@github.com>2025-05-31 18:51:47 +0200
commit05debb0d0d2ec468e75ffb1bf6b53c702cb819b2 (patch)
tree8d8d0e4cd3e677662fce7dd036a5f4ba8cffb235 /compiler
parent738c08b63c4f9e3ebdaec5eece7b6fbc354f6467 (diff)
parent17352e6937fde53c4f75dfd8600ecff4f1077ca2 (diff)
downloadrust-05debb0d0d2ec468e75ffb1bf6b53c702cb819b2.tar.gz
rust-05debb0d0d2ec468e75ffb1bf6b53c702cb819b2.zip
Rollup merge of #140787 - xizheyin:issue-140491, r=nnethercote
Note expr being cast when encounter NonScalar cast error

Fixes #140491

I added note for `expr` so that it doesn't treat `&x as T` as `&(x as T)` but `(&x) as T`. But I'm not sure if I want to add note for all NonScalar, maybe for specific `expr_ty`?

r? compiler
Diffstat (limited to 'compiler')
-rw-r--r--compiler/rustc_hir_typeck/src/cast.rs10
1 files changed, 10 insertions, 0 deletions
diff --git a/compiler/rustc_hir_typeck/src/cast.rs b/compiler/rustc_hir_typeck/src/cast.rs
index c044c4f7c37..e144a6ab599 100644
--- a/compiler/rustc_hir_typeck/src/cast.rs
+++ b/compiler/rustc_hir_typeck/src/cast.rs
@@ -408,6 +408,16 @@ impl<'a, 'tcx> CastCheck<'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() {