rust-lang/rust · error · B::Error

Value overflow: cannot convert

Error message

Value overflow: cannot convert `{value}` to `{ty_internal}`.

What it means

Thrown by `try_new_const_uint` (impls.rs:531) when a `u128` value does not fit into the target integer type's bit width. `ScalarInt::try_from_uint(value, size)` returns `None` because the value exceeds the type's scalar size (e.g. 256 into `u8`).

Solutions

  1. Mask the value to the target type's bit width before calling: `value & ((1 << bits) - 1)`.
  2. Validate `value <= max_of_type(ty)` and reject/truncate oversized inputs upstream.
  3. Use a wider target type that actually fits the value.

Example fix

// before
let c = ctxt.try_new_const_uint(256, u8_type)?;

// after
let bits = layout_of(u8_type).size.bits() as u128;
let c = ctxt.try_new_const_uint(256 & ((1 << bits) - 1), u8_type)?;
Defensive patterns

Strategy: validation

Validate before calling

// Verify the value fits the target integer type before constructing the constant.
let bits = tcx.layout_of(env.as_query_input(ty))?.size.bits() as u128;
let max = if bits >= 128 { u128::MAX } else { (1u128 << bits) - 1 };
if value > max {
    return Err(format!("value {value} exceeds {ty}"));
}

Type guard

fn fits_uint_type<'tcx>(value: u128, ty: Ty<'tcx>, tcx: TyCtxt<'tcx>, env: TypingEnv<'tcx>) -> bool {
    let bits = tcx.layout_of(env.as_query_input(ty)).map(|l| l.size.bits() as u128).unwrap_or(0);
    let max = if bits >= 128 { u128::MAX } else { (1u128 << bits) - 1 };
    value <= max
}

Try / catch

let scalar = ScalarInt::try_from_uint(value, size)
    .ok_or_else(|| /* truncate or report */ ScalarInt::try_from_uint(value & mask, size).unwrap())?;

Prevention

When it happens

Trigger: Calling `try_new_const_uint(value, ty)` where `value` requires more bits than `ty` provides — e.g. `300` into `u8`, or `1 << 40` into `u32`.

Common situations: Generating MIR constants from untrusted/checked integer inputs; building enum discriminant constants whose computed value exceeds the repr type; porting code that assumed a wider type.

Related errors


AI-assisted analysis of rust-lang/rust@7088e4b63a (2026-08-10). Data as JSON: /api/errors/fbc2c2bdbe79e597. Report an issue: GitHub.

Appendix: source

Thrown at compiler/rustc_public_bridge/src/context/impls.rs:531

    }

    /// Create a new constant that represents the given boolean value.
    pub fn new_const_bool(&self, value: bool) -> MirConst<'tcx> {
        MirConst::from_bool(self.tcx, value)
    }

    pub fn try_new_const_uint(
        &self,
        value: u128,
        ty_internal: Ty<'tcx>,
    ) -> Result<MirConst<'tcx>, B::Error> {
        let size = self
            .tcx
            .layout_of(self.fully_monomorphized().as_query_input(ty_internal))
            .unwrap()
            .size;
        let scalar = ScalarInt::try_from_uint(value, size).ok_or_else(|| {
            B::Error::new(format!("Value overflow: cannot convert `{value}` to `{ty_internal}`."))
        })?;
        Ok(self.mir_const_from_scalar(Scalar::Int(scalar), ty_internal))
    }

    pub fn try_new_ty_const_uint(
        &self,
        value: u128,
        ty_internal: Ty<'tcx>,
    ) -> Result<ty::Const<'tcx>, B::Error> {
        let size = self
            .tcx
            .layout_of(self.fully_monomorphized().as_query_input(ty_internal))
            .unwrap()
            .size;
        let scalar = ScalarInt::try_from_uint(value, size).ok_or_else(|| {
            B::Error::new(format!("Value overflow: cannot convert `{value}` to `{ty_internal}`."))
        })?;

View on GitHub (pinned to 7088e4b63a)