gleam-lang/gleam

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

Open

#5748 opened on May 24, 2026

View on GitHub
 (1 comment) (0 reactions) (0 assignees)Rust (21,417 stars) (960 forks)batch import
help wanted

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