Skip to content

Commit fff2016

Browse files
authored
Add args and constraint to class_infos (#2502)
1 parent c30558b commit fff2016

File tree

9 files changed

+33
-47
lines changed

9 files changed

+33
-47
lines changed

lib/Ast.ml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1138,8 +1138,12 @@ end = struct
11381138
| Pcl_constraint (_, x) -> x == cty
11391139
| Pcl_extension _ -> false
11401140
| Pcl_open _ -> false )
1141-
| Cd _ -> assert false
1142-
| Ctd ctx -> assert (ctx.pci_expr == cty)
1141+
| Cd ctx ->
1142+
assert (Option.exists ctx.pci_constraint ~f:(fun x -> x == cty))
1143+
| Ctd ctx ->
1144+
assert (
1145+
Option.exists ctx.pci_constraint ~f:(fun x -> x == cty)
1146+
|| ctx.pci_expr == cty )
11431147
| Clf _ -> assert false
11441148
| Ctf {pctf_desc; _} ->
11451149
assert (

lib/Fmt_ast.ml

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3741,6 +3741,7 @@ and fmt_signature_item c ?ext {ast= si; _} =
37413741

37423742
and fmt_class_types ?ext c ~pre ~sep cls =
37433743
list_fl cls (fun ~first ~last:_ cl ->
3744+
(* [pci_args] and [pci_constraint] are not used for class types. *)
37443745
update_config_maybe_disabled c cl.pci_loc cl.pci_attributes
37453746
@@ fun c ->
37463747
let ctx = Ctd cl in
@@ -3773,19 +3774,12 @@ and fmt_class_exprs ?ext c cls =
37733774
update_config_maybe_disabled c cl.pci_loc cl.pci_attributes
37743775
@@ fun c ->
37753776
let ctx = Cd cl in
3776-
let xargs, xbody = Sugar.cl_fun c.cmts (sub_cl ~ctx cl.pci_expr) in
3777-
let ty, e =
3778-
match xbody.ast with
3779-
| {pcl_desc= Pcl_constraint (e, t); _} as ce ->
3780-
let ctx = Cl ce in
3781-
(Some (sub_cty ~ctx t), sub_cl ~ctx e)
3782-
| _ -> (None, xbody)
3783-
in
3777+
let xargs = cl.pci_args in
37843778
let doc_before, doc_after, atrs =
37853779
let force_before = not (Cl.is_simple cl.pci_expr) in
37863780
fmt_docstring_around_item ~force_before c cl.pci_attributes
37873781
in
3788-
let class_exprs =
3782+
let class_expr =
37893783
let pro =
37903784
box_fun_decl_args c 2
37913785
( hovbox 2
@@ -3799,19 +3793,20 @@ and fmt_class_exprs ?ext c cls =
37993793
$ wrap_fun_decl_args c (fmt_fun_args c xargs) )
38003794
in
38013795
let intro =
3802-
match ty with
3796+
match cl.pci_constraint with
38033797
| Some ty ->
3804-
let pro = pro $ fmt " :@ " in
3805-
fmt_class_type c ~pro ty
3798+
fmt_class_type c ~pro:(pro $ fmt " :@ ") (sub_cty ~ctx ty)
38063799
| None -> pro
38073800
in
38083801
hovbox 2
3809-
(hovbox 2 (intro $ fmt "@ =") $ fmt "@;" $ fmt_class_expr c e)
3802+
( hovbox 2 (intro $ fmt "@ =")
3803+
$ fmt "@;"
3804+
$ fmt_class_expr c (sub_cl ~ctx cl.pci_expr) )
38103805
$ fmt_item_attributes c ~pre:(Break (1, 0)) atrs
38113806
in
38123807
fmt_if (not first) "\n@;<1000 0>"
38133808
$ hovbox 0
3814-
@@ Cmts.fmt c cl.pci_loc (doc_before $ class_exprs $ doc_after) )
3809+
@@ Cmts.fmt c cl.pci_loc (doc_before $ class_expr $ doc_after) )
38153810

38163811
and fmt_module c ctx ?rec_ ?epi ?(can_sparse = false) keyword ?(eqty = "=")
38173812
name xargs xbody xmty ~attrs ~rec_flag =

lib/Sugar.ml

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -50,16 +50,6 @@ let fun_ cmts ?(will_keep_first_ast_node = true) xexp =
5050
in
5151
fun_ ~will_keep_first_ast_node xexp
5252

53-
let cl_fun cmts ({ast= exp; _} as xexp) =
54-
let ctx = Cl exp in
55-
match (exp.pcl_attributes, exp.pcl_desc) with
56-
| [], Pcl_fun (p, body) ->
57-
let before = (List.hd_exn p).pparam_loc in
58-
let after = body.pcl_loc in
59-
Cmts.relocate cmts ~src:exp.pcl_loc ~before ~after ;
60-
(p, sub_cl ~ctx body)
61-
| _ -> ([], xexp)
62-
6353
module Exp = struct
6454
let infix cmts prec xexp =
6555
let assoc = Option.value_map prec ~default:Assoc.Non ~f:Assoc.of_prec in

lib/Sugar.mli

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,6 @@ val fun_ :
2222
and the body of the function [exp]. [will_keep_first_ast_node] is set by
2323
default, otherwise the [exp] is returned without modification. *)
2424

