Add uninhabitedness tests w/ #[non_exhaustive].

This commit adds tests checking that uninhabited non-exhaustive types
are considered inhabited when used in another crate.
This commit is contained in:
David Wood 2019-05-04 01:15:26 +01:00
parent 0ac53da03d
commit 0db087e684
No known key found for this signature in database
GPG Key ID: 01760B4F9F53F154
4 changed files with 193 additions and 0 deletions

View File

@ -0,0 +1,25 @@
#![crate_type = "rlib"]
#![feature(never_type)]
#![feature(non_exhaustive)]
#[non_exhaustive]
pub enum UninhabitedEnum {
}
#[non_exhaustive]
pub struct UninhabitedStruct {
_priv: !,
}
#[non_exhaustive]
pub struct UninhabitedTupleStruct(!);
pub enum UninhabitedVariants {
#[non_exhaustive] Tuple(!),
#[non_exhaustive] Struct { x: ! }
}
pub enum PartiallyInhabitedVariants {
Tuple(u8),
#[non_exhaustive] Struct { x: ! }
}

View File

@ -0,0 +1,59 @@
// aux-build:uninhabited.rs
// compile-pass
#![deny(unreachable_patterns)]
#![feature(exhaustive_patterns)]
extern crate uninhabited;
use uninhabited::{
PartiallyInhabitedVariants,
UninhabitedEnum,
UninhabitedStruct,
UninhabitedTupleStruct,
UninhabitedVariants,
};
fn uninhabited_enum() -> Option<UninhabitedEnum> {
None
}
fn uninhabited_variant() -> Option<UninhabitedVariants> {
None
}
fn partially_inhabited_variant() -> PartiallyInhabitedVariants {
PartiallyInhabitedVariants::Tuple(3)
}
fn uninhabited_struct() -> Option<UninhabitedStruct> {
None
}
fn uninhabited_tuple_struct() -> Option<UninhabitedTupleStruct> {
None
}
// This test checks that non-exhaustive types that would normally be considered uninhabited within
// the defining crate are not considered uninhabited from extern crates.
fn main() {
match uninhabited_enum() {
Some(_x) => (), // This line would normally error.
None => (),
}
match uninhabited_variant() {
Some(_x) => (), // This line would normally error.
None => (),
}
// This line would normally error.
while let PartiallyInhabitedVariants::Struct { x, .. } = partially_inhabited_variant() {
}
while let Some(_x) = uninhabited_struct() { // This line would normally error.
}
while let Some(_x) = uninhabited_tuple_struct() { // This line would normally error.
}
}

View File

@ -0,0 +1,71 @@
#![deny(unreachable_patterns)]
#![feature(exhaustive_patterns)]
#![feature(never_type)]
#![feature(non_exhaustive)]
#[non_exhaustive]
pub enum UninhabitedEnum {
}
#[non_exhaustive]
pub struct UninhabitedTupleStruct(!);
#[non_exhaustive]
pub struct UninhabitedStruct {
_priv: !,
}
pub enum UninhabitedVariants {
#[non_exhaustive] Tuple(!),
#[non_exhaustive] Struct { x: ! }
}
pub enum PartiallyInhabitedVariants {
Tuple(u8),
#[non_exhaustive] Struct { x: ! }
}
fn uninhabited_enum() -> Option<UninhabitedEnum> {
None
}
fn uninhabited_variant() -> Option<UninhabitedVariants> {
None
}
fn partially_inhabited_variant() -> PartiallyInhabitedVariants {
PartiallyInhabitedVariants::Tuple(3)
}
fn uninhabited_struct() -> Option<UninhabitedStruct> {
None
}
fn uninhabited_tuple_struct() -> Option<UninhabitedTupleStruct> {
None
}
// This test checks that non-exhaustive types that would normally be considered uninhabited within
// the defining crate are still considered uninhabited.
fn main() {
match uninhabited_enum() {
Some(_x) => (), //~ ERROR unreachable pattern
None => (),
}
match uninhabited_variant() {
Some(_x) => (), //~ ERROR unreachable pattern
None => (),
}
while let PartiallyInhabitedVariants::Struct { x } = partially_inhabited_variant() {
//~^ ERROR unreachable pattern
}
while let Some(_x) = uninhabited_struct() { //~ ERROR unreachable pattern
}
while let Some(_x) = uninhabited_tuple_struct() { //~ ERROR unreachable pattern
}
}

View File

@ -0,0 +1,38 @@
error: unreachable pattern
--> $DIR/uninhabited_same_crate.rs:53:9
|
LL | Some(_x) => (),
| ^^^^^^^^
|
note: lint level defined here
--> $DIR/uninhabited_same_crate.rs:1:9
|
LL | #![deny(unreachable_patterns)]
| ^^^^^^^^^^^^^^^^^^^^
error: unreachable pattern
--> $DIR/uninhabited_same_crate.rs:58:9
|
LL | Some(_x) => (),
| ^^^^^^^^
error: unreachable pattern
--> $DIR/uninhabited_same_crate.rs:62:15
|
LL | while let PartiallyInhabitedVariants::Struct { x } = partially_inhabited_variant() {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: unreachable pattern
--> $DIR/uninhabited_same_crate.rs:66:15
|
LL | while let Some(_x) = uninhabited_struct() {
| ^^^^^^^^
error: unreachable pattern
--> $DIR/uninhabited_same_crate.rs:69:15
|
LL | while let Some(_x) = uninhabited_tuple_struct() {
| ^^^^^^^^
error: aborting due to 5 previous errors