{"record":{"id":"b1c90c80937611d9","repo":"gleam-lang/gleam","slug":"errortype-segmentmusthavesize","errorCode":null,"errorMessage":"ErrorType::SegmentMustHaveSize","messagePattern":"ErrorType::SegmentMustHaveSize","errorType":"validation","errorClass":"ErrorType","httpStatus":null,"severity":"error","filePath":"compiler-core/src/bit_array.rs","lineNumber":222,"sourceCode":"                signed: Some(opt), ..\n            }\n            | SegmentOptionCategories {\n                type_: Some(opt @ Bytes { .. }),\n                ..\n            } => return err(ErrorType::OptionNotAllowedInValue, opt.location()),\n            _ => (),\n        }\n    }\n\n    // All but the last segment in a pattern must have an exact size\n    if must_have_size\n        && let SegmentOptionCategories {\n            type_: Some(opt @ (Bytes { .. } | Bits { .. })),\n            size: None,\n            ..\n        } = categories\n    {\n        return err(ErrorType::SegmentMustHaveSize, opt.location());\n    }\n\n    // Endianness is only valid for int, utf16, utf16_codepoint, utf32,\n    // utf32_codepoint and float\n    match categories {\n        SegmentOptionCategories {\n            type_:\n                None\n                | Some(\n                    Int { .. }\n                    | Utf16 { .. }\n                    | Utf32 { .. }\n                    | Utf16Codepoint { .. }\n                    | Utf32Codepoint { .. }\n                    | Float { .. },\n                ),\n            ..\n        } => {}","sourceCodeStart":204,"sourceCodeEnd":240,"githubUrl":"https://github.com/gleam-lang/gleam/blob/15b07c783065c92bb016b1b74ed0995a5259850f/compiler-core/src/bit_array.rs#L204-L240","documentation":"ErrorType::SegmentMustHaveSize: in a bit array PATTERN, all segments except the last must have an exact size so the compiler knows where subsequent segments start. When the segment's type is bytes or bits and no size option was given, this error is reported at the type option's location.","triggerScenarios":"A non-final pattern segment typed bytes or bits without a size option, e.g. <<a:bytes, b:int>>; must_have_size is set because the segment is not the last one in the pattern.","commonSituations":"Parsing protocols where an intermediate variable-length field precedes other fields; converting an expression (where unsized bytes is fine) into a pattern without adding sizes.","solutions":["Add a size option (or a bound variable via size:) to every non-final bytes/bits segment","Reorder the pattern so the unsized bytes/bits segment comes last","Use a fixed-width type like int-size(n) for intermediate fields"],"exampleFix":"// before\nlet assert <<header:bytes, flag:int>> = data\n// after\nlet assert <<header:bytes-size(4), flag:int>> = data","handlingStrategy":"validation","validationCode":"// In patterns, every non-final segment needs an exact size\nfn unsized_nonfinal_segment(pattern: List(#(String, Int))) -> Bool {\n  let segments = list.drop_last(pattern)\n  list.any(segments, fn(seg) {\n    let #(type_str, size) = seg\n    { type_str == \"bytes\" || type_str == \"bits\" } && size < 0\n  })\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Rule of thumb: only the last pattern segment may be unsized","Give every intermediate field an explicit size or bind its length with size:var","Recheck patterns after converting them from value expressions"],"tags":["gleam","bit-array","pattern-matching"],"backgroundTag":"missing-required-option","analyzedSha":"15b07c783065c92bb016b1b74ed0995a5259850f","analyzedAt":"2026-09-14T11:14:59.388Z","contentChangedAt":"2026-09-14T11:14:59.388Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}