{"record":{"id":"fbc2c2bdbe79e597","repo":"rust-lang/rust","slug":"value-overflow-cannot-convert-value-to-ty-i","errorCode":null,"errorMessage":"Value overflow: cannot convert `{value}` to `{ty_internal}`.","messagePattern":"Value overflow: cannot convert `(.+?)` to `(.+?)`\\.","errorType":"exception","errorClass":"B::Error","httpStatus":null,"severity":"error","filePath":"compiler/rustc_public_bridge/src/context/impls.rs","lineNumber":531,"sourceCode":"    }\n\n    /// Create a new constant that represents the given boolean value.\n    pub fn new_const_bool(&self, value: bool) -> MirConst<'tcx> {\n        MirConst::from_bool(self.tcx, value)\n    }\n\n    pub fn try_new_const_uint(\n        &self,\n        value: u128,\n        ty_internal: Ty<'tcx>,\n    ) -> Result<MirConst<'tcx>, B::Error> {\n        let size = self\n            .tcx\n            .layout_of(self.fully_monomorphized().as_query_input(ty_internal))\n            .unwrap()\n            .size;\n        let scalar = ScalarInt::try_from_uint(value, size).ok_or_else(|| {\n            B::Error::new(format!(\"Value overflow: cannot convert `{value}` to `{ty_internal}`.\"))\n        })?;\n        Ok(self.mir_const_from_scalar(Scalar::Int(scalar), ty_internal))\n    }\n\n    pub fn try_new_ty_const_uint(\n        &self,\n        value: u128,\n        ty_internal: Ty<'tcx>,\n    ) -> Result<ty::Const<'tcx>, B::Error> {\n        let size = self\n            .tcx\n            .layout_of(self.fully_monomorphized().as_query_input(ty_internal))\n            .unwrap()\n            .size;\n        let scalar = ScalarInt::try_from_uint(value, size).ok_or_else(|| {\n            B::Error::new(format!(\"Value overflow: cannot convert `{value}` to `{ty_internal}`.\"))\n        })?;\n","sourceCodeStart":513,"sourceCodeEnd":549,"githubUrl":"https://github.com/rust-lang/rust/blob/7088e4b63a9516ebfbfe2ab2d999cf01a528ac14/compiler/rustc_public_bridge/src/context/impls.rs#L513-L549","documentation":"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`).","triggerScenarios":"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`.","commonSituations":"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.","solutions":["Mask the value to the target type's bit width before calling: `value & ((1 << bits) - 1)`.","Validate `value <= max_of_type(ty)` and reject/truncate oversized inputs upstream.","Use a wider target type that actually fits the value."],"exampleFix":"// before\nlet c = ctxt.try_new_const_uint(256, u8_type)?;\n\n// after\nlet bits = layout_of(u8_type).size.bits() as u128;\nlet c = ctxt.try_new_const_uint(256 & ((1 << bits) - 1), u8_type)?;","handlingStrategy":"validation","validationCode":"// Verify the value fits the target integer type before constructing the constant.\nlet bits = tcx.layout_of(env.as_query_input(ty))?.size.bits() as u128;\nlet max = if bits >= 128 { u128::MAX } else { (1u128 << bits) - 1 };\nif value > max {\n    return Err(format!(\"value {value} exceeds {ty}\"));\n}","typeGuard":"fn fits_uint_type<'tcx>(value: u128, ty: Ty<'tcx>, tcx: TyCtxt<'tcx>, env: TypingEnv<'tcx>) -> bool {\n    let bits = tcx.layout_of(env.as_query_input(ty)).map(|l| l.size.bits() as u128).unwrap_or(0);\n    let max = if bits >= 128 { u128::MAX } else { (1u128 << bits) - 1 };\n    value <= max\n}","tryCatchPattern":"let scalar = ScalarInt::try_from_uint(value, size)\n    .ok_or_else(|| /* truncate or report */ ScalarInt::try_from_uint(value & mask, size).unwrap())?;","preventionTips":["Range-check integer values against the target type width before const construction.","Prefer explicit truncation/masking for generated code rather than relying on the error path."],"tags":["rustc-public-bridge","const-eval","scalar","integer-overflow"],"backgroundTag":null,"analyzedSha":"7088e4b63a9516ebfbfe2ab2d999cf01a528ac14","analyzedAt":"2026-08-10T14:17:03.603Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}