Skip to content

Commit 5a6c2e8

Browse files
committed
Fix rec flag generation.
1 parent 0f7491b commit 5a6c2e8

File tree

1 file changed

+18
-24
lines changed

1 file changed

+18
-24
lines changed

src/analysis/refactor_extract_region.ml

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -25,29 +25,26 @@ end
2525
module Gen = struct
2626
let unit = Longident.Lident "()" |> Location.mknoloc
2727

28-
let rec_flag recursive =
29-
if recursive then Asttypes.Recursive else Nonrecursive
30-
3128
(* Generates [let name = body]. *)
32-
let toplevel_let ~recursive ~name ~body =
29+
let toplevel_let ~name ~body =
3330
let open Ast_helper in
3431
let pattern = Pat.mk (Ppat_var { txt = name; loc = Location.none }) in
3532
let body = Parsetree_utils.filter_expr_attr body in
36-
Str.value (rec_flag recursive) [ Vb.mk pattern body ]
33+
Str.value Nonrecursive [ Vb.mk pattern body ]
3734

3835
(* Generates [let name () = body]. *)
39-
let let_unit_toplevel ~recursive ~name ~body =
36+
let let_unit_toplevel ~name ~body =
4037
let open Ast_helper in
4138
let unit_param =
4239
{ Parsetree.pparam_loc = Location.none;
4340
pparam_desc = Pparam_val (Nolabel, None, Pat.construct unit None)
4441
}
4542
in
4643
let body = Exp.function_ [ unit_param ] None (Pfunction_body body) in
47-
toplevel_let ~recursive ~name ~body
44+
toplevel_let ~name ~body
4845

4946
(* Generates [let name params = body]. *)
50-
let toplevel_function ~recursive params ~name ~body =
47+
let toplevel_function params ~name ~body =
5148
let open Ast_helper in
5249
let params =
5350
List.map
@@ -63,7 +60,7 @@ module Gen = struct
6360
params
6461
in
6562
let body = Exp.function_ params None (Pfunction_body body) in
66-
toplevel_let ~recursive ~name ~body
63+
toplevel_let ~name ~body
6764

6865
let ident ~name =
6966
Longident.Lident name |> Location.mknoloc |> Ast_helper.Exp.ident
@@ -121,14 +118,11 @@ and toplevel_item = { rec_flag : Asttypes.rec_flag; loc : Location.t }
121118
(* A convenient type for grouping info. *)
122119

123120
and generated_binding =
124-
recursive:bool ->
125-
name:string ->
126-
body:Parsetree.expression ->
127-
Parsetree.structure_item
121+
name:string -> body:Parsetree.expression -> Parsetree.structure_item
128122

129123
and generated_call = name:string -> Parsetree.expression
130124

131-
let is_recursive_vb = function
125+
let is_recursive = function
132126
| { rec_flag = Asttypes.Recursive; _ } -> true
133127
| { rec_flag = Nonrecursive; _ } -> false
134128

@@ -170,7 +164,6 @@ let analyze_expr expr env ~toplevel_item ~mconfig ~local_defs =
170164
loc_ghost = false
171165
}
172166
in
173-
let is_parent_recursive = is_recursive_vb toplevel_item in
174167
Browse_tree.of_node ~env (Browse_raw.Expression expr)
175168
|> occuring_vars
176169
|> List.fold_left
@@ -182,10 +175,13 @@ let analyze_expr expr env ~toplevel_item ~mconfig ~local_defs =
182175
~env ~local_defs ~namespace:Value var_path
183176
with
184177
| `Found { location; approximated = false; _ } ->
185-
if Location_aux.included location ~into:unbounded_enclosing then
186-
{ acc with bounded_vars = var_path :: acc.bounded_vars }
187-
else if
188-
is_parent_recursive
178+
let acc =
179+
if Location_aux.included location ~into:unbounded_enclosing then
180+
{ acc with bounded_vars = var_path :: acc.bounded_vars }
181+
else acc
182+
in
183+
if
184+
is_recursive toplevel_item
189185
&& Location_aux.included location ~into:toplevel_item.loc
190186
then { acc with gen_binding_kind = Rec_and }
191187
else acc
@@ -235,15 +231,13 @@ let extract_to_toplevel
235231
match gen_binding_kind with
236232
| Non_recursive ->
237233
let fresh_let_binding =
238-
generated_binding
239-
~recursive:(is_recursive_vb toplevel_item)
240-
~name:val_name ~body:untyped_expr
234+
generated_binding ~name:val_name ~body:untyped_expr
241235
|> Format.asprintf "%a" Pprintast.structure_item
242236
in
243237
fresh_let_binding ^ "\n" ^ substitued_toplevel_binding
244238
| Rec_and ->
245239
let fresh_let_binding =
246-
generated_binding ~recursive:false ~name:val_name ~body:untyped_expr
240+
generated_binding ~name:val_name ~body:untyped_expr
247241
|> Format.asprintf "%a" Pprintast.structure_item
248242
in
249243
let fresh_and_binding =
@@ -260,7 +254,7 @@ let extract_to_toplevel
260254
let prefix_length =
261255
match gen_binding_kind with
262256
| Non_recursive ->
263-
if is_recursive_vb toplevel_item then String.length "let rec "
257+
if is_recursive toplevel_item then String.length "let rec "
264258
else String.length "let "
265259
| Rec_and -> String.length "and "
266260
in

0 commit comments

Comments
 (0)