erl_pp botches try-catch clauses

Ulf Wiger (AL/EAB) ulf.wiger@REDACTED
Sun Dec 25 01:46:06 CET 2005


erl_pp writes out try-catch clauses in a very strange
way that doesn't compile.

I was experimenting with the following test function:

generate_src_file(File, Tabs, L1) ->
    Forms = codegen(Tabs, L1),
    Out = [[erl_pp:form(F), "\n"] || F <- Forms],
    {ok, _Mod, _Bin} = compile:forms(Forms),
    {ok, Fd} = file:open(File, [write]),
    io:fwrite(Fd, "~s~n", [Out]),
    file:close(Fd).

The generated output compiled with a few warnings,
but when I tried compiling the file created using
erl_pp, I got a compilation error on the following:

validate_string(S,Attr,Expected) ->
    try
        list_to_binary(S)
    of
        _ ->
            true
    catch
        error:_:_ ->
            throw({type_error,Attr,{expected,Expected}})
    end.

For some reason, erl_parse adds a {var, L, '_'} to the
catch clause in a try expression:

                                [{clause,
                                     44,
                                     [{tuple,
                                          44,
                                          [{atom,44,error},
                                           {var,44,'_'},
                                           {var,44,'_'}]}],
                                     [],
                                     [{call,
 
and erl_pp happily puts a colon between the first pattern
and the extra '_'.

I modified erl_pp.erl as given below (using a case statement,
since I guessed that there would be a purpose to that extra
pattern, and that it wouldn't always be '_' in the future).
Well, at least compiling the erl_pp output gave the same
result as compiling the forms (I think... - it's not that 
easy to compare the results.)

/Uffe


ws12858> diff -c erl_pp.erl $OTP_ROOT/lib/stdlib-1.13.10/src/erl_pp.erl
*** erl_pp.erl  Sun Dec 25 01:00:53 2005
--- OTP/lib/stdlib-1.13.10/src/erl_pp.erl       Tue Oct 25 13:25:38 2005
***************
*** 466,478 ****
       body(B, I+4, Hook)];
  try_clause({clause,_,[{tuple,_,[C,V,S]}],G,B}, I, Hook) ->
      Cs = expr(C, I, 0, Hook),
!     [case S of
!        {var, _, '_'} ->
!            Cs, ":", expr(V, indentation(Cs, I)+1, 0, Hook);
!        _ ->
!            [Cs, ":", expr(V, indentation(Cs, I)+1, 0, Hook),
!             ":", expr(S, indentation(Cs, I)+1, 0, Hook)]
!      end,
       guard(G, I, Hook),
       body(B, I+4, Hook)].
  
--- 466,473 ----
       body(B, I+4, Hook)];
  try_clause({clause,_,[{tuple,_,[C,V,S]}],G,B}, I, Hook) ->
      Cs = expr(C, I, 0, Hook),
!     [Cs, ":", expr(V, indentation(Cs, I)+1, 0, Hook),
!      ":", expr(S, indentation(Cs, I)+1, 0, Hook),
       guard(G, I, Hook),
       body(B, I+4, Hook)].
  



More information about the erlang-bugs mailing list