Skip to content
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

RFC 2008: Uninhabitedness fixes for enum variants and tests #60529

Merged
merged 5 commits into from
May 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions src/librustc/ty/inhabitedness/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,14 @@ impl<'a, 'gcx, 'tcx> AdtDef {
tcx: TyCtxt<'a, 'gcx, 'tcx>,
substs: SubstsRef<'tcx>) -> DefIdForest
{
DefIdForest::intersection(tcx, self.variants.iter().map(|v| {
v.uninhabited_from(tcx, substs, self.adt_kind())
}))
// Non-exhaustive ADTs from other crates are always considered inhabited.
if self.is_variant_list_non_exhaustive() && !self.did.is_local() {
DefIdForest::empty()
} else {
DefIdForest::intersection(tcx, self.variants.iter().map(|v| {
v.uninhabited_from(tcx, substs, self.adt_kind())
}))
}
}
}

Expand All @@ -134,9 +139,14 @@ impl<'a, 'gcx, 'tcx> VariantDef {
AdtKind::Enum => true,
AdtKind::Struct => false,
};
DefIdForest::union(tcx, self.fields.iter().map(|f| {
f.uninhabited_from(tcx, substs, is_enum)
}))
// Non-exhaustive variants from other crates are always considered inhabited.
if self.is_field_list_non_exhaustive() && !self.def_id.is_local() {
DefIdForest::empty()
} else {
DefIdForest::union(tcx, self.fields.iter().map(|f| {
f.uninhabited_from(tcx, substs, is_enum)
}))
}
}
}

Expand Down
27 changes: 23 additions & 4 deletions src/librustc_mir/hair/pattern/_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,18 @@ impl<'a, 'tcx> MatchCheckCtxt<'a, 'tcx> {
}
}

fn is_non_exhaustive_variant<'p>(&self, pattern: &'p Pattern<'tcx>) -> bool
where 'a: 'p
{
match *pattern.kind {
PatternKind::Variant { adt_def, variant_index, .. } => {
let ref variant = adt_def.variants[variant_index];
variant.is_field_list_non_exhaustive()
}
_ => false,
}
}

fn is_non_exhaustive_enum(&self, ty: Ty<'tcx>) -> bool {
match ty.sty {
ty::Adt(adt_def, ..) => adt_def.is_variant_list_non_exhaustive(),
Expand Down Expand Up @@ -1097,10 +1109,17 @@ pub fn is_useful<'p, 'a: 'p, 'tcx: 'a>(cx: &mut MatchCheckCtxt<'a, 'tcx>,
debug!("is_useful_expand_first_col: pcx={:#?}, expanding {:#?}", pcx, v[0]);

if let Some(constructors) = pat_constructors(cx, v[0], pcx) {
debug!("is_useful - expanding constructors: {:#?}", constructors);
split_grouped_constructors(cx.tcx, constructors, matrix, pcx.ty).into_iter().map(|c|
is_useful_specialized(cx, matrix, v, c, pcx.ty, witness)
).find(|result| result.is_useful()).unwrap_or(NotUseful)
let is_declared_nonexhaustive = cx.is_non_exhaustive_variant(v[0]) && !cx.is_local(pcx.ty);
debug!("is_useful - expanding constructors: {:#?}, is_declared_nonexhaustive: {:?}",
constructors, is_declared_nonexhaustive);

if is_declared_nonexhaustive {
Useful
} else {
split_grouped_constructors(cx.tcx, constructors, matrix, pcx.ty).into_iter().map(|c|
is_useful_specialized(cx, matrix, v, c, pcx.ty, witness)
).find(|result| result.is_useful()).unwrap_or(NotUseful)
}
} else {
debug!("is_useful - expanding wildcard");

Expand Down
6 changes: 5 additions & 1 deletion src/librustc_mir/hair/pattern/check_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,11 @@ impl<'a, 'tcx> MatchVisitor<'a, 'tcx> {
.map(|variant| variant.ident)
.collect();
}
def.variants.is_empty()

let is_non_exhaustive_and_non_local =
def.is_variant_list_non_exhaustive() && !def.did.is_local();

!(is_non_exhaustive_and_non_local) && def.variants.is_empty()
},
_ => false
}
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/pattern/const-pat-ice.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
thread 'rustc' panicked at 'assertion failed: rows.iter().all(|r| r.len() == v.len())', src/librustc_mir/hair/pattern/_match.rs:1071:5
thread 'rustc' panicked at 'assertion failed: rows.iter().all(|r| r.len() == v.len())', src/librustc_mir/hair/pattern/_match.rs:1083:5
note: Run with `RUST_BACKTRACE=1` environment variable to display a backtrace.

error: internal compiler error: unexpected panic
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#![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: ! }
}

