gleam-lang/gleam · error

invalid segment

Error message

invalid segment

What it means

An internal compiler assertion in bit-array size codegen: a BitArraySize::Variable with no resolvable constant literal resolved to a ModuleFn or Record value constructor instead of a LocalVariable or ModuleConstant. Bit-array segment sizes can only be integers, local variables, or constants, so this resolution is invalid and should have been caught by the type checker.

Solutions

  1. Use an integer variable, literal, or Int constant as the segment size instead.
  2. Update gleam to get the proper type-checker diagnostic for this mistake.
  3. Report the minimal reproducer to github.com/gleam-lang/gleam.

Example fix

// before
fn two() { 2 }
let assert <<a:size(two)>> = bits
// after
let assert <<a:size(2)>> = bits
Defensive patterns

Strategy: type-guard

Validate before calling

// Verify the size name refers to an Int value, not a function or type
const n: Int = 4
let assert <<a:size(n)>> = bits

Type guard

fn valid_size_value(v: &ValueConstructor) -> bool {
    matches!(v.variant,
        ValueConstructorVariant::LocalVariable { .. } |
        ValueConstructorVariant::ModuleConstant { .. })
}

Try / catch

let status = Command::new("gleam").args(["build"]).status()?;
if !status.success() { /* handle failure */ }

Prevention

When it happens

Trigger: Compiling a `<<..:size(name)>>` pattern where `name` refers to a function or type constructor rather than an int value/constant, and the type checker failed to reject it.

Common situations: Mistakenly using a function name or record constructor as a bit-array segment size; reveals a type-checker hole rather than user-facing behavior.

Understand the failure class

Background: "This is a bug, please report it": internal invariant violations, unreachable panics, and SNH errors explained — this error's family across 47 libraries.

Related errors


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

Appendix: source

Thrown at compiler-core/src/erlang/pattern.rs:325

            BitArraySize::Variable {
                constructor, name, ..
            } => match self.variables_to_add_later.get(name) {
                Some(AliasedLiteral::Int { value, location }) => {
                    builder.int_expression(*location, value.clone())
                }
                Some(_) => panic!("segment size that is not int made it through type checking"),
                None => {
                    let constructor = constructor.as_ref().expect("variable with no constructor");
                    match &constructor.variant {
                        ValueConstructorVariant::ModuleConstant { literal, .. } => {
                            self.generator.inlined_constant(builder, literal);
                        }
                        ValueConstructorVariant::LocalVariable { location, .. } => {
                            builder.variable(*location, &self.generator.local_var_name(location));
                        }
                        ValueConstructorVariant::ModuleFn { .. }
                        | ValueConstructorVariant::Record { .. } => panic!("invalid segment"),
                    }
                }
            },

            BitArraySize::BinaryOperator {
                operator,
                left,
                right,
                location,
            } => {
                let operator = match operator {
                    IntOperator::Add => "+",
                    IntOperator::Subtract => "-",
                    IntOperator::Multiply => "*",
                    IntOperator::Divide => {
                        return self.bit_array_size_divide(location, builder, left, right, "div");
                    }
                    IntOperator::Remainder => {

View on GitHub (pinned to 15b07c7830)