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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

xizheyin
Copy link
Contributor

@xizheyin xizheyin commented May 8, 2025

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

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels May 8, 2025
Copy link
Contributor

@nnethercote nnethercote left a comment

Choose a reason for hiding this comment

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

Most of the time this results in a longer message but no extra information, e.g.:

LL | fn main() { let u = 0u32 as (); }
   |                     ^^^^^^^^^^ an `as` expression can only be used to convert between primitive types or to coerce to a specific trait object
   |
   = note: casting expr `0u32` as `()`

It's obvious that 0u32 is cast as (), it's right there above. Is it possible to only add the note when the type being cast to is partially/fully unspecified?

Also, in the issue-73886.rs we see some cases where the type is partially unspecified but the error message doesn't give any extra information:

   = note: casting expr `&&[0]` as `&[_]`
   = note: casting expr `7u32` as `Option<_>`

It would be nice if the _ were actually expanded here.

BTW, two thumbs up for adding the new test in the first commit and then doing the rest of the changes in the second commit. That makes reviewing easier.


if let Ok(snippet) = fcx.tcx.sess.source_map().span_to_snippet(self.expr_span) {
err.note(format!(
"casting expr `{}` as `{}`",
Copy link
Contributor

Choose a reason for hiding this comment

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

Change expr to expression, please.

@nnethercote nnethercote added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels May 9, 2025
@xizheyin
Copy link
Contributor Author

xizheyin commented May 9, 2025

Thanks for review :)

It's obvious that 0u32 is cast as (), it's right there above. Is it possible to only add the note when the type being cast to is partially/fully unspecified?

Indeed, it doesn't seem necessary to add duplicate notes for some simple types, and we can add notes when expressions are composite types such as references, for example, are added. However, the type may not be what this PR needs to address, but rather should indicate which expression is being converted.

error[E0605]: non-primitive cast: &&[i32; 1] as &[_]

In issue-73886.rs, the original message is here. I think this is a very good expansion to know what _ is and needs a separate PR. this PR is specific to #140491 and we may just need to hint at the expression.

BTW, two thumbs up for adding the new test in the first commit and then doing the rest of the changes in the second commit. That makes reviewing easier.

Yes, this was handed to me the last time you reviewed my PR, and I've been doing this ever since.

Copy link
Contributor Author

@xizheyin xizheyin left a comment

Choose a reason for hiding this comment

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

@rustbot ready

Comment on lines +411 to +417

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 `{}`", snippet));
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Now, it will emit note only when the expression is &....

@@ -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]`
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The explicit note looks like making sense, bacause some body would mistake it as &(&[0] as &[_]) subconsciously.

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels May 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Function pointer primitive cast not working
3 participants