Skip to content

Commit 63c7a3e

Browse files
committed
Reuse conservative_is_uninhabited
1 parent a656814 commit 63c7a3e

File tree

7 files changed

+16
-26
lines changed

7 files changed

+16
-26
lines changed

src/librustc/ty/mod.rs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -529,18 +529,6 @@ impl<'tcx> TyS<'tcx> {
529529
_ => true,
530530
}
531531
}
532-
533-
// Returns true if the construction of `self` would require a value of type `!`
534-
// to have been constructed. This check is conservative.
535-
pub fn requires_never_value(&self) -> bool {
536-
match self.sty {
537-
ty::TyNever => true,
538-
ty::TyRawPtr(ty_and_mut) |
539-
ty::TyRef(_, ty_and_mut) => ty_and_mut.ty.requires_never_value(),
540-
ty::TyTuple(comps, _) => comps.iter().any(|ty| ty.requires_never_value()),
541-
_ => false
542-
}
543-
}
544532
}
545533

546534
impl<'a, 'gcx> HashStable<StableHashingContext<'a>> for ty::TyS<'gcx> {

src/librustc/ty/sty.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1282,6 +1282,16 @@ impl<'a, 'gcx, 'tcx> TyS<'tcx> {
12821282
}
12831283
}
12841284

1285+
pub fn conservative_is_uninhabited(&self) -> bool {
1286+
// "rustc-1.0-style" uncontentious uninhabitableness check
1287+
match self.sty {
1288+
ty::TyNever => true,
1289+
ty::TyAdt(def, _) => def.variants.is_empty(),
1290+
ty::TyTuple(tys, _) => tys.iter().any(|ty| ty.conservative_is_uninhabited()),
1291+
_ => false
1292+
}
1293+
}
1294+
12851295
pub fn is_primitive(&self) -> bool {
12861296
match self.sty {
12871297
TyBool | TyChar | TyInt(_) | TyUint(_) | TyFloat(_) => true,

src/librustc_mir/build/expr/into.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
320320
let ty = expr.ty;
321321
let rvalue = unpack!(block = this.as_local_rvalue(block, expr));
322322

323-
if ty.requires_never_value() {
323+
if ty.conservative_is_uninhabited() {
324324
// It's impossible to have an rvalue of type `!`, so if we encounter one,
325325
// we can terminate the block as unreachable immediately.
326326
this.cfg.terminate(block, source_info, TerminatorKind::Unreachable);

src/librustc_mir/build/matches/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
5555
self.cfg.push_assign(block, dummy_source_info, &dummy_temp, dummy_access);
5656

5757
let arms: Vec<Arm<'tcx>> = arms.into_iter().filter(|arm|
58-
!arm.patterns.iter().any(|pat| pat.is_unreachable())
58+
arm.patterns.iter().any(|pat| !pat.is_unreachable())
5959
).collect();
6060

6161
let mut arm_blocks = ArmBlocks {

src/librustc_mir/hair/pattern/check_match.rs

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ impl<'a, 'tcx> MatchVisitor<'a, 'tcx> {
225225
let scrutinee_is_uninhabited = if self.tcx.features().exhaustive_patterns {
226226
self.tcx.is_ty_uninhabited_from(module, pat_ty)
227227
} else {
228-
self.conservative_is_uninhabited(pat_ty)
228+
pat_ty.conservative_is_uninhabited()
229229
};
230230
if !scrutinee_is_uninhabited {
231231
// We know the type is inhabited, so this must be wrong
@@ -253,15 +253,6 @@ impl<'a, 'tcx> MatchVisitor<'a, 'tcx> {
253253
})
254254
}
255255

256-
fn conservative_is_uninhabited(&self, scrutinee_ty: Ty<'tcx>) -> bool {
257-
// "rustc-1.0-style" uncontentious uninhabitableness check
258-
match scrutinee_ty.sty {
259-
ty::TyNever => true,
260-
ty::TyAdt(def, _) => def.variants.is_empty(),
261-
_ => false
262-
}
263-
}
264-
265256
fn check_irrefutable(&self, pat: &'tcx Pat, origin: &str) {
266257
let module = self.tcx.hir.get_module_parent(pat.id);
267258
MatchCheckCtxt::create_and_enter(self.tcx, module, |ref mut cx| {

src/librustc_mir/hair/pattern/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -311,12 +311,12 @@ impl<'a, 'tcx> Pattern<'tcx> {
311311
// Returns true if the pattern cannot bind, as it would require a value of type `!` to have
312312
// been constructed. This check is conservative.
313313
pub fn is_unreachable(&self) -> bool {
314-
if self.ty.requires_never_value() {
314+
if self.ty.conservative_is_uninhabited() {
315315
return true;
316316
}
317317
match *self.kind {
318318
PatternKind::Binding { ty, ref subpattern, .. } => {
319-
if ty.requires_never_value() {
319+
if ty.conservative_is_uninhabited() {
320320
return true;
321321
}
322322
if let &Some(ref subpattern) = subpattern {

src/test/mir-opt/never_type_unreachable.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ pub fn main() { }
5555

5656
// START rustc.never_ref.SimplifyCfg-initial.after.mir
5757
// bb0: {
58+
// ...
5859
// unreachable;
5960
// }
6061
// END rustc.never_ref.SimplifyCfg-initial.after.mir

0 commit comments

Comments
 (0)