-
Notifications
You must be signed in to change notification settings - Fork 13.5k
Add note when matching on tuples/ADTs containing non-exhaustive types #114397
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
struct A<T> { | ||
a: T, | ||
} | ||
|
||
struct B<T, U>(T, U); | ||
|
||
fn main() { | ||
match 0 { | ||
//~^ ERROR non-exhaustive patterns: `_` not covered [E0004] | ||
0 => (), | ||
1..=usize::MAX => (), | ||
} | ||
|
||
match (0usize, 0usize) { | ||
//~^ ERROR non-exhaustive patterns: `(_, _)` not covered [E0004] | ||
(0, 0) => (), | ||
(1..=usize::MAX, 1..=usize::MAX) => (), | ||
} | ||
|
||
match (0isize, 0usize) { | ||
//~^ ERROR non-exhaustive patterns: `(_, _)` not covered [E0004] | ||
(isize::MIN..=isize::MAX, 0) => (), | ||
(isize::MIN..=isize::MAX, 1..=usize::MAX) => (), | ||
} | ||
Comment on lines
+20
to
+24
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For this case, should the I would think it should, but my PR currently outputs the note for 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 commentThe 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. |
||
|
||
// Should not report note about usize not having fixed max value | ||
match Some(1usize) { | ||
//~^ ERROR non-exhaustive patterns: `Some(_)` not covered | ||
None => {} | ||
} | ||
|
||
match Some(4) { | ||
//~^ ERROR non-exhaustive patterns: `Some(_)` not covered | ||
Some(0) => (), | ||
Some(1..=usize::MAX) => (), | ||
None => (), | ||
} | ||
|
||
match Some(Some(Some(0))) { | ||
//~^ ERROR non-exhaustive patterns: `Some(Some(Some(_)))` not covered | ||
Some(Some(Some(0))) => (), | ||
Some(Some(Some(1..=usize::MAX))) => (), | ||
Some(Some(None)) => (), | ||
Some(None) => (), | ||
None => (), | ||
} | ||
|
||
match (A { a: 0usize }) { | ||
//~^ ERROR non-exhaustive patterns: `A { .. }` not covered [E0004] | ||
A { a: 0 } => (), | ||
A { a: 1..=usize::MAX } => (), | ||
} | ||
|
||
match B(0isize, 0usize) { | ||
//~^ ERROR non-exhaustive patterns: `B(_, _)` not covered [E0004] | ||
B(isize::MIN..=isize::MAX, 0) => (), | ||
B(isize::MIN..=isize::MAX, 1..=usize::MAX) => (), | ||
} | ||
|
||
// Should report only the note about usize not having fixed max value and not report | ||
// report the note about isize | ||
match B(0isize, 0usize) { | ||
//~^ ERROR non-exhaustive patterns: `B(_, _)` not covered [E0004] | ||
B(_, 0) => (), | ||
B(_, 1..=usize::MAX) => (), | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,170 @@ | ||
error[E0004]: non-exhaustive patterns: `_` not covered | ||
--> $DIR/issue-85222-types-containing-non-exhaustive-types.rs:8:11 | ||
| | ||
LL | match 0 { | ||
| ^ pattern `_` not covered | ||
| | ||
= note: the matched value is of type `usize` | ||
= note: `usize` does not have a fixed maximum value, so a wildcard `_` is necessary to match exhaustively | ||
= help: add `#![feature(precise_pointer_size_matching)]` to the crate attributes to enable precise `usize` matching | ||
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown | ||
| | ||
LL ~ 1..=usize::MAX => (), | ||
LL ~ _ => todo!(), | ||
| | ||
|
||
error[E0004]: non-exhaustive patterns: `(_, _)` not covered | ||
--> $DIR/issue-85222-types-containing-non-exhaustive-types.rs:14:11 | ||
| | ||
LL | match (0usize, 0usize) { | ||
| ^^^^^^^^^^^^^^^^ pattern `(_, _)` not covered | ||
| | ||
= note: the matched value is of type `(usize, usize)` | ||
= note: `usize` does not have a fixed maximum value, so a wildcard `_` is necessary to match exhaustively | ||
= help: add `#![feature(precise_pointer_size_matching)]` to the crate attributes to enable precise `usize` matching | ||
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown | ||
| | ||
LL ~ (1..=usize::MAX, 1..=usize::MAX) => (), | ||
LL ~ (_, _) => todo!(), | ||
| | ||
|
||
error[E0004]: non-exhaustive patterns: `(_, _)` not covered | ||
--> $DIR/issue-85222-types-containing-non-exhaustive-types.rs:20:11 | ||
| | ||
LL | match (0isize, 0usize) { | ||
| ^^^^^^^^^^^^^^^^ pattern `(_, _)` not covered | ||
| | ||
= note: the matched value is of type `(isize, usize)` | ||
= note: `isize` does not have a fixed maximum value, so a wildcard `_` is necessary to match exhaustively | ||
= help: add `#![feature(precise_pointer_size_matching)]` to the crate attributes to enable precise `isize` matching | ||
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown | ||
| | ||
LL ~ (isize::MIN..=isize::MAX, 1..=usize::MAX) => (), | ||
LL ~ (_, _) => todo!(), | ||
| | ||
|
||
error[E0004]: non-exhaustive patterns: `Some(_)` not covered | ||
--> $DIR/issue-85222-types-containing-non-exhaustive-types.rs:27:11 | ||
| | ||
LL | match Some(1usize) { | ||
| ^^^^^^^^^^^^ pattern `Some(_)` not covered | ||
| | ||
note: `Option<usize>` defined here | ||
--> $SRC_DIR/core/src/option.rs:LL:COL | ||
::: $SRC_DIR/core/src/option.rs:LL:COL | ||
| | ||
= note: not covered | ||
= note: the matched value is of type `Option<usize>` | ||
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown | ||
| | ||
LL ~ None => {}, | ||
LL + Some(_) => todo!() | ||
| | ||
|
||
error[E0004]: non-exhaustive patterns: `Some(_)` not covered | ||
--> $DIR/issue-85222-types-containing-non-exhaustive-types.rs:32:11 | ||
| | ||
LL | match Some(4) { | ||
| ^^^^^^^ pattern `Some(_)` not covered | ||
| | ||
note: `Option<usize>` defined here | ||
--> $SRC_DIR/core/src/option.rs:LL:COL | ||
::: $SRC_DIR/core/src/option.rs:LL:COL | ||
| | ||
= note: not covered | ||
= note: the matched value is of type `Option<usize>` | ||
= note: `usize` does not have a fixed maximum value, so a wildcard `_` is necessary to match exhaustively | ||
= help: add `#![feature(precise_pointer_size_matching)]` to the crate attributes to enable precise `usize` matching | ||
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown | ||
| | ||
LL ~ None => (), | ||
LL ~ Some(_) => todo!(), | ||
| | ||
|
||
error[E0004]: non-exhaustive patterns: `Some(Some(Some(_)))` not covered | ||
--> $DIR/issue-85222-types-containing-non-exhaustive-types.rs:39:11 | ||
| | ||
LL | match Some(Some(Some(0))) { | ||
| ^^^^^^^^^^^^^^^^^^^ pattern `Some(Some(Some(_)))` not covered | ||
| | ||
note: `Option<Option<Option<usize>>>` defined here | ||
--> $SRC_DIR/core/src/option.rs:LL:COL | ||
::: $SRC_DIR/core/src/option.rs:LL:COL | ||
| | ||
= note: not covered | ||
| | ||
= note: not covered | ||
| | ||
= note: not covered | ||
= note: the matched value is of type `Option<Option<Option<usize>>>` | ||
= note: `usize` does not have a fixed maximum value, so a wildcard `_` is necessary to match exhaustively | ||
= help: add `#![feature(precise_pointer_size_matching)]` to the crate attributes to enable precise `usize` matching | ||
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown | ||
| | ||
LL ~ None => (), | ||
LL ~ Some(Some(Some(_))) => todo!(), | ||
| | ||
|
||
error[E0004]: non-exhaustive patterns: `A { .. }` not covered | ||
--> $DIR/issue-85222-types-containing-non-exhaustive-types.rs:48:11 | ||
| | ||
LL | match (A { a: 0usize }) { | ||
| ^^^^^^^^^^^^^^^^^ pattern `A { .. }` not covered | ||
| | ||
note: `A<usize>` defined here | ||
--> $DIR/issue-85222-types-containing-non-exhaustive-types.rs:1:8 | ||
| | ||
LL | struct A<T> { | ||
| ^ | ||
= note: the matched value is of type `A<usize>` | ||
= note: `usize` does not have a fixed maximum value, so a wildcard `_` is necessary to match exhaustively | ||
= help: add `#![feature(precise_pointer_size_matching)]` to the crate attributes to enable precise `usize` matching | ||
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown | ||
| | ||
LL ~ A { a: 1..=usize::MAX } => (), | ||
LL ~ A { .. } => todo!(), | ||
| | ||
|
||
error[E0004]: non-exhaustive patterns: `B(_, _)` not covered | ||
--> $DIR/issue-85222-types-containing-non-exhaustive-types.rs:54:11 | ||
| | ||
LL | match B(0isize, 0usize) { | ||
| ^^^^^^^^^^^^^^^^^ pattern `B(_, _)` not covered | ||
| | ||
note: `B<isize, usize>` defined here | ||
--> $DIR/issue-85222-types-containing-non-exhaustive-types.rs:5:8 | ||
| | ||
LL | struct B<T, U>(T, U); | ||
| ^ | ||
= note: the matched value is of type `B<isize, usize>` | ||
= note: `isize` does not have a fixed maximum value, so a wildcard `_` is necessary to match exhaustively | ||
= help: add `#![feature(precise_pointer_size_matching)]` to the crate attributes to enable precise `isize` matching | ||
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown | ||
| | ||
LL ~ B(isize::MIN..=isize::MAX, 1..=usize::MAX) => (), | ||
LL ~ B(_, _) => todo!(), | ||
| | ||
|
||
error[E0004]: non-exhaustive patterns: `B(_, _)` not covered | ||
--> $DIR/issue-85222-types-containing-non-exhaustive-types.rs:62:11 | ||
| | ||
LL | match B(0isize, 0usize) { | ||
| ^^^^^^^^^^^^^^^^^ pattern `B(_, _)` not covered | ||
| | ||
note: `B<isize, usize>` defined here | ||
--> $DIR/issue-85222-types-containing-non-exhaustive-types.rs:5:8 | ||
| | ||
LL | struct B<T, U>(T, U); | ||
| ^ | ||
= note: the matched value is of type `B<isize, usize>` | ||
= note: `usize` does not have a fixed maximum value, so a wildcard `_` is necessary to match exhaustively | ||
= help: add `#![feature(precise_pointer_size_matching)]` to the crate attributes to enable precise `usize` matching | ||
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown | ||
| | ||
LL ~ B(_, 1..=usize::MAX) => (), | ||
LL ~ B(_, _) => todo!(), | ||
| | ||
|
||
error: aborting due to 9 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0004`. |
Uh oh!
There was an error while loading. Please reload this page.