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

Record definition whitespace #255

Closed snewell92 closed 5 years ago

snewell92 commented 6 years ago

Consider the following elm program that does nothing but fail to compile:

type alias Stuff = {
    a : String,
    bananas : Int
}

myStuff : Stuff
myStuff = {
    a = "Hi",
    bananas = 5
}

Compiler error says:

I need whitespace, but got stuck on what looks like a new declaration. You are
either missing some stuff in the declaration above or just need to add some
spaces here:

This is definitely a correct error message ✔️, but it doesn't necessarily clearly explain why to new comers (ie me), and in the spirit of improving the error messages I'd like to submit this reasoning:

In some areas in the docs about records it would seem that records aren't indentation or white-space sensitive, but maybe the error message could give an example of a style-guide-conforming record definition, like this:

myStuff : Stuff
myStuff =
    { a = "Hi"
    , bananas = 5
    }

And perhaps a link, reference, or a short blurb on the reasoning about this, or maybe just something like "record definitions like this example one are definitively unambiguous, and sticking to it will ensure your program is readable by other elm programmers and quickly compilable"..? 😕

❓ Idk, maybe it's a non issue, feel free to close since the existing error message does mention white-space, which is what is needed to change to clear the error, I just spent a few minute fighting the compiler on it though.

One thing that's interesting is the type alias definition compiles fine, but to get the record definition to work you just need to add at least one space on the last line.

evancz commented 5 years ago

I am finishing up an overhaul of the parser that makes it easier to give more precise error messages for syntax problems. This report encouraged me to make one last tweak in https://github.com/elm/compiler/commit/11ffbf5910ac9d3d59290d46f307528334255b9d so my development build is now giving the following message for your code:

-- NEED MORE INDENTATION ---------------------------------------------- temp.elm

I was partway through parsing a record type, but I got stuck here:

1| type alias Stuff = {
2|     a : String,
3|     bananas : Int
4| }
   ^
I need this curly brace to be indented more. Try adding some spaces before it!

Note: If you are trying to define a record type across multiple lines, I
recommend using this format:

    { name : String
    , age : Int
    , height : Float
    }

Notice that each line starts with some indentation. Usually two or four spaces.
This is the stylistic convention in the Elm ecosystem.

Thanks for reporting this case! The improved version should become available along with a bunch of other improvements once Elm 0.19.1 is released.