1
Fork 0

Factor out conservative_is_privately_uninhabited

This commit is contained in:
Cameron Steffen 2022-10-23 17:32:40 -05:00
parent 34cbe72780
commit cc8dddbac9
11 changed files with 25 additions and 81 deletions

View file

@ -442,8 +442,7 @@ fn layout_of_uncached<'tcx>(
let element = cx.layout_of(element)?;
let size = element.size.checked_mul(count, dl).ok_or(LayoutError::SizeOverflow(ty))?;
let abi = if count != 0 && tcx.conservative_is_privately_uninhabited(param_env.and(ty))
{
let abi = if count != 0 && ty.is_privately_uninhabited(tcx, param_env) {
Abi::Uninhabited
} else {
Abi::Aggregate { sized: true }

View file

@ -12,7 +12,7 @@ pub(super) fn sanity_check_layout<'tcx>(
layout: &TyAndLayout<'tcx>,
) {
// Type-level uninhabitedness should always imply ABI uninhabitedness.
if cx.tcx.conservative_is_privately_uninhabited(cx.param_env.and(layout.ty)) {
if layout.ty.is_privately_uninhabited(cx.tcx, cx.param_env) {
assert!(layout.abi.is_uninhabited());
}

View file

@ -416,62 +416,6 @@ fn asyncness(tcx: TyCtxt<'_>, def_id: DefId) -> hir::IsAsync {
node.fn_sig().map_or(hir::IsAsync::NotAsync, |sig| sig.header.asyncness)
}
/// Don't call this directly: use ``tcx.conservative_is_privately_uninhabited`` instead.
pub fn conservative_is_privately_uninhabited_raw<'tcx>(
tcx: TyCtxt<'tcx>,
param_env_and: ty::ParamEnvAnd<'tcx, Ty<'tcx>>,
) -> bool {
let (param_env, ty) = param_env_and.into_parts();
match ty.kind() {
ty::Never => {
debug!("ty::Never =>");
true
}
ty::Adt(def, _) if def.is_union() => {
debug!("ty::Adt(def, _) if def.is_union() =>");
// For now, `union`s are never considered uninhabited.
false
}
ty::Adt(def, substs) => {
debug!("ty::Adt(def, _) if def.is_not_union() =>");
// Any ADT is uninhabited if either:
// (a) It has no variants (i.e. an empty `enum`);
// (b) Each of its variants (a single one in the case of a `struct`) has at least
// one uninhabited field.
def.variants().iter().all(|var| {
var.fields.iter().any(|field| {
let ty = tcx.bound_type_of(field.did).subst(tcx, substs);
tcx.conservative_is_privately_uninhabited(param_env.and(ty))
})
})
}
ty::Tuple(fields) => {
debug!("ty::Tuple(..) =>");
fields.iter().any(|ty| tcx.conservative_is_privately_uninhabited(param_env.and(ty)))
}
ty::Array(ty, len) => {
debug!("ty::Array(ty, len) =>");
match len.try_eval_usize(tcx, param_env) {
Some(0) | None => false,
// If the array is definitely non-empty, it's uninhabited if
// the type of its elements is uninhabited.
Some(1..) => tcx.conservative_is_privately_uninhabited(param_env.and(*ty)),
}
}
ty::Ref(..) => {
debug!("ty::Ref(..) =>");
// References to uninitialised memory is valid for any type, including
// uninhabited types, in unsafe code, so we treat all references as
// inhabited.
false
}
_ => {
debug!("_ =>");
false
}
}
}
pub fn provide(providers: &mut ty::query::Providers) {
*providers = ty::query::Providers {
asyncness,
@ -481,7 +425,6 @@ pub fn provide(providers: &mut ty::query::Providers) {
instance_def_size_estimate,
issue33140_self_ty,
impl_defaultness,
conservative_is_privately_uninhabited: conservative_is_privately_uninhabited_raw,
..*providers
};
}