Skip to content

Commit ffe810f

Browse files
authored
Merge pull request #4207 from BuckleScript/uniform_nonrecur_handling
uniform nonrecursive handling for bs.deriving
2 parents c28c6ae + d5aa5c4 commit ffe810f

19 files changed

+284
-271
lines changed

jscomp/main/native_ppx_main.ml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,17 +66,17 @@ let typ_mapper (self : mapper) (typ : Parsetree.core_type) =
6666
let structure_item_mapper (self : mapper) (str : Parsetree.structure_item) =
6767
match str.pstr_desc with
6868
| Pstr_type (
69-
_rf,
69+
rf,
7070
(_ :: _ as tdcls )) ->
71-
Ast_tdcls.handleTdclsInStru self str tdcls
71+
Ast_tdcls.handleTdclsInStru self str rf tdcls
7272
| _ -> default_str_mapper self str
7373

7474
let signature_item_mapper (self : mapper) (sigi : Parsetree.signature_item) =
7575
match sigi.psig_desc with
7676
| Psig_type (
77-
_rf,
77+
rf,
7878
(_ :: _ as tdcls)) -> (*FIXME: check recursive handling*)
79-
Ast_tdcls.handleTdclsInSigi self sigi tdcls
79+
Ast_tdcls.handleTdclsInSigi self sigi rf tdcls
8080
| _ -> default_sig_mapper self sigi
8181

8282
let my_mapper : mapper = {

jscomp/syntax/ast_attributes.ml

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -154,12 +154,11 @@ let has_inline_payload_in_sig (attrs : t) =
154154
)
155155

156156
type derive_attr = {
157-
explict_nonrec : bool;
158157
bs_deriving : Ast_payload.action list option
159-
}
158+
} [@@unboxed]
160159

161160
let process_derive_type (attrs : t) : derive_attr * t =
162-
Ext_list.fold_left attrs ({explict_nonrec = false; bs_deriving = None }, [])
161+
Ext_list.fold_left attrs ({bs_deriving = None }, [])
163162
(fun (st, acc) ({txt ; loc}, payload as attr) ->
164163
match st, txt with
165164
| {bs_deriving = None}, "bs.deriving"
@@ -172,10 +171,6 @@ let process_derive_type (attrs : t) : derive_attr * t =
172171
Bs_syntaxerr.err loc Duplicated_bs_deriving
173172

174173
| _ , _ ->
175-
let st =
176-
if txt = "nonrec" then
177-
{ st with explict_nonrec = true }
178-
else st in
179174
st, attr::acc
180175
)
181176

jscomp/syntax/ast_attributes.mli

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,8 @@ val has_inline_payload_in_sig :
5959
attr option
6060

6161
type derive_attr = {
62-
explict_nonrec : bool;
6362
bs_deriving : Ast_payload.action list option
64-
}
63+
} [@@unboxed]
6564

6665

6766
val iter_process_bs_string_int_unwrap_uncurry :

jscomp/syntax/ast_compatible.ml

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -196,27 +196,25 @@ let opt_arrow ?(loc=default_loc) ?(attrs=[]) s a b : core_type =
196196
ptyp_attributes = attrs
197197
}
198198

199-
let rec_type_str ?(loc=default_loc) tds : structure_item =
199+
let rec_type_str
200+
?(loc=default_loc)
201+
rf tds : structure_item =
200202
{
201203
pstr_loc = loc;
202204
pstr_desc = Pstr_type (
203-
Recursive,
205+
rf,
204206
tds)
205207
}
206208

207-
(* let nonrec_type_str ?(loc=default_loc) tds : structure_item =
208-
{
209-
pstr_loc = loc;
210-
pstr_desc = Pstr_type (
211-
Nonrecursive,
212-
tds)
213-
} *)
214209

215-
let rec_type_sig ?(loc=default_loc) tds : signature_item =
210+
211+
let rec_type_sig
212+
?(loc=default_loc)
213+
rf tds : signature_item =
216214
{
217215
psig_loc = loc;
218216
psig_desc = Psig_type (
219-
Recursive,
217+
rf,
220218
tds)
221219
}
222220

jscomp/syntax/ast_compatible.mli

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ val object_:
157157

158158
val rec_type_str:
159159
?loc:loc ->
160+
Asttypes.rec_flag ->
160161
type_declaration list ->
161162
structure_item
162163

@@ -167,6 +168,7 @@ val rec_type_str:
167168

168169
val rec_type_sig:
169170
?loc:loc ->
171+
Asttypes.rec_flag ->
170172
type_declaration list ->
171173
signature_item
172174

jscomp/syntax/ast_derive.ml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@
2525
type tdcls = Parsetree.type_declaration list
2626

2727
type gen = {
28-
structure_gen : tdcls -> bool -> Ast_structure.t ;
29-
signature_gen : tdcls -> bool -> Ast_signature.t ;
28+
structure_gen : tdcls -> Asttypes.rec_flag -> Ast_structure.t ;
29+
signature_gen : tdcls -> Asttypes.rec_flag -> Ast_signature.t ;
3030
expression_gen : (Parsetree.core_type -> Parsetree.expression) option ;
3131
}
3232

