-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Expected u32 found usize error on array/slice gives unhelpful help text #47168
Comments
I believe the output for type errors where the expected and found types are numeric should be:
The last
The checks can be added to |
@leonardo-m, @nikomatsakis what would be the appropriate suggestion in this case? |
The proposed messages seem OK to me. More generally I think that using naked "as" casts for narrowing is the wrong default to use in Rust. You can't have a nicely designed language that performs overflow tests in debug mode and at the same time encourage the use of "as". In my code I use a to!{} macro that shows the default behaviour that Rust missed to use (I'll try to push for this change in the next Epoch):
(Recently the situation is improving for FP<->Int conversions with -Zsaturating-float-casts that should become the default). |
I agree that Anyway, the messages seem ok. |
It's part of a more global suggestion engine that is quite "simple" (aka stupid) I wrote a few months ago. Maybe we should start thinking about it and how we could improve depending on the situation? |
FWIW, as someone who used rust a year+ ago before stopping and only just now picking it up 95% of the times the suggestion engine works great and has done a good job on pointing me in the right direction. It was only this scenario that stuck out to me. |
Suggest casting on numeric type error Re rust-lang#47168.
On nightly the compiler now only complains about the type mismatch, without any hints as to casting:
The compiler will suggest now if the conversion is lossless to use The compiler could suggest using a cast when dealing with |
That might be fine for now. I'd hesitate to suggest Presumably the eventual recommendation will be something like |
Is there any work to be done in this task? |
I do not believe so. I think this is blocked on @scottmcm we could potentially suggest |
I think it's worth pointing out that if there is no recommendation, the first thing someone who encounters this problem is going to do is Google their way out of it. For me, that results in a Stackoverflow post advising the use of |
"as" doesn't perform overflow tests at run-time in debug builds, so its usage should be minimized and not advised. |
Sorry in advance if this seems argumentative or is rehashing old topics, but I'm new here and don't fully understand the advice. Per @estebank above,
If |
Also, integer <-> float and f32 <-> f64 conversions can still only be accomplished with |
Sorry for the spam, but I believe we need a ruling here on how to proceed here and this falls under language design/documentation. @rust-lang/lang you have better visibility onto what the language's long-term view for how to address this case is (particularly @rust-lang/docs you have better insight on what our current documentation actually recommends users, what direction are we pushing people towards? I believe this to be a pebble in the road with the same intensity as |
Nominating for T-Lang discussion re. @estebank's comment. I'm not really sure myself... |
@estebank We discussed this briefly in the lang team meeting; we have no plans for any new features here, so expect something |
@scottmcm that should be reasonable for type errors involving |
I definitely wouldn't recommend |
For that we can suggest |
Integer to float, you mean? Yeah. |
Yes, that's what I meant. I'll implement the suggestions this weekend. |
Would adding implementations of |
@estebank There's a conversation about that going on in https://internals.rust-lang.org/t/tryfrom-for-f64/9793?u=scottmcm -- TLDR people seem unsure about what invertibility semantics the trait has. Personally I find there's some goodness in rust-lang/rfcs#2484, but I don't know where that's going. |
Suggest `try_into` when possible CC #47168
Current output for the original code:
|
Nice, @estebank! That sounds like issue resolved to me. |
@estebank Shouldn't that
Also it seems like a better message would be along the lines of
|
@KallDrexx yes that seems like the way to go. |
The problem with this is that test is performed even in optimized builds, unlike the usual Rust behaviour of performing overflow tests in debug builds only (or on request). |
@leonardo-m that is true, but the aim here is to unblock newcomers who would be stumped by this error and the only two options available at this point to suggest are Also, shouldn't we be able to elide the performance hit of turning an |
Hi, new to rust and ran into this with a
If the given advice requires something extra to be enabled, I think it would be helpful to add that to the comment. I have no idea how to proceed (not asking for help, just giving message feedback). |
What version of
|
Thanks! It looks like the message in Visual Studio Code is truncated: but the compilation is correctly reporting the full message as you said:
I'll look into reporting this to the rust vscode extension. Thank you and sorry for the confusion! |
Is there a "policy" around whether or not suggestions like this should include the required
As an early-stage Rust programmer who encountered this issue, it seems a more friendly experience would be to include the (Bonus points: only including the |
@follower many times the error being emitted is in a stage that lacks the necessary context to provide a 100% valid suggestion. As an informal policy, if applying a suggestion gives you a follow up suggestion(s) that if followed land you in working code likely to have been the original intent, that is considered acceptable. Because of this, I feel that the current state is good enough, but do feel free to file a new ticket for this specific case. I can't promise it will be tackled anytime soon, but it is almost assured we won't do anything about it ever if it's not in our backlog :) |
Thanks for the response & suggestion. I created #77270 because I'd hate to think that anyone might run out of things to do after the other *checks notes* 5,952 issues are closed. :D |
Gives the following compile time error on rustc 1.22.1 (stable):
I'm assuming there's a lot of nuance in the engine that gives suggestion so I don't know how I would personally codify the rules (and even if casting as a u32 is always appropriate since I guess it could overflow) but I do know that the suggestions definitely do not point me in the right direction.
The text was updated successfully, but these errors were encountered: