nickel icon indicating copy to clipboard operation
nickel copied to clipboard

give more descriptive error on unmatched pattern

Open KiaraGrouwstra opened this issue 11 months ago • 2 comments

Is your feature request related to a problem? Please describe.

when faced with an unmatched pattern, nickel seems to not yet give much of a hint as to where to look.

Describe the solution you'd like give more info as to what fields of a record might not be matched.

Describe alternatives you've considered manually comparing records

Additional context

minimum repro:

nickel> let {a,b} = {a=1} in a
error: unmatched pattern
  ┌─ <repl-input-1>:1:5
  │
1 │ let {a,b} = {a=1} in a
  │     ^^^^^^^^^^^^^
  │     │       │
  │     │       this value doesn't match any branch
  │     in this match expression

KiaraGrouwstra avatar Dec 19 '24 19:12 KiaraGrouwstra

Hello,

Thanks for reporting. Good error messages are indeed important for us. Just to make sure I understand, I imagine you would like to have an additional message like the following (I added a line at the end)?

error: unmatched pattern
  ┌─ <repl-input-1>:1:5
  │
1 │ let {a,b} = {a=1} in a
  │     ^^^^^^^^^^^^^
  │     │       │
  │     │       this value doesn't match any branch
  │     in this match expression
  + Pattern expected a field `b` to be present, but it couldn't be found in the value.

So it's a bit of an implementation detail, but destructuring let <pattern> = <value> in <rest> is desugared (almost) to pattern matching <value> |> match { <pattern> => <rest>, _ => <error> }. In the general case of pattern matching, I think it can be hard to have a nice and concise explanation of why the value didn't match, because it means that none of potentially many branches did match. For example, in {a = 1} |> match { {a,b} => 0, {a,c} => 1, {a,d} => 2, 'Tag => 3, false = 4}, what reason could you give to the user? I personally think the error message is doing the best it can do: point the pattern, and let the user see why none of the branches matched.

For destructuring, since there's only one branch,we could indeed do better in theory. I'm not sure yet how hard this would be; I'll have to think about how to integrate better error reporting with the way patterns are currently compiled.

yannham avatar Dec 20 '24 14:12 yannham

Hi there, That's fair, yeah. Thanks for thinking along!

KiaraGrouwstra avatar Dec 20 '24 15:12 KiaraGrouwstra