Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Confusing error message when using spread operator #677

Closed
Quantumplation opened this issue Jul 15, 2023 · 1 comment
Closed

Confusing error message when using spread operator #677

Quantumplation opened this issue Jul 15, 2023 · 1 comment

Comments

@Quantumplation
Copy link
Contributor

Quantumplation commented Jul 15, 2023

What Git revision are you using?

$ aiken --version
aiken v1.0.11-alpha unknown

What operating system are you using, and which version?

NixOS

Describe what the problem is?

When I have the following definition

pub type Redeemer {
  First(Int),
  Second
}

and I attempt to pattern match on it like so

when redeemer is {
  First(..) -> todo
  Second -> todo
}

I get the following confusing error message

aiken::check::arity::pattern (link)

  × While trying to make sense of your code...
  ╰─▶ I saw a pattern on a constructor that has 1 field(s) be matched with 0 argument(s).
      
    ╭─[/home/pi/proj/example.ak:21:1]
 21 │         )
 22 │       First(..) ->
    ·       ──────┬─────
    ·             ╰── Try instead: First(..)
 23 │         dict.has_key(
    ╰────
  help: When pattern-matching on constructors, you must either match the exact number of fields, or use the spread operator '..'. Note that unused fields must be discarded by prefixing their name with '_'.

Here are the things that are confusing about this:

  • I saw a pattern on a constructor that has 1 field(s); based on a discussion with @rvcas, my understanding is that "constructor that has 1 field(s)" would be something defined with First { field: Int }, so it's odd that it's referring to a constructor with 1 field here.
  • be matched with 0 argument(s). I don't believe this to be true, considering I'm using the spread operator
  • Try instead: First(..) it suggests that I use the exact same code that I have written

Note that even doing

First { .. } ->

Doesn't seem to work, and gives a similar error message (but with Try instead: First { .. }, for bonus points!)

What should be the expected behavior?

First, I would expect to be able to use the spread operator to ignore () style arguments as well, instead of being forced to _name, _each, _one.

Second, if that's not possible, I would expect the error message to be something like:

aiken::check::arity::pattern (link)

  × While trying to make sense of your code...
  ╰─▶ I saw a pattern on a constructor that has 1 argument(s) (defined positionally, between parentheses) be matched with a spread operator.
      
    ╭─[/home/pi/proj/example.ak:21:1]
 21 │         )
 22 │       First(..) ->
    ·       ──────┬─────
    ·             ╰── Try instead: First(_) ->
 23 │         dict.has_key(
    ╰────
  help: When pattern-matching on constructors with positional arguments, you cannot use the spread operator.

or something similar.

@rvcas
Copy link
Member

rvcas commented Jul 15, 2023

Cool

As an initial first improvement we can make the error message more clear and probably allow the spread for none labeled constructors. Eventually I want to just have these two different forms be captured as two different AST nodes allowing for more fine grained error messages.

@rvcas rvcas moved this to 🪣 Backlog in Project Tracking Aug 17, 2023
@rvcas rvcas closed this as completed in 6ce30bd Nov 28, 2023
@github-project-automation github-project-automation bot moved this from 🪣 Backlog to ✅ Done in Project Tracking Nov 28, 2023
@rvcas rvcas mentioned this issue Nov 28, 2023
52 tasks
@rvcas rvcas moved this to 🚀 Released in Project Tracking May 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: 🚀 Released
Development

No branches or pull requests

2 participants