Constant evaluation is the process of computing values at compile time. For a specific item (constant/static/array length) this happens after the MIR for the item is borrow-checked and optimized. In many cases trying to const evaluate an item will trigger the computation of its MIR for the first time.
Prominent examples are:
- The initializer of a
- Array length
- needs to be known to reserve stack or heap space
- Enum variant discriminants
- needs to be known to prevent two variants from having the same discriminant
- need to be known to check for overlapping patterns
Additionally constant evaluation can be used to reduce the workload or binary size at runtime by precomputing complex operations at compiletime and only storing the result.
Constant evaluation can be done by calling the
const_eval_* functions of
They're the wrappers of the
const_eval_* functions use a
ParamEnv of environment
in which the constant is evaluated (e.g. the function within which the constant is used)
GlobalId is made up of an
Instance referring to a constant
or static or of an
Instance of a function and an index into the function's
Constant evaluation returns a
EvalToConstValueResult with either the error, or a
representation of the constant.
static initializers are always represented as
miri virtual memory allocations (via
Other constants get represented as
ConstValue::Slice if possible. This means that the
functions cannot be used to create miri-pointers to the evaluated constant.
If you need the value of a constant inside Miri, you need to directly work with