gleam-lang/gleam · error

invalid pattern inside aliased bit array pattern segment

Error message

invalid pattern inside aliased bit array pattern segment

What it means

An internal compiler assertion raised while generating code for an aliased bit-array pattern segment (`<<x:size(n) as name>>`): the value bound with `as` is a pattern kind that cannot appear in that position (list, constructor, tuple, bit array, string prefix, or invalid pattern). Only simple variable-like patterns are legal there, and the type checker should have rejected anything else.

Solutions

  1. Bind the alias to a plain variable name, then destructure separately.
  2. Update gleam so this mistake produces a proper diagnostic instead of a panic.
  3. Report the minimal example at github.com/gleam-lang/gleam.

Example fix

// before
let assert <<a:size(8) as [b, c]>> = bits
// after
let assert <<a:size(8) as pair>> = bits
let [b, c] = pair
Defensive patterns

Strategy: validation

Validate before calling

// Only alias to a plain variable in bit-array patterns
let assert <<a:size(8) as pair>> = bits // OK
// reject: as [b, c], as #(x, y), etc.

Type guard

fn is_simple_alias(p: &TypedPattern) -> bool {
    matches!(p, TypedPattern::Variable { .. })
}

Try / catch

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

Prevention

When it happens

Trigger: Compiling a bit-array segment whose `as` alias binds a complex pattern (e.g. `<<a:size(8) as [b, c]>>`) that the type checker failed to reject.

Common situations: Attempting to destructure with a complex pattern after `as` in a bit-array pattern; user error that should surface as a type error but instead panics due to a checking gap.

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/500f0fa2b9f9d8ca. Report an issue: GitHub.

Appendix: source

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

            // code for a variable pattern with the wanted name and call it a day
            Pattern::Discard { .. } => {
                builder.variable_pattern(
                    *location,
                    &self.generator.new_erlang_variable(name, *location),
                );
                return;
            }

            Pattern::Variable { .. }
            | Pattern::BitArraySize(_)
            | Pattern::Assign { .. }
            | Pattern::List { .. }
            | Pattern::Constructor { .. }
            | Pattern::Tuple { .. }
            | Pattern::BitArray { .. }
            | Pattern::StringPrefix { .. }
            | Pattern::Invalid { .. } => {
                panic!("invalid pattern inside aliased bit array pattern segment")
            }
        };

        let _ = self
            .variables_to_add_later
            .insert(name.clone(), aliased_value);
        self.pattern(builder, pattern);
    }

    fn bit_array_pattern_segment_size<Output>(
        &mut self,
        builder: &mut impl ErlangBuilder<Output>,
        segment: &'a TypedPatternBitArraySegment,
    ) {
        let Some(size) = segment.size() else {
            builder.bit_array_segment_default_size();
            return;
        };

View on GitHub (pinned to 15b07c7830)