rust-lang/rust · error · B::Error
Const ` ` cannot be encoded as u64
Error message
Const `{cnst:?}` cannot be encoded as u64 What it means
Thrown by `eval_target_usize` (impls.rs:462) when a `MirConst` cannot be evaluated to a concrete target `usize` (u64). `try_eval_target_usize` returns `None` when the constant is unevaluated, too generic, or not an integer-like scalar.
Solutions
- Confirm the constant is fully monomorphized and has been evaluated before requesting a usize.
- If the value may legitimately be non-integer, branch on the const's kind and only call `eval_target_usize` for scalar integer consts.
- Use `try_eval_target_usize` directly and handle `None` instead of erroring.
Example fix
// before
let n: u64 = ctxt.eval_target_usize(cnst)?;
// after
let n: u64 = match cnst.try_eval_target_usize(ctxt.tcx, typing_env) {
Some(v) => v,
None => return Ok(default_len), // constant is unevaluated/generic
}; Defensive patterns
Strategy: try-catch
Validate before calling
// Only request usize encoding for constants that actually evaluate to a scalar.
let opt = cnst.try_eval_target_usize(tcx, typing_env);
if opt.is_none() {
return Ok(0); // or skip this constant
} Type guard
fn is_evaluable_usize<'tcx>(cnst: &MirConst<'tcx>, tcx: TyCtxt<'tcx>, env: TypingEnv<'tcx>) -> bool {
cnst.try_eval_target_usize(tcx, env).is_some()
} Try / catch
let v = ctxt.eval_target_usize(cnst).unwrap_or_else(|_| {
// constant could not be encoded; use a fallback or propagate
0
}); Prevention
- Evaluate/normalize constants before assuming they are concrete integers.
- Prefer the underlying try_eval_* helpers that return Option when None is a normal case.
When it happens
Trigger: Calling `eval_target_usize` on a MIR constant that is `Unevaluated`, depends on generic params (TooGeneric), or holds a non-scalar/aggregate value that cannot be read as a usize.
Common situations: Reading array lengths, enum discriminants, or padding from generic MIR during analysis; evaluating constants from upstream crates whose bodies are not available.
Related errors
- Cannot create a zero-sized constant for type
- Cannot create a zero-sized constant for type
- expected monomorphic const in codegen
- {self:?}
- Value overflow: cannot convert
AI-assisted analysis of rust-lang/rust@7088e4b63a (2026-08-10).
Data as JSON: /api/errors/3f5feb9fade3ae3c.
Report an issue: GitHub.
Appendix: source
Thrown at compiler/rustc_public_bridge/src/context/impls.rs:462
pub fn coroutine_discr_for_variant(
&self,
coroutine: DefId,
args: GenericArgsRef<'tcx>,
variant: rustc_abi::VariantIdx,
) -> Discr<'tcx> {
args.as_coroutine().discriminant_for_variant(coroutine, self.tcx, variant)
}
/// The name of a variant.
pub fn variant_name(&self, def: &'tcx VariantDef) -> String {
def.name.to_string()
}
/// Evaluate constant as a target usize.
pub fn eval_target_usize(&self, cnst: MirConst<'tcx>) -> Result<u64, B::Error> {
use crate::context::TypingEnvHelpers;
cnst.try_eval_target_usize(self.tcx, self.fully_monomorphized())
.ok_or_else(|| B::Error::new(format!("Const `{cnst:?}` cannot be encoded as u64")))
}
pub fn eval_target_usize_ty(&self, cnst: ty::Const<'tcx>) -> Result<u64, B::Error> {
cnst.try_to_target_usize(self.tcx)
.ok_or_else(|| B::Error::new(format!("Const `{cnst:?}` cannot be encoded as u64")))
}
pub fn try_new_const_zst(&self, ty_internal: Ty<'tcx>) -> Result<MirConst<'tcx>, B::Error> {
let size = self
.tcx
.layout_of(self.fully_monomorphized().as_query_input(ty_internal))
.map_err(|err| {
B::Error::new(format!(
"Cannot create a zero-sized constant for type `{ty_internal}`: {err}"
))
})?
.size;
if size.bytes() != 0 {View on GitHub (pinned to 7088e4b63a)