Skip to content

Commit cc8dddb

Browse files
committed
Factor out conservative_is_privately_uninhabited
1 parent 34cbe72 commit cc8dddb

File tree

11 files changed

+25
-81
lines changed

11 files changed

+25
-81
lines changed

compiler/rustc_borrowck/src/type_check/mod.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -1564,10 +1564,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
15641564
}
15651565
}
15661566
None => {
1567-
if !self
1568-
.tcx()
1569-
.conservative_is_privately_uninhabited(self.param_env.and(sig.output()))
1570-
{
1567+
if !sig.output().is_privately_uninhabited(self.tcx(), self.param_env) {
15711568
span_mirbug!(self, term, "call to converging function {:?} w/o dest", sig);
15721569
}
15731570
}

compiler/rustc_middle/src/query/mod.rs

-11
Original file line numberDiff line numberDiff line change
@@ -2078,17 +2078,6 @@ rustc_queries! {
20782078
desc { "normalizing opaque types in `{:?}`", key }
20792079
}
20802080

2081-
/// Checks whether a type is definitely uninhabited. This is
2082-
/// conservative: for some types that are uninhabited we return `false`,
2083-
/// but we only return `true` for types that are definitely uninhabited.
2084-
/// `ty.conservative_is_privately_uninhabited` implies that any value of type `ty`
2085-
/// will be `Abi::Uninhabited`. (Note that uninhabited types may have nonzero
2086-
/// size, to account for partial initialisation. See #49298 for details.)
2087-
query conservative_is_privately_uninhabited(key: ty::ParamEnvAnd<'tcx, Ty<'tcx>>) -> bool {
2088-
desc { "conservatively checking if `{}` is privately uninhabited", key.value }
2089-
remap_env_constness
2090-
}
2091-
20922081
query limits(key: ()) -> Limits {
20932082
desc { "looking up limits" }
20942083
}

compiler/rustc_middle/src/ty/inhabitedness/inhabited_predicate.rs

+7
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,13 @@ impl<'tcx> InhabitedPredicate<'tcx> {
4141
self.apply_inner(tcx, param_env, &|_| Err(())).ok()
4242
}
4343

44+
/// Same as `apply`, but `NotInModule(_)` predicates yield `false`. That is,
45+
/// privately uninhabited types are considered always uninhabited.
46+
pub fn apply_ignore_module(self, tcx: TyCtxt<'tcx>, param_env: ParamEnv<'tcx>) -> bool {
47+
let Ok(result) = self.apply_inner::<!>(tcx, param_env, &|_| Ok(true));
48+
result
49+
}
50+
4451
fn apply_inner<E>(
4552
self,
4653
tcx: TyCtxt<'tcx>,

compiler/rustc_middle/src/ty/inhabitedness/mod.rs

+9
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,15 @@ impl<'tcx> Ty<'tcx> {
169169
) -> bool {
170170
self.inhabited_predicate(tcx).apply(tcx, param_env, module)
171171
}
172+
173+
/// Returns true if the type is uninhabited without regard to visibility
174+
pub fn is_privately_uninhabited(
175+
self,
176+
tcx: TyCtxt<'tcx>,
177+
param_env: ty::ParamEnv<'tcx>,
178+
) -> bool {
179+
!self.inhabited_predicate(tcx).apply_ignore_module(tcx, param_env)
180+
}
172181
}
173182

174183
/// N.B. this query should only be called through `Ty::inhabited_predicate`

compiler/rustc_mir_build/src/build/matches/simplify.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -264,10 +264,10 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
264264
let irrefutable = adt_def.variants().iter_enumerated().all(|(i, v)| {
265265
i == variant_index || {
266266
self.tcx.features().exhaustive_patterns
267-
&& v.inhabited_predicate(self.tcx, adt_def)
267+
&& !v
268+
.inhabited_predicate(self.tcx, adt_def)
268269
.subst(self.tcx, substs)
269-
.apply_any_module(self.tcx, self.param_env)
270-
!= Some(true)
270+
.apply_ignore_module(self.tcx, self.param_env)
271271
}
272272
}) && (adt_def.did().is_local()
273273
|| !adt_def.is_variant_list_non_exhaustive());

compiler/rustc_mir_transform/src/generator.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1015,7 +1015,7 @@ fn insert_panic_block<'tcx>(
10151015

10161016
fn can_return<'tcx>(tcx: TyCtxt<'tcx>, body: &Body<'tcx>, param_env: ty::ParamEnv<'tcx>) -> bool {
10171017
// Returning from a function with an uninhabited return type is undefined behavior.
1018-
if tcx.conservative_is_privately_uninhabited(param_env.and(body.return_ty())) {
1018+
if body.return_ty().is_privately_uninhabited(tcx, param_env) {
10191019
return false;
10201020
}
10211021

compiler/rustc_ty_utils/src/layout.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -442,8 +442,7 @@ fn layout_of_uncached<'tcx>(
442442
let element = cx.layout_of(element)?;
443443
let size = element.size.checked_mul(count, dl).ok_or(LayoutError::SizeOverflow(ty))?;
444444

445-
let abi = if count != 0 && tcx.conservative_is_privately_uninhabited(param_env.and(ty))
446-
{
445+
let abi = if count != 0 && ty.is_privately_uninhabited(tcx, param_env) {
447446
Abi::Uninhabited
448447
} else {
449448
Abi::Aggregate { sized: true }

compiler/rustc_ty_utils/src/layout_sanity_check.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ pub(super) fn sanity_check_layout<'tcx>(
1212
layout: &TyAndLayout<'tcx>,
1313
) {
1414
// Type-level uninhabitedness should always imply ABI uninhabitedness.
15-
if cx.tcx.conservative_is_privately_uninhabited(cx.param_env.and(layout.ty)) {
15+
if layout.ty.is_privately_uninhabited(cx.tcx, cx.param_env) {
1616
assert!(layout.abi.is_uninhabited());
1717
}
1818

compiler/rustc_ty_utils/src/ty.rs

-57
Original file line numberDiff line numberDiff line change
@@ -416,62 +416,6 @@ fn asyncness(tcx: TyCtxt<'_>, def_id: DefId) -> hir::IsAsync {
416416
node.fn_sig().map_or(hir::IsAsync::NotAsync, |sig| sig.header.asyncness)
417417
}
418418

419-
/// Don't call this directly: use ``tcx.conservative_is_privately_uninhabited`` instead.
420-
pub fn conservative_is_privately_uninhabited_raw<'tcx>(
421-
tcx: TyCtxt<'tcx>,
422-
param_env_and: ty::ParamEnvAnd<'tcx, Ty<'tcx>>,
423-
) -> bool {
424-
let (param_env, ty) = param_env_and.into_parts();
425-
match ty.kind() {
426-
ty::Never => {
427-
debug!("ty::Never =>");
428-
true
429-
}
430-
ty::Adt(def, _) if def.is_union() => {
431-
debug!("ty::Adt(def, _) if def.is_union() =>");
432-
// For now, `union`s are never considered uninhabited.
433-
false
434-
}
435-
ty::Adt(def, substs) => {
436-
debug!("ty::Adt(def, _) if def.is_not_union() =>");
437-
// Any ADT is uninhabited if either:
438-
// (a) It has no variants (i.e. an empty `enum`);
439-
// (b) Each of its variants (a single one in the case of a `struct`) has at least
440-
// one uninhabited field.
441-
def.variants().iter().all(|var| {
442-
var.fields.iter().any(|field| {
443-
let ty = tcx.bound_type_of(field.did).subst(tcx, substs);
444-
tcx.conservative_is_privately_uninhabited(param_env.and(ty))
445-
})
446-
})
447-
}
448-
ty::Tuple(fields) => {
449-
debug!("ty::Tuple(..) =>");
450-
fields.iter().any(|ty| tcx.conservative_is_privately_uninhabited(param_env.and(ty)))
451-
}
452-
ty::Array(ty, len) => {
453-
debug!("ty::Array(ty, len) =>");
454-
match len.try_eval_usize(tcx, param_env) {
455-
Some(0) | None => false,
456-
// If the array is definitely non-empty, it's uninhabited if
457-
// the type of its elements is uninhabited.
458-
Some(1..) => tcx.conservative_is_privately_uninhabited(param_env.and(*ty)),
459-
}
460-
}
461-
ty::Ref(..) => {
462-
debug!("ty::Ref(..) =>");
463-
// References to uninitialised memory is valid for any type, including
464-
// uninhabited types, in unsafe code, so we treat all references as
465-
// inhabited.
466-
false
467-
}
468-
_ => {
469-
debug!("_ =>");
470-
false
471-
}
472-
}
473-
}
474-
475419
pub fn provide(providers: &mut ty::query::Providers) {
476420
*providers = ty::query::Providers {
477421
asyncness,
@@ -481,7 +425,6 @@ pub fn provide(providers: &mut ty::query::Providers) {
481425
instance_def_size_estimate,
482426
issue33140_self_ty,
483427
impl_defaultness,
484-
conservative_is_privately_uninhabited: conservative_is_privately_uninhabited_raw,
485428
..*providers
486429
};
487430
}
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#![feature(generic_const_exprs)]
33
#![allow(incomplete_features)]
44

5-
// This tests that the `conservative_is_privately_uninhabited` fn doesn't cause
5+
// This tests that the inhabited check doesn't cause
66
// ICEs by trying to evaluate `T::ASSOC` with an incorrect `ParamEnv`.
77

88
trait Foo {
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#![feature(generic_const_exprs)]
33
#![allow(incomplete_features)]
44

5-
// This tests that the `conservative_is_privately_uninhabited` fn doesn't cause
5+
// This tests that the inhabited check doesn't cause
66
// ICEs by trying to evaluate `T::ASSOC` with an incorrect `ParamEnv`.
77

88
trait Foo {

0 commit comments

Comments
 (0)