Skip to content

Commit 68bb10f

Browse files
committed
More syntax upgrade
1 parent 8cab4a4 commit 68bb10f

7 files changed

+76
-40
lines changed

jscomp/syntax/ast_core_type_class_type.ml

+9-3
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,13 @@ let process_getter_setter ~not_getter_setter ~get ~set
5252
in
5353
if st.set = None then get_acc
5454
else
55-
set ty (name ^ Literals.setter_suffix) pctf_attributes
55+
set ty
56+
#if OCAML_VERSION =~ ">4.03.0" then
57+
({name with txt = name.Asttypes.txt ^ Literals.setter_suffix} : _ Asttypes.loc)
58+
#else
59+
(name ^ Literals.setter_suffix)
60+
#end
61+
pctf_attributes
5662
:: get_acc
5763

5864

@@ -101,7 +107,7 @@ let handle_class_type_field self
101107
private_flag,
102108
virtual_flag,
103109
Ast_util.to_method_type
104-
loc self "" ty
110+
loc self Ast_compatible.no_label ty
105111
(Ast_literal.type_unit ~loc ())
106112
);
107113
pctf_attributes} in
@@ -181,7 +187,7 @@ let handle_core_type
181187
| Meth_callback attr, attrs ->
182188
attrs, attr +> ty
183189
in
184-
name, attrs, Ast_util.to_method_type loc self "" core_type
190+
name, attrs, Ast_util.to_method_type loc self Ast_compatible.no_label core_type
185191
(Ast_literal.type_unit ~loc ()) in
186192
let not_getter_setter ty =
187193
let attrs, core_type =

jscomp/syntax/ast_exp_apply.ml

