Skip to content

Commit 6928546

Browse files
committed
OCaml 5.4: labelled tuple and bivariance
Add support for labelled tuple (x:int * y:int) and bivariance in odoc model.
1 parent 4fe1af9 commit 6928546

File tree

15 files changed

+63
-24
lines changed

15 files changed

+63
-24
lines changed

sherlodoc/index/load_doc.ml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,8 @@ let searchable_type_of_constructor args res =
8181
match args with
8282
| TypeDecl.Constructor.Tuple args -> begin
8383
match args with
84-
| _ :: _ :: _ -> TypeExpr.(Arrow (None, Tuple args, res))
84+
| _ :: _ :: _ ->
85+
TypeExpr.(Arrow (None, Tuple (List.map (fun x -> None, x) args), res))
8586
| [ arg ] -> TypeExpr.(Arrow (None, arg, res))
8687
| _ -> res
8788
end

sherlodoc/index/type_cache.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,5 @@ let rec of_odoc ~cache otyp =
1919
| Arrow (_lbl, left, right) -> cache (Arrow (of_odoc ~cache left, of_odoc ~cache right))
2020
| Constr (name, args) ->
2121
cache (Constr (Typename.to_string name, List.map (of_odoc ~cache) args))
22-
| Tuple li -> cache (Tuple (List.map (of_odoc ~cache) li))
22+
| Tuple li -> cache (Tuple (List.map (fun (_, ty) -> of_odoc ~cache ty) li))
2323
| _ -> Unhandled

src/document/generator.ml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,13 @@ module Make (Syntax : SYNTAX) = struct
448448
let res =
449449
O.box_hv_no_indent
450450
(O.list lst ~sep:Syntax.Type.Tuple.element_separator
451-
~f:(type_expr ~needs_parentheses:true))
451+
~f:(fun (lbl, ty) ->
452+
match lbl with
453+
| None -> type_expr ~needs_parentheses:true ty
454+
| Some lbl ->
455+
tag "label" (O.txt lbl)
456+
++ O.txt ":" ++ O.cut
457+
++ type_expr ~needs_parentheses:true ty))
452458
in
453459
if Syntax.Type.Tuple.always_parenthesize || needs_parentheses then
454460
enclose ~l:"(" res ~r:")"
@@ -772,6 +778,7 @@ module Make (Syntax : SYNTAX) = struct
772778
| None -> desc
773779
| Some Odoc_model.Lang.TypeDecl.Pos -> "+" :: desc
774780
| Some Odoc_model.Lang.TypeDecl.Neg -> "-" :: desc
781+
| Some Odoc_model.Lang.TypeDecl.Bivariant -> "+" :: "-" :: desc
775782
in
776783
let final = if injectivity then "!" :: var_desc else var_desc in
777784
String.concat ~sep:"" final

src/loader/cmi.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -467,7 +467,7 @@ let rec read_type_expr env typ =
467467
let res = read_type_expr env res in
468468
Arrow(lbl, arg, res)
469469
| Ttuple typs ->
470-
let typs = List.map (read_type_expr env) typs in
470+
let typs = List.map (fun x -> None, read_type_expr env x) typs in
471471
Tuple typs
472472
| Tconstr(p, params, _) ->
473473
let p = Env.Path.read_type env.ident_env p in

src/loader/cmti.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ let rec read_core_type env container ctyp =
6464
let res = read_core_type env container res in
6565
Arrow(lbl, arg, res)
6666
| Ttyp_tuple typs ->
67-
let typs = List.map (read_core_type env container) typs in
67+
let typs = List.map (fun x -> None, read_core_type env container x) typs in
6868
Tuple typs
6969
| Ttyp_constr(p, _, params) ->
7070
let p = Env.Path.read_type env.ident_env p in

src/model/lang.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ and TypeDecl : sig
231231
| Extensible
232232
end
233233

234-
type variance = Pos | Neg
234+
type variance = Pos | Neg | Bivariant
235235

236236
type param_desc = Any | Var of string
237237

@@ -439,7 +439,7 @@ and TypeExpr : sig
439439
| Any
440440
| Alias of t * string
441441
| Arrow of label option * t * t
442-
| Tuple of t list
442+
| Tuple of (string option * t) list
443443
| Constr of Path.Type.t * t list
444444
| Polymorphic_variant of TypeExpr.Polymorphic_variant.t
445445
| Object of TypeExpr.Object.t

src/model_desc/lang_desc.ml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,8 @@ and typedecl_representation =
336336

337337
and typedecl_variance =
338338
let open Lang.TypeDecl in
339-
Variant (function Pos -> C0 "Pos" | Neg -> C0 "Neg")
339+
Variant
340+
(function Pos -> C0 "Pos" | Neg -> C0 "Neg" | Bivariant -> C0 "Bivariant")
340341

341342
and typedecl_param_desc =
342343
let open Lang.TypeDecl in
@@ -628,7 +629,7 @@ and typeexpr_t =
628629
( "Arrow",
629630
(x1, x2, x3),
630631
Triple (Option typeexpr_label, typeexpr_t, typeexpr_t) )
631-
| Tuple x -> C ("Tuple", x, List typeexpr_t)
632+
| Tuple x -> C ("Tuple", x, List (Pair (Option string, typeexpr_t)))
632633
| Constr (x1, x2) ->
633634
C ("Constr", ((x1 :> Paths.Path.t), x2), Pair (path, List typeexpr_t))
634635
| Polymorphic_variant x ->

