{"record":{"id":"7a610342e0a674fc","repo":"gleam-lang/gleam","slug":"bit-array-segment-size-to-be-a-valid-u32","errorCode":null,"errorMessage":"bit array segment size to be a valid u32","messagePattern":"bit array segment size to be a valid u32","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"compiler-core/src/javascript.rs","lineNumber":1641,"sourceCode":"///\npub(crate) const SAFE_INT_SEGMENT_MAX_SIZE: usize = 48;\n\n/// Evaluates the value of an Int segment in a bit array into its corresponding bytes. This avoids\n/// needing to do the evaluation at runtime when all inputs are known at compile-time.\n///\npub(crate) fn bit_array_segment_int_value_to_bytes(\n    mut value: BigInt,\n    size: BigInt,\n    endianness: Endianness,\n) -> Vec<u8> {\n    // Clamp negative sizes to zero\n    let size = size.max(BigInt::ZERO);\n\n    // Convert size to u32. This is safe because this function isn't called with a size greater\n    // than `SAFE_INT_SEGMENT_MAX_SIZE`.\n    let size = size\n        .to_u32()\n        .expect(\"bit array segment size to be a valid u32\");\n\n    // Convert negative number to two's complement representation\n    if value < BigInt::ZERO {\n        let value_modulus = BigInt::from(2).pow(size);\n        value = &value_modulus + (value % &value_modulus);\n    }\n\n    // Convert value to the desired number of bytes\n    let mut bytes = vec![0u8; size as usize / 8];\n    for byte in bytes.iter_mut() {\n        *byte = (&value % BigInt::from(256))\n            .to_u8()\n            .expect(\"modulo result to be a valid u32\");\n        value /= BigInt::from(256);\n    }\n\n    if endianness.is_big() {\n        bytes.reverse();","sourceCodeStart":1623,"sourceCodeEnd":1659,"githubUrl":"https://github.com/gleam-lang/gleam/blob/7e623aa83da3776faee50ca4ab9a6c40124acd95/compiler-core/src/javascript.rs#L1623-L1659","documentation":"Error \"bit array segment size to be a valid u32\" thrown in gleam-lang/gleam.","triggerScenarios":"Thrown at compiler-core/src/javascript.rs:1641 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":[],"exampleFix":null,"handlingStrategy":null,"validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"7e623aa83da3776faee50ca4ab9a6c40124acd95","analyzedAt":"2026-08-17T00:07:02.091Z","schemaVersion":2},"datasetVersion":"2026-08-17T04:17:16.089Z"}