+4-4
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ let handle_exp_apply
4545
(e : exp)
4646
(self : Bs_ast_mapper.mapper)
4747
(fn : exp)
48-
(args : (Asttypes.label * Parsetree.expression) list)
48+
(args : (Ast_compatible.arg_label * Parsetree.expression) list)
4949
=
5050
let loc = e.pexp_loc in
5151
begin match fn.pexp_desc with
@@ -80,7 +80,7 @@ let handle_exp_apply
8080
let fn = self.expr self fn in
8181
let args = Ext_list.map (fun (lab,exp) -> lab, self.expr self exp ) args in
8282
Bs_ast_invariant.warn_unused_attributes pexp_attributes;
83-
{ pexp_desc = Pexp_apply(fn, ("", new_obj_arg) :: args);
83+
{ pexp_desc = Pexp_apply(fn, (Ast_compatible.no_label, new_obj_arg) :: args);
8484
pexp_attributes = [];
8585
pexp_loc = pexp_loc}
8686
| {pexp_desc = Pexp_construct(ctor,None); pexp_loc; pexp_attributes} ->
@@ -99,7 +99,7 @@ let handle_exp_apply
9999
let fn = self.expr self fn in
100100
let args = Ext_list.map (fun (lab,exp) -> lab, self.expr self exp ) args in
101101
Bs_ast_invariant.warn_unused_attributes pexp_attributes;
102-
{ Parsetree.pexp_desc = Pexp_apply(fn, ("", bounded_obj_arg) :: args);
102+
{ Parsetree.pexp_desc = Pexp_apply(fn, (Ast_compatible.no_label, bounded_obj_arg) :: args);
103103
pexp_attributes = [];
104104
pexp_loc = pexp_loc}
105105
| {pexp_desc = Pexp_construct(ctor,None); pexp_loc; pexp_attributes}
@@ -184,7 +184,7 @@ let handle_exp_apply
184184
{ e with
185185
pexp_desc =
186186
Ast_util.method_apply loc self obj
187-
(name ^ Literals.setter_suffix) ["", arg ] }
187+
(name ^ Literals.setter_suffix) [Ast_compatible.no_label, arg ] }
188188
(Ast_literal.type_unit ~loc ())
189189
| _ -> Bs_ast_mapper.default_mapper.expr self e
190190
end

jscomp/syntax/ast_exp_extension.ml

+21-3
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,13 @@ let rec unroll_function_aux
2727
(acc : string list)
2828
(body : Parsetree.expression) : string list * string =
2929
match body.pexp_desc with
30-
| Pexp_constant(Const_string(block,_)) -> acc, block
30+
| Pexp_constant(
31+
#if OCAML_VERSION =~ ">4.03.0" then
32+
Pconst_string
33+
#else
34+
Const_string
35+
#end
36+
(block,_)) -> acc, block
3137
| Pexp_fun(arg_label,_,{ppat_desc = Ppat_var s},cont)
3238
when Ast_compatible.is_arg_label_simple arg_label ->
3339
unroll_function_aux (s.txt::acc) cont
@@ -59,7 +65,13 @@ let handle_extension record_as_js_object e (self : Bs_ast_mapper.mapper)
5965
when Ast_compatible.is_arg_label_simple arg_label
6066
->
6167
begin match pat.ppat_desc, body.pexp_desc with
62-
| Ppat_construct ({txt = Lident "()"}, None), Pexp_constant(Const_string(block,_))
68+
| Ppat_construct ({txt = Lident "()"}, None), Pexp_constant(
69+
#if OCAML_VERSION =~ ">4.03.0" then
70+
Pconst_string
71+
#else
72+
Const_string
73+
#end
74+
(block,_))
6375
->
6476
Ast_compatible.app1 ~loc
6577
(Exp.ident ~loc {txt = Ldot (Ast_literal.Lid.js_unsafe, Literals.raw_function);loc})
@@ -162,7 +174,13 @@ let handle_extension record_as_js_object e (self : Bs_ast_mapper.mapper)
162174
(Exp.construct ~loc {txt = Lident "false";loc} None)
163175
else
164176
(raiseWithString locString)
165-
| Pexp_constant (Const_string (r, _)) ->
177+
| Pexp_constant (
178+
#if OCAML_VERSION =~ ">4.03.0" then
179+
Pconst_string
180+
#else
181+
Const_string
182+
#end
183+
(r, _)) ->
166184
if !Clflags.noassert then
167185
Exp.assert_ ~loc (Exp.construct ~loc {txt = Lident "true"; loc} None)
168186
(* Need special handling to make it type check*)

jscomp/syntax/ast_tdcls.ml

+6-7
Original file line numberDiff line numberDiff line change
@@ -66,17 +66,17 @@ let handleTdclsInSigi
6666
(Mty.typeof_ ~loc
6767
(Mod.constraint_ ~loc
6868
(Mod.structure ~loc [
69-
{ pstr_loc = loc;
70-
pstr_desc =
71-
Pstr_type newTdclsNewAttrs
72-
}] )
69+
Ast_compatible.rec_type_str ~loc newTdclsNewAttrs
70+
] )
7371
(Mty.signature ~loc [])) ) )
7472
:: (* include module type of struct [processed_code for checking like invariance ]end *)
7573
self.signature self codes
7674
)
7775
else
7876
Ast_signature.fuseAll ~loc
79-
( {psig_desc = Psig_type newTdclsNewAttrs; psig_loc = loc}::
77+
(
78+
Ast_compatible.rec_type_sig ~loc newTdclsNewAttrs
79+
::
8080
self.signature
8181
self
8282
(Ast_derive.gen_signature tdcls actions explict_nonrec))
@@ -100,8 +100,7 @@ let handleTdclsInStru
100100
let loc = str.pstr_loc in
101101
let originalTdclsNewAttrs = newTdcls tdcls newAttrs in
102102
let newStr : Parsetree.structure_item =
103-
{ pstr_desc = Pstr_type (self.type_declaration_list self originalTdclsNewAttrs);
104-
pstr_loc = loc}
103+
Ast_compatible.rec_type_str ~loc (self.type_declaration_list self originalTdclsNewAttrs)
105104
in
106105
if Ast_payload.isAbstract actions then
107106
let codes = Ast_derive_abstract.handleTdclsInStr originalTdclsNewAttrs in

jscomp/syntax/ast_util.ml

+32-19
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,14 @@
2525
open Ast_helper
2626
type 'a cxt = Ast_helper.loc -> Bs_ast_mapper.mapper -> 'a
2727
type loc = Location.t
28-
type args = (string * Parsetree.expression) list
28+
type args = (Ast_compatible.arg_label * Parsetree.expression) list
2929
type label_exprs = (Longident.t Asttypes.loc * Parsetree.expression) list
3030
type uncurry_expression_gen =
3131
(Parsetree.pattern ->
3232
Parsetree.expression ->
3333
Parsetree.expression_desc) cxt
3434
type uncurry_type_gen =
35-
(string ->
35+
(Ast_compatible.arg_label ->
3636
Parsetree.core_type ->
3737
Parsetree.core_type ->
3838
Parsetree.core_type) cxt
@@ -48,9 +48,16 @@ let method_call_back_id =
4848

4949
let arity_lit = "Arity_"
5050

51-
let mk_args loc n tys =
51+
let mk_args loc (n : int) (tys : Parsetree.core_type list) : Parsetree.core_type =
5252
Typ.variant ~loc
53-
[ Rtag (arity_lit ^ string_of_int n, [], (n = 0), tys)] Closed None
53+
[ Rtag (
54+
#if OCAML_VERSION =~ ">4.03.0" then
55+
{loc; txt = arity_lit ^ string_of_int n}
56+
#else
57+
arity_lit ^ string_of_int n
58+
#end
59+
,
60+
[], (n = 0), tys)] Closed None
5461

5562
let generic_lift txt loc args result =
5663
let xs =
@@ -89,13 +96,19 @@ let lift_js_method_callback loc
8996
let arrow = Ast_compatible.arrow
9097

9198

92-
let js_property loc obj name =
99+
let js_property loc obj (name : string) =
93100
Parsetree.Pexp_send
94101
((Ast_compatible.app1 ~loc
95102
(Exp.ident ~loc
96103
{loc;
97104
txt = Ldot (Ast_literal.Lid.js_unsafe, Literals.unsafe_downgrade)})
98-
obj), name)
105+
obj),
106+
#if OCAML_VERSION =~ ">4.03.0" then
107+
{loc; txt = name}
108+
#else
109+
name
110+
#end
111+
)
99112

