Skip to main content

Module inhabitedness

Module inhabitedness 

Source
Expand description

This module contains logic for determining whether a type is inhabited or uninhabited. The InhabitedPredicate type captures the minimum information needed to determine whether a type is inhabited given a ParamEnv and module ID.

ยงExample

mod a {
    pub mod b {
        pub struct SecretlyUninhabited {
            _priv: !,
        }
    }
}

mod c {
    enum Void {}
    pub struct AlsoSecretlyUninhabited {
        _priv: Void,
    }
    mod d {
    }
}

struct Foo {
    x: a::b::SecretlyUninhabited,
    y: c::AlsoSecretlyUninhabited,
}

In this code, the type Foo will only be visibly uninhabited inside the modules b, c and d. Calling inhabited_predicate on Foo will return NotInModule(b) AND NotInModule(c).

We need this information for pattern-matching on Foo or types that contain Foo.

ยงExample

โ“˜
let foo_result: Result<T, Foo> = ... ;
let Ok(t) = foo_result;

This code should only compile in modules where the uninhabitedness of Foo is visible.

Re-exportsยง

use rustc_data_structures::fx::FxHashSet; ๐Ÿ”’
use rustc_hir::def::DefKind; ๐Ÿ”’
use rustc_span::bug; ๐Ÿ”’
use rustc_span::def_id::LocalModId; ๐Ÿ”’
use tracing::instrument; ๐Ÿ”’
use tracing::instrument; ๐Ÿ”’
use crate::query::Providers; ๐Ÿ”’
use crate::ty; ๐Ÿ”’
use crate::ty::AdtDef; ๐Ÿ”’
use crate::ty::DefId; ๐Ÿ”’
use crate::ty::Ty; ๐Ÿ”’
use crate::ty::TyCtxt; ๐Ÿ”’
use crate::ty::TypeVisitableExt; ๐Ÿ”’
use crate::ty::TypingEnv; ๐Ÿ”’
use crate::ty::VariantDef; ๐Ÿ”’
use crate::ty::Visibility; ๐Ÿ”’
pub use inhabited_predicate::InhabitedPredicate;
use rustc_type_ir::TyKind::*; ๐Ÿ”’

Modulesยง

inhabited_predicate

Structsยง

OpsemInhabitedCtx ๐Ÿ”’
Context for computing whether a type is inhabited on the opsem level. See is_opsem_inhabited above for the spec of what we compute.

Functionsยง

inhabited_predicate_for_def ๐Ÿ”’
Returns an InhabitedPredicate that is generic over type parameters and requires calling InhabitedPredicate::instantiate
inhabited_predicate_type ๐Ÿ”’
N.B. this query should only be called through Ty::inhabited_predicate
is_opsem_inhabited_adt_cached ๐Ÿ”’
provide ๐Ÿ”’
variant_inhabited_predicate ๐Ÿ”’