Skip to content

Commit 0a608f6

Browse files
cristianocclaude
andcommitted
Delete Bs_ast_mapper, keeping Ast_mapper at upstream shape
Bs_ast_mapper was a 600-line mechanical fork of the vendored Ast_mapper whose entire delta - still delimited by fossilized cppo (* #if *) comments - was three group-level hooks: type_declaration_list, value_bindings, and value_bindings_rec. Only value_bindings ever had a client (tuple/module-record pattern flattening for non-recursive lets); the other two were never overridden by anything. Meanwhile the fork had drifted: it dropped ~loc/~attrs when rebuilding JSX fragments and did not remap JSX element-name locations. Every parsetree change paid the duplication tax twice. Rather than folding hooks into Ast_mapper, keep it byte-identical to its upstream shape and move the one real behavior into the client: bs_builtin_ppx's default_expr_mapper is now a thin wrapper that flattens non-recursive Pexp_let groups before delegating, so every fallback in expr_mapper inherits the flattening uniformly (hello_res's nested [let {length: l, add} = module(List)] reaches it only through a fallback and pins this), and structure_item_mapper handles Pstr_value(Nonrecursive) the same way. Recursive groups map per binding - their patterns are restricted to variables, so group rewrites do not apply. Also: inline Ast_typ_uncurry's one function into its one consumer and delete the module; replace ast_external_mk's hand-rolled expression, structure, and module-expr records with Ast_helper constructors; replace ast_tdcls's two dead-hook accessor calls with plain List.map. Generated output is byte-identical across the test suite. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Signed-off-by: Cristiano Calcagno <cristianoc@users.noreply.github.com>
1 parent cf8dd8c commit 0a608f6

23 files changed

Lines changed: 94 additions & 872 deletions

compiler/frontend/ast_core_type_class_type.ml

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,27 @@ let process_getter_setter ~not_getter_setter
5656
pctf_attributes
5757
:: get_acc
5858

59-
let default_typ_mapper = Bs_ast_mapper.default_mapper.typ
59+
let default_typ_mapper = Ast_mapper.default_mapper.typ
6060
(*
6161
Attributes are very hard to attribute
6262
(since ptyp_attributes could happen in so many places),
6363
and write ppx extensions correctly,
6464
we can only use it locally
6565
*)
6666

67-
let typ_mapper (self : Bs_ast_mapper.mapper) (ty : Parsetree.core_type) =
67+
(* Turns [(t1, .., tn) => ret] with a method-callback attribute into
68+
[Js.MethodCallback.arityN<full type>]. *)
69+
let to_method_callback_type loc (mapper : Ast_mapper.mapper) ~arity
70+
(meth_type : Parsetree.core_type) =
71+
let meth_type = Ast_mapper.default_mapper.typ mapper meth_type in
72+
Ast_helper.Typ.constr
73+
{
74+
txt = Ldot (Ast_literal.Lid.method_callback, "arity" ^ string_of_int arity);
75+
loc;
76+
}
77+
[meth_type]
78+
79+
let typ_mapper (self : Ast_mapper.mapper) (ty : Parsetree.core_type) =
6880
let loc = ty.ptyp_loc in
6981
match ty.ptyp_desc with
7082
| Ptyp_arrow {params = _}
@@ -76,10 +88,9 @@ let typ_mapper (self : Bs_ast_mapper.mapper) (ty : Parsetree.core_type) =
7688
| Meth_callback _ -> (
7789
match ty.ptyp_desc with
7890
| Ptyp_arrow {params} ->
79-
Ast_typ_uncurry.to_method_callback_type loc self
80-
~arity:(List.length params) ty
91+
to_method_callback_type loc self ~arity:(List.length params) ty
8192
| _ -> assert false)
82-
| Nothing -> Bs_ast_mapper.default_mapper.typ self ty)
93+
| Nothing -> Ast_mapper.default_mapper.typ self ty)
8394
| Ptyp_object (methods, closed_flag) ->
8495
let ( +> ) attr (typ : Parsetree.core_type) =
8596
{typ with ptyp_attributes = attr :: typ.ptyp_attributes}

compiler/frontend/ast_core_type_class_type.mli

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,4 @@
2222
* along with this program; if not, write to the Free Software
2323
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
2424

25-
val typ_mapper :
26-
Bs_ast_mapper.mapper -> Parsetree.core_type -> Parsetree.core_type
25+
val typ_mapper : Ast_mapper.mapper -> Parsetree.core_type -> Parsetree.core_type

compiler/frontend/ast_exp_apply.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ let bound (e : exp) (cb : exp -> _) =
4242
[Vb.mk ~loc (Pat.var ~loc {txt = tuple_obj_id; loc}) e]
4343
(cb (Exp.ident ~loc {txt = Lident tuple_obj_id; loc}))
4444

45-
let default_expr_mapper = Bs_ast_mapper.default_mapper.expr
45+
let default_expr_mapper = Ast_mapper.default_mapper.expr
4646

