trivial_const

Function trivial_const 

Source
pub(crate) fn trivial_const<'a, 'tcx: 'a, F, B>(
    tcx: TyCtxt<'tcx>,
    def: LocalDefId,
    body_provider: F,
) -> Option<(ConstValue, Ty<'tcx>)>
where F: FnOnce() -> B, B: Deref<Target = Body<'tcx>>,
Expand description

If the given def is a trivial const, returns the value and type the const evaluates to.

A “trivial const” is a const which can be easily proven to evaluate successfully, and the value that it evaluates to can be easily found without going through the usual MIR phases for a const.

Currently the only form of trivial const that is supported is this:

const A: usize = 0;

which has this MIR:

const A: usize = {
    let mut _0: usize;

    bb0: {
        _0 = const 0_usize;
        return;
    }
}

Which we recognize by looking for a Body which has a single basic block with a return terminator and a single statement which assigns an Operand::Constant(Const::Val) to the return place. This scenario meets the required criteria because:

  • Control flow cannot panic, we don’t have any calls or assert terminators
  • The value of the const is already computed, so it cannot fail