Skip to content

Commit a07793a

Browse files
committed
Basic OCaml 5.4 support
- update vendored parsers to mirror upstream at 5.4: * introduce locations for Longident.t components * distinguish (module M:S) and ((module M):(module S)) for expressions - support for new syntaxes: * bivariance * labelled tuples
1 parent 1851bc4 commit a07793a

28 files changed

+986
-342
lines changed

lib/Ast.ml

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ let longident_is_simple c x =
6161
let rec length x =
6262
match x with
6363
| Longident.Lident x -> String.length x
64-
| Ldot (x, y) -> length x + 1 + String.length y
65-
| Lapply (x, y) -> length x + length y + 3
64+
| Ldot (x, y) -> length x.txt + 1 + String.length y.txt
65+
| Lapply (x, y) -> length x.txt + length y.txt + 3
6666
in
6767
longident_fit_margin c (length x)
6868

@@ -977,14 +977,15 @@ end = struct
977977
| Ptyp_alias (t1, _) | Ptyp_poly (_, t1) -> assert (typ == t1)
978978
| Ptyp_arrow (t, t2) ->
979979
assert (List.exists t ~f:(fun x -> typ == x.pap_type) || typ == t2)
980-
| Ptyp_tuple t1N | Ptyp_constr (_, t1N) -> assert (List.exists t1N ~f)
980+
| Ptyp_tuple t1N -> assert (List.exists t1N ~f:snd_f)
981+
| Ptyp_constr (_, t1N) -> assert (List.exists t1N ~f)
981982
| Ptyp_variant (r1N, _, _) ->
982983
assert (
983984
List.exists r1N ~f:(function
984985
| {prf_desc= Rtag (_, _, t1N); _} -> List.exists t1N ~f
985986
| {prf_desc= Rinherit t1; _} -> typ == t1 ) )
986987
| Ptyp_open (_, t1) -> assert (t1 == typ)
987-
| Ptyp_package (_, it1N, _) -> assert (List.exists it1N ~f:snd_f)
988+
| Ptyp_package ptyp -> assert (List.exists ptyp.ppt_cstrs ~f:snd_f)
988989
| Ptyp_object (fields, _) ->
989990
assert (
990991
List.exists fields ~f:(function
@@ -1017,15 +1018,15 @@ end = struct
10171018
match ctx.ppat_desc with
10181019
| Ppat_constraint (_, t1) -> assert (typ == t1)
10191020
| Ppat_extension (_, PTyp t) -> assert (typ == t)
1020-
| Ppat_unpack (_, Some (_, l, _)) ->
1021-
assert (List.exists l ~f:(fun (_, t) -> typ == t))
1021+
| Ppat_unpack (_, Some ptyp) ->
1022+
assert (List.exists ptyp.ppt_cstrs ~f:(fun (_, t) -> typ == t))
10221023
| Ppat_record (l, _) ->
10231024
assert (List.exists l ~f:(fun (_, t, _) -> Option.exists t ~f))
10241025
| _ -> assert false )
10251026
| Exp ctx -> (
10261027
match ctx.pexp_desc with
1027-
| Pexp_pack (_, Some (_, it1N, _), _) ->
1028-
assert (List.exists it1N ~f:snd_f)
1028+
| Pexp_pack (_, Some ptyp, _) ->
1029+
assert (List.exists ptyp.ppt_cstrs ~f:snd_f)
10291030
| Pexp_constraint (_, t1)
10301031
|Pexp_coerce (_, None, t1)
10311032
|Pexp_extension (_, PTyp t1) ->
@@ -1063,7 +1064,7 @@ end = struct
10631064
| Mod ctx -> (
10641065
match ctx.pmod_desc with
10651066
| Pmod_unpack (_, ty1, ty2) ->
1066-
let f (_, cstrs, _) = List.exists cstrs ~f:(fun (_, x) -> f x) in
1067+
let f ptyp = List.exists ptyp.ppt_cstrs ~f:snd_f in
10671068
assert (Option.exists ty1 ~f || Option.exists ty2 ~f)
10681069
| _ -> assert false )
10691070
| Sig ctx -> (
@@ -1255,7 +1256,9 @@ end = struct
12551256
| Pat ctx -> (
12561257
let f pI = pI == pat in
12571258
match ctx.ppat_desc with
1258-
| Ppat_array p1N | Ppat_list p1N | Ppat_tuple p1N | Ppat_cons p1N ->
1259+
| Ppat_tuple (p1N, _) ->
1260+
assert (List.exists p1N ~f:(fun (_, x) -> f x))
1261+
| Ppat_array p1N | Ppat_list p1N | Ppat_cons p1N ->
12591262
assert (List.exists p1N ~f)
12601263
| Ppat_record (p1N, _) ->
12611264
assert (List.exists p1N ~f:(fun (_, _, x) -> Option.exists x ~f))
@@ -1423,7 +1426,8 @@ end = struct
14231426
| Pexp_apply (e0, e1N) ->
14241427
(* FAIL *)
14251428
assert (e0 == exp || List.exists e1N ~f:snd_f)
1426-
| Pexp_tuple e1N | Pexp_array e1N | Pexp_list e1N | Pexp_cons e1N ->
1429+
| Pexp_tuple e1N -> assert (List.exists e1N ~f:snd_f)
1430+
| Pexp_array e1N | Pexp_list e1N | Pexp_cons e1N ->
14271431
assert (List.exists e1N ~f)
14281432
| Pexp_construct (_, e) | Pexp_variant (_, e) ->
14291433
assert (Option.exists e ~f)
@@ -1529,7 +1533,10 @@ end = struct
15291533
&& fit_margin c (width xexp)
15301534
| Pexp_construct (_, Some e0) | Pexp_variant (_, Some e0) ->
15311535
Exp.is_trivial e0
1532-
| Pexp_array e1N | Pexp_list e1N | Pexp_tuple e1N ->
1536+
| Pexp_tuple e1N ->
1537+
List.for_all e1N ~f:(snd >> Exp.is_trivial)
1538+
&& fit_margin c (width xexp)
1539+
| Pexp_array e1N | Pexp_list e1N ->
15331540
List.for_all e1N ~f:Exp.is_trivial && fit_margin c (width xexp)
15341541
| Pexp_record (e1N, e0) ->
15351542
Option.for_all e0 ~f:Exp.is_trivial
@@ -1631,7 +1638,7 @@ end = struct
16311638
| {ast= Typ _; _} -> None
16321639
| {ctx= Exp {pexp_desc; _}; ast= Exp exp} -> (
16331640
match pexp_desc with
1634-
| Pexp_tuple (e0 :: _) ->
1641+
| Pexp_tuple ((_, e0) :: _) ->
16351642
Some (Comma, if exp == e0 then Left else Right)
16361643
| Pexp_cons l ->
16371644
Some (ColonColon, if exp == List.last_exn l then Right else Left)
@@ -1848,6 +1855,9 @@ end = struct
18481855
( Str {pstr_desc= Pstr_exception _; _}
18491856
| Sig {psig_desc= Psig_exception _; _} ) } ->
18501857
true
1858+
| { ast= {ptyp_desc= Ptyp_tuple ((Some _, _) :: _); _}
1859+
; ctx= Typ {ptyp_desc= Ptyp_arrow _; _} } ->
1860+
true
18511861
| _ -> (
18521862
match ambig_prec (sub_ast ~ctx (Typ typ)) with
18531863
| `Ambiguous -> true
@@ -2087,7 +2097,7 @@ end = struct
20872097
|Pexp_try (_, cases, _) ->
20882098
continue (List.last_exn cases).pc_rhs
20892099
| Pexp_apply (_, args) -> continue (snd (List.last_exn args))
2090-
| Pexp_tuple es -> continue (List.last_exn es)
2100+
| Pexp_tuple es -> continue (snd @@ List.last_exn es)
20912101
| Pexp_array _ | Pexp_list _ | Pexp_coerce _ | Pexp_constant _
20922102
|Pexp_constraint _
20932103
|Pexp_construct (_, None)
@@ -2168,7 +2178,7 @@ end = struct
21682178
| Pexp_indexop_access {pia_rhs= rhs; _} -> (
21692179
match rhs with Some e -> continue e | None -> false )
21702180
| Pexp_apply (_, args) -> continue (snd (List.last_exn args))
2171-
| Pexp_tuple es -> continue (List.last_exn es)
2181+
| Pexp_tuple es -> continue (snd @@ List.last_exn es)
21722182
| Pexp_array _ | Pexp_list _ | Pexp_coerce _ | Pexp_constant _
21732183
|Pexp_constraint _
21742184
|Pexp_construct (_, None)
@@ -2220,7 +2230,7 @@ end = struct
22202230
&& Option.value_map ~default:false (prec_ast ctx) ~f:(fun p ->
22212231
Prec.compare p Apply < 0 ) ->
22222232
true
2223-
| Pexp_tuple e1N -> List.last_exn e1N == xexp.ast
2233+
| Pexp_tuple e1N -> snd (List.last_exn e1N) == xexp.ast
22242234
| _ -> false
22252235
in
22262236
match ambig_prec (sub_ast ~ctx (Exp exp)) with

lib/Conf.ml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1068,7 +1068,10 @@ module Formatting = struct
10681068

10691069
let module_indent =
10701070
let docv = "COLS" in
1071-
let doc = "Indentation of items within struct ... end and sig ... end ($(docv) columns)." in
1071+
let doc =
1072+
"Indentation of items within struct ... end and sig ... end ($(docv) \
1073+
columns)."
1074+
in
10721075
let names = ["module-indent"] in
10731076
Decl.int ~names ~default ~doc ~docv ~kind
10741077
(fun conf elt -> update conf ~f:(fun f -> {f with module_indent= elt}))

lib/Exposed.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ module Left = struct
1515
let rec core_type typ =
1616
match typ.ptyp_desc with
1717
| Ptyp_arrow (t :: _, _) -> core_type t.pap_type
18-
| Ptyp_tuple l -> core_type (List.hd_exn l)
18+
| Ptyp_tuple l -> core_type (snd @@ List.hd_exn l)
1919
| Ptyp_object _ -> true
2020
| Ptyp_alias (typ, _) -> core_type typ
2121
| _ -> false
@@ -29,7 +29,7 @@ module Right = struct
2929
| {ptyp_desc; _} -> (
3030
match ptyp_desc with
3131
| Ptyp_arrow (_, t) -> core_type t
32-
| Ptyp_tuple l -> core_type (List.last_exn l)
32+
| Ptyp_tuple l -> core_type (snd @@ List.last_exn l)
3333
| Ptyp_object _ -> true
3434
| _ -> false )
3535

lib/Extended_ast.ml

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -222,26 +222,6 @@ module Parse = struct
222222
&& not (Std_longident.is_monadic_binding longident) ->
223223
let label_loc = {txt= op; loc= loc_op} in
224224
{e with pexp_desc= Pexp_infix (label_loc, m.expr m l, m.expr m r)}
225-
(* [(module M) : (module T)] -> [(module M : T)] *)
226-
| { pexp_desc=
227-
Pexp_constraint
228-
( { pexp_desc=
229-
Pexp_pack (name, None, {infix_ext= None; infix_attrs= []})
230-
; pexp_attributes= []
231-
; pexp_loc
232-
; _ }
233-
, {ptyp_desc= Ptyp_package pt; ptyp_attributes= []; ptyp_loc; _}
234-
)
235-
; _ } as p
236-
when Migrate_ast.Location.compare_start ptyp_loc pexp_loc > 0 ->
237-
(* Match locations to differentiate between the two position for
238-
the constraint, we want to shorten the second: - [let _ :
239-
(module S) = (module M)] - [let _ = ((module M) : (module
240-
S))] *)
241-
{ p with
242-
pexp_desc=
243-
Pexp_pack (name, Some pt, {infix_ext= None; infix_attrs= []})
244-
}
245225
| e -> Ast_mapper.default_mapper.expr m e
246226
in
247227
Ast_mapper.{default_mapper with expr; pat; binding_op}

0 commit comments

Comments
 (0)