4747
let check_and_discard (args : (Asttypes.arg_label * Parsetree.expression) list)
4848
=
@@ -72,7 +72,7 @@ let view_as_app (fn : exp) (s : string list) : app_pattern option =
7272

7373
let infix_ops = ["->"; "#="; "##"]
7474

75-
let app_exp_mapper (e : exp) (self : Bs_ast_mapper.mapper) : exp =
75+
let app_exp_mapper (e : exp) (self : Ast_mapper.mapper) : exp =
7676
match view_as_app e infix_ops with
7777
| Some {op = "->"; args = [a_; f_]; loc} -> (
7878
(*

compiler/frontend/ast_exp_apply.mli

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,4 @@
2323
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
2424

2525
val app_exp_mapper :
26-
Parsetree.expression -> Bs_ast_mapper.mapper -> Parsetree.expression
26+
Parsetree.expression -> Ast_mapper.mapper -> Parsetree.expression

compiler/frontend/ast_exp_extension.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
2424
open Ast_helper
2525

26-
let handle_extension e (self : Bs_ast_mapper.mapper)
26+
let handle_extension e (self : Ast_mapper.mapper)
2727
(({txt; loc}, payload) : Parsetree.extension) =
2828
match txt with
2929
| "todo" ->
@@ -81,4 +81,4 @@ let handle_extension e (self : Bs_ast_mapper.mapper)
8181
(* For an unknown extension, we don't really need to process further*)
8282
(* Exp.extension ~loc ~attrs:e.pexp_attributes (
8383
self.extension self extension) *)
84-
(* Bs_ast_mapper.default_mapper.expr self e *)
84+
(* Ast_mapper.default_mapper.expr self e *)

compiler/frontend/ast_exp_extension.mli

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,6 @@
2424

2525
val handle_extension :
2626
Parsetree.expression ->
27-
Bs_ast_mapper.mapper ->
27+
Ast_mapper.mapper ->
2828
Parsetree.extension ->
2929
Parsetree.expression

compiler/frontend/ast_external.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
* along with this program; if not, write to the Free Software
2323
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
2424

25-
let handle_external_in_sig (self : Bs_ast_mapper.mapper)
25+
let handle_external_in_sig (self : Ast_mapper.mapper)
2626
(prim : Parsetree.value_description) (sigi : Parsetree.signature_item) :
2727
Parsetree.signature_item =
2828
let loc = prim.pval_loc in
@@ -51,7 +51,7 @@ let handle_external_in_sig (self : Bs_ast_mapper.mapper)
5151
};
5252
})
5353

54-
let handle_external_in_stru (self : Bs_ast_mapper.mapper)
54+
let handle_external_in_stru (self : Ast_mapper.mapper)
5555
(prim : Parsetree.value_description) (str : Parsetree.structure_item) :
5656
Parsetree.structure_item =
5757
let loc = prim.pval_loc in

compiler/frontend/ast_external.mli

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@
2323
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
2424

2525
val handle_external_in_sig :
26-
Bs_ast_mapper.mapper ->
26+
Ast_mapper.mapper ->
2727
Parsetree.value_description ->
2828
Parsetree.signature_item ->
2929
Parsetree.signature_item
3030

3131
val handle_external_in_stru :
32-
Bs_ast_mapper.mapper ->
32+
Ast_mapper.mapper ->
3333
Parsetree.value_description ->
3434
Parsetree.structure_item ->
3535
Parsetree.structure_item

compiler/frontend/ast_external_mk.ml

Lines changed: 26 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -28,70 +28,40 @@ let local_external_apply loc ?(pval_attributes = []) ~(pval_prim : string list)
2828
Parsetree.expression_desc =
2929
Pexp_letmodule
3030
( {txt = local_module_name; loc},
31-
{
32-
pmod_desc =
33-
Pmod_structure
34-
[
35-
{
36-
pstr_desc =
37-
Pstr_primitive
38-
{
39-
pval_name = {txt = local_fun_name; loc};
40-
pval_type;
41-
pval_loc = loc;
42-
pval_prim;
43-
pval_attributes;
44-
};
45-
pstr_loc = loc;
46-
};
47-
];
48-
pmod_loc = loc;
49-
pmod_attributes = [];
50-
},
31+
Ast_helper.Mod.structure ~loc
32+
[
33+
Ast_helper.Str.primitive ~loc
34+
{
35+
pval_name = {txt = local_fun_name; loc};
36+
pval_type;
37+
pval_loc = loc;
38+
pval_prim;
39+
pval_attributes;
40+
};
41+
],
5142
Ast_helper.Exp.apply ~loc
52-
({
53-
pexp_desc =
54-
Pexp_ident
55-
{txt = Ldot (Lident local_module_name, local_fun_name); loc};
56-
pexp_attributes = [];
57-
pexp_loc = loc;
58-
}
59-
: Parsetree.expression)
43+
(Ast_helper.Exp.ident ~loc
44+
{txt = Ldot (Lident local_module_name, local_fun_name); loc})
6045
(Ext_list.map args (fun x -> (Asttypes.Nolabel, x))) )
6146

