gleam-lang/gleam · error

Unknown labels first label

Error message

Unknown labels first label

What it means

When the compiler reports unknown record-constructor (labelled) argument errors, it builds a `Label` iterator over the mismatched labels and unconditionally takes the first one: `labels.next().expect("Unknown labels first label")`. The invariant is that the error being rendered was only constructed because at least one unknown label existed, so the iterator must be non-empty; if the label list was emptied or never populated, this `expect` panics during diagnostic construction.

Solutions

  1. Fix the mislabelled constructor call in your Gleam code — check the type definition and use the exact declared labels (or remove labels if the type has none)
  2. Reduce the snippet and file a compiler bug if the panic occurs on code that looks correct
  3. Upgrade Gleam to the latest release where label diagnostics have been reworked (label_references/label_definitions tracking)

Example fix

// before (Gleam)
let pet = Dog(name: "Rex", legs: 4)  // `legs` is not a label of Dog
// after
let pet = Dog(name: "Rex")            // use only labels declared on the constructor
Defensive patterns

Strategy: fallback

Validate before calling

// Gleam-side: verify constructor argument labels before calling
case Dog { Dog(..) -> io.println("check labels against the type definition") }

Try / catch

gleam build 2>err.log || echo "ICE while reporting labels — inspect err.log, reduce snippet, file a bug"

Prevention

When it happens

Trigger: Rendering an unknown-labels diagnostic (e.g. calling a constructor/function with a wrong or missing label) when the underlying `UnknownLabels` error carries zero labels — a compiler-internal inconsistency, e.g. a bug in how unknown labels are collected in `analyse.rs` / error construction.

Common situations: Users see this as an ICE while compiling code with mislabelled constructor or function arguments on an affected Gleam version; typically right after calling a custom type constructor with an argument label the type does not define.

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@49f8762da5 (2026-09-14). Data as JSON: /api/errors/b53f8081b51a2ae1. Report an issue: GitHub.

Appendix: source

Thrown at compiler-core/src/error.rs:2818

                .cloned()
                .collect();

            let title = if unknown.len() > 1 {
                "Unknown labels"
            } else {
                "Unknown label"
            }
            .into();

            let mut labels = unknown.iter().map(|(label, location)| {
                let text =
                    did_you_mean(label, &other_labels).unwrap_or_else(|| "Unexpected label".into());
                Label {
                    text: Some(text),
                    span: *location,
                }
            });
            let label = labels.next().expect("Unknown labels first label");
            let extra_labels = labels
                .map(|label| ExtraLabel {
                    src_info: None,
                    label,
                })
                .collect();
            let text = if valid.is_empty() {
                "This constructor does not accept any labelled arguments.".into()
            } else if other_labels.is_empty() {
                "You have already supplied all the labelled arguments that this
constructor accepts."
                    .into()
            } else {
                let mut label_text = String::from("It accepts these labels:\n");
                for label in other_labels.iter().sorted() {
                    label_text.push_str("\n    ");
                    label_text.push_str(label);
                }

View on GitHub (pinned to 49f8762da5)