on master help sign in
Merge pull request #1105 from elm-lang/fix-tco-labels

Fix tco labels

commit822e1643132fb42b56042394891f6b7f92eafc99 authorEvan Czaplicki <info@elm-lang.org> dateSun 11 Oct 2015, 22:49 (11 years ago) parentec821405df0721
3 files changed+52−2
src/Generate/JavaScript/Expression.hs+2−2 tests/test-files/good-expected-js/CodeGenBugs/TailRecursion_UnsafeLabel.elm.js+30 tests/test-files/good/CodeGenBugs/TailRecursion_UnsafeLabel.elm+20
src/Generate/JavaScript/Expression.hs+2−2
@@ -164,7 +164,7 @@ generateCode expr =
164164 jsBlock $
165165 VarDeclStmt () (zipWith varDecl tempNames args')
166166 : zipWith reassign argNames tempNames
167− ++ [ContinueStmt () (Just (Id () name))]
167+ ++ [ContinueStmt () (Just (Id () (Var.safe name)))]
168168
169169 Let defs body ->
170170 do stmts <- mapM generateDef defs
@@ -226,7 +226,7 @@ generateTailFunction name args body =
226226 do code <- generateCode body
227227 return $ generateFunctionWithArity args $ JsBlock $ (:[]) $
228228 LabelledStmt ()
229− (Id () name)
229+ (Id () (Var.safe name))
230230 (WhileStmt () (BoolLit () True) (toStatement code))
231231
232232
tests/test-files/good-expected-js/CodeGenBugs/TailRecursion_UnsafeLabel.elm.jsnew file+30
@@ -0,0 +1,30 @@
1+Elm.Main = Elm.Main || {};
2+Elm.Main.make = function (_elm) {
3+ "use strict";
4+ _elm.Main = _elm.Main || {};
5+ if (_elm.Main.values) return _elm.Main.values;
6+ var _U = Elm.Native.Utils.make(_elm);
7+ var _op = {};
8+ var foldl$ = F3(function (update,state,items) {
9+ foldl$: while (true) {
10+ var _p0 = items;
11+ if (_p0.ctor === "Nil") {
12+ return state;
13+ } else {
14+ var _v1 = update,_v2 = A2(update,_p0._0,state),_v3 = _p0._1;
15+ update = _v1;
16+ state = _v2;
17+ items = _v3;
18+ continue foldl$;
19+ }
20+ }
21+ });
22+ var Cons = F2(function (a,b) {
23+ return {ctor: "Cons",_0: a,_1: b};
24+ });
25+ var Nil = {ctor: "Nil"};
26+ return _elm.Main.values = {_op: _op
27+ ,Nil: Nil
28+ ,Cons: Cons
29+ ,foldl$: foldl$};
30+};
tests/test-files/good/CodeGenBugs/TailRecursion_UnsafeLabel.elmnew file+20
@@ -0,0 +1,20 @@
1+-- From https://github.com/elm-lang/elm-compiler/issues/1104
2+--
3+-- When code can be tail call optimized, we use the name of the function as
4+-- the label. The label must be a safe name, not a raw thing. Otherwise it
5+-- could have primes in it!
6+
7+
8+type List a
9+ = Nil
10+ | Cons a (List a)
11+
12+
13+foldl' : (a -> b -> b) -> b -> List a -> b
14+foldl' update state items =
15+ case items of
16+ Nil ->
17+ state
18+
19+ Cons item rest ->
20+ foldl' update (update item state) rest