-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Provide hint when cast needs a dereference #37442
Conversation
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @Aatch (or someone else) soon. If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes. Please see the contribution instructions for more information. |
@@ -139,6 +140,21 @@ impl<'a, 'gcx, 'tcx> CastCheck<'tcx> { | |||
|
|||
fn report_cast_error(&self, fcx: &FnCtxt<'a, 'gcx, 'tcx>, e: CastError) { | |||
match e { | |||
CastError::NeedDeref => { | |||
let cast_ty = fcx.ty_to_string(self.cast_ty); | |||
let mut err = fcx.type_error_struct(self.cast_span, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pointing at the self.cast_span
instead of self.span
(i16
instead of s as i16
in the example) because when trying to apply the secondary label on just self.expr.span
(s
in the example) the actual output looks like this:
error: casting `&f64` as `i16` is invalid
--> file3.rs:2:30
|
2 | vec![0.0].iter().map(|s| s as i16).collect::<Vec<i16>>();
| ^^^^^^^^
| |
| casting `&f64` as `i16` is invalid // <<< this in red
| try dereferencing for the cast to work // <<< this in blue
losing the fact that the secondary label's hint should only apply to the s
only.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Filed #37453 for this.
42f99b7
to
3790a03
Compare
self.expr_ty); | ||
err.span_label(self.cast_span, | ||
&format!("casting `{}` as `{}` is invalid", self.expr_ty, cast_ty)); | ||
err.span_label(self.expr.span, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This shouldn’t be a span_label, but rather span_help
instead.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we show the deref, it should be span_help
.
Another way would be to change the label to, for example, "did you mean *{}?"
CastError::NeedViaUsize => "a usize", | ||
_ => bug!(), | ||
})) | ||
.span_label(self.span, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I disagree about label here as well.
} | ||
} | ||
_ => Err(CastError::NeedViaPtr), | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
struct A;
&A as A;
stays without a note/help. I’m fine with it personally, but that’s something that could use such a help message.
f85bf8f
to
5a5df0a
Compare
@estebank - I like it! Can we do one small tweak while you're at it?
|
a843721
to
f291bbe
Compare
@jonathandturner done! |
Great! @bors r+ rollup |
📌 Commit f291bbe has been approved by |
☔ The latest upstream changes (presumably #37375) made this pull request unmergeable. Please resolve the merge conflicts. |
For a given code: ```rust vec![0.0].iter().map(|s| s as i16).collect::<Vec<i16>>(); ``` display: ```nocode error: casting `&f64` as `i16` is invalid --> foo.rs:2:35 | 2 | vec![0.0].iter().map(|s| s as i16).collect::<Vec<i16>>(); | - ^^^ cannot cast `&f64` as `i16` | | | did you mean `*s`? ``` instead of: ```nocode error: casting `&f64` as `i16` is invalid --> <anon>:2:30 | 2 | vec![0.0].iter().map(|s| s as i16).collect(); | ^^^^^^^^ | = help: cast through a raw pointer first ```
f291bbe
to
ec24442
Compare
@jonathandturner fixed merge conflict. |
@bors r+ rollup |
📌 Commit ec24442 has been approved by |
…ndturner Provide hint when cast needs a dereference For a given code: ``` rust vec![0.0].iter().map(|s| s as i16).collect::<Vec<i16>>(); ``` display: ``` nocode error: casting `&f64` as `i16` is invalid --> file3.rs:2:35 | 2 | vec![0.0].iter().map(|s| s as i16).collect::<Vec<i16>>(); | - ^^^ | | | did you mean `*s`? ``` instead of: ``` nocode error: casting `&f64` as `i16` is invalid --> <anon>:2:30 | 2 | vec![0.0].iter().map(|s| s as i16).collect(); | ^^^^^^^^ | = help: cast through a raw pointer first ``` Fixes rust-lang#37338.
For a given code:
display:
instead of:
Fixes #37338.