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
- Remove the `size` option from the unicode segment.
- Validate string length before constructing the bit array if a fixed-width string is required.
- 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
- Never write `size(...)` on utf8/utf16/utf32 segments.
- Validate string lengths in application code if fixed-width strings are needed.
- Encode to bytes manually via `gleam/bit_array` when exact byte control is required.
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
- ErrorType::SignednessUsedOnNonInt
- ErrorType::TypeDoesNotAllowUnit
- ErrorType::ConstantSizeNotPositive
- ErrorType::FloatWithSize
- ErrorType::UnitMustHaveSize
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)