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

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.

@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 420

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 &....

@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
@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 21, 2025
@rustbot

This comment has been minimized.

Signed-off-by: xizheyin <xizheyin@smail.nju.edu.cn>
@xizheyin
Copy link
Contributor Author

xizheyin commented May 24, 2025

Never mind.

Is it worth explaining that & binds tighter than as?

I think it is a useful hint!

It would be even better if it could suggest &(&[0] as &[_]) as a fix

I'm concerned that simply suggesting parentheses may be inconsistent with the original intent of the code writer. There are a variety of situations that trigger such hints, such as &[0] as &&[_], which may require further categorization to refine the suggestions.

The current use of NOTE seems to be sufficient without being unnecessarily intrusive. Specific suggestion information can be left to be enhanced later. :)

@rustbot ready

@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 24, 2025
@@ -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]`, where `&` binds tighter than `as`
Copy link
Contributor

Choose a reason for hiding this comment

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

I think because works better than where. Otherwise, this looks good.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good suggestion. I will revise it later.

@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 27, 2025
Signed-off-by: xizheyin <xizheyin@smail.nju.edu.cn>
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

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.

@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 31, 2025
@nnethercote
Copy link
Contributor

@bors r+ rollup

@bors
Copy link
Collaborator

bors commented May 31, 2025

📌 Commit 17352e6 has been approved by nnethercote

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels May 31, 2025
bors added a commit that referenced this pull request May 31, 2025
Rollup of 8 pull requests

Successful merges:

 - #140787 (Note expr being cast when encounter NonScalar cast error)
 - #141112 (std: note that `std::str::from_utf8*` functions are aliases to `<str>::from_utf8*` methods)
 - #141646 (Document what `distcheck` is intended to exercise)
 - #141740 (Hir item kind field order)
 - #141793 (`tests/ui`: A New Order [1/N])
 - #141805 (Update `compiler-builtins` to 0.1.160)
 - #141815 (Enable non-leaf Frame Pointers for mingw-w64 Arm64 Windows)
 - #141819 (Fixes for building windows-gnullvm hosts)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 05debb0 into rust-lang:master May 31, 2025
9 checks passed
@rustbot rustbot added this to the 1.89.0 milestone May 31, 2025
rust-timer added a commit that referenced this pull request May 31, 2025
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
@xizheyin xizheyin deleted the issue-140491 branch June 1, 2025 06:25
github-actions bot pushed a commit to model-checking/verify-rust-std that referenced this pull request Jun 3, 2025
…iaskrgr

Rollup of 8 pull requests

Successful merges:

 - rust-lang#140787 (Note expr being cast when encounter NonScalar cast error)
 - rust-lang#141112 (std: note that `std::str::from_utf8*` functions are aliases to `<str>::from_utf8*` methods)
 - rust-lang#141646 (Document what `distcheck` is intended to exercise)
 - rust-lang#141740 (Hir item kind field order)
 - rust-lang#141793 (`tests/ui`: A New Order [1/N])
 - rust-lang#141805 (Update `compiler-builtins` to 0.1.160)
 - rust-lang#141815 (Enable non-leaf Frame Pointers for mingw-w64 Arm64 Windows)
 - rust-lang#141819 (Fixes for building windows-gnullvm hosts)

r? `@ghost`
`@rustbot` modify labels: rollup
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. 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
4 participants