gleam icon indicating copy to clipboard operation
gleam copied to clipboard

Confusing error message for a not exhaustive pattern match in a let statement

Open silicium14 opened this issue 2 years ago • 3 comments

When a pattern in a let statement is not statically guaranteed to match, the error message tells that this is a case statement, which is confusing.

fn a_function() {
  let result = Ok(1)
  let Ok(value) = result
}
error: Not exhaustive pattern match
  ┌─ ./src/example/example.gleam:3:3
  │
3 │   let Ok(value) = result
  │   ^^^^^^^^^^^^^^^^^^^^^^

This case expression does not match all possibilities. Each constructor
must have a pattern that matches it or else it could crash.

These values are not matched:

  - Error

silicium14 avatar Feb 26 '22 15:02 silicium14

Thank you

lpil avatar Feb 27 '22 12:02 lpil

Here's where the error is defined. The message was a bit more general but was changed to mention "case expression" in https://github.com/gleam-lang/gleam/commit/a8340ba909b64ed26a1d1604fdb936677b1978f6 . Here is where it is reported for let/assert and here is where it is reported for case. To fix this could be either converted to two separate error variants, or the NotExhaustivePatternMatch error variant could carry additional information as to where it came from, and then the message would be returned appropriately.

michallepicki avatar Mar 10 '22 19:03 michallepicki

I think having an additional field on the error sounds good as it'll be otherwise be the same.

lpil avatar Mar 10 '22 23:03 lpil