25-
val cl_fun :
26-
Cmts.t -> class_expr Ast.xt -> function_param list * class_expr Ast.xt
27-
(** [cl_fun cmts exp] returns the list of arguments and the body of the function [exp]. *)
28-
2925
module Exp : sig
3026
val infix :
3127
Cmts.t

vendor/parser-extended/ast_helper.ml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,11 +454,14 @@ end
454454
module Ci = struct
455455
let mk ?(loc = !default_loc) ?(attrs = [])
456456
?(docs = empty_docs) ?(text = [])
457+
?(args = []) ?constraint_
457458
?(virt = Concrete) ?(params = []) name expr =
458459
{
459460
pci_virt = virt;
460461
pci_params = params;
461462
pci_name = name;
463+
pci_args = args;
464+
pci_constraint = constraint_;
462465
pci_expr = expr;
463466
pci_attributes =
464467
add_text_attrs text (add_docs_attrs docs attrs);

vendor/parser-extended/ast_mapper.ml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -739,12 +739,14 @@ module CE = struct
739739
}
740740

741741
let class_infos sub f {pci_virt; pci_params = pl; pci_name; pci_expr;
742-
pci_loc; pci_attributes} =
742+
pci_loc; pci_attributes; pci_args; pci_constraint} =
743743
let loc = sub.location sub pci_loc in
744744
let attrs = sub.attributes sub pci_attributes in
745745
Ci.mk ~loc ~attrs
746746
~virt:(Flag.map_virtual sub pci_virt)
747747
~params:(List.map (map_fst (sub.typ sub)) pl)
748+
~args:(List.map (map_function_param sub) pci_args)
749+
?constraint_:(map_opt (sub.class_type sub) pci_constraint)
748750
(map_loc sub pci_name)
749751
(f pci_expr)
750752
end

vendor/parser-extended/parser.mly

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1773,14 +1773,15 @@ module_type_subst:
17731773
virt = virtual_flag
17741774
params = formal_class_parameters
17751775
id = mkrhs(LIDENT)
1776-
body = class_fun_binding
1776+
cfb = class_fun_binding
17771777
attrs2 = post_item_attributes
17781778
{
17791779
let attrs = attrs1 @ attrs2 in
17801780
let loc = make_loc $sloc in
17811781
let docs = symbol_docs $sloc in
1782+
let (args, constraint_, body) = cfb in
17821783
ext,
1783-
Ci.mk id body ~virt ~params ~attrs ~loc ~docs
1784+
Ci.mk id body ~virt ~params ~attrs ~loc ~docs ~args ?constraint_
17841785
}
17851786
;
17861787
%inline and_class_declaration:
@@ -1789,14 +1790,15 @@ module_type_subst:
17891790
virt = virtual_flag
17901791
params = formal_class_parameters
17911792
id = mkrhs(LIDENT)
1792-
body = class_fun_binding
1793+
cfb = class_fun_binding
17931794
attrs2 = post_item_attributes
17941795
{
17951796
let attrs = attrs1 @ attrs2 in
17961797
let loc = make_loc $sloc in
17971798
let docs = symbol_docs $sloc in
17981799
let text = symbol_text $symbolstartpos in
1799-
Ci.mk id body ~virt ~params ~attrs ~loc ~text ~docs
1800+
let (args, constraint_, body) = cfb in
1801+
Ci.mk id body ~virt ~params ~attrs ~loc ~text ~docs ~args ?constraint_
18001802
}
18011803
;
18021804

@@ -1805,18 +1807,7 @@ class_fun_binding:
18051807
ct = ioption(COLON class_type { $2 })
18061808
EQUAL
18071809
ce = class_expr
1808-
{
1809-
let ce =
1810-
match ct with
1811-
| Some ct ->
1812-
let loc = ($startpos(ct), $endpos(ce)) in
1813-
mkclass ~loc (Pcl_constraint (ce, ct))
1814-
| None -> ce
1815-
in
1816-
match params with
1817-
| [] -> ce
1818-
| _ :: _ -> mkclass ~loc:$sloc (Pcl_fun (params, ce))
1819-
}
1810+
{ params, ct, ce }
18201811
;
18211812

18221813
formal_class_parameters:

vendor/parser-extended/parsetree.mli

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -750,6 +750,8 @@ and 'a class_infos =
750750
pci_virt: virtual_flag;
751751
pci_params: (core_type * variance_and_injectivity) list;
752752
pci_name: string loc;
753+
pci_args: function_param list;
754+
pci_constraint: class_type option;
753755
pci_expr: 'a;
754756
pci_loc: Location.t;
755757
pci_attributes: attributes; (** [... [\@\@id1] [\@\@id2]] *)

vendor/parser-extended/printast.ml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -703,6 +703,9 @@ and class_infos : 'a. _ -> (_ -> _ -> 'a -> _) -> _ -> _ -> 'a class_infos -> _
703703
line i ppf "pci_params =\n";
704704
list (i+1) type_parameter ppf x.pci_params;
705705
line i ppf "pci_name = %a\n" fmt_string_loc x.pci_name;
706+
line i ppf "pci_args =\n";
707+
list (i+1) function_param ppf x.pci_args;
708+
line i ppf "pci_constraint = %a\n" (fmt_opt (class_type i)) x.pci_constraint;
706709
line i ppf "pci_expr =\n";
707710
f (i+1) ppf x.pci_expr
708711

0 commit comments

Comments
 (0)