gleam-lang/gleam

Duplicate let when a variable is rebound after a directly matching case

Closed

#5,748 opened on May 24, 2026

 (1 comment) (0 reactions) (0 assignees)Rust (960 forks)batch import
help wanted

Repository metrics

Stars
 (21,417 stars)
PR merge metrics
 (Avg merge 10d 19h) (69 merged PRs in 30d)

Description

  • gleam 1.17.0-rc1
  • target javascript

A variable shadowed inside a directly matching case branch and then rebound after the case compiles to two let declarations with the same name, which is a SyntaxError.

pub fn go() -> Int {
  let n = 1
  case True {
    True -> {
      let n = 99
      n
    }
    False -> 0
  }
  let n = n + 5
  n
}

Compiles to:

export function go() {
  let n = 1;
  let $ = true;
  let n$1 = 99;
  n$1
  let n$1 = n + 5;
  return n$1;
}

SyntaxError: Identifier 'n$1' has already been declared.

This is the same duplicate let family as #3379 and #5612, which were fixed for pattern bound variables. It still happens when the inner binding is a plain let inside the branch block. I have a fix that tracks the names already declared in a function so a leaked branch declaration isn't reused, which also lets the #5493 same-name workaround go. Happy to open a PR if that approach sounds right.

Contributor guide