gleam-lang/gleam · error · ErrorType

ErrorType::TypeDoesNotAllowSize

Error message

ErrorType::TypeDoesNotAllowSize { type_: type_.label() }

What it means

Unicode string segments (`utf8`, `utf16`, `utf32` and `_codepoint` variants) cannot have an explicit numeric `size` option. When `type_options` sees a segment whose type passes `is_unicode` and that carries a `size` option, it raises this error at the size option's location, reporting the type's label.

Solutions

  1. Remove the `size` option from the unicode segment.
  2. Validate string length before constructing the bit array if a fixed-width string is required.
  3. Use a non-unicode bytes segment with explicit encoding via `gleam/bit_array` when exact sizing is needed.

Example fix

// before
let a = <<name:utf8-size(10)>>
// after
let a = <<name:utf8>>
Defensive patterns

Strategy: validation

Validate before calling

// Unicode segments take no explicit size option.
fn unicode_size_forbidden(t: String, has_size: Bool) -> Bool {
  let is_unicode = ["Utf8", "Utf16", "Utf32"] |> list.contains(t)
  is_unicode && has_size
}

Prevention

When it happens

Trigger: Writing `<<name:utf8-size(10)>>` or `<<s:utf16-size(2)>>` — any unicode-typed segment (expression or pattern) specifying `size(N)` or a size expression.

Common situations: Assuming utf8 segments are byte-sized like in other languages; copying size options from binary/integer segments onto string segments.

Related errors


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

Appendix: source

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

        SegmentOptionCategories {
            type_: Some(type_),
            unit: Some(unit),
            ..
        } if is_unicode(type_) => {
            return err(
                ErrorType::TypeDoesNotAllowUnit {
                    type_: type_.label(),
                },
                unit.location(),
            );
        }

        SegmentOptionCategories {
            type_: Some(type_),
            size: Some(size),
            ..
        } if is_unicode(type_) => {
            return err(
                ErrorType::TypeDoesNotAllowSize {
                    type_: type_.label(),
                },
                size.location(),
            );
        }

        _ => {}
    }

    // if unit specified, size must be specified
    if let SegmentOptionCategories {
        unit: Some(unit),
        size: None,
        ..
    } = categories
    {
        return err(ErrorType::UnitMustHaveSize, unit.location());

View on GitHub (pinned to 15b07c7830)