Repository: gleam-lang/gleam

Issue #3506: Compiler crash when checking exhaustiveness of a pattern on a type with duplicate variants
Created: 2024-08-12T14:28:13Z
Hello, thank you for all the wonderful work on Gleam! On v1.4.1 and the current main branch, but not on v1.2.1, running on Ubuntu and compiling to BEAM, I noticed the following small program will crash the compiler:
pub type Bug {
  Ant
  Bee
  Bee
  Caterpillar
}

pub fn get_bugs(bug: Bug) {
  case bug {
    Ant -> "And"
    Bee -> "Bee"
    Caterpillar -> "Caterpillar"
  }
}
The error msg given by the compiler on v1.4.1 is:
Panic: compiler-core/src/exhaustiveness.rs:746
        Custom type variants must exist: Type { name: "Bug", hint: AlternativeTypes(["Bool", "Int", "List", "UtfCodepoint", "Result", "BitArray", "Nil", "String", "Bug", "Float"]) }
Gleam version: 1.4.1
Operating system: linux

The error msg given by the compiler on the current main branch is:
Panic: compiler-core/src/exhaustiveness.rs:539
        Case must exist
Gleam version: 1.4.1
Operating system: linux

On v1.2.1 and v1.4.1:
This code will compile and throw an error about the duplicate Caterpillar definition:
pub type Bug {
  Ant
  Bee
  Caterpillar
  Caterpillar
}

pub fn get_bugs(bug: Bug) {
  "Bug!"
}
On v1.2.1 and v1.4.1 (but not the current main branch) this code will crash the compiler:
pub type Bug {
  Ant
  Bee
  Caterpillar
  Caterpillar
}

pub fn get_bugs(bug: Bug) {
  case bug {
    Ant -> "And"
    Bee -> "Bee"
    Caterpillar -> "Caterpillar"
  }
}
on v1.4.1 it will give the following error:
Panic: compiler-core/src/exhaustiveness.rs:746
        Custom type variants must exist: Type { name: "Bug", hint: AlternativeTypes(["Bool", "Int", "List", "UtfCodepoint", "Result", "BitArray", "Nil", "String", "Bug", "Float"]) }
Gleam version: 1.4.1
Operating system: linux

So it seems that when the exhaustiveness check is run for the case statement in get_bugs, it will crash when there is some set of duplicate variants. It seems like #3490 fixed a subset of the issue, but there is still a case where this pattern crashes the compiler.
Hope this helps! Let me know if any more info is needed.

Duplicate issue #3575: Compiler error using redefined custom types
Created: 2024-08-29T16:28:40Z
Description
I'm learning gleam by writing the good old Thorsten Ball's "Writing An Interpreter In Go" . at some point I wrote a custom type
type LiteralExpressionWrap {
  IntWrap(Int)
  StringWrap(String)
  BooleanWrap(Bool)
  StringWrap(String)
}
and this code crashed the LSP and  gleam check is returning the following message:
error: Fatal compiler bug!

This is a bug in the Gleam compiler, sorry!

Please report this crash to https://github.com/gleam-lang/gleam/issues/new
and include this error message with your report.

Panic: compiler-core/src/exhaustiveness.rs:746
        Custom type variants must exist: Type { name: "LiteralExpressionWrap", hint: AlternativeTypes(["Int", "Bool", "BitArray", "List", "String", "LiteralExpressionWrap", "UtfCodepoint", "Result", "Float", "Nil"]) }
Gleam version: 1.3.0
Operating system: linux

If you can also share your code and say what file you were editing or any
steps to reproduce the crash that would be a great help.

You may also want to try again with the `GLEAM_LOG=trace` environment
variable set.

I tried to isolate the issue by trying  to define a custom type in the same fashion just down below the other type definition
type LiteralExpressionWrap {
  IntWrap(Int)
  StringWrap(String)
  BooleanWrap(Bool)
  // StringWrap(String)
}

type Bug {
  IntBug(Int)
  StringBug(String)
  BooleanBug(Bool)
  StringBug(String)
}
but this time I have the correct error and the expected behavior happening when I run gleam check
error: Duplicate definition
    ÔöîÔöÇ /home/wildchild/gleam/interpreter/test/parser/parser_test.gleam:284:3
    Ôöé
284 Ôöé   StringBug(String)
    Ôöé   ^^^^^^^^^^^^^^^^^ First defined here
285 Ôöé   BooleanBug(Bool)
286 Ôöé   StringBug(String)
    Ôöé   ^^^^^^^^^^^^^^^^^ Redefined here

I can reproduce this issue in Windows WLS running ubuntu and also in Mac OS using gleam 1.3

Ubuntu 22.04.4 LTS on Windows 10 x86_64
Sonoma 14.6.1

Bug Reproduction Steps

Get the code from:
https://github.com/wildchild04/gleam-interpreter/tree/gleam-compiler-bug
make sure you are in the branch gleam-compiler-bug
run gleam check

Closing comment (2024-08-29T16:58:06Z): Hello! Thanks for the bug report! It looks like this is a duplicate of #3506
The fix will ship with Gleam v1.5 :)