-
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
Improve error message for non-exhaustive patterns #31020
Conversation
Changes error message from displaying first found missing constructor witness to showing up to 10, if necessary. Fixes issue #16884.
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @brson (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. |
span_err!(cx.tcx.sess, sp, E0004, | ||
"non-exhaustive patterns: `{}` not covered", | ||
pat_to_string(witness) | ||
pattern_strings.join("`, `") |
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.
Maybe delimit the last element with "and"? I think this reads a bit weird otherwise.
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.
Good point. Was wondering about this, too.
Would something like this look good to you?
let joined_string = match pattern_strings {
[pattern] => pattern,
[head.., tail] => head.join("`, `") + "` and `" + tail
}
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 also uses slice patterns, and apparently requires the "advanced_slice_pattern" feature gate.
I'm not sure about the optimal solution, maybe something like this?
let joined_string = match pattern_strings.split_last() {
Some((tail, head)) => {
if head.is_empty() {
tail.clone()
} else {
head.join("`, `") + "` and `" + tail
}
},
None => unreachable!()
};
More human-readable error message showing ellipsis for excessively long witness lists.
I'm guessing you are making this change because it's annoying to fix the error just to be given a nearly identical message. I think this is a fine reason. I might suggest that 10 is too many though, based on the rule of thumb that people can generally keep 7 +/3 'things' straight at a time. @nikomatsakis do we have any precedent for how many 'things' to display before cutting errors off? It may be worth saying "... and more (not shown)" or something for the case where we do cut them off. @nikomatsakis do we have existing precedent for what language to use for that? |
Hi brson, thanks for the comment! I totally agree, that 10 is probably quite a bit too much. I guess even three would be suffice in the end. After all if you're missing that many patterns in the first place, then you might want to consult the subject's type definition anyway, in order to proceed (instead of like copy/paste the missing patterns from the error message). As such, I suppose, even a limit as low as 3 should work here. After all it should do, is make it clear that there's more than one missing case, when there is. I quickly searched the project for Maybe @steveklabnik knows more about such a precedent or has suggestions for a proper fit from the overall point of view of the docs? Further more I used an actual Unicode ellipsis glyph ( |
Hmm, I was trying to think of good precedent, but I'm coming up blank. I think there are other places we've added semi-arbitrary cutoffs, but I'm not sure where that is right now. In particular, I was looking at the suggestions for traits to import in method dispatch, but those seem to print all applicable options (those are in Still, I agree that just printing 2 or 3 is plenty. If we know the full set, you could print something like "...and 7 more possibilities." Otherwise, it feels like we ought to be able to just insert some wording to make it clear that these are just representative, and there are more possibilities than the ones we've printed. |
…atterns for excessive cases.
I adjusted the error message to now print in either of these formats:
depending on the number of missing patterns. |
@brson, @nikomatsakis: Any other changes you'd like me to make to this? |
@regexident sounds awesome! :) |
let used_constructors: Vec<Constructor> = rows.iter() | ||
.flat_map(|row| pat_constructors(cx, row[0], left_ty, max_slice_length)) | ||
.collect(); | ||
all_constructors(cx, left_ty, max_slice_length) | ||
.into_iter() | ||
.find(|c| !used_constructors.contains(c)) | ||
.filter(|c| !used_constructors.contains(c)).collect() |
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.
Nit: can you put the .collect()
on the next line, to match the formatting above?
This looks good to me modulo the nits above. @regexident can you update with those small nits addressed (and then leave a comment, since otherwise I don't get any notifications)? |
Sure, I'm at it already. Thanks for the comments! |
@nikomatsakis: |
@bors r+ Thanks @regexident |
📌 Commit 2c7a19a has been approved by |
Changes error message from displaying first found missing constructor witness to showing up to 10, if necessary. Fixes issue #16884.
✋ @brson ✋ @nikomatsakis 🙇 |
the assert was invalidated by PR rust-lang#31020 Fixes rust-lang#31561
the assert was invalidated by PR rust-lang#31020 Fixes rust-lang#31561
An issue was just opened relevant to this: #36410 |
Changes error message from displaying first found missing constructor witness to showing up to 10, if necessary.
Fixes issue #16884.