pub struct IndirectUninhabitedEnum(UninhabitedEnum);

pub struct IndirectUninhabitedStruct(UninhabitedStruct);

pub struct IndirectUninhabitedTupleStruct(UninhabitedTupleStruct);

pub struct IndirectUninhabitedVariants(UninhabitedVariants);
38 changes: 38 additions & 0 deletions src/test/ui/rfc-2008-non-exhaustive/uninhabited/coercions.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// aux-build:uninhabited.rs
#![feature(never_type)]

extern crate uninhabited;

use uninhabited::{
UninhabitedEnum,
UninhabitedStruct,
UninhabitedTupleStruct,
UninhabitedVariants,
};

// This test checks that uninhabited non-exhaustive types cannot coerce to any type, as the never
// type can.

struct A;

fn can_coerce_never_type_to_anything(x: !) -> A {
x
}

fn cannot_coerce_empty_enum_to_anything(x: UninhabitedEnum) -> A {
x //~ ERROR mismatched types
}

fn cannot_coerce_empty_tuple_struct_to_anything(x: UninhabitedTupleStruct) -> A {
x //~ ERROR mismatched types
}

fn cannot_coerce_empty_struct_to_anything(x: UninhabitedStruct) -> A {
x //~ ERROR mismatched types
}

fn cannot_coerce_enum_with_empty_variants_to_anything(x: UninhabitedVariants) -> A {
x //~ ERROR mismatched types
}