6247
let local_external_obj loc ?(pval_attributes = []) ~pval_prim ~pval_type
6348
?(local_module_name = "J") ?(local_fun_name = "unsafe_expr") args :
6449
Parsetree.expression_desc =
6550
Pexp_letmodule
6651
( {txt = local_module_name; loc},
67-
{
68-
pmod_desc =
69-
Pmod_structure
70-
[
71-
{
72-
pstr_desc =
73-
Pstr_primitive
74-
{
75-
pval_name = {txt = local_fun_name; loc};
76-
pval_type;
77-
pval_loc = loc;
78-
pval_prim;
79-
pval_attributes;
80-
};
81-
pstr_loc = loc;
82-
};
83-
];
84-
pmod_loc = loc;
85-
pmod_attributes = [];
86-
},
52+
Ast_helper.Mod.structure ~loc
53+
[
54+
Ast_helper.Str.primitive ~loc
55+
{
56+
pval_name = {txt = local_fun_name; loc};
57+
pval_type;
58+
pval_loc = loc;
59+
pval_prim;
60+
pval_attributes;
61+
};
62+
],
8763
Ast_helper.Exp.apply ~loc
88-
({
89-
pexp_desc =
90-
Pexp_ident
91-
{txt = Ldot (Lident local_module_name, local_fun_name); loc};
92-
pexp_attributes = [];
93-
pexp_loc = loc;
94-
}
95-
: Parsetree.expression)
64+
(Ast_helper.Exp.ident ~loc
65+
{txt = Ldot (Lident local_module_name, local_fun_name); loc})
9666
(Ext_list.map args (fun (l, a) ->
9767
(Asttypes.Labelled {txt = l; loc = Location.none}, a))) )

compiler/frontend/ast_tdcls.ml

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ let new_tdcls (tdcls : Parsetree.type_declaration list)
3535
Ext_list.map_last tdcls (fun last x ->
3636
if last then {x with Parsetree.ptype_attributes = new_attrs} else x)
3737

38-
let handle_tdcls_in_sigi (self : Bs_ast_mapper.mapper)
38+
let handle_tdcls_in_sigi (self : Ast_mapper.mapper)
3939
(sigi : Parsetree.signature_item) rf
4040
(tdcls : Parsetree.type_declaration list) : Ast_signature.item =
4141
match
@@ -46,7 +46,7 @@ let handle_tdcls_in_sigi (self : Bs_ast_mapper.mapper)
4646
let original_tdcls_new_attrs = new_tdcls tdcls new_attrs in
4747
(* remove the processed attr*)
4848
let new_tdcls_new_attrs =
49-
self.type_declaration_list self original_tdcls_new_attrs
49+
List.map (self.type_declaration self) original_tdcls_new_attrs
5050
in
5151
let kind = Ast_derive_abstract.is_abstract actions in
5252
if kind <> Not_abstract then
@@ -68,9 +68,9 @@ let handle_tdcls_in_sigi (self : Bs_ast_mapper.mapper)
6868
(Sig.type_ ~loc rf new_tdcls_new_attrs
6969
:: self.signature self (Ast_derive.gen_signature tdcls actions rf))
7070
| {bs_deriving = None}, _ ->
71-
Bs_ast_mapper.default_mapper.signature_item self sigi
71+
Ast_mapper.default_mapper.signature_item self sigi
7272

73-
let handle_tdcls_in_stru (self : Bs_ast_mapper.mapper)
73+
let handle_tdcls_in_stru (self : Ast_mapper.mapper)
7474
(str : Parsetree.structure_item) rf
7575
(tdcls : Parsetree.type_declaration list) : Ast_structure.item =
7676
match
@@ -81,7 +81,7 @@ let handle_tdcls_in_stru (self : Bs_ast_mapper.mapper)
8181
let original_tdcls_new_attrs = new_tdcls tdcls new_attrs in
8282
let new_str : Parsetree.structure_item =
8383
Str.type_ ~loc rf
84-
(self.type_declaration_list self original_tdcls_new_attrs)
84+
(List.map (self.type_declaration self) original_tdcls_new_attrs)
8585
in
8686
let kind = Ast_derive_abstract.is_abstract actions in
8787
if kind <> Not_abstract then
@@ -102,5 +102,4 @@ let handle_tdcls_in_stru (self : Bs_ast_mapper.mapper)
102102
(fun action ->
103103
Ast_derive.gen_structure_signature loc tdcls action rf)
104104
actions))
105-
| {bs_deriving = None}, _ ->
106-
Bs_ast_mapper.default_mapper.structure_item self str
105+
| {bs_deriving = None}, _ -> Ast_mapper.default_mapper.structure_item self str

0 commit comments

Comments
 (0)