100113
(* TODO:
101114
have a final checking for property arities
@@ -110,7 +123,7 @@ let generic_apply kind loc
110123
let obj = self.expr self obj in
111124
let args =
112125
Ext_list.map (fun (label,e) ->
113-
if label <> "" then
126+
if not (Ast_compatible.is_arg_label_simple label) then
114127
Bs_syntaxerr.err loc Label_in_uncurried_bs_attribute;
115128
self.expr self e
116129
) args in
@@ -133,7 +146,7 @@ let generic_apply kind loc
133146
Longident.Ldot(Ast_literal.Lid.js_unsafe,
134147
Literals.method_run ^ string_of_int arity
135148
) in
136-
Parsetree.Pexp_apply (Exp.ident {txt ; loc}, ("",fn) :: Ext_list.map (fun x -> "",x) args)
149+
Parsetree.Pexp_apply (Exp.ident {txt ; loc}, (Ast_compatible.no_label,fn) :: Ext_list.map (fun x -> Ast_compatible.no_label,x) args)
137150
else
138151
let fn_type, args_type, result_type = Ast_comb.tuple_type_pair ~loc `Run arity in
139152
let string_arity = string_of_int arity in
@@ -164,7 +177,7 @@ let method_apply loc self obj name args =
164177
let generic_to_uncurry_type kind loc (mapper : Bs_ast_mapper.mapper) label
165178
(first_arg : Parsetree.core_type)
166179
(typ : Parsetree.core_type) =
167-
if label <> "" then
180+
if not (Ast_compatible.is_arg_label_simple label) then
168181
Bs_syntaxerr.err loc Label_in_uncurried_bs_attribute;
169182

170183
let rec aux acc (typ : Parsetree.core_type) =
@@ -178,7 +191,7 @@ let generic_to_uncurry_type kind loc (mapper : Bs_ast_mapper.mapper) label
178191
begin match typ.ptyp_desc with
179192
| Ptyp_arrow (label, arg, body)
180193
->
181-
if label <> "" then
194+
if not (Ast_compatible.is_arg_label_simple label) then
182195
Bs_syntaxerr.err typ.ptyp_loc Label_in_uncurried_bs_attribute;
183196
aux (mapper.typ mapper arg :: acc) body
184197
| _ -> mapper.typ mapper typ, acc
@@ -261,7 +274,7 @@ let generic_to_uncurry_exp kind loc (self : Bs_ast_mapper.mapper) pat body
261274
Longident.Ldot ( Ast_literal.Lid.js_unsafe, Literals.fn_mk ^ string_of_int arity)
262275
| `Method_callback ->
263276
Longident.Ldot (Ast_literal.Lid.js_unsafe, Literals.fn_method ^ string_of_int arity) in
264-
Parsetree.Pexp_apply (Exp.ident {txt;loc} , ["",body])
277+
Parsetree.Pexp_apply (Exp.ident {txt;loc} , [ Ast_compatible.no_label, body])
265278

266279
else
267280
let pval_prim =
@@ -291,7 +304,7 @@ let handle_debugger loc (payload : Ast_payload.t) =
291304
| PStr [] ->
292305
Parsetree.Pexp_apply
293306
(Exp.ident {txt = Ldot(Ast_literal.Lid.js_unsafe, Literals.debugger ); loc},
294-
["", Ast_literal.val_unit ~loc ()])
307+
[ Ast_compatible.no_label, Ast_literal.val_unit ~loc ()])
295308
| _ ->
296309
Location.raise_errorf ~loc "bs.debugger does not accept payload"
297310

