Skip to content

Commit

Permalink
Rollup merge of rust-lang#56110 - varkor:inhabitedness-union-enum, r=…
Browse files Browse the repository at this point in the history
…cramertj

Consider references and unions potentially inhabited during privacy-respecting inhabitedness checks

It isn't settled exactly how references to uninhabited types and unions of uninhabited types should act, but we should be more conservative here, as it's likely it will be permitted to soundly have values of such types.

This will also be more important in light of the changes at rust-lang#54125.

cc @RalfJung
  • Loading branch information
Centril authored Dec 1, 2018
2 parents d311571 + 1cdf5df commit 9765fc3
Show file tree
Hide file tree
Showing 9 changed files with 105 additions and 61 deletions.
47 changes: 23 additions & 24 deletions src/librustc/ty/inhabitedness/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,23 +167,16 @@ impl<'a, 'gcx, 'tcx> VariantDef {
substs: &'tcx Substs<'tcx>,
adt_kind: AdtKind) -> DefIdForest
{
match adt_kind {
AdtKind::Union => {
DefIdForest::intersection(tcx, self.fields.iter().map(|f| {
f.uninhabited_from(visited, tcx, substs, false)
}))
},
AdtKind::Struct => {
DefIdForest::union(tcx, self.fields.iter().map(|f| {
f.uninhabited_from(visited, tcx, substs, false)
}))
},
AdtKind::Enum => {
DefIdForest::union(tcx, self.fields.iter().map(|f| {
f.uninhabited_from(visited, tcx, substs, true)
}))
},
}
let is_enum = match adt_kind {
// For now, `union`s are never considered uninhabited.
// The precise semantics of inhabitedness with respect to unions is currently undecided.
AdtKind::Union => return DefIdForest::empty(),
AdtKind::Enum => true,
AdtKind::Struct => false,
};
DefIdForest::union(tcx, self.fields.iter().map(|f| {
f.uninhabited_from(visited, tcx, substs, is_enum)
}))
}
}

Expand All @@ -194,8 +187,8 @@ impl<'a, 'gcx, 'tcx> FieldDef {
visited: &mut FxHashMap<DefId, FxHashSet<&'tcx Substs<'tcx>>>,
tcx: TyCtxt<'a, 'gcx, 'tcx>,
substs: &'tcx Substs<'tcx>,
is_enum: bool) -> DefIdForest
{
is_enum: bool,
) -> DefIdForest {
let mut data_uninhabitedness = move || {
self.ty(tcx, substs).uninhabited_from(visited, tcx)
};
Expand Down Expand Up @@ -253,14 +246,16 @@ impl<'a, 'gcx, 'tcx> TyS<'tcx> {
let substs_set = visited.get_mut(&def.did).unwrap();
substs_set.remove(substs);
ret
},
}

Never => DefIdForest::full(tcx),

Tuple(ref tys) => {
DefIdForest::union(tcx, tys.iter().map(|ty| {
ty.uninhabited_from(visited, tcx)
}))
},
}

Array(ty, len) => {
match len.assert_usize(tcx) {
// If the array is definitely non-empty, it's uninhabited if
Expand All @@ -269,9 +264,13 @@ impl<'a, 'gcx, 'tcx> TyS<'tcx> {
_ => DefIdForest::empty()
}
}
Ref(_, ty, _) => {
ty.uninhabited_from(visited, tcx)
}

// References to uninitialised memory is valid for any type, including
// uninhabited types, in unsafe code, so we treat all references as
// inhabited.
// The precise semantics of inhabitedness with respect to references is currently
// undecided.
Ref(..) => DefIdForest::empty(),

_ => DefIdForest::empty(),
}
Expand Down
1 change: 1 addition & 0 deletions src/test/run-pass/binding/empty-types-in-patterns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ fn main() {
let x: Result<u32, &!> = Ok(123);
match x {
Ok(y) => y,
Err(_) => unimplemented!(),
};

bar(&[]);
Expand Down
32 changes: 32 additions & 0 deletions src/test/ui/always-inhabited-union-ref.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// The precise semantics of inhabitedness with respect to unions and references is currently
// undecided. This test file currently checks a conservative choice.

#![feature(exhaustive_patterns)]
#![feature(never_type)]

#![allow(dead_code)]
#![allow(unreachable_code)]

pub union Foo {
foo: !,
}

fn uninhab_ref() -> &'static ! {
unimplemented!()
}

fn uninhab_union() -> Foo {
unimplemented!()
}

fn match_on_uninhab() {
match uninhab_ref() {
//~^ ERROR non-exhaustive patterns: type `&'static !` is non-empty
}

match uninhab_union() {
//~^ ERROR non-exhaustive patterns: type `Foo` is non-empty
}
}

fn main() {}
27 changes: 27 additions & 0 deletions src/test/ui/always-inhabited-union-ref.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
error[E0004]: non-exhaustive patterns: type `&'static !` is non-empty
--> $DIR/always-inhabited-union-ref.rs:23:11
|
LL | match uninhab_ref() {
| ^^^^^^^^^^^^^
|
help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
--> $DIR/always-inhabited-union-ref.rs:23:11
|
LL | match uninhab_ref() {
| ^^^^^^^^^^^^^

error[E0004]: non-exhaustive patterns: type `Foo` is non-empty
--> $DIR/always-inhabited-union-ref.rs:27:11
|
LL | match uninhab_union() {
| ^^^^^^^^^^^^^^^
|
help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
--> $DIR/always-inhabited-union-ref.rs:27:11
|
LL | match uninhab_union() {
| ^^^^^^^^^^^^^^^

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0004`.
26 changes: 0 additions & 26 deletions src/test/ui/inhabitedness-infinite-loop.rs

This file was deleted.

4 changes: 0 additions & 4 deletions src/test/ui/inhabitedness-infinite-loop.stderr

This file was deleted.

5 changes: 4 additions & 1 deletion src/test/ui/issues/issue-44402.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ fn test_a() {

fn test_b() {
let x: Option<Bar> = None;
match x { None => () }
match x {
Some(_) => (),
None => ()
}
}

fn main() { }
18 changes: 15 additions & 3 deletions src/test/ui/unreachable/unreachable-loop-patterns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,26 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// compile-fail

#![feature(never_type)]
#![feature(exhaustive_patterns)]

#![allow(unreachable_code)]
#![deny(unreachable_patterns)]

fn main() {
let x: &[!] = &[];
enum Void {}

impl Iterator for Void {
type Item = Void;

for _ in x {}
fn next(&mut self) -> Option<Void> {
None
}
}

fn main() {
for _ in unimplemented!() as Void {}
//~^ ERROR unreachable pattern
}

6 changes: 3 additions & 3 deletions src/test/ui/unreachable/unreachable-loop-patterns.stderr
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
error: unreachable pattern
--> $DIR/unreachable-loop-patterns.rs:18:9
--> $DIR/unreachable-loop-patterns.rs:30:9
|
LL | for _ in x {}
LL | for _ in unimplemented!() as Void {}
| ^
|
note: lint level defined here
--> $DIR/unreachable-loop-patterns.rs:13:9
--> $DIR/unreachable-loop-patterns.rs:17:9
|
LL | #![deny(unreachable_patterns)]
| ^^^^^^^^^^^^^^^^^^^^
Expand Down

0 comments on commit 9765fc3

Please sign in to comment.