src/search/html.ml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ let display_constructor_args args =
3535
match args with
3636
| TypeDecl.Constructor.Tuple args ->
3737
(match args with
38-
| _ :: _ :: _ -> Some TypeExpr.(Tuple args)
38+
| _ :: _ :: _ ->
39+
Some TypeExpr.(Tuple (List.map (fun x -> (None, x)) args))
3940
| [ arg ] -> Some arg
4041
| _ -> None)
4142
|> map_option Text.of_type
@@ -63,6 +64,7 @@ let typedecl_params ?(delim = `parens) params =
6364
| None -> desc
6465
| Some TypeDecl.Pos -> "+" :: desc
6566
| Some TypeDecl.Neg -> "-" :: desc
67+
| Some TypeDecl.Bivariant -> "+" :: "-" :: desc
6668
in
6769
let final = if injectivity then "!" :: var_desc else var_desc in
6870
String.concat "" final

src/xref2/compile.ml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -903,7 +903,9 @@ and type_expression : Env.t -> Id.LabelParent.t -> _ -> _ =
903903
| Var _ | Any -> texpr
904904
| Alias (t, str) -> Alias (type_expression env parent t, str)
905905
| Arrow (lbl, t1, t2) -> handle_arrow env parent lbl t1 t2
906-
| Tuple ts -> Tuple (List.map (type_expression env parent) ts)
906+
| Tuple ts ->
907+
Tuple
908+
(List.map (fun (lbl, ty) -> (lbl, type_expression env parent ty)) ts)
907909
| Constr (path, ts') -> (
908910
let cp = Component.Of_Lang.(type_path (empty ()) path) in
909911
let ts = List.map (type_expression env parent) ts' in

src/xref2/component.ml

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ and TypeExpr : sig
122122
| Any
123123
| Alias of t * string
124124
| Arrow of label option * t * t
125-
| Tuple of t list
125+
| Tuple of (string option * t) list
126126
| Constr of Cpath.type_ * t list
127127
| Polymorphic_variant of TypeExpr.Polymorphic_variant.t
128128
| Object of TypeExpr.Object.t
@@ -1010,7 +1010,7 @@ module Fmt = struct
10101010
and type_decl_constructor_arg c ppf =
10111011
let open TypeDecl.Constructor in
10121012
function
1013-
| Tuple ts -> type_tuple c ppf ts
1013+
| Tuple ts -> type_constructor_params c ppf ts
10141014
| Record fs -> type_decl_fields c ppf fs
10151015

10161016
and type_decl_field c ppf t =
@@ -1021,13 +1021,18 @@ module Fmt = struct
10211021
and type_decl_fields c ppf fs =
10221022
fpp_list "; " "{ %a }" (type_decl_field c) ppf fs
10231023

1024-
and type_tuple c ppf ts = fpp_list " * " "%a" (type_expr c) ppf ts
1024+
and type_constructor_params c ppf ts =
1025+
fpp_list " * " "%a" (type_expr c) ppf ts
10251026

10261027
and type_param ppf t =
10271028
let desc =
10281029
match t.Odoc_model.Lang.TypeDecl.desc with Any -> "_" | Var n -> n
10291030
and variance =
1030-
match t.variance with Some Pos -> "+" | Some Neg -> "-" | None -> ""
1031+
match t.variance with
1032+
| Some Pos -> "+"
1033+
| Some Neg -> "-"
1034+
| Some Bivariant -> "+-"
1035+
| None -> ""
10311036
and injectivity = if t.injectivity then "!" else "" in
10321037
Format.fprintf ppf "%s%s%s" variance injectivity desc
10331038

@@ -1086,6 +1091,18 @@ module Fmt = struct
10861091
Format.fprintf ppf "%a * %a" (type_expr c) t (type_expr_list c) ts
10871092
| [] -> ()
10881093

1094+
and type_labeled_tuple c ppf l =
1095+
match l with
1096+
| [ t ] -> with_label c ppf t
1097+
| t :: ts ->
1098+
Format.fprintf ppf "%a * %a" (with_label c) t (type_labeled_tuple c) ts
1099+
| [] -> ()
1100+
1101+
and with_label c ppf (l, ty) =
1102+
match l with
1103+
| None -> type_expr c ppf ty
1104+
| Some lbl -> Format.fprintf ppf "%s:%a" lbl (type_expr c) ty
1105+
10891106
and type_object _c ppf _o = Format.fprintf ppf "(object)"
10901107

10911108
and type_class c ppf (x, ys) =
@@ -1121,7 +1138,7 @@ module Fmt = struct
11211138
| Arrow (l, t1, t2) ->
11221139
Format.fprintf ppf "%a(%a) -> %a" type_expr_label l (type_expr c) t1
11231140
(type_expr c) t2
1124-
| Tuple ts -> Format.fprintf ppf "(%a)" (type_expr_list c) ts
1141+
| Tuple ts -> Format.fprintf ppf "(%a)" (type_labeled_tuple c) ts
11251142
| Constr (p, args) -> (
11261143
match args with
11271144
| [] -> Format.fprintf ppf "%a" (type_path c) p
@@ -2244,7 +2261,9 @@ module Of_Lang = struct
22442261
Constr (type_path ident_map p, List.map (type_expression ident_map) xs)
22452262
| Arrow (lbl, t1, t2) ->
22462263
Arrow (lbl, type_expression ident_map t1, type_expression ident_map t2)
2247-
| Tuple ts -> Tuple (List.map (type_expression ident_map) ts)
2264+
| Tuple ts ->
2265+
Tuple
2266+
(List.map (fun (lbl, ty) -> (lbl, type_expression ident_map ty)) ts)
22482267
| Polymorphic_variant v ->
22492268
Polymorphic_variant (type_expr_polyvar ident_map v)
22502269
| Poly (s, ts) -> Poly (s, type_expression ident_map ts)

0 commit comments

Comments
 (0)