Skip to content

Commit 2b7a23e

Browse files
committed
Hide uninhabitedness checks behind feature gate
1 parent c0d0e68 commit 2b7a23e

File tree

5 files changed

+81
-41
lines changed

5 files changed

+81
-41
lines changed

src/librustc/ty/inhabitedness/mod.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -191,11 +191,7 @@ impl<'a, 'gcx, 'tcx> TyS<'tcx> {
191191
}
192192
}
193193
TyRef(_, ref tm) => {
194-
if tcx.sess.features.borrow().never_type {
195-
tm.ty.uninhabited_from(visited, tcx)
196-
} else {
197-
DefIdForest::empty()
198-
}
194+
tm.ty.uninhabited_from(visited, tcx)
199195
}
200196

201197
_ => DefIdForest::empty(),

src/librustc_const_eval/_match.rs

+13-4
Original file line numberDiff line numberDiff line change
@@ -379,19 +379,24 @@ impl<'tcx> Witness<'tcx> {
379379
fn all_constructors<'a, 'tcx: 'a>(cx: &mut MatchCheckCtxt<'a, 'tcx>,
380380
pcx: PatternContext<'tcx>) -> Vec<Constructor>
381381
{
382+
let check_inhabited = cx.tcx.sess.features.borrow().never_type;
382383
debug!("all_constructors({:?})", pcx.ty);
383384
match pcx.ty.sty {
384385
ty::TyBool =>
385386
[true, false].iter().map(|b| ConstantValue(ConstVal::Bool(*b))).collect(),
386387
ty::TySlice(ref sub_ty) => {
387-
if sub_ty.is_uninhabited_from(cx.module, cx.tcx) {
388+
if sub_ty.is_uninhabited_from(cx.module, cx.tcx)
389+
&& check_inhabited
390+
{
388391
vec![Slice(0)]
389392
} else {
390393
(0..pcx.max_slice_length+1).map(|length| Slice(length)).collect()
391394
}
392395
}
393396
ty::TyArray(ref sub_ty, length) => {
394-
if length == 0 || !sub_ty.is_uninhabited_from(cx.module, cx.tcx) {
397+
if length == 0 || !(sub_ty.is_uninhabited_from(cx.module, cx.tcx)
398+
&& check_inhabited)
399+
{
395400
vec![Slice(length)]
396401
} else {
397402
vec![]
@@ -403,15 +408,19 @@ fn all_constructors<'a, 'tcx: 'a>(cx: &mut MatchCheckCtxt<'a, 'tcx>,
403408
let forest = v.uninhabited_from(&mut visited,
404409
cx.tcx, substs,
405410
AdtKind::Enum);
406-
if forest.contains(cx.tcx, cx.module) {
411+
if forest.contains(cx.tcx, cx.module)
412+
&& check_inhabited
413+
{
407414
None
408415
} else {
409416
Some(Variant(v.did))
410417
}
411418
}).collect()
412419
}
413420
_ => {
414-
if pcx.ty.is_uninhabited_from(cx.module, cx.tcx) {
421+
if pcx.ty.is_uninhabited_from(cx.module, cx.tcx)
422+
&& check_inhabited
423+
{
415424
vec![]
416425
} else {
417426
vec![Single]

src/librustc_mir/build/matches/simplify.rs

+17-13
Original file line numberDiff line numberDiff line change
@@ -99,20 +99,24 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
9999
}
100100

101101
PatternKind::Variant { adt_def, substs, variant_index, ref subpatterns } => {
102-
let irrefutable = adt_def.variants.iter().enumerate().all(|(i, v)| {
103-
i == variant_index || {
104-
let mut visited = FxHashSet::default();
105-
let node_set = v.uninhabited_from(&mut visited,
106-
self.hir.tcx(),
107-
substs,
108-
adt_def.adt_kind());
109-
!node_set.is_empty()
102+
if self.hir.tcx().sess.features.borrow().never_type {
103+
let irrefutable = adt_def.variants.iter().enumerate().all(|(i, v)| {
104+
i == variant_index || {
105+
let mut visited = FxHashSet::default();
106+
let node_set = v.uninhabited_from(&mut visited,
107+
self.hir.tcx(),
108+
substs,
109+
adt_def.adt_kind());
110+
!node_set.is_empty()
111+
}
112+
});
113+
if irrefutable {
114+
let lvalue = match_pair.lvalue.downcast(adt_def, variant_index);
115+
candidate.match_pairs.extend(self.field_match_pairs(lvalue, subpatterns));
116+
Ok(())
117+
} else {
118+
Err(match_pair)
110119
}
111-
});
112-
if irrefutable {
113-
let lvalue = match_pair.lvalue.downcast(adt_def, variant_index);
114-
candidate.match_pairs.extend(self.field_match_pairs(lvalue, subpatterns));
115-
Ok(())
116120
} else {
117121
Err(match_pair)
118122
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![feature(slice_patterns)]
12+
13+
enum Void {}
14+
15+
fn main() {
16+
let x: Result<u32, &'static Void> = Ok(23);
17+
let _ = match x { //~ ERROR non-exhaustive
18+
Ok(n) => n,
19+
};
20+
21+
let x: &Void = unsafe { std::mem::uninitialized() };
22+
let _ = match x {};
23+
//~^ ERROR non-exhaustive
24+
25+
let x: (Void,) = unsafe { std::mem::uninitialized() };
26+
let _ = match x {};
27+
//~^ ERROR non-exhaustive
28+
29+
let x: [Void; 1] = unsafe { std::mem::uninitialized() };
30+
let _ = match x {};
31+
//~^ ERROR non-exhaustive
32+
33+
let x: &[Void] = unsafe { std::mem::uninitialized() };
34+
let _ = match x { //~ ERROR non-exhaustive
35+
&[] => (),
36+
};
37+
38+
let x: Void = unsafe { std::mem::uninitialized() };
39+
let _ = match x {}; // okay
40+
41+
let x: Result<u32, Void> = Ok(23);
42+
let _ = match x { //~ ERROR non-exhaustive
43+
Ok(x) => x,
44+
};
45+
46+
let x: Result<u32, Void> = Ok(23);
47+
let Ok(x) = x;
48+
//~^ ERROR refutable
49+
}
50+

src/test/compile-fail/uninhabited-reference-type-feature-gated.rs

-19
This file was deleted.

0 commit comments

Comments
 (0)