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

Error for wrong lambda syntax should talk about the missing backslash #241

Closed Janiczek closed 5 years ago

Janiczek commented 6 years ago

If somebody forgets to put the backslash before the arguments of an anonymous function, they get an error about arrows. They should(?) get an error about the missing backslash.

module Main exposing (..)

id = a -> a
-- meant: id = \a -> a
-- SYNTAX PROBLEM ----------------------------------------------------- Main.elm

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

3| id = a -> a
          ^
Maybe <http://elm-lang.org/docs/syntax> can help you figure it out.

Detected errors in 1 module.                
gyzerok commented 6 years ago

I am wondering, if parser can be clever enough to understand that it is lambda missing backslash, then why backslash is needed in the first place?

evancz commented 5 years ago

Thanks for the report! I am finishing up an overhaul of the parser that makes it a bunch easier to give very specific error messages. My development build is currently producing:

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

I was not expecting an arrow here:

1| id = a -> a
          ^^
Arrows should only appear in `case` expressions and anonymous functions. Maybe
you want > or >= instead?

I think it's possible to do a bit better though, so I'll see what I can do!

evancz commented 5 years ago

Okay, based on this issue, I made some tweaks in https://github.com/elm/compiler/commit/00ee15406890dd8a3aeed2a30ed64b5b11772ebc to produce the following:

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

I was partway through parsing an expression when I got stuck on this arrow:

1| id = a -> a
          ^^
Arrows should only appear in `case` expressions and anonymous functions.
Maybe it was supposed to be a > sign instead?

Note: The syntax for anonymous functions is (\x -> x + 1) so the arguments all
appear after the backslash and before the arrow. Maybe you forgot the backslash
earlier?

It doesn't directly address the scenario where someone wanted to write a type annotation and wrote = instead of :, but I think it makes sense to wait for someone to report that scenario organically.

This should become available once Elm 0.19.1 is released.

Janiczek commented 5 years ago

I think that solves this issue pretty well :+1: thanks!