Skip to content

Note expr being cast when encounter NonScalar cast error #140787

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 31, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions compiler/rustc_hir_typeck/src/cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
7 changes: 7 additions & 0 deletions tests/ui/cast/func-pointer-issue-140491.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
fn my_fn(event: &Event<'_>) {}

struct Event<'a>(&'a ());

fn main() {
const ptr: &fn(&Event<'_>) = &my_fn as _; //~ ERROR non-primitive cast: `&for<'a, 'b> fn(&'a Event<'b>) {my_fn}` as `&for<'a, 'b> fn(&'a Event<'b>)` [E0605]
}
11 changes: 11 additions & 0 deletions tests/ui/cast/func-pointer-issue-140491.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
error[E0605]: non-primitive cast: `&for<'a, 'b> fn(&'a Event<'b>) {my_fn}` as `&for<'a, 'b> fn(&'a Event<'b>)`
--> $DIR/func-pointer-issue-140491.rs:6:34
|
LL | ..._>) = &my_fn as _;
| ^^^^^^^^^^^ an `as` expression can only be used to convert between primitive types or to coerce to a specific trait object
|
= note: casting reference expression `&my_fn` because `&` binds tighter than `as`
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

because is indeed better.


error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0605`.
2 changes: 2 additions & 0 deletions tests/ui/coercion/issue-73886.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ error[E0605]: non-primitive cast: `&&[i32; 1]` as `&[_]`
|
LL | let _ = &&[0] as &[_];
| ^^^^^^^^^^^^^ an `as` expression can only be used to convert between primitive types or to coerce to a specific trait object
|
= note: casting reference expression `&&[0]` because `&` binds tighter than `as`

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