@@ -58,7 +58,7 @@ let register key value =
5858
let gen_signature
5959
tdcls
6060
(actions : Ast_payload.action list )
61-
(explict_nonrec : bool )
61+
(explict_nonrec : Asttypes.rec_flag )
6262
: Ast_signature.t =
6363
Ext_list.flat_map actions
6464
(fun action ->
@@ -80,7 +80,7 @@ let gen_structure_signature
8080
loc
8181
(tdcls : tdcls)
8282
(action : Ast_payload.action)
83-
(explicit_nonrec : bool) =
83+
(explicit_nonrec : Asttypes.rec_flag) =
8484
let derive_table = !derive_table in
8585
let u =
8686
Ast_payload.table_dispatch derive_table action in

jscomp/syntax/ast_derive.mli

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@
2525
type tdcls = Parsetree.type_declaration list
2626

2727
type gen = {
28-
structure_gen : tdcls -> bool -> Ast_structure.t ;
29-
signature_gen : tdcls -> bool -> Ast_signature.t ;
28+
structure_gen : tdcls -> Asttypes.rec_flag -> Ast_structure.t ;
29+
signature_gen : tdcls -> Asttypes.rec_flag -> Ast_signature.t ;
3030
expression_gen : (Parsetree.core_type -> Parsetree.expression) option ;
3131
}
3232

@@ -48,7 +48,7 @@ val register :
4848
val gen_signature:
4949
tdcls ->
5050
Ast_payload.action list ->
51-
bool ->
51+
Asttypes.rec_flag ->
5252
Ast_signature.t
5353

5454

@@ -63,5 +63,5 @@ val gen_structure_signature :
6363
Location.t ->
6464
Parsetree.type_declaration list ->
6565
Ast_payload.action ->
66-
bool ->
66+
Asttypes.rec_flag ->
6767
Parsetree.structure_item

jscomp/syntax/ast_derive_abstract.ml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -197,23 +197,23 @@ let handleTdcl
197197
(* U.notApplicable tdcl.ptype_loc derivingName; *)
198198
tdcl, []
199199

200-
let handleTdclsInStr ~light tdcls =
200+
let handleTdclsInStr ~light rf tdcls =
201201
let tdcls, code =
202202
Ext_list.fold_right tdcls ([],[]) (fun tdcl (tdcls, sts) ->
203203
match handleTdcl light tdcl with
204204
ntdcl, value_descriptions ->
205205
ntdcl::tdcls,
206206
Ext_list.map_append value_descriptions sts (fun x -> Str.primitive x)
207207
) in
208-
Ast_compatible.rec_type_str tdcls :: code
208+
Ast_compatible.rec_type_str rf tdcls :: code
209209
(* still need perform transformation for non-abstract type*)
210210

211-
let handleTdclsInSig ~light tdcls =
211+
let handleTdclsInSig ~light rf tdcls =
212212
let tdcls, code =
213213
Ext_list.fold_right tdcls ([],[]) (fun tdcl (tdcls, sts) ->
214214
match handleTdcl light tdcl with
215215
ntdcl, value_descriptions ->
216216
ntdcl::tdcls,
217217
Ext_list.map_append value_descriptions sts (fun x -> Sig.value x)
218218
) in
219-
Ast_compatible.rec_type_sig tdcls :: code
219+
Ast_compatible.rec_type_sig rf tdcls :: code

jscomp/syntax/ast_derive_abstract.mli

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,12 @@ val isAbstract :
3737

3838
val handleTdclsInStr :
3939
light:bool ->
40-
Parsetree.type_declaration list -> Parsetree.structure
40+
Asttypes.rec_flag ->
41+
Parsetree.type_declaration list ->
42+
Parsetree.structure
4143

4244
val handleTdclsInSig:
4345
light:bool ->
44-
Parsetree.type_declaration list -> Parsetree.signature
46+
Asttypes.rec_flag ->
47+
Parsetree.type_declaration list ->
48+
Parsetree.signature

jscomp/syntax/ast_derive_js_mapper.ml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,9 @@ let init () =
181181
let exp_param = Exp.ident ident_param in
182182
let newType,newTdcl =
183183
U.new_type_of_type_declaration tdcl ("abs_" ^ name) in
184-
let newTypeStr = Ast_compatible.rec_type_str [newTdcl] in
184+
let newTypeStr =
185+
(* Abstract type *)
186+
Ast_compatible.rec_type_str Nonrecursive [newTdcl] in
185187
let toJsBody body =
186188
Ast_comb.single_non_rec_value patToJs
187189
(Ast_compatible.fun_ (Pat.constraint_ (Pat.var pat_param) core_type)
@@ -428,7 +430,7 @@ let init () =
428430
Ast_comb.single_non_rec_val patToJs (Ast_compatible.arrow core_type result) in
429431
let newType,newTdcl =
430432
U.new_type_of_type_declaration tdcl ("abs_" ^ name) in
431-
let newTypeStr = Ast_compatible.rec_type_sig [newTdcl] in
433+
let newTypeStr = Ast_compatible.rec_type_sig Nonrecursive [newTdcl] in
432434
let (+?) v rest = if createType then v :: rest else rest in
433435
match tdcl.ptype_kind with
434436
| Ptype_record label_declarations ->

0 commit comments

Comments
 (0)