Browse Source

Clean up generated code.

Fixes #109.
We had too many warnings from the generated code on for loops,
in order to continue working on #98.
Andreas Stenius 11 years ago
parent
commit
a94d1fe237

+ 200 - 128
src/erlydtl_compiler.erl

@@ -52,6 +52,7 @@
          format/3,
          format/3,
          value_ast/5,
          value_ast/5,
          resolve_scoped_variable_ast/2,
          resolve_scoped_variable_ast/2,
+         resolve_scoped_variable_ast/3,
          interpret_args/3,
          interpret_args/3,
          unescape_string_literal/1
          unescape_string_literal/1
         ]).
         ]).
@@ -61,24 +62,20 @@
 compile(FileOrBinary, Module) ->
 compile(FileOrBinary, Module) ->
     compile(FileOrBinary, Module, [verbose, report_errors]).
     compile(FileOrBinary, Module, [verbose, report_errors]).
 
 
-compile(Binary, Module, Options0) when is_binary(Binary) ->
-    File = "/<text>",
-    Options = process_opts(File, Options0),
-    Context = init_dtl_context(File, Module, Options),
+compile(Binary, Module, Options) when is_binary(Binary) ->
+    Context = process_opts(undefined, Module, Options),
     compile(Context#dtl_context{ bin = Binary });
     compile(Context#dtl_context{ bin = Binary });
 
 
-compile(File, Module, Options0) ->
-    Options = process_opts(File, Options0),
-    Context = init_dtl_context(File, Module, Options),
+compile(File, Module, Options) ->
+    Context = process_opts(File, Module, Options),
     print("Compile template: ~s~n", [File], Context),
     print("Compile template: ~s~n", [File], Context),
     compile(Context).
     compile(Context).
 
 
 compile_dir(Dir, Module) ->
 compile_dir(Dir, Module) ->
-    compile_dir(Dir, Module, []).
+    compile_dir(Dir, Module, [verbose, report_errors]).
 
 
-compile_dir(Dir, Module, Options0) ->
-    Options = process_opts(Dir, Options0),
-    Context = init_dtl_context_dir(Dir, Module, Options),
+compile_dir(Dir, Module, Options) ->
+    Context = process_opts(Dir, Module, Options),
     %% Find all files in Dir (recursively), matching the regex (no
     %% Find all files in Dir (recursively), matching the regex (no
     %% files ending in "~").
     %% files ending in "~").
     Files = filelib:fold_files(Dir, ".+[^~]$", true, fun(F1,Acc1) -> [F1 | Acc1] end, []),
     Files = filelib:fold_files(Dir, ".+[^~]$", true, fun(F1,Acc1) -> [F1 | Acc1] end, []),
@@ -113,7 +110,7 @@ parse(Data) ->
     parse(Data, #dtl_context{}).
     parse(Data, #dtl_context{}).
 
 
 format_error(no_out_dir) ->
 format_error(no_out_dir) ->
-    "Compiled template not saved (need outdir option)";
+    "Compiled template not saved (need out_dir option)";
 format_error(unexpected_extends_tag) ->
 format_error(unexpected_extends_tag) ->
     "The extends tag must be at the very top of the template";
     "The extends tag must be at the very top of the template";
 format_error(circular_include) ->
 format_error(circular_include) ->
@@ -140,13 +137,27 @@ format_error(Other) ->
 %% Internal functions
 %% Internal functions
 %%====================================================================
 %%====================================================================
 
 
-process_opts(Source, Options) ->
+process_opts(File, Module, Options0) ->
     Options1 = proplists:normalize(
     Options1 = proplists:normalize(
-                 update_defaults(Options),
+                 update_defaults(Options0),
                  [{aliases, [{outdir, out_dir}]}
                  [{aliases, [{outdir, out_dir}]}
                  ]),
                  ]),
-    [{compiler_options, [{source, Source}]}
-     |compiler_opts(Options1, [])].
+    Source = case File of
+                 undefined ->
+                     filename:join(
+                       [proplists:get_value(out_dir, Options1, ""),
+                        Module]);
+                 {dir, Dir} -> Dir;
+                 _ -> File
+             end,
+    Options = [{compiler_options, [{source, lists:concat([Source, ".erl"])}]}
+               |compiler_opts(Options1, [])],
+    case File of
+        {dir, Dir1} ->
+            init_context([], Dir1, Module, Options);
+        _ ->
+            init_context([Source], filename:dirname(Source), Module, Options)
+    end.
 
 
 compiler_opts([CompilerOption|Os], Acc)
 compiler_opts([CompilerOption|Os], Acc)
   when
   when
@@ -157,7 +168,8 @@ compiler_opts([CompilerOption|Os], Acc)
       CompilerOption =:= report_warnings;
       CompilerOption =:= report_warnings;
       CompilerOption =:= report_errors;
       CompilerOption =:= report_errors;
       CompilerOption =:= warnings_as_errors;
       CompilerOption =:= warnings_as_errors;
-      CompilerOption =:= verbose ->
+      CompilerOption =:= verbose;
+      CompilerOption =:= debug_info ->
     compiler_opts(Os, [CompilerOption, {compiler_options, [CompilerOption]}|Acc]);
     compiler_opts(Os, [CompilerOption, {compiler_options, [CompilerOption]}|Acc]);
 compiler_opts([O|Os], Acc) ->
 compiler_opts([O|Os], Acc) ->
     compiler_opts(Os, [O|Acc]);
     compiler_opts(Os, [O|Acc]);
@@ -302,7 +314,7 @@ compile_to_binary(DjangoParseTree, CheckSum, Context) ->
     end.
     end.
 
 
 compile_forms(Forms, Context) ->
 compile_forms(Forms, Context) ->
-    dump_forms(Forms, Context),
+    maybe_debug_template(Forms, Context),
     Options = Context#dtl_context.compiler_options,
     Options = Context#dtl_context.compiler_options,
     case compile:forms(Forms, Options) of
     case compile:forms(Forms, Options) of
         Compiled when element(1, Compiled) =:= ok ->
         Compiled when element(1, Compiled) =:= ok ->
@@ -331,7 +343,7 @@ maybe_write(Module, Bin, Context) ->
         undefined ->
         undefined ->
             add_warning(no_out_dir, Context);
             add_warning(no_out_dir, Context);
         OutDir ->
         OutDir ->
-            BeamFile = filename:join([OutDir, atom_to_list(Module) ++ ".beam"]),
+            BeamFile = filename:join([OutDir, [Module, ".beam"]]),
             print("Template module: ~w -> ~s\n", [Module, BeamFile], Context),
             print("Template module: ~w -> ~s\n", [Module, BeamFile], Context),
             case file:write_file(BeamFile, Bin) of
             case file:write_file(BeamFile, Bin) of
                 ok -> Context;
                 ok -> Context;
@@ -353,23 +365,30 @@ load_code(Module, Bin, Context) ->
         Error -> add_warning({load, Error}, Context)
         Error -> add_warning({load, Error}, Context)
     end.
     end.
 
 
-dump_forms(Forms, Context) ->
-    %% undocumented option to debug the compiled Forms
-    case proplists:get_value(debug_compiler, Context#dtl_context.all_options) of
+maybe_debug_template(Forms, Context) ->
+    %% undocumented option to debug the compiled template
+    case proplists:get_bool(debug_info, Context#dtl_context.all_options) of
+        false -> nop;
         true ->
         true ->
-            io:format("template source:~n~s~n",
-                      [try
-                           erl_prettypr:format(erl_syntax:form_list(Forms))
-                       catch
-                           error:Err ->
-                               io_lib:format("Pretty printing failed: ~p~nContext: ~n~p~nForms: ~n~p~n",
-                                             [Err, Context, Forms])
-                       end
-                      ]);
-        _ -> nop
+            Options = Context#dtl_context.compiler_options,
+            print("Compiler options: ~p~n", [Options], Context),
+            try
+                Source = erl_prettypr:format(erl_syntax:form_list(Forms)),
+                File = proplists:get_value(source, Options),
+                io:format("Saving template source to: ~s.. ~p~n",
+                          [File, file:write_file(File, Source)])
+            catch
+                error:Err ->
+                    io:format("Pretty printing failed: ~p~n"
+                              "Context: ~n~p~n"
+                              "Forms: ~n~p~n",
+                              [Err, Context, Forms])
+            end
     end.
     end.
 
 
-init_context(IsCompilingDir, ParseTrail, DefDir, Module, Options) ->
+init_context(ParseTrail, DefDir, Module, Options) when is_list(Module) ->
+    init_context(ParseTrail, DefDir, list_to_atom(Module), Options);
+init_context(ParseTrail, DefDir, Module, Options) ->
     Ctx = #dtl_context{},
     Ctx = #dtl_context{},
     Context = #dtl_context{
     Context = #dtl_context{
                  all_options = Options,
                  all_options = Options,
@@ -392,7 +411,7 @@ init_context(IsCompilingDir, ParseTrail, DefDir, Module, Options) ->
                  force_recompile = proplists:get_bool(force_recompile, Options),
                  force_recompile = proplists:get_bool(force_recompile, Options),
                  locale = proplists:get_value(locale, Options, Ctx#dtl_context.locale),
                  locale = proplists:get_value(locale, Options, Ctx#dtl_context.locale),
                  verbose = proplists:get_value(verbose, Options, Ctx#dtl_context.verbose),
                  verbose = proplists:get_value(verbose, Options, Ctx#dtl_context.verbose),
-                 is_compiling_dir = IsCompilingDir,
+                 is_compiling_dir = ParseTrail == [],
                  extension_module = proplists:get_value(extension_module, Options, Ctx#dtl_context.extension_module),
                  extension_module = proplists:get_value(extension_module, Options, Ctx#dtl_context.extension_module),
                  scanner_module = proplists:get_value(scanner_module, Options, Ctx#dtl_context.scanner_module),
                  scanner_module = proplists:get_value(scanner_module, Options, Ctx#dtl_context.scanner_module),
                  record_info = [{R, lists:zip(I, lists:seq(2, length(I) + 1))}
                  record_info = [{R, lists:zip(I, lists:seq(2, length(I) + 1))}
@@ -436,16 +455,6 @@ get_error_info_opts(Class, Options) ->
          {Value, proplists:get_bool(Key, Options)}
          {Value, proplists:get_bool(Key, Options)}
      end || Flag <- Flags].
      end || Flag <- Flags].
     
     
-init_dtl_context(File, Module, Options) when is_list(Module) ->
-    init_dtl_context(File, list_to_atom(Module), Options);
-init_dtl_context(File, Module, Options) ->
-    init_context(false, [File], filename:dirname(File), Module, Options).
-
-init_dtl_context_dir(Dir, Module, Options) when is_list(Module) ->
-    init_dtl_context_dir(Dir, list_to_atom(Module), Options);
-init_dtl_context_dir(Dir, Module, Options) ->
-    init_context(true, [], Dir, Module, Options).
-
 init_treewalker(Context) ->
 init_treewalker(Context) ->
     TreeWalker = #treewalker{},
     TreeWalker = #treewalker{},
     case call_extension(Context, init_treewalker, [TreeWalker]) of
     case call_extension(Context, init_treewalker, [TreeWalker]) of
@@ -603,22 +612,24 @@ reset_token_stream(Ts, [_|S]) ->
 
 
 
 
 custom_tags_ast(CustomTags, Context, TreeWalker) ->
 custom_tags_ast(CustomTags, Context, TreeWalker) ->
-    {{CustomTagsClauses, CustomTagsInfo}, TreeWalker1} = custom_tags_clauses_ast(CustomTags, Context, TreeWalker),
-    %% This doesn't work since erl_syntax:revert/1 chokes on the airity_qualifier in the -compile attribute..
-    %% bug report sent to the erlang-bugs mailing list.
-    %% {{erl_syntax:form_list(
-    %%     [erl_syntax:attribute(
-    %%        erl_syntax:atom(compile),
-    %%        [erl_syntax:tuple(
-    %%           [erl_syntax:atom(nowarn_unused_function),
-    %%            erl_syntax:arity_qualifier(
-    %%              erl_syntax:atom(render_tag),
-    %%              erl_syntax:integer(3))])]),
-    %%      erl_syntax:function(erl_syntax:atom(render_tag), CustomTagsClauses)]),
-    {{erl_syntax:function(erl_syntax:atom(render_tag), CustomTagsClauses),
-      CustomTagsInfo},
-     TreeWalker1}.
+    %% avoid adding the render_tag/3 fun if it isn't used,
+    %% since we can't add a -compile({nowarn_unused_function, render_tag/3}).
+    %% attribute due to a bug in syntax_tools.
+    case custom_tags_clauses_ast(CustomTags, Context, TreeWalker) of
+        skip ->
+            {{erl_syntax:comment(
+                ["% render_tag/3 is not used in this template."]),
+              #ast_info{}},
+             TreeWalker};
+        {{CustomTagsClauses, CustomTagsInfo}, TreeWalker1} ->
+            {{erl_syntax:function(
+                erl_syntax:atom(render_tag),
+                CustomTagsClauses),
+              CustomTagsInfo},
+             TreeWalker1}
+    end.
 
 
+custom_tags_clauses_ast([], _Context, _TreeWalker) -> skip;
 custom_tags_clauses_ast(CustomTags, Context, TreeWalker) ->
 custom_tags_clauses_ast(CustomTags, Context, TreeWalker) ->
     custom_tags_clauses_ast1(CustomTags, [], [], #ast_info{}, Context, TreeWalker).
     custom_tags_clauses_ast1(CustomTags, [], [], #ast_info{}, Context, TreeWalker).
 
 
@@ -783,16 +794,16 @@ forms(Module, {BodyAst, BodyInfo}, {CustomTagsFunctionAst, CustomTagsInfo}, Chec
     Render1FunctionAst = erl_syntax:function(
     Render1FunctionAst = erl_syntax:function(
                            erl_syntax:atom(render),
                            erl_syntax:atom(render),
                            [erl_syntax:clause(
                            [erl_syntax:clause(
-                              [erl_syntax:variable("_Variables")],
+                              [erl_syntax:variable("Variables")],
                               none,
                               none,
                               [erl_syntax:application(
                               [erl_syntax:application(
                                  none, erl_syntax:atom(render),
                                  none, erl_syntax:atom(render),
-                                 [erl_syntax:variable("_Variables"),
+                                 [erl_syntax:variable("Variables"),
                                   erl_syntax:list([])])
                                   erl_syntax:list([])])
                               ])
                               ])
                            ]),
                            ]),
     Function2 = erl_syntax:application(none, erl_syntax:atom(render_internal),
     Function2 = erl_syntax:application(none, erl_syntax:atom(render_internal),
-                                       [erl_syntax:variable("_Variables"), erl_syntax:variable("RenderOptions")]),
+                                       [erl_syntax:variable("Variables"), erl_syntax:variable("RenderOptions")]),
     ClauseOk = erl_syntax:clause([erl_syntax:variable("Val")],
     ClauseOk = erl_syntax:clause([erl_syntax:variable("Val")],
                                  none,
                                  none,
                                  [erl_syntax:tuple([erl_syntax:atom(ok), erl_syntax:variable("Val")])]),
                                  [erl_syntax:tuple([erl_syntax:atom(ok), erl_syntax:variable("Val")])]),
@@ -802,7 +813,7 @@ forms(Module, {BodyAst, BodyInfo}, {CustomTagsFunctionAst, CustomTagsInfo}, Chec
     Render2FunctionAst = erl_syntax:function(
     Render2FunctionAst = erl_syntax:function(
                            erl_syntax:atom(render),
                            erl_syntax:atom(render),
                            [erl_syntax:clause(
                            [erl_syntax:clause(
-                              [erl_syntax:variable("_Variables"),
+                              [erl_syntax:variable("Variables"),
                                erl_syntax:variable("RenderOptions")],
                                erl_syntax:variable("RenderOptions")],
                               none,
                               none,
                               [erl_syntax:try_expr([Function2], [ClauseOk], [ClauseCatch])])
                               [erl_syntax:try_expr([Function2], [ClauseOk], [ClauseCatch])])
@@ -1460,12 +1471,18 @@ resolve_variable_ast1({variable, {identifier, Pos, VarName}}, Context, TreeWalke
     {{VarValue, #ast_info{ var_names=[VarName] }}, TreeWalker}.
     {{VarValue, #ast_info{ var_names=[VarName] }}, TreeWalker}.
 
 
 resolve_scoped_variable_ast(VarName, Context) ->
 resolve_scoped_variable_ast(VarName, Context) ->
-    lists:foldl(fun(Scope, Value) ->
-                        case Value of
-                            undefined -> proplists:get_value(VarName, Scope);
-                            _ -> Value
-                        end
-                end, undefined, Context#dtl_context.local_scopes).
+    resolve_scoped_variable_ast(VarName, Context, undefined).
+
+resolve_scoped_variable_ast(VarName, Context, Default) ->
+    lists:foldl(
+      fun (Scope, Res) ->
+              if Res =:= Default ->
+                      proplists:get_value(VarName, Scope, Default);
+                 true -> Res
+              end
+      end,
+      Default,
+      Context#dtl_context.local_scopes).
 
 
 format(Ast, Context, TreeWalker) ->
 format(Ast, Context, TreeWalker) ->
     auto_escape(format_number_ast(Ast), Context, TreeWalker).
     auto_escape(format_number_ast(Ast), Context, TreeWalker).
@@ -1566,59 +1583,109 @@ to_list_ast(Value, IsReversed, Context, TreeWalker) ->
     end.
     end.
 
 
 for_loop_ast(IteratorList, LoopValue, IsReversed, Contents, {EmptyContentsAst, EmptyContentsInfo}, Context, TreeWalker) ->
 for_loop_ast(IteratorList, LoopValue, IsReversed, Contents, {EmptyContentsAst, EmptyContentsInfo}, Context, TreeWalker) ->
-    Vars = lists:map(fun({identifier, _, Iterator}) ->
-                             erl_syntax:variable(lists:concat(["Var_", Iterator]))
-                     end, IteratorList),
-    {{InnerAst, Info}, TreeWalker1} =
+    %% create unique namespace for this instance
+    Level = length(Context#dtl_context.local_scopes),
+    {Row, Col} = element(2, hd(IteratorList)),
+    ForId = lists:concat(["/", Level, "_", Row, ":", Col]),
+
+    Counters = lists:concat(["Counters", ForId]),
+    Vars = lists:concat(["Vars", ForId]),
+
+    %% setup
+    VarScope = lists:map(
+                 fun({identifier, {R, C}, Iterator}) ->
+                         {Iterator, erl_syntax:variable(
+                                      lists:concat(["Var_", Iterator,
+                                                    "/", Level, "_", R, ":", C
+                                                   ]))}
+                 end, IteratorList),
+    {Iterators, IteratorVars} = lists:unzip(VarScope),
+    IteratorCount = length(IteratorVars),
+
+    {{LoopBodyAst, Info}, TreeWalker1} =
         body_ast(
         body_ast(
           Contents,
           Contents,
           Context#dtl_context{
           Context#dtl_context{
             local_scopes =
             local_scopes =
-                [[{'forloop', erl_syntax:variable("Counters")}
-                  | lists:map(
-                      fun({identifier, _, Iterator}) ->
-                              {Iterator,
-                               erl_syntax:variable(
-                                 lists:concat(["Var_", Iterator]))}
-                      end, IteratorList)
-                 ]| Context#dtl_context.local_scopes]
+                [[{'forloop', erl_syntax:variable(Counters)} | VarScope]
+                 | Context#dtl_context.local_scopes]
            },
            },
           TreeWalker),
           TreeWalker),
-    CounterAst = erl_syntax:application(erl_syntax:atom(erlydtl_runtime),
-                                        erl_syntax:atom(increment_counter_stats), [erl_syntax:variable("Counters")]),
+
+    CounterAst = erl_syntax:application(
+                   erl_syntax:atom(erlydtl_runtime),
+                   erl_syntax:atom(increment_counter_stats),
+                   [erl_syntax:variable(Counters)]),
 
 
     {{LoopValueAst, LoopValueInfo}, TreeWalker2} = value_ast(LoopValue, false, true, Context, TreeWalker1),
     {{LoopValueAst, LoopValueInfo}, TreeWalker2} = value_ast(LoopValue, false, true, Context, TreeWalker1),
 
 
     LoopValueAst0 = to_list_ast(LoopValueAst, erl_syntax:atom(IsReversed), Context, TreeWalker2),
     LoopValueAst0 = to_list_ast(LoopValueAst, erl_syntax:atom(IsReversed), Context, TreeWalker2),
 
 
-    CounterVars0 = case resolve_scoped_variable_ast('forloop', Context) of
-                       undefined ->
-                           erl_syntax:application(erl_syntax:atom(erlydtl_runtime), erl_syntax:atom(init_counter_stats), [LoopValueAst0]);
-                       Value ->
-                           erl_syntax:application(erl_syntax:atom(erlydtl_runtime), erl_syntax:atom(init_counter_stats), [LoopValueAst0, Value])
-                   end,
+    ParentLoop = resolve_scoped_variable_ast('forloop', Context, erl_syntax:atom(undefined)),
+
+    %% call for loop
     {{erl_syntax:case_expr(
     {{erl_syntax:case_expr(
         erl_syntax:application(
         erl_syntax:application(
           erl_syntax:atom('erlydtl_runtime'), erl_syntax:atom('forloop'),
           erl_syntax:atom('erlydtl_runtime'), erl_syntax:atom('forloop'),
-          [erl_syntax:fun_expr([
-                                erl_syntax:clause([erl_syntax:tuple(Vars), erl_syntax:variable("Counters")], none,
-                                                  [erl_syntax:tuple([InnerAst, CounterAst])]),
-                                erl_syntax:clause(case Vars of [H] -> [H, erl_syntax:variable("Counters")];
-                                                      _ -> [erl_syntax:list(Vars), erl_syntax:variable("Counters")] end, none,
-                                                  [erl_syntax:tuple([InnerAst, CounterAst])])
-                               ]),
-           CounterVars0, LoopValueAst0]),
+          [erl_syntax:fun_expr(
+             [erl_syntax:clause(
+                [erl_syntax:variable(Vars),
+                 erl_syntax:variable(Counters)],
+                none,
+                [erl_syntax:match_expr(
+                   erl_syntax:tuple(IteratorVars),
+                   erl_syntax:if_expr(
+                     [erl_syntax:clause(
+                        [], [erl_syntax:application(none, erl_syntax:atom(is_tuple), [erl_syntax:variable(Vars)]),
+                             erl_syntax:infix_expr(
+                               erl_syntax:application(none, erl_syntax:atom(size), [erl_syntax:variable(Vars)]),
+                               erl_syntax:operator('=='),
+                               erl_syntax:integer(IteratorCount))
+                            ],
+                        [erl_syntax:variable(Vars)])
+                      | if IteratorCount > 1 ->
+                                [erl_syntax:clause(
+                                   [], [erl_syntax:application(none, erl_syntax:atom(is_list), [erl_syntax:variable(Vars)]),
+                                        erl_syntax:infix_expr(
+                                          erl_syntax:application(none, erl_syntax:atom(length), [erl_syntax:variable(Vars)]),
+                                          erl_syntax:operator('=='),
+                                          erl_syntax:integer(IteratorCount))
+                                       ],
+                                   [erl_syntax:application(none, erl_syntax:atom(list_to_tuple), [erl_syntax:variable(Vars)])]),
+                                 erl_syntax:clause(
+                                   [], [erl_syntax:atom(true)],
+                                   [erl_syntax:application(
+                                      none, erl_syntax:atom(throw),
+                                      [erl_syntax:tuple(
+                                         [erl_syntax:atom(for_loop),
+                                          erl_syntax:abstract(Iterators),
+                                          erl_syntax:variable(Vars)])
+                                      ])
+                                   ])
+                                ];
+                           true ->
+                                [erl_syntax:clause(
+                                   [], [erl_syntax:atom(true)],
+                                   [erl_syntax:tuple([erl_syntax:variable(Vars)])])
+                                ]
+                        end
+                     ])),
+                 erl_syntax:tuple([LoopBodyAst, CounterAst])
+                ])
+             ]),
+           LoopValueAst0, ParentLoop
+          ]),
+        %% of
         [erl_syntax:clause(
         [erl_syntax:clause(
-           [erl_syntax:tuple([erl_syntax:underscore(),
-                              erl_syntax:list([erl_syntax:tuple([erl_syntax:atom(counter), erl_syntax:integer(1)])],
-                                              erl_syntax:underscore())])],
-           none, [EmptyContentsAst]),
+           [erl_syntax:atom(empty)],
+           none,
+           [EmptyContentsAst]),
          erl_syntax:clause(
          erl_syntax:clause(
            [erl_syntax:tuple([erl_syntax:variable("L"), erl_syntax:underscore()])],
            [erl_syntax:tuple([erl_syntax:variable("L"), erl_syntax:underscore()])],
-           none, [erl_syntax:variable("L")])]
-       ),
-      merge_info(merge_info(Info, EmptyContentsInfo), LoopValueInfo)
-     }, TreeWalker2}.
+           none, [erl_syntax:variable("L")])
+        ]),
+      merge_info(merge_info(Info, EmptyContentsInfo), LoopValueInfo)},
+     TreeWalker2}.
 
 
 ifchanged_values_ast(Values, {IfContentsAst, IfContentsInfo}, {ElseContentsAst, ElseContentsInfo}, Context, TreeWalker) ->
 ifchanged_values_ast(Values, {IfContentsAst, IfContentsInfo}, {ElseContentsAst, ElseContentsInfo}, Context, TreeWalker) ->
     Info = merge_info(IfContentsInfo, ElseContentsInfo),
     Info = merge_info(IfContentsInfo, ElseContentsInfo),
@@ -1645,38 +1712,43 @@ ifchanged_contents_ast(Contents, {IfContentsAst, IfContentsInfo}, {ElseContentsA
 
 
 
 
 cycle_ast(Names, Context, TreeWalker) ->
 cycle_ast(Names, Context, TreeWalker) ->
-    {NamesTuple, VarNames} = lists:mapfoldl(fun
-                                                ({string_literal, _, Str}, VarNamesAcc) ->
-                                                   {{S, _}, _} = string_ast(unescape_string_literal(Str), Context, TreeWalker),
-                                                   {S, VarNamesAcc};
-                                                ({variable, _}=Var, VarNamesAcc) ->
-                                                   {{V, #ast_info{ var_names=[VarName] }}, _} = resolve_variable_ast(Var, Context, TreeWalker, true),
-                                                   {V, [VarName|VarNamesAcc]};
-                                                ({number_literal, _, Num}, VarNamesAcc) ->
-                                                   {format(erl_syntax:integer(Num), Context, TreeWalker), VarNamesAcc};
-                                                (_, VarNamesAcc) ->
-                                                   {[], VarNamesAcc}
-                                           end, [], Names),
+    {NamesTuple, VarNames}
+        = lists:mapfoldl(
+            fun ({string_literal, _, Str}, VarNamesAcc) ->
+                    {{S, _}, _} = string_ast(unescape_string_literal(Str), Context, TreeWalker),
+                    {S, VarNamesAcc};
+                ({variable, _}=Var, VarNamesAcc) ->
+                    {{V, #ast_info{ var_names=[VarName] }}, _} = resolve_variable_ast(Var, Context, TreeWalker, true),
+                    {V, [VarName|VarNamesAcc]};
+                ({number_literal, _, Num}, VarNamesAcc) ->
+                    {format(erl_syntax:integer(Num), Context, TreeWalker), VarNamesAcc};
+                (_, VarNamesAcc) ->
+                    {[], VarNamesAcc}
+            end, [], Names),
     {{erl_syntax:application(
     {{erl_syntax:application(
         erl_syntax:atom('erlydtl_runtime'), erl_syntax:atom('cycle'),
         erl_syntax:atom('erlydtl_runtime'), erl_syntax:atom('cycle'),
-        [erl_syntax:tuple(NamesTuple), erl_syntax:variable("Counters")]), #ast_info{ var_names = VarNames }}, TreeWalker}.
+        [erl_syntax:tuple(NamesTuple), resolve_scoped_variable_ast('forloop', Context)]),
+      #ast_info{ var_names = VarNames }},
+     TreeWalker}.
 
 
 %% Older Django templates treat cycle with comma-delimited elements as strings
 %% Older Django templates treat cycle with comma-delimited elements as strings
 cycle_compat_ast(Names, Context, TreeWalker) ->
 cycle_compat_ast(Names, Context, TreeWalker) ->
-    NamesTuple = lists:map(fun
-                               ({identifier, _, X}) ->
-                                  {{S, _}, _} = string_ast(X, Context, TreeWalker),
-                                  S
-                          end, Names),
+    NamesTuple = lists:map(
+                   fun ({identifier, _, X}) ->
+                           {{S, _}, _} = string_ast(X, Context, TreeWalker),
+                           S
+                   end, Names),
     {{erl_syntax:application(
     {{erl_syntax:application(
         erl_syntax:atom('erlydtl_runtime'), erl_syntax:atom('cycle'),
         erl_syntax:atom('erlydtl_runtime'), erl_syntax:atom('cycle'),
-        [erl_syntax:tuple(NamesTuple), erl_syntax:variable("Counters")]), #ast_info{}}, TreeWalker}.
+        [erl_syntax:tuple(NamesTuple), resolve_scoped_variable_ast('forloop', Context)]),
+      #ast_info{}},
+     TreeWalker}.
 
 
 now_ast(FormatString, Context, TreeWalker) ->
 now_ast(FormatString, Context, TreeWalker) ->
-                                                % Note: we can't use unescape_string_literal here
-                                                % because we want to allow escaping in the format string.
-                                                % We only want to remove the surrounding escapes,
-                                                % i.e. \"foo\" becomes "foo"
+    %% Note: we can't use unescape_string_literal here
+    %% because we want to allow escaping in the format string.
+    %% We only want to remove the surrounding escapes,
+    %% i.e. \"foo\" becomes "foo"
     UnescapeOuter = string:strip(FormatString, both, 34),
     UnescapeOuter = string:strip(FormatString, both, 34),
     {{StringAst, Info}, TreeWalker1} = string_ast(UnescapeOuter, Context, TreeWalker),
     {{StringAst, Info}, TreeWalker1} = string_ast(UnescapeOuter, Context, TreeWalker),
     {{erl_syntax:application(
     {{erl_syntax:application(

+ 7 - 5
src/erlydtl_runtime.erl

@@ -238,12 +238,13 @@ init_counter_stats(List) ->
     init_counter_stats(List, undefined).
     init_counter_stats(List, undefined).
 
 
 init_counter_stats(List, Parent) when is_list(List) ->
 init_counter_stats(List, Parent) when is_list(List) ->
+    ListLen = length(List),
     [{counter, 1},
     [{counter, 1},
      {counter0, 0},
      {counter0, 0},
-     {revcounter, length(List)},
-     {revcounter0, length(List) - 1},
+     {revcounter, ListLen},
+     {revcounter0, ListLen - 1},
      {first, true},
      {first, true},
-     {last, length(List) =:= 1},
+     {last, ListLen =:= 1},
      {parentloop, Parent}].
      {parentloop, Parent}].
 
 
 increment_counter_stats([{counter, Counter}, {counter0, Counter0}, {revcounter, RevCounter},
 increment_counter_stats([{counter, Counter}, {counter0, Counter0}, {revcounter, RevCounter},
@@ -255,9 +256,10 @@ increment_counter_stats([{counter, Counter}, {counter0, Counter0}, {revcounter,
      {first, false}, {last, RevCounter0 =:= 1},
      {first, false}, {last, RevCounter0 =:= 1},
      {parentloop, Parent}].
      {parentloop, Parent}].
 
 
-forloop(Fun, Acc0, Values) ->
+forloop(_Fun, [], _Parent) -> empty;
+forloop(Fun, Values, Parent) ->
     push_ifchanged_context(),
     push_ifchanged_context(),
-    Result = lists:mapfoldl(Fun, Acc0, Values),
+    Result = lists:mapfoldl(Fun, init_counter_stats(Values, Parent), Values),
     pop_ifchanged_context(),
     pop_ifchanged_context(),
     Result.
     Result.
 
 

+ 2 - 1
tests/src/erlydtl_functional_tests.erl

@@ -253,11 +253,12 @@ test_compile_render(Name) ->
                        {vars, CompileVars},
                        {vars, CompileVars},
                        force_recompile,
                        force_recompile,
                        return_errors,
                        return_errors,
+                       return_warnings,
                        %% debug_compiler,
                        %% debug_compiler,
                        {custom_tags_modules, [erlydtl_custom_tags]}],
                        {custom_tags_modules, [erlydtl_custom_tags]}],
             io:format("compiling ... "),
             io:format("compiling ... "),
             case erlydtl:compile(File, Module, Options) of
             case erlydtl:compile(File, Module, Options) of
-                {ok, Mod} ->
+                {ok, Mod, [{File, [{none,erlydtl_compiler,no_out_dir}]}]} ->
                     if CompileStatus =:= ok -> test_render(Name, Mod);
                     if CompileStatus =:= ok -> test_render(Name, Mod);
                        true ->
                        true ->
                             io:format("missing error"),
                             io:format("missing error"),

+ 33 - 5
tests/src/erlydtl_unittests.erl

@@ -275,7 +275,9 @@ tests() ->
                <<"Al\nAlbert\nJo\nJoseph\n">>},
                <<"Al\nAlbert\nJo\nJoseph\n">>},
               {"Access parent loop counters",
               {"Access parent loop counters",
                <<"{% for outer in list %}{% for inner in outer %}({{ forloop.parentloop.counter0 }}, {{ forloop.counter0 }})\n{% endfor %}{% endfor %}">>,
                <<"{% for outer in list %}{% for inner in outer %}({{ forloop.parentloop.counter0 }}, {{ forloop.counter0 }})\n{% endfor %}{% endfor %}">>,
-               [{'list', [["One", "two"], ["One", "two"]]}], <<"(0, 0)\n(0, 1)\n(1, 0)\n(1, 1)\n">>},
+               [{'list', [["One", "two"], ["One", "two"]]}], [], [], <<"(0, 0)\n(0, 1)\n(1, 0)\n(1, 1)\n">>,
+               %% the warnings we get from the erlang compiler still needs some care..
+               [error_info([no_out_dir]), {"/erlydtl_running_test.erl", [{0, erl_lint, {unused_var, 'Var_inner/1_1:31'}}]}]},
               {"If changed",
               {"If changed",
                <<"{% for x in list %}{% ifchanged %}{{ x }}\n{% endifchanged %}{% endfor %}">>,
                <<"{% for x in list %}{% ifchanged %}{{ x }}\n{% endifchanged %}{% endfor %}">>,
                [{'list', ["one", "two", "two", "three", "three", "three"]}], <<"one\ntwo\nthree\n">>},
                [{'list', ["one", "two", "two", "three", "three", "three"]}], <<"one\ntwo\nthree\n">>},
@@ -1220,7 +1222,7 @@ tests() ->
          [{extension_module, erlydtl_extension_test}], <<"ok">>},
          [{extension_module, erlydtl_extension_test}], <<"ok">>},
         {"proper error message", <<"{{ bar # }}">>, [{bar, "ok"}], [],
         {"proper error message", <<"{{ bar # }}">>, [{bar, "ok"}], [],
          [{extension_module, erlydtl_extension_test}],
          [{extension_module, erlydtl_extension_test}],
-         {error, [{"/<text>", [{1,erlydtl_extension_test,"Unexpected '#' in code at column 8"}]}], []}},
+         {error, [error_info([{1,erlydtl_extension_test,"Unexpected '#' in code at column 8"}])], []}},
         %% accept identifiers as expressions (this is a dummy functionality to test the parser extensibility)
         %% accept identifiers as expressions (this is a dummy functionality to test the parser extensibility)
         {"identifiers as expressions", <<"{{ foo.bar or baz }}">>, [{baz, "ok"}], [],
         {"identifiers as expressions", <<"{{ foo.bar or baz }}">>, [{baz, "ok"}], [],
          [{extension_module, erlydtl_extension_test}], <<"ok">>}
          [{extension_module, erlydtl_extension_test}], <<"ok">>}
@@ -1321,12 +1323,14 @@ test_fail(Name, Fmt, Args, T) ->
     {T, {Name, io_lib:format(Fmt, Args)}}.
     {T, {Name, io_lib:format(Fmt, Args)}}.
 
 
 process_unit_test({Name, DTL, Vars, Output}) ->
 process_unit_test({Name, DTL, Vars, Output}) ->
-    process_unit_test({Name, DTL, Vars, [], [], Output});
+    process_unit_test({Name, DTL, Vars, [], [], Output, default_warnings()});
 process_unit_test({Name, DTL, Vars, RenderOpts, Output}) ->
 process_unit_test({Name, DTL, Vars, RenderOpts, Output}) ->
-    process_unit_test({Name, DTL, Vars, RenderOpts, [], Output});
+    process_unit_test({Name, DTL, Vars, RenderOpts, [], Output, default_warnings()});
 process_unit_test({Name, DTL, Vars, RenderOpts, CompilerOpts, Output}) ->
 process_unit_test({Name, DTL, Vars, RenderOpts, CompilerOpts, Output}) ->
+    process_unit_test({Name, DTL, Vars, RenderOpts, CompilerOpts, Output, default_warnings()});
+process_unit_test({Name, DTL, Vars, RenderOpts, CompilerOpts, Output, Warnings}) ->
     case compile_test(DTL, CompilerOpts) of
     case compile_test(DTL, CompilerOpts) of
-        {Tcompile, {ok, _, _}} ->
+        {Tcompile, {ok, _, Warnings}} ->
             Tc = [{compile, Tcompile}],
             Tc = [{compile, Tcompile}],
             case render_test(Vars, RenderOpts) of
             case render_test(Vars, RenderOpts) of
                 {TrenderL, {ok, IOList}} ->
                 {TrenderL, {ok, IOList}} ->
@@ -1372,6 +1376,12 @@ process_unit_test({Name, DTL, Vars, RenderOpts, CompilerOpts, Output}) ->
                     test_fail(Name, "Render error (with list variables): ~p", [Err],
                     test_fail(Name, "Render error (with list variables): ~p", [Err],
                               [{render_list, TrenderL}|Tc])
                               [{render_list, TrenderL}|Tc])
             end;
             end;
+        {Tcompile, {ok, _, ActualWarnings}} ->
+            test_fail(
+              Name,
+              "Unexpected warnings: ~p~n"
+              "Expected: ~p",
+              [ActualWarnings, Warnings], [{compile, Tcompile}]);
         {Tcompile, Output} -> test_pass([{compile, Tcompile}]);
         {Tcompile, Output} -> test_pass([{compile, Tcompile}]);
         {Tcompile, Err} ->
         {Tcompile, Err} ->
             test_fail(Name, "Compile error: ~p", [Err], [{compile, Tcompile}])
             test_fail(Name, "Compile error: ~p", [Err], [{compile, Tcompile}])
@@ -1410,3 +1420,21 @@ generate_test_date() ->
                     " of ", lists:nth(M, MonthName),
                     " of ", lists:nth(M, MonthName),
                     " ", integer_to_list(Y), "."
                     " ", integer_to_list(Y), "."
                    ]).
                    ]).
+
+
+default_warnings() ->
+    [error_info([no_out_dir])].
+
+error_info(File, Ws) ->
+    {File, [error_info(W) || W <- Ws]}.
+
+error_info({Line, ErrorDesc})
+  when is_integer(Line) ->
+  {Line, erlydtl_compiler, ErrorDesc};
+error_info({Line, Module, ErrorDesc})
+  when is_integer(Line), is_atom(Module) ->
+    {Line, Module, ErrorDesc};
+error_info(Ws) when is_list(Ws) ->
+    error_info("/erlydtl_running_test", Ws);
+error_info(ErrorDesc) ->
+    {none, erlydtl_compiler, ErrorDesc}.