unisonweb / unison

A friendly programming language from the future
https://unison-lang.org
Other
5.73k stars 266 forks source link

Better error message for assignment at the end of an expression #3351

Closed ceedubs closed 2 months ago

ceedubs commented 2 years ago

If you accidentally end an expression/block with a variable assignment, the error message should be more helpful.

example

Here's an example of the current behavior (a trunk build from 2022-08-23):

scratch file

foo : Nat
foo =
  x = 3

ucm output

  I couldn't find any definitions matching the name _150003 inside the namespace .tmp

      3 |   x = 3

  Some common causes of this error include:
    * Your current namespace is too deep to contain the definition in its subtree
    * The definition is part of a library which hasn't been added to this project

  To add a library to this project use the command: `fork <.path.to.lib> .tmp.lib.<libname>`

  Whatever it is, its type should conform to Nat.

notes

This is especially easy to encounter when you are debugging code. Sometimes I'll find myself changing the last line of an expression from:

foo 3

to:

result = foo 3
trace "result" result
result

When I get done debugging I often remove the trace and the line below it and forget to change the result = foo 3 line to no longer perform assignment.

aryairani commented 2 months ago

The current output is:

  The last element of a block must be an expression, but this is a definition:

      3 |   x = 3

  Try adding an expression at the end of the block. It should be of type Nat.

@ceedubs Is this good?

ceedubs commented 2 months ago

Yeah this is great!