Maybe a bug compiling a list of records without comma between them

We have a big list of static records in our system, but today I realized that some records are missing, and the reason is because commas are missing between some of them, for example:

-module(foo).
-export([list/0]).

-record(foo, {bar}).

list() ->
    [
        #foo{bar = foo}
        #foo{bar = bar}
    ].

Note that there is no comma between the records.
All compile fine, but no warning or error is emitted, and the result is [{foo,bar}].

Can this be considered a bug? Should a warning or error be emitted? Or this is the expected behavior? Why would someone write two records in sequence?

If I change the list/0 to:

list() ->
    [
        {foo, foo}
        {foo, bar}
    ].

a syntax error is raised, as expected:

foo.erl:9:9: syntax error before: '{'
%    9|         {foo, bar}

It took hours to find the problem :sweat:

Oof. Thatā€™s parsed as #foo{bar = foo}#foo{bar = bar}, which can be thought of as:

A = #foo{bar = foo},
A#foo{bar = bar}.

Weā€™ll look into raising a warning for this. :slight_smile:

3 Likes

Exactly the same topic, but for maps: Dark corner of Erlang syntax . Would be nice to have a warning for both

1 Like

Nice! Thank you!

Yes, the same problem. Thanks for pointing it out.

That became a side effect of some PR a long time ago, that removed the required parenthesis around the first expression.

I donā€™t remember why it was removed, it was a couple of years ago.
Allow A#foo{a = X)#foo{a = X+1} ?

IIRC it was to allow you to write: A#foo.bar#foo.bar instead of (A#foo.bar)#foo.bar.

2 Likes

I know itā€™s silly but thatā€™s the reason I absolutely love comma first style.
Unfortunately I didnā€™t find a formatter that could handle that, but I may be wrong.

Yeah, weā€™re still waiting for @robertoaloi to write the comma-first formatter module for GitHub - AdRoll/rebar3_format: Erlang Formatter for Rebar3 :stuck_out_tongue:

3 Likes

When I give courses I mention that you donā€™t need parentheses around the leading record access but stress that I think that with out them the syntax becomes messier. :wink:

4 Likes