-
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
Compilation error reporting: Incomplete match over types #38234
Comments
I'm not sure what you are asking for here? Once you cover the other cases you will still have to write |
@durka: The thing is that the error isn't very intuitive for beginners because they bind a super specific case and then all the others without thinking (in this case) that |
Indeed if you leave out the bad arm or fix it, the error is amazing:
so I guess the focus should be improving "match arm with incompatible type" so you can get through to "non-exhaustive patterns". |
Although... even that has a small bug... it should say " |
The fact is that they're covered. But it's shadowed by the other arms. But indeed, a similar error/note would be awesome. |
Indeed, it would have made much more sense to me as a beginner than a "type mismatch" kind of error. |
Current output:
Would it make sense if the output were:
Looking at this it looks a bit long, I feel it could be pruned a bit by doing:
|
The last message you are suggesting looks pretty neat, imho.
Note that I'm still a bit wary of the incompatible types and the Long story short: Yes it would make sense :) |
I've been looking at this and the
|
But this would still be an error if you covered all of the use std::fs::{self,File};
use std::net::{self,TcpStream,TcpListener};
enum FdImplementor {
File(File),
TcpStream(TcpStream),
TcpListener(TcpListener),
}
fn do_smthg() -> Result<FdImplementor, String> {
Ok(FdImplementor::File(fs::File::create("/tmp/pathtest.txt").map_err(|_| "failed to create file")?))
}
fn test() -> Result<bool, String> {
match do_smthg() {
Ok(_) => Ok(true),
e => e //~ ERROR still a type error, you still want Err(e)
}
}
fn main() {
test();
} I think we should just recommend wrapping a variant in this case, but I'm not sure how generic to make it. I don't think exhaustiveness needs to be integrated - after you'll fix the type error, you'll get the correct exhaustiveness error. |
Tweak "incompatible match arms" error - Point at the body expression of the match arm with the type error. - Point at the prior match arms explicitly stating the evaluated type. - Point at the entire match expr in a secondary span, instead of primary. - For type errors in the first match arm, the cause is outside of the match, treat as implicit block error to give a more appropriate error. Fix rust-lang#46776, fix rust-lang#57206. CC rust-lang#24157, rust-lang#38234.
Tweak "incompatible match arms" error - Point at the body expression of the match arm with the type error. - Point at the prior match arms explicitly stating the evaluated type. - Point at the entire match expr in a secondary span, instead of primary. - For type errors in the first match arm, the cause is outside of the match, treat as implicit block error to give a more appropriate error. Fix rust-lang#46776, fix rust-lang#57206. CC rust-lang#24157, rust-lang#38234.
|
This is indeed much better, in the sense that the "returned" I think it can be considered ok to close this. The only difficulty left here for beginners is to understand that the expected return type of I'll leave it up to you to determine whether you want to close this issue or not at this point, but I personally think this is already a great improvement :) 👍 |
Closing in view of the output after #60455 closing the last somewhat confusing part (missing types in the match arm): I see very little improvement we could make over this. |
@estebank Looks fine, we have both the expected return type, and the expectation vs reality on the mismatching match arm. All info is there, and I agree it looks hard to improve :) |
Heya,
I'm currently learning rust through a small systems-related project, with a knowledgeable mentor ( @GuillaumeGomez ) ;)
When (badly) attempting to match over the Result<EnumType, String> returned by one of my functions, I only made a match explicit for one
Ok
case, and was expecting to have one kitchen-sink case to catch all other cases (This was aimed at testing).The error I got rightly claimed that I was using incompatible types, but did not make it clear that I was either missing some potential cases (Ok over some other types of my enum) .
I do not know whether this would be easy or feasible, but it could be nice to have a suggestion pointing at the potential error that the match may deserve to be more detailed.
Here is a sample test code:
And the error:
The text was updated successfully, but these errors were encountered: