gleam-lang/gleam · error · ErrorType

ErrorType::SegmentMustHaveSize

Error message

ErrorType::SegmentMustHaveSize

What it means

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.

Solutions

  1. Add a size option (or a bound variable via size:) to every non-final bytes/bits segment
  2. Reorder the pattern so the unsized bytes/bits segment comes last
  3. Use a fixed-width type like int-size(n) for intermediate fields

Example fix

// before
let assert <<header:bytes, flag:int>> = data
// after
let assert <<header:bytes-size(4), flag:int>> = data
Defensive patterns

Strategy: validation

Validate before calling

// In patterns, every non-final segment needs an exact size
fn unsized_nonfinal_segment(pattern: List(#(String, Int))) -> Bool {
  let segments = list.drop_last(pattern)
  list.any(segments, fn(seg) {
    let #(type_str, size) = seg
    { type_str == "bytes" || type_str == "bits" } && size < 0
  })
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: "Must pass :limit option" / "Missing required option" — required option errors explained — this error's family across 41 libraries.

Related errors


AI-assisted analysis of gleam-lang/gleam@15b07c7830 (2026-09-14). Data as JSON: /api/errors/b1c90c80937611d9. Report an issue: GitHub.

Appendix: source

Thrown at compiler-core/src/bit_array.rs:222

                signed: Some(opt), ..
            }
            | SegmentOptionCategories {
                type_: Some(opt @ Bytes { .. }),
                ..
            } => return err(ErrorType::OptionNotAllowedInValue, opt.location()),
            _ => (),
        }
    }

    // All but the last segment in a pattern must have an exact size
    if must_have_size
        && let SegmentOptionCategories {
            type_: Some(opt @ (Bytes { .. } | Bits { .. })),
            size: None,
            ..
        } = categories
    {
        return err(ErrorType::SegmentMustHaveSize, opt.location());
    }

    // Endianness is only valid for int, utf16, utf16_codepoint, utf32,
    // utf32_codepoint and float
    match categories {
        SegmentOptionCategories {
            type_:
                None
                | Some(
                    Int { .. }
                    | Utf16 { .. }
                    | Utf32 { .. }
                    | Utf16Codepoint { .. }
                    | Utf32Codepoint { .. }
                    | Float { .. },
                ),
            ..
        } => {}

View on GitHub (pinned to 15b07c7830)