diff --git a/src/passes/4-imperative_to_sugar/imperative_to_sugar.ml b/src/passes/4-imperative_to_sugar/imperative_to_sugar.ml index 359a3bb61..99433d272 100644 --- a/src/passes/4-imperative_to_sugar/imperative_to_sugar.ml +++ b/src/passes/4-imperative_to_sugar/imperative_to_sugar.ml @@ -552,13 +552,14 @@ and compile_for I.{binder;start;final;increment;body} = ok @@ restore_mutable_variable return_expr captured_name_list env_rec and compile_for_each I.{binder;collection;collection_type; body} = + let env_rec = Var.fresh () in let args = Var.fresh () in + let%bind element_names = ok @@ match snd binder with | Some v -> [fst binder;v] | None -> [fst binder] in - let env = Var.fresh () in let%bind body = compile_expression body in let%bind ((_,free_vars), body) = repair_mutable_variable_in_loops body element_names args in let for_body = add_to_end body @@ (O.e_record_accessor (O.e_variable args) (Label "0")) in @@ -582,9 +583,9 @@ and compile_for_each I.{binder;collection;collection_type; body} = | Map -> ok @@ O.C_MAP_FOLD | Set -> ok @@ O.C_SET_FOLD | List -> ok @@ O.C_LIST_FOLD in let fold = fun expr -> - O.e_let_in (env,None) false false (O.e_constant op_name [lambda; collect ; init_record]) expr + O.e_let_in (env_rec,None) false false (O.e_constant op_name [lambda; collect ; init_record]) expr in - ok @@ restore_mutable_variable fold free_vars env + ok @@ restore_mutable_variable fold free_vars env_rec let compile_declaration : I.declaration Location.wrap -> _ = fun {wrap_content=declaration;location} ->