@@ -310,7 +323,7 @@ let handle_raw ~check_js_regex loc payload =
310323
txt =
311324
Ldot (Ast_literal.Lid.js_unsafe,
312325
Literals.raw_expr)},
313-
["",exp]
326+
[Ast_compatible.no_label,exp]
314327
)
315328
in
316329
{ exp with pexp_desc }
@@ -352,7 +365,7 @@ let handle_raw_structure loc payload =
352365
let pexp_desc =
353366
Parsetree.Pexp_apply(
354367
Exp.ident {txt = Ldot (Ast_literal.Lid.js_unsafe, Literals.raw_stmt); loc},
355-
["",exp]) in
368+
[ Ast_compatible.no_label,exp]) in
356369
Ast_helper.Str.eval
357370
{ exp with pexp_desc }
358371

@@ -393,7 +406,7 @@ let ocaml_obj_as_js_object
393406
((val_name , [], result ) ::
394407
(if is_mutable then
395408
[val_name ^ Literals.setter_suffix,[],
396-
to_method_type loc mapper "" result (Ast_literal.type_unit ~loc ()) ]
409+
to_method_type loc mapper Ast_compatible.no_label result (Ast_literal.type_unit ~loc ()) ]
397410
else
398411
[]) )
399412
in
@@ -406,7 +419,7 @@ let ocaml_obj_as_js_object
406419
method_name arity : Ast_core_type.t =
407420
let result = Typ.var ~loc method_name in
408421
if arity = 0 then
409-
to_method_type loc mapper "" (Ast_literal.type_unit ~loc ()) result
422+
to_method_type loc mapper Ast_compatible.no_label (Ast_literal.type_unit ~loc ()) result
410423

411424
else
412425
let tyvars =
@@ -417,7 +430,7 @@ let ocaml_obj_as_js_object
417430
let method_rest =
418431
Ext_list.fold_right (fun v acc -> Ast_compatible.arrow ~loc v acc)
419432
rest result in
420-
to_method_type loc mapper "" x method_rest
433+
to_method_type loc mapper Ast_compatible.no_label x method_rest
421434
| _ -> assert false
422435
end in
423436

@@ -434,7 +447,7 @@ let ocaml_obj_as_js_object
434447
| Some ty -> Typ.alias ~loc ty self_type_lit
435448
in
436449
if arity = 0 then
437-
to_method_callback_type loc mapper "" self_type result
450+
to_method_callback_type loc mapper Ast_compatible.no_label self_type result
438451
else
439452
let tyvars =
440453
Ext_list.init arity (fun i -> Typ.var ~loc (method_name ^ string_of_int i))
@@ -444,7 +457,7 @@ let ocaml_obj_as_js_object
444457
let method_rest =
445458
Ext_list.fold_right (fun v acc -> Ast_compatible.arrow ~loc v acc)
446459
rest result in
447-
(to_method_callback_type loc mapper "" self_type
460+
(to_method_callback_type loc mapper Ast_compatible.no_label self_type
448461
(Ast_compatible.arrow ~loc x method_rest))
449462
| _ -> assert false
450463
end in

jscomp/syntax/ast_util.mli

+2-2
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

2525

26-
type args = (string * Parsetree.expression) list
26+
type args = (Ast_compatible.arg_label * Parsetree.expression) list
2727
type loc = Location.t
2828
type label_exprs = (Longident.t Asttypes.loc * Parsetree.expression) list
2929
type 'a cxt = loc -> Bs_ast_mapper.mapper -> 'a
@@ -38,7 +38,7 @@ type uncurry_expression_gen =
3838
Parsetree.expression ->
3939
Parsetree.expression_desc) cxt
4040
type uncurry_type_gen =
41-
(string -> (* label for error checking *)
41+
(Ast_compatible.arg_label -> (* label for error checking *)
4242
Parsetree.core_type ->
4343
Parsetree.core_type ->
4444
Parsetree.core_type) cxt

jscomp/syntax/external_process.ml

+2-2
Original file line numberDiff line numberDiff line change
@@ -558,7 +558,7 @@ let handle_attributes
558558
(* ?x:([`x of int ] [@bs.string]) does not make sense *)
559559
Location.raise_errorf
560560
~loc
561-
"[@@bs.string] does not work with optional when it has arities in label %s" label
561+
"[@@bs.string] does not work with optional when it has arities in label %s" s
562562
| _ ->
563563
External_arg_spec.optional s, arg_type,
564564
((label, Ast_core_type.lift_option_type new_ty , attr,loc) :: arg_types) end
@@ -599,7 +599,7 @@ let handle_attributes
599599
(* more error checking *)
600600
[External_arg_spec.empty_kind arg_type]
601601
,
602-
["", new_ty, [], obj.ptyp_loc]
602+
[Ast_compatible.no_label, new_ty, [], obj.ptyp_loc]
603603
,0
604604
end
605605

0 commit comments

Comments
 (0)