1 #![deny(unreachable_patterns)] 2 #![feature(exhaustive_patterns)] 3 #![feature(never_type)] 4 5 #[non_exhaustive] 6 pub enum UninhabitedEnum { 7 } 8 9 #[non_exhaustive] 10 pub struct UninhabitedTupleStruct(!); 11 12 #[non_exhaustive] 13 pub struct UninhabitedStruct { 14 _priv: !, 15 } 16 17 pub enum UninhabitedVariants { 18 #[non_exhaustive] Tuple(!), 19 #[non_exhaustive] Struct { x: ! } 20 } 21 22 pub enum PartiallyInhabitedVariants { 23 Tuple(u8), 24 #[non_exhaustive] Struct { x: ! } 25 } 26 uninhabited_enum() -> Option<UninhabitedEnum>27fn uninhabited_enum() -> Option<UninhabitedEnum> { 28 None 29 } 30 uninhabited_variant() -> Option<UninhabitedVariants>31fn uninhabited_variant() -> Option<UninhabitedVariants> { 32 None 33 } 34 partially_inhabited_variant() -> PartiallyInhabitedVariants35fn partially_inhabited_variant() -> PartiallyInhabitedVariants { 36 PartiallyInhabitedVariants::Tuple(3) 37 } 38 uninhabited_struct() -> Option<UninhabitedStruct>39fn uninhabited_struct() -> Option<UninhabitedStruct> { 40 None 41 } 42 uninhabited_tuple_struct() -> Option<UninhabitedTupleStruct>43fn uninhabited_tuple_struct() -> Option<UninhabitedTupleStruct> { 44 None 45 } 46 47 // This test checks that non-exhaustive types that would normally be considered uninhabited within 48 // the defining crate are still considered uninhabited. 49 main()50fn main() { 51 match uninhabited_enum() { 52 Some(_x) => (), //~ ERROR unreachable pattern 53 None => (), 54 } 55 56 match uninhabited_variant() { 57 Some(_x) => (), //~ ERROR unreachable pattern 58 None => (), 59 } 60 61 while let PartiallyInhabitedVariants::Struct { x } = partially_inhabited_variant() { 62 //~^ ERROR unreachable pattern 63 } 64 65 while let Some(_x) = uninhabited_struct() { //~ ERROR unreachable pattern 66 } 67 68 while let Some(_x) = uninhabited_tuple_struct() { //~ ERROR unreachable pattern 69 } 70 } 71