fn main() {}
47 changes: 47 additions & 0 deletions src/test/ui/rfc-2008-non-exhaustive/uninhabited/coercions.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
error[E0308]: mismatched types
--> $DIR/coercions.rs:23:5
|
LL | fn cannot_coerce_empty_enum_to_anything(x: UninhabitedEnum) -> A {
| - expected `A` because of return type
LL | x
| ^ expected struct `A`, found enum `uninhabited::UninhabitedEnum`
|
= note: expected type `A`
found type `uninhabited::UninhabitedEnum`

error[E0308]: mismatched types
--> $DIR/coercions.rs:27:5
|
LL | fn cannot_coerce_empty_tuple_struct_to_anything(x: UninhabitedTupleStruct) -> A {
| - expected `A` because of return type
LL | x
| ^ expected struct `A`, found struct `uninhabited::UninhabitedTupleStruct`
|
= note: expected type `A`
found type `uninhabited::UninhabitedTupleStruct`

error[E0308]: mismatched types
--> $DIR/coercions.rs:31:5
|
LL | fn cannot_coerce_empty_struct_to_anything(x: UninhabitedStruct) -> A {
| - expected `A` because of return type
LL | x
| ^ expected struct `A`, found struct `uninhabited::UninhabitedStruct`
|
= note: expected type `A`
found type `uninhabited::UninhabitedStruct`

error[E0308]: mismatched types
--> $DIR/coercions.rs:35:5
|
LL | fn cannot_coerce_enum_with_empty_variants_to_anything(x: UninhabitedVariants) -> A {
| - expected `A` because of return type
LL | x
| ^ expected struct `A`, found enum `uninhabited::UninhabitedVariants`
|
= note: expected type `A`
found type `uninhabited::UninhabitedVariants`

error: aborting due to 4 previous errors

For more information about this error, try `rustc --explain E0308`.
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#![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: ! }
}

struct A;

// This test checks that uninhabited non-exhaustive types defined in the same crate cannot coerce
// to any type, as the never type can.

fn can_coerce_never_type_to_anything(x: !) -> A {
x
}

fn cannot_coerce_empty_enum_to_anything(x: UninhabitedEnum) -> A {
x //~ ERROR mismatched types
}

fn cannot_coerce_empty_tuple_struct_to_anything(x: UninhabitedTupleStruct) -> A {
x //~ ERROR mismatched types
}

fn cannot_coerce_empty_struct_to_anything(x: UninhabitedStruct) -> A {
x //~ ERROR mismatched types
}

fn cannot_coerce_enum_with_empty_variants_to_anything(x: UninhabitedVariants) -> A {
x //~ ERROR mismatched types
}

fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
error[E0308]: mismatched types
--> $DIR/coercions_same_crate.rs:31:5
|
LL | fn cannot_coerce_empty_enum_to_anything(x: UninhabitedEnum) -> A {
| - expected `A` because of return type
LL | x
| ^ expected struct `A`, found enum `UninhabitedEnum`
|
= note: expected type `A`
found type `UninhabitedEnum`

error[E0308]: mismatched types
--> $DIR/coercions_same_crate.rs:35:5
|
LL | fn cannot_coerce_empty_tuple_struct_to_anything(x: UninhabitedTupleStruct) -> A {
| - expected `A` because of return type
LL | x
| ^ expected struct `A`, found struct `UninhabitedTupleStruct`
|
= note: expected type `A`
found type `UninhabitedTupleStruct`

error[E0308]: mismatched types
--> $DIR/coercions_same_crate.rs:39:5
|
LL | fn cannot_coerce_empty_struct_to_anything(x: UninhabitedStruct) -> A {
| - expected `A` because of return type
LL | x
| ^ expected struct `A`, found struct `UninhabitedStruct`
|
= note: expected type `A`
found type `UninhabitedStruct`

error[E0308]: mismatched types
--> $DIR/coercions_same_crate.rs:43:5
|
LL | fn cannot_coerce_enum_with_empty_variants_to_anything(x: UninhabitedVariants) -> A {
| - expected `A` because of return type
LL | x
| ^ expected struct `A`, found enum `UninhabitedVariants`
|
= note: expected type `A`
found type `UninhabitedVariants`

error: aborting due to 4 previous errors

For more information about this error, try `rustc --explain E0308`.
36 changes: 36 additions & 0 deletions src/test/ui/rfc-2008-non-exhaustive/uninhabited/indirect_match.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// aux-build:uninhabited.rs
#![feature(never_type)]

extern crate uninhabited;

use uninhabited::{
IndirectUninhabitedEnum,
IndirectUninhabitedStruct,
IndirectUninhabitedTupleStruct,
IndirectUninhabitedVariants,
};

struct A;

// This test checks that an empty match on a non-exhaustive uninhabited type through a level of
// indirection from an extern crate will not compile.

fn cannot_empty_match_on_empty_enum_to_anything(x: IndirectUninhabitedEnum) -> A {
match x {} //~ ERROR non-exhaustive patterns
}

fn cannot_empty_match_on_empty_struct_to_anything(x: IndirectUninhabitedStruct) -> A {
match x {} //~ ERROR non-exhaustive patterns
}

fn cannot_empty_match_on_empty_tuple_struct_to_anything(x: IndirectUninhabitedTupleStruct) -> A {
match x {} //~ ERROR non-exhaustive patterns
}

fn cannot_empty_match_on_enum_with_empty_variants_struct_to_anything(
x: IndirectUninhabitedVariants,
) -> A {
match x {} //~ ERROR non-exhaustive patterns
}

fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
error[E0004]: non-exhaustive patterns: pattern `IndirectUninhabitedEnum` of type `uninhabited::IndirectUninhabitedEnum` is not handled
--> $DIR/indirect_match.rs:19:11
|
LL | match x {}
| ^
|
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms

error[E0004]: non-exhaustive patterns: pattern `IndirectUninhabitedStruct` of type `uninhabited::IndirectUninhabitedStruct` is not handled
--> $DIR/indirect_match.rs:23:11
|
LL | match x {}
| ^
|
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms

error[E0004]: non-exhaustive patterns: pattern `IndirectUninhabitedTupleStruct` of type `uninhabited::IndirectUninhabitedTupleStruct` is not handled
--> $DIR/indirect_match.rs:27:11
|
LL | match x {}
| ^
|
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms

error[E0004]: non-exhaustive patterns: pattern `IndirectUninhabitedVariants` of type `uninhabited::IndirectUninhabitedVariants` is not handled
--> $DIR/indirect_match.rs:33:11
|
LL | match x {}
| ^
|
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms

error: aborting due to 4 previous errors

For more information about this error, try `rustc --explain E0004`.
Loading