-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
Add note when matching on tuples/ADTs containing non-exhaustive types #114397
Conversation
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @Nadrieril (or someone else) soon. Please see the contribution instructions for more information. Namely, in order to ensure the minimum review times lag, PR authors and assigned reviewers should ensure that the review label (
|
Some changes might have occurred in exhaustiveness checking cc @Nadrieril |
Hi, thanks for tackling this! This is a good attempt! However I think looking at the type is not the right approach. For example, in match Some(1usize) {
None => {}
} we want to report that match Some(1usize) {
None => {}
Some(0) => {}
} we do want the note. We can't tell the difference from the types alone. The information we want is however present in the The |
thanks for the feedback!
I'm not sure how can I "recursively look inside witnesses[0]`. + debug!("witnesses={:?}", witnesses);
+ debug!("witnesses[0].ctor()={:?}", witnesses[0].ctor());
+ debug!("witnesses[0].fields()={:?}", witnesses[0].iter_fields().collect::<Vec<_>>());
+ debug!("witnesses[0].ty()={:?}", witnesses[0].ty());
if !is_empty_match && witnesses.len() == 1 {
report_non_exhaustive_tys(cx, scrut_ty, &mut err, &mut FxHashSet::default());
} Adding the above debugging code, I get the same outputs for both of the examples you listed. Specifically, it doesn't seem possible to recurse into
Is it correct to say that I need to do the following:
if (scrut_ty.contains(cx.tcx.types.usize) || scrut_ty.contains(cx.tcx.types.isize))
&& !is_empty_match
&& witnesses.len() == 1
&& matches!(witnesses[0].ctor(), Constructor::NonExhaustive) { ... } |
What you need to look into recursively is To explain quickly the constructor/fields thing, the idea is that all patterns/values either look like DeconstructedPat {
ctor: "Some",
fields: [
DeconstructedPat {
ctor: "Ok",
fields: [
DeconstructedPat {
ctor: "Wildcard",
fields: [],
}
],
}
],
} |
I see, thanks for the guidance. I've updated my PR. Hopefully it's correct now! |
This comment has been minimized.
This comment has been minimized.
match (0isize, 0usize) { | ||
//~^ ERROR non-exhaustive patterns: `(_, _)` not covered [E0004] | ||
(isize::MIN..=isize::MAX, 0) => (), | ||
(isize::MIN..=isize::MAX, 1..=usize::MAX) => (), | ||
} |
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.
For this case, should the ... does not have a fixed maximum value
note be reported for both isize
and usize
?
I would think it should, but my PR currently outputs the note for isize
only. When I recurse into the witness of (0isize, 0usize)
, I get NonExhaustive
for the isize
, but Wildcard
for the usize
. Is this intentional?
Likewise for the case at https://github.com/rust-lang/rust/pull/114397/files#diff-079dd1fff457e8e3896d1365b6d8780efeadcf0b79e01928f36f4f81874213a9R54-R58
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.
Ah yeah, this is a limitation/feature of the exhaustiveness algorithm: it reports the "first pattern it finds", so in this case it didn't bother looking into the second part of the tuple before returning the error. It seems fine to report only the first one.
That looks great! Love the thoroughness of the tests. Let's merge this @bors r+ |
☀️ Test successful - checks-actions |
Finished benchmarking commit (c75b6bd): comparison URL. Overall result: ✅ improvements - no action needed@rustbot label: -perf-regression Instruction countThis is a highly reliable metric that was used to determine the overall result at the top of this comment.
Max RSS (memory usage)ResultsThis is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.
CyclesResultsThis is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.
Binary sizeThis benchmark run did not return any relevant results for this metric. Bootstrap: 631.442s -> 629.758s (-0.27%) |
Fixes #85222
r? @Nadrieril