-
Notifications
You must be signed in to change notification settings - Fork 13k
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
Binop type mismatch error could be better #38564
Comments
On the other hand, one might argue that it's important for users to understand how to interpret this error. It seems like binops though could be special case to say "The operator |
I also think unimplemented operators in general could be described more accessibly in the way you described @brson. The error message you gave as an example would be optimal, I think, if it pointed out instead that it pertains to |
I think that the general gist of the error is good, but it should add something mentioning that the reason why it needs this trait is because the function Perhaps we could explain this using the regular trait-not-found error and do something like: "The compiler converts this operation into the form That way, it tells users 1) what the compiler actually is doing with the operation and 2) doesn't add any special case for this error, just adds on information in addition to the trait error to help the user understand the conversion. |
I think as a first step it would be helpful to add type annotations to
This way, people can at least recognize the For operators in particular, it might be nice to add a sentence saying |
Explicit help message for binop type mismatch When trying to do `1 + Some(2)`, or some other binary operation on two types different types without an appropriate trait implementation, provide an explicit help message: ```rust help: `{integer} + std::option::Option<{integer}>` has no implementation ``` Re: #39579, #38564, #37626, #39942, #34698.
This has now a
|
The new error I think closes this, since we note that
|
@Mark-Simulacrum - not to be a spoil sport, but this error doesn't feel beginner friendly, yet, which was the original bug. |
Specifically, a beginner friendly message probably doesn't talk about traits at all. Though we want users to understand traits as they grow in their knowledge of Rust, having to learn about them in your first few minutes of Rust is probably too much to ask. We may want to simplify well-known traits (and their errors) to the point of being easier to read by new users. We can still leave a note to help more advanced folks, but something like this:
aside: would be nice to get rid of all the std:: stuff, since well, it's std 😛
(in this particular error, we can be even more specific, not mention traits, but instead point them at match statements or the like to get the i8 out of the Option) |
Hm, yeah, true. Reopening. I worry about having this much of a special case, but I agree it's probably worth it. |
Yeah, definitely a good thing to worry about. Just realised something. You actually can't do what the error note says in this message, right? Because of coherence you won't be able to implement that trait, if I understand correctly. |
Not sure I follow you. I don't see anything about "maybe implement this" in the error. Perhaps we could detect the impossible case and special case it, though I worry that's overly ambitious -- technically, any two types that expose some form of mutable access to internals could be "added" through newtypes. |
Heh, I've been doing Rust for a year and a half, and I'm still not 100% on exactly how the newtype trick is supposed to work. ;) For me the main point is we should be cognisant of just how much someone needs to understand to get benefit from the error message. There's a big benefit to having simpler phrases like |
I agree with swapping the note and annotation. Perhaps an extra note could just state "the value inside an |
Further suggestions about using The reverse doesn't trigger E0277, but E0369 instead:
|
I'm going to close this issue as I see very little we could improve over the following, other than maybe unifying them both:
Filed #60497 to track the wording change. |
From the new book.
If we run this code, we get an error message like this:
This error could be a lot more newbie friendly. The next section of the book even has to apologize and explain:
"Intense! What this error message is trying to say is that Rust does not understand how to add an Option and an i8, since they're different types."
cc @jonathandturner @rust-lang/docs
The text was updated successfully, but these errors were encountered: