diff --git a/compiler/gen/tests/gen_primitives.rs b/compiler/gen/tests/gen_primitives.rs index e5e7e5b817..89409319b5 100644 --- a/compiler/gen/tests/gen_primitives.rs +++ b/compiler/gen/tests/gen_primitives.rs @@ -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 + ); + } } diff --git a/compiler/mono/src/decision_tree.rs b/compiler/mono/src/decision_tree.rs index 41709cdcbb..ac1e9bd827 100644 --- a/compiler/mono/src/decision_tree.rs +++ b/compiler/mono/src/decision_tree.rs @@ -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)); }