fix bug: incorrect var definition before switch

This commit is contained in:
Folkert 2021-01-18 23:52:58 +01:00
parent 0ac5a16e86
commit 83fd1de89b
2 changed files with 37 additions and 1 deletions

View file

@ -2081,4 +2081,40 @@ mod gen_primitives {
i64
);
}
#[test]
fn deriv_pow() {
// exposed bug with ordering of variable declarations before switch
assert_evals_to!(
indoc!(
r#"
app "test" provides [ main ] to "./platform"
Expr : [ Ln Expr, Pow Expr Expr, Var Str, Val I64 ]
count : Expr -> I64
count = \expr ->
when expr is
(Var _) -> 1
(Val n) -> n
(Pow f g) -> count f + count g
(Ln f) -> count f
pow : Expr, Expr -> Expr
pow = \a,b ->
when Pair a b is
Pair (Val _) (Val _) -> Val -1
Pair _ (Val 0) -> Val 1
Pair f (Val 1) -> f
Pair (Val 0) _ -> Val 0
Pair f g -> Pow f g
main : I64
main = count (pow (Var "x") (Var "x"))
"#
),
2,
i64
);
}
}

View file

@ -1533,7 +1533,7 @@ fn decide_to_branching<'a>(
ret_layout,
};
for (symbol, layout, expr) in cond_stores_vec.into_iter() {
for (symbol, layout, expr) in cond_stores_vec.into_iter().rev() {
switch = Stmt::Let(symbol, expr, layout, env.arena.alloc(switch));
}