25
25
module Gen = struct
26
26
let unit = Longident. Lident " ()" |> Location. mknoloc
27
27
28
- let rec_flag recursive =
29
- if recursive then Asttypes. Recursive else Nonrecursive
30
-
31
28
(* Generates [let name = body]. *)
32
- let toplevel_let ~recursive ~ name ~body =
29
+ let toplevel_let ~name ~body =
33
30
let open Ast_helper in
34
31
let pattern = Pat. mk (Ppat_var { txt = name; loc = Location. none }) in
35
32
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 ]
37
34
38
35
(* Generates [let name () = body]. *)
39
- let let_unit_toplevel ~recursive ~ name ~body =
36
+ let let_unit_toplevel ~name ~body =
40
37
let open Ast_helper in
41
38
let unit_param =
42
39
{ Parsetree. pparam_loc = Location. none;
43
40
pparam_desc = Pparam_val (Nolabel , None , Pat. construct unit None )
44
41
}
45
42
in
46
43
let body = Exp. function_ [ unit_param ] None (Pfunction_body body) in
47
- toplevel_let ~recursive ~ name ~body
44
+ toplevel_let ~name ~body
48
45
49
46
(* Generates [let name params = body]. *)
50
- let toplevel_function ~ recursive params ~name ~body =
47
+ let toplevel_function params ~name ~body =
51
48
let open Ast_helper in
52
49
let params =
53
50
List. map
@@ -63,7 +60,7 @@ module Gen = struct
63
60
params
64
61
in
65
62
let body = Exp. function_ params None (Pfunction_body body) in
66
- toplevel_let ~recursive ~ name ~body
63
+ toplevel_let ~name ~body
67
64
68
65
let ident ~name =
69
66
Longident. Lident name |> Location. mknoloc |> Ast_helper.Exp. ident
@@ -121,14 +118,11 @@ and toplevel_item = { rec_flag : Asttypes.rec_flag; loc : Location.t }
121
118
(* A convenient type for grouping info. *)
122
119
123
120
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
128
122
129
123
and generated_call = name:string -> Parsetree. expression
130
124
131
- let is_recursive_vb = function
125
+ let is_recursive = function
132
126
| { rec_flag = Asttypes. Recursive ; _ } -> true
133
127
| { rec_flag = Nonrecursive ; _ } -> false
134
128
@@ -170,7 +164,6 @@ let analyze_expr expr env ~toplevel_item ~mconfig ~local_defs =
170
164
loc_ghost = false
171
165
}
172
166
in
173
- let is_parent_recursive = is_recursive_vb toplevel_item in
174
167
Browse_tree. of_node ~env (Browse_raw. Expression expr)
175
168
|> occuring_vars
176
169
|> List. fold_left
@@ -182,10 +175,13 @@ let analyze_expr expr env ~toplevel_item ~mconfig ~local_defs =
182
175
~env ~local_defs ~namespace: Value var_path
183
176
with
184
177
| `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
189
185
&& Location_aux. included location ~into: toplevel_item.loc
190
186
then { acc with gen_binding_kind = Rec_and }
191
187
else acc
@@ -235,15 +231,13 @@ let extract_to_toplevel
235
231
match gen_binding_kind with
236
232
| Non_recursive ->
237
233
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
241
235
|> Format. asprintf " %a" Pprintast. structure_item
242
236
in
243
237
fresh_let_binding ^ " \n " ^ substitued_toplevel_binding
244
238
| Rec_and ->
245
239
let fresh_let_binding =
246
- generated_binding ~recursive: false ~ name: val_name ~body: untyped_expr
240
+ generated_binding ~name: val_name ~body: untyped_expr
247
241
|> Format. asprintf " %a" Pprintast. structure_item
248
242
in
249
243
let fresh_and_binding =
@@ -260,7 +254,7 @@ let extract_to_toplevel
260
254
let prefix_length =
261
255
match gen_binding_kind with
262
256
| 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 "
264
258
else String. length " let "
265
259
| Rec_and -> String. length " and "
266
260
in
0 commit comments