Skip to content

Commit f6efde4

Browse files
committed
Note ref expr being cast when encounter NonScalar cast error
Signed-off-by: xizheyin <xizheyin@smail.nju.edu.cn>
1 parent a18d29f commit f6efde4

File tree

3 files changed

+11
-0
lines changed

3 files changed

+11
-0
lines changed

compiler/rustc_hir_typeck/src/cast.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,13 @@ impl<'a, 'tcx> CastCheck<'tcx> {
408408
self.expr_ty,
409409
fcx.ty_to_string(self.cast_ty)
410410
);
411+
412+
if let Ok(snippet) = fcx.tcx.sess.source_map().span_to_snippet(self.expr_span)
413+
&& matches!(self.expr.kind, ExprKind::AddrOf(..))
414+
{
415+
err.note(format!("casting reference expression `{}`", snippet));
416+
}
417+
411418
let mut sugg = None;
412419
let mut sugg_mutref = false;
413420
if let ty::Ref(reg, cast_ty, mutbl) = *self.cast_ty.kind() {

tests/ui/cast/func-pointer-issue-140491.stderr

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ error[E0605]: non-primitive cast: `&for<'a, 'b> fn(&'a Event<'b>) {my_fn}` as `&
33
|
44
LL | ..._>) = &my_fn as _;
55
| ^^^^^^^^^^^ an `as` expression can only be used to convert between primitive types or to coerce to a specific trait object
6+
|
7+
= note: casting reference expression `&my_fn`
68

79
error: aborting due to 1 previous error
810

tests/ui/coercion/issue-73886.stderr

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ error[E0605]: non-primitive cast: `&&[i32; 1]` as `&[_]`
33
|
44
LL | let _ = &&[0] as &[_];
55
| ^^^^^^^^^^^^^ an `as` expression can only be used to convert between primitive types or to coerce to a specific trait object
6+
|
7+
= note: casting reference expression `&&[0]`
68

79
error[E0605]: non-primitive cast: `u32` as `Option<_>`
810
--> $DIR/issue-73886.rs:4:13

0 commit comments

Comments
 (0)