elm / error-message-catalog

A catalog of broken Elm programs / data to improve error messages
BSD 3-Clause "New" or "Revised" License
173 stars 17 forks source link

Case statement with wrong indents could give better hints #225

Closed dmag closed 5 years ago

dmag commented 7 years ago

I had a working program (elm 0.18.0), and tried to add a simple NoOp branch to my update function. I was doing a copy-paste, which accidentally added an extra space.

update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
  case msg of
    Name name ->
      ( { model | name = name }, Cmd.none )
     NoOp ->
      ( model, Cmd.none )

The compiler gave me this error, which was quite confusing.

-- SYNTAX PROBLEM --------------------------------------------- ./src/Update.elm

Arrows are reserved for cases and anonymous functions. Maybe you want > or >=
instead?

30|      NoOp ->
              ^
Maybe <http://elm-lang.org/docs/syntax> can help you figure it out.

Detected errors in 1 module.                                        

It would have helped if the compiler saw the arrow and gave a hint: "if was supposed to be part of your existing case statement, it's not indented properly." For completeness, if you have too little indent, you get this error, which could use a similar heuristic to give the same hint.

-- SYNTAX PROBLEM --------------------------------------------- ./src/Update.elm

I ran into something unexpected when parsing your code!

30|   NoOp ->
      ^
I am looking for one of the following things:

    end of input
    whitespace
evancz commented 5 years ago

Thank you for reporting this! I am finishing up a revamp of the compiler that is able to produce much nicer error messages. Based on your example, I made a tweak in https://github.com/elm/compiler/commit/aaa2d65d09672b31b6da6c3c317e12bf6b796d47 to make the message super relevant! My development build is producing:

-- UNEXPECTED ARROW --------------------------------------------------- temp.elm

I am parsing a `case` expression right now, but this arrow is confusing me:

6|      NoOp ->
             ^^
It makes sense to see arrows around here, so I suspect it is something earlier.
Maybe this pattern is indented a bit farther than the previous patterns?

Note: Sometimes I get confused by indentation, so try to make your `case` look
something like this:

    case maybeWidth of
      Just width ->
        width + 200

      Nothing ->
        400

Notice the indentation! Patterns are aligned with each other. Same indentation.
The expressions after each arrow are all indented a bit more than the patterns.
That is important!

Thank you for reporting this! It makes it really easy to check if the error message is giving information that is relevant to the reader!