From 69821cf8df86c8f4366e0c16a0a7de8d0135b90f Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Mon, 16 Nov 2020 21:13:00 +0000 Subject: [PATCH] Add a test for foreign empty enums --- .../ui/pattern/usefulness/auxiliary/empty.rs | 2 + .../match-empty-exhaustive_patterns.rs | 20 ++++++- .../match-empty-exhaustive_patterns.stderr | 56 +++++++++++-------- src/test/ui/pattern/usefulness/match-empty.rs | 20 ++++++- .../ui/pattern/usefulness/match-empty.stderr | 52 ++++++++++------- 5 files changed, 102 insertions(+), 48 deletions(-) create mode 100644 src/test/ui/pattern/usefulness/auxiliary/empty.rs diff --git a/src/test/ui/pattern/usefulness/auxiliary/empty.rs b/src/test/ui/pattern/usefulness/auxiliary/empty.rs new file mode 100644 index 000000000000..0b0719f48ee0 --- /dev/null +++ b/src/test/ui/pattern/usefulness/auxiliary/empty.rs @@ -0,0 +1,2 @@ +#![crate_type = "rlib"] +pub enum EmptyForeignEnum {} diff --git a/src/test/ui/pattern/usefulness/match-empty-exhaustive_patterns.rs b/src/test/ui/pattern/usefulness/match-empty-exhaustive_patterns.rs index c8d6e95dd6e0..c5c3a214f9af 100644 --- a/src/test/ui/pattern/usefulness/match-empty-exhaustive_patterns.rs +++ b/src/test/ui/pattern/usefulness/match-empty-exhaustive_patterns.rs @@ -1,8 +1,12 @@ +// aux-build:empty.rs #![feature(never_type)] #![feature(never_type_fallback)] #![feature(exhaustive_patterns)] #![deny(unreachable_patterns)] -enum Foo {} + +extern crate empty; + +enum EmptyEnum {} struct NonEmptyStruct(bool); //~ `NonEmptyStruct` defined here union NonEmptyUnion1 { //~ `NonEmptyUnion1` defined here @@ -42,7 +46,17 @@ macro_rules! match_false { }; } -fn foo(x: Foo) { +fn empty_enum(x: EmptyEnum) { + match x {} // ok + match x { + _ => {}, //~ ERROR unreachable pattern + } + match x { + _ if false => {}, //~ ERROR unreachable pattern + } +} + +fn empty_foreign_enum(x: empty::EmptyForeignEnum) { match x {} // ok match x { _ => {}, //~ ERROR unreachable pattern @@ -67,7 +81,7 @@ fn main() { None => {} Some(_) => {} //~ ERROR unreachable pattern } - match None:: { + match None:: { None => {} Some(_) => {} //~ ERROR unreachable pattern } diff --git a/src/test/ui/pattern/usefulness/match-empty-exhaustive_patterns.stderr b/src/test/ui/pattern/usefulness/match-empty-exhaustive_patterns.stderr index 9ec9d737e89b..9d8b5f38e8cf 100644 --- a/src/test/ui/pattern/usefulness/match-empty-exhaustive_patterns.stderr +++ b/src/test/ui/pattern/usefulness/match-empty-exhaustive_patterns.stderr @@ -1,47 +1,59 @@ error: unreachable pattern - --> $DIR/match-empty-exhaustive_patterns.rs:48:9 + --> $DIR/match-empty-exhaustive_patterns.rs:52:9 | LL | _ => {}, | ^ | note: the lint level is defined here - --> $DIR/match-empty-exhaustive_patterns.rs:4:9 + --> $DIR/match-empty-exhaustive_patterns.rs:5:9 | LL | #![deny(unreachable_patterns)] | ^^^^^^^^^^^^^^^^^^^^ error: unreachable pattern - --> $DIR/match-empty-exhaustive_patterns.rs:51:9 + --> $DIR/match-empty-exhaustive_patterns.rs:55:9 | LL | _ if false => {}, | ^ error: unreachable pattern - --> $DIR/match-empty-exhaustive_patterns.rs:58:9 + --> $DIR/match-empty-exhaustive_patterns.rs:62:9 | LL | _ => {}, | ^ error: unreachable pattern - --> $DIR/match-empty-exhaustive_patterns.rs:61:9 + --> $DIR/match-empty-exhaustive_patterns.rs:65:9 | LL | _ if false => {}, | ^ error: unreachable pattern - --> $DIR/match-empty-exhaustive_patterns.rs:68:9 + --> $DIR/match-empty-exhaustive_patterns.rs:72:9 + | +LL | _ => {}, + | ^ + +error: unreachable pattern + --> $DIR/match-empty-exhaustive_patterns.rs:75:9 + | +LL | _ if false => {}, + | ^ + +error: unreachable pattern + --> $DIR/match-empty-exhaustive_patterns.rs:82:9 | LL | Some(_) => {} | ^^^^^^^ error: unreachable pattern - --> $DIR/match-empty-exhaustive_patterns.rs:72:9 + --> $DIR/match-empty-exhaustive_patterns.rs:86:9 | LL | Some(_) => {} | ^^^^^^^ error[E0004]: non-exhaustive patterns: type `u8` is non-empty - --> $DIR/match-empty-exhaustive_patterns.rs:75:18 + --> $DIR/match-empty-exhaustive_patterns.rs:89:18 | LL | match_empty!(0u8); | ^^^ @@ -50,7 +62,7 @@ LL | match_empty!(0u8); = note: the matched value is of type `u8` error[E0004]: non-exhaustive patterns: type `NonEmptyStruct` is non-empty - --> $DIR/match-empty-exhaustive_patterns.rs:77:18 + --> $DIR/match-empty-exhaustive_patterns.rs:91:18 | LL | struct NonEmptyStruct(bool); | ---------------------------- `NonEmptyStruct` defined here @@ -62,7 +74,7 @@ LL | match_empty!(NonEmptyStruct(true)); = note: the matched value is of type `NonEmptyStruct` error[E0004]: non-exhaustive patterns: type `NonEmptyUnion1` is non-empty - --> $DIR/match-empty-exhaustive_patterns.rs:79:18 + --> $DIR/match-empty-exhaustive_patterns.rs:93:18 | LL | / union NonEmptyUnion1 { LL | | foo: (), @@ -76,7 +88,7 @@ LL | match_empty!((NonEmptyUnion1 { foo: () })); = note: the matched value is of type `NonEmptyUnion1` error[E0004]: non-exhaustive patterns: type `NonEmptyUnion2` is non-empty - --> $DIR/match-empty-exhaustive_patterns.rs:81:18 + --> $DIR/match-empty-exhaustive_patterns.rs:95:18 | LL | / union NonEmptyUnion2 { LL | | foo: (), @@ -91,7 +103,7 @@ LL | match_empty!((NonEmptyUnion2 { foo: () })); = note: the matched value is of type `NonEmptyUnion2` error[E0004]: non-exhaustive patterns: `Foo(_)` not covered - --> $DIR/match-empty-exhaustive_patterns.rs:83:18 + --> $DIR/match-empty-exhaustive_patterns.rs:97:18 | LL | / enum NonEmptyEnum1 { LL | | Foo(bool), @@ -108,7 +120,7 @@ LL | match_empty!(NonEmptyEnum1::Foo(true)); = note: the matched value is of type `NonEmptyEnum1` error[E0004]: non-exhaustive patterns: `Foo(_)` and `Bar` not covered - --> $DIR/match-empty-exhaustive_patterns.rs:85:18 + --> $DIR/match-empty-exhaustive_patterns.rs:99:18 | LL | / enum NonEmptyEnum2 { LL | | Foo(bool), @@ -129,7 +141,7 @@ LL | match_empty!(NonEmptyEnum2::Foo(true)); = note: the matched value is of type `NonEmptyEnum2` error[E0004]: non-exhaustive patterns: `V1`, `V2`, `V3` and 2 more not covered - --> $DIR/match-empty-exhaustive_patterns.rs:87:18 + --> $DIR/match-empty-exhaustive_patterns.rs:101:18 | LL | / enum NonEmptyEnum5 { LL | | V1, V2, V3, V4, V5, @@ -143,7 +155,7 @@ LL | match_empty!(NonEmptyEnum5::V1); = note: the matched value is of type `NonEmptyEnum5` error[E0004]: non-exhaustive patterns: `_` not covered - --> $DIR/match-empty-exhaustive_patterns.rs:90:18 + --> $DIR/match-empty-exhaustive_patterns.rs:104:18 | LL | match_false!(0u8); | ^^^ pattern `_` not covered @@ -152,7 +164,7 @@ LL | match_false!(0u8); = note: the matched value is of type `u8` error[E0004]: non-exhaustive patterns: `NonEmptyStruct(_)` not covered - --> $DIR/match-empty-exhaustive_patterns.rs:92:18 + --> $DIR/match-empty-exhaustive_patterns.rs:106:18 | LL | struct NonEmptyStruct(bool); | ---------------------------- `NonEmptyStruct` defined here @@ -164,7 +176,7 @@ LL | match_false!(NonEmptyStruct(true)); = note: the matched value is of type `NonEmptyStruct` error[E0004]: non-exhaustive patterns: `NonEmptyUnion1 { .. }` not covered - --> $DIR/match-empty-exhaustive_patterns.rs:94:18 + --> $DIR/match-empty-exhaustive_patterns.rs:108:18 | LL | / union NonEmptyUnion1 { LL | | foo: (), @@ -178,7 +190,7 @@ LL | match_false!((NonEmptyUnion1 { foo: () })); = note: the matched value is of type `NonEmptyUnion1` error[E0004]: non-exhaustive patterns: `NonEmptyUnion2 { .. }` not covered - --> $DIR/match-empty-exhaustive_patterns.rs:96:18 + --> $DIR/match-empty-exhaustive_patterns.rs:110:18 | LL | / union NonEmptyUnion2 { LL | | foo: (), @@ -193,7 +205,7 @@ LL | match_false!((NonEmptyUnion2 { foo: () })); = note: the matched value is of type `NonEmptyUnion2` error[E0004]: non-exhaustive patterns: `Foo(_)` not covered - --> $DIR/match-empty-exhaustive_patterns.rs:98:18 + --> $DIR/match-empty-exhaustive_patterns.rs:112:18 | LL | / enum NonEmptyEnum1 { LL | | Foo(bool), @@ -210,7 +222,7 @@ LL | match_false!(NonEmptyEnum1::Foo(true)); = note: the matched value is of type `NonEmptyEnum1` error[E0004]: non-exhaustive patterns: `Foo(_)` and `Bar` not covered - --> $DIR/match-empty-exhaustive_patterns.rs:100:18 + --> $DIR/match-empty-exhaustive_patterns.rs:114:18 | LL | / enum NonEmptyEnum2 { LL | | Foo(bool), @@ -231,7 +243,7 @@ LL | match_false!(NonEmptyEnum2::Foo(true)); = note: the matched value is of type `NonEmptyEnum2` error[E0004]: non-exhaustive patterns: `V1`, `V2`, `V3` and 2 more not covered - --> $DIR/match-empty-exhaustive_patterns.rs:102:18 + --> $DIR/match-empty-exhaustive_patterns.rs:116:18 | LL | / enum NonEmptyEnum5 { LL | | V1, V2, V3, V4, V5, @@ -244,6 +256,6 @@ LL | match_false!(NonEmptyEnum5::V1); = help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms = note: the matched value is of type `NonEmptyEnum5` -error: aborting due to 20 previous errors +error: aborting due to 22 previous errors For more information about this error, try `rustc --explain E0004`. diff --git a/src/test/ui/pattern/usefulness/match-empty.rs b/src/test/ui/pattern/usefulness/match-empty.rs index 835df1f551b1..10ea2a10406e 100644 --- a/src/test/ui/pattern/usefulness/match-empty.rs +++ b/src/test/ui/pattern/usefulness/match-empty.rs @@ -1,7 +1,11 @@ +// aux-build:empty.rs #![feature(never_type)] #![feature(never_type_fallback)] #![deny(unreachable_patterns)] -enum Foo {} + +extern crate empty; + +enum EmptyEnum {} struct NonEmptyStruct(bool); //~ `NonEmptyStruct` defined here union NonEmptyUnion1 { //~ `NonEmptyUnion1` defined here @@ -41,7 +45,17 @@ macro_rules! match_false { }; } -fn foo(x: Foo) { +fn empty_enum(x: EmptyEnum) { + match x {} // ok + match x { + _ => {}, //~ ERROR unreachable pattern + } + match x { + _ if false => {}, //~ ERROR unreachable pattern + } +} + +fn empty_foreign_enum(x: empty::EmptyForeignEnum) { match x {} // ok match x { _ => {}, //~ ERROR unreachable pattern @@ -67,7 +81,7 @@ fn main() { None => {} Some(_) => {} } - match None:: { + match None:: { None => {} Some(_) => {} } diff --git a/src/test/ui/pattern/usefulness/match-empty.stderr b/src/test/ui/pattern/usefulness/match-empty.stderr index af666b3a9218..6065c552390e 100644 --- a/src/test/ui/pattern/usefulness/match-empty.stderr +++ b/src/test/ui/pattern/usefulness/match-empty.stderr @@ -1,35 +1,47 @@ error: unreachable pattern - --> $DIR/match-empty.rs:47:9 + --> $DIR/match-empty.rs:51:9 | LL | _ => {}, | ^ | note: the lint level is defined here - --> $DIR/match-empty.rs:3:9 + --> $DIR/match-empty.rs:4:9 | LL | #![deny(unreachable_patterns)] | ^^^^^^^^^^^^^^^^^^^^ error: unreachable pattern - --> $DIR/match-empty.rs:50:9 + --> $DIR/match-empty.rs:54:9 | LL | _ if false => {}, | ^ error: unreachable pattern - --> $DIR/match-empty.rs:57:9 + --> $DIR/match-empty.rs:61:9 | LL | _ => {}, | ^ error: unreachable pattern - --> $DIR/match-empty.rs:60:9 + --> $DIR/match-empty.rs:64:9 + | +LL | _ if false => {}, + | ^ + +error: unreachable pattern + --> $DIR/match-empty.rs:71:9 + | +LL | _ => {}, + | ^ + +error: unreachable pattern + --> $DIR/match-empty.rs:74:9 | LL | _ if false => {}, | ^ error[E0004]: non-exhaustive patterns: type `u8` is non-empty - --> $DIR/match-empty.rs:75:18 + --> $DIR/match-empty.rs:89:18 | LL | match_empty!(0u8); | ^^^ @@ -38,7 +50,7 @@ LL | match_empty!(0u8); = note: the matched value is of type `u8` error[E0004]: non-exhaustive patterns: type `NonEmptyStruct` is non-empty - --> $DIR/match-empty.rs:77:18 + --> $DIR/match-empty.rs:91:18 | LL | struct NonEmptyStruct(bool); | ---------------------------- `NonEmptyStruct` defined here @@ -50,7 +62,7 @@ LL | match_empty!(NonEmptyStruct(true)); = note: the matched value is of type `NonEmptyStruct` error[E0004]: non-exhaustive patterns: type `NonEmptyUnion1` is non-empty - --> $DIR/match-empty.rs:79:18 + --> $DIR/match-empty.rs:93:18 | LL | / union NonEmptyUnion1 { LL | | foo: (), @@ -64,7 +76,7 @@ LL | match_empty!((NonEmptyUnion1 { foo: () })); = note: the matched value is of type `NonEmptyUnion1` error[E0004]: non-exhaustive patterns: type `NonEmptyUnion2` is non-empty - --> $DIR/match-empty.rs:81:18 + --> $DIR/match-empty.rs:95:18 | LL | / union NonEmptyUnion2 { LL | | foo: (), @@ -79,7 +91,7 @@ LL | match_empty!((NonEmptyUnion2 { foo: () })); = note: the matched value is of type `NonEmptyUnion2` error[E0004]: non-exhaustive patterns: `Foo(_)` not covered - --> $DIR/match-empty.rs:83:18 + --> $DIR/match-empty.rs:97:18 | LL | / enum NonEmptyEnum1 { LL | | Foo(bool), @@ -96,7 +108,7 @@ LL | match_empty!(NonEmptyEnum1::Foo(true)); = note: the matched value is of type `NonEmptyEnum1` error[E0004]: non-exhaustive patterns: `Foo(_)` and `Bar` not covered - --> $DIR/match-empty.rs:85:18 + --> $DIR/match-empty.rs:99:18 | LL | / enum NonEmptyEnum2 { LL | | Foo(bool), @@ -117,7 +129,7 @@ LL | match_empty!(NonEmptyEnum2::Foo(true)); = note: the matched value is of type `NonEmptyEnum2` error[E0004]: non-exhaustive patterns: `V1`, `V2`, `V3` and 2 more not covered - --> $DIR/match-empty.rs:87:18 + --> $DIR/match-empty.rs:101:18 | LL | / enum NonEmptyEnum5 { LL | | V1, V2, V3, V4, V5, @@ -131,7 +143,7 @@ LL | match_empty!(NonEmptyEnum5::V1); = note: the matched value is of type `NonEmptyEnum5` error[E0004]: non-exhaustive patterns: `_` not covered - --> $DIR/match-empty.rs:90:18 + --> $DIR/match-empty.rs:104:18 | LL | match_false!(0u8); | ^^^ pattern `_` not covered @@ -140,7 +152,7 @@ LL | match_false!(0u8); = note: the matched value is of type `u8` error[E0004]: non-exhaustive patterns: `NonEmptyStruct(_)` not covered - --> $DIR/match-empty.rs:92:18 + --> $DIR/match-empty.rs:106:18 | LL | struct NonEmptyStruct(bool); | ---------------------------- `NonEmptyStruct` defined here @@ -152,7 +164,7 @@ LL | match_false!(NonEmptyStruct(true)); = note: the matched value is of type `NonEmptyStruct` error[E0004]: non-exhaustive patterns: `NonEmptyUnion1 { .. }` not covered - --> $DIR/match-empty.rs:94:18 + --> $DIR/match-empty.rs:108:18 | LL | / union NonEmptyUnion1 { LL | | foo: (), @@ -166,7 +178,7 @@ LL | match_false!((NonEmptyUnion1 { foo: () })); = note: the matched value is of type `NonEmptyUnion1` error[E0004]: non-exhaustive patterns: `NonEmptyUnion2 { .. }` not covered - --> $DIR/match-empty.rs:96:18 + --> $DIR/match-empty.rs:110:18 | LL | / union NonEmptyUnion2 { LL | | foo: (), @@ -181,7 +193,7 @@ LL | match_false!((NonEmptyUnion2 { foo: () })); = note: the matched value is of type `NonEmptyUnion2` error[E0004]: non-exhaustive patterns: `Foo(_)` not covered - --> $DIR/match-empty.rs:98:18 + --> $DIR/match-empty.rs:112:18 | LL | / enum NonEmptyEnum1 { LL | | Foo(bool), @@ -198,7 +210,7 @@ LL | match_false!(NonEmptyEnum1::Foo(true)); = note: the matched value is of type `NonEmptyEnum1` error[E0004]: non-exhaustive patterns: `Foo(_)` and `Bar` not covered - --> $DIR/match-empty.rs:100:18 + --> $DIR/match-empty.rs:114:18 | LL | / enum NonEmptyEnum2 { LL | | Foo(bool), @@ -219,7 +231,7 @@ LL | match_false!(NonEmptyEnum2::Foo(true)); = note: the matched value is of type `NonEmptyEnum2` error[E0004]: non-exhaustive patterns: `V1`, `V2`, `V3` and 2 more not covered - --> $DIR/match-empty.rs:102:18 + --> $DIR/match-empty.rs:116:18 | LL | / enum NonEmptyEnum5 { LL | | V1, V2, V3, V4, V5, @@ -232,6 +244,6 @@ LL | match_false!(NonEmptyEnum5::V1); = help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms = note: the matched value is of type `NonEmptyEnum5` -error: aborting due to 18 previous errors +error: aborting due to 20 previous errors For more information about this error, try `rustc --explain E0004`.