[erlang-bugs] erl_parse, different argument processing for "-module" and "-import"

Alexander Demidenko <>
Thu Jul 7 15:17:39 CEST 2011

Hi, friends!

Module erl_parse have different AST processing for "-module" and
"-include" directive.
This bug tested on erlang 13B03, 13B04 and 14B03.
Bug demonstration:

Erlang R13B03 (erts-5.7.4) [source] [64-bit] [smp:4:4] [rq:4]
[async-threads:0] [hipe] [kernel-poll:false]
Eshell V5.7.4  (abort with ^G)
1> Str2AST = fun (S) -> {ok, Tokens, _} = erl_scan:string(S), {ok, R}
= erl_parse:parse_form(Tokens), R end.
2> AST1 = Str2AST("-import(m).").

Look at module name in result. You can see list of atoms: [m].
But they must be list only for complex module name (for example:

3> {Tree, _} = erl_syntax_lib:mapfold(fun (T, Acc) -> {T, Acc} end, [], AST1).
4> AST2 = erl_syntax:revert(Tree).
5> AST1 == AST2.

Ops, magic, inside AST2, module name is atom!
But if we try run example with "-module(m)", everything ok. We have
module name like atom in anyway.

6> AST3 = Str2AST("-module(m).").

Ok, now lets look at source of erl_parse:
Part of import processing (should be fixed):

build_attribute({atom,La,import}, Val) ->
    case Val of
    [Name] ->
        case package_segments(Name) of    % <--- Here we always take list
        error ->
            error_bad_decl(La, import);
        Module ->
            {attribute,La,import,Module}      % <--- as result we have
list of atom, like [module_name]
    [{atom,_Lm,Mod},ImpList] ->
    [Name, ImpList] ->
        case package_segments(Name) of
        error ->
            error_bad_decl(La, import);
        Module ->
    _Other -> error_bad_decl(La, import)

Part of module processing (correct version):
build_attribute({atom,La,module}, Val) ->
    case Val of
    [{atom,_Lm,Module}] ->                    % <--- I think, we must
have same special case clause for 'import' version of
    [{atom,_Lm,Module},ExpList] ->
    [Name] ->
        case package_segments(Name) of
        error ->
            error_bad_decl(La, module);
        Module ->
    [Name,ExpList] ->
        case package_segments(Name) of
        error ->
            error_bad_decl(La, module);
        Module ->
    _Other ->
        error_bad_decl(La, module)

With best regards,

More information about the erlang-bugs mailing list