Skip to content

Commit aafc274

Browse files
committed
Add Strengthen extension for module types
1 parent b1dd7ec commit aafc274

File tree

8 files changed

+61
-24
lines changed

8 files changed

+61
-24
lines changed

ocamldoc/odoc_sig.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1521,7 +1521,7 @@ module Analyser =
15211521
and analyse_module_type_kind
15221522
?(erased = Name.Map.empty) env current_module_name module_type sig_module_type =
15231523
match Extensions.Module_type.get_desc module_type with
1524-
| Extension _ -> .
1524+
| Extension (Emty_strengthen _) -> failwith "strengthen not implemented yet"
15251525
| Regular desc -> match desc with
15261526
Parsetree.Pmty_ident longident ->
15271527
let name =
@@ -1621,7 +1621,7 @@ module Analyser =
16211621
and analyse_module_kind
16221622
?(erased = Name.Map.empty) env current_module_name module_type sig_module_type =
16231623
match Extensions.Module_type.get_desc module_type with
1624-
| Extension _ -> .
1624+
| Extension (Emty_strengthen _) -> failwith "strengthen not implemented yet"
16251625
| Regular desc -> match desc with
16261626
| Parsetree.Pmty_ident _longident ->
16271627
let k = analyse_module_type_kind env current_module_name module_type sig_module_type in

parsing/ast_iterator.ml

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,12 @@ module MT = struct
303303
sub.attributes sub attrs;
304304
sub.extension sub x
305305
| Psig_attribute x -> sub.attribute sub x
306+
307+
let iter_extension sub : Extensions.Module_type.t -> _ = function
308+
| Emty_strengthen { mty; mod_id } ->
309+
iter sub mty;
310+
iter_loc sub mod_id
311+
306312
end
307313

308314

@@ -596,6 +602,7 @@ let default_iterator =
596602
signature = (fun this l -> List.iter (this.signature_item this) l);
597603
signature_item = MT.iter_signature_item;
598604
module_type = MT.iter;
605+
module_type_extension = MT.iter_extension;
599606
with_constraint = MT.iter_with_constraint;
600607
class_declaration =
601608
(fun this -> CE.class_infos this (this.class_expr this));
@@ -656,9 +663,6 @@ let default_iterator =
656663
this.attributes this pmtd_attributes;
657664
);
658665

659-
module_type_extension = (fun _this emty -> match emty with
660-
| _ -> .);
661-
662666
module_binding =
663667
(fun this {pmb_name; pmb_expr; pmb_attributes; pmb_loc} ->
664668
iter_loc this pmb_name; this.module_expr this pmb_expr;

parsing/ast_mapper.ml

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ module MT = struct
284284
| Extension emty -> begin
285285
Extensions_parsing.Module_type.wrap_desc ~loc ~attrs @@
286286
match sub.module_type_extension sub emty with
287-
| _ -> .
287+
| Emty_strengthen smty -> Extensions.Strengthen.mty_of ~loc smty
288288
end
289289
| Regular desc ->
290290
match desc with
@@ -342,6 +342,13 @@ module MT = struct
342342
let attrs = sub.attributes sub attrs in
343343
extension ~loc ~attrs (sub.extension sub x)
344344
| Psig_attribute x -> attribute ~loc (sub.attribute sub x)
345+
346+
let map_extension sub :
347+
Extensions.Module_type.t -> Extensions.Module_type.t = function
348+
| Emty_strengthen { mty; mod_id } ->
349+
let mty = sub.module_type sub mty in
350+
let mod_id = map_loc sub mod_id in
351+
Emty_strengthen { mty; mod_id }
345352
end
346353

347354

@@ -666,6 +673,7 @@ let default_mapper =
666673
signature = (fun this l -> List.map (this.signature_item this) l);
667674
signature_item = MT.map_signature_item;
668675
module_type = MT.map;
676+
module_type_extension = MT.map_extension;
669677
with_constraint = MT.map_with_constraint;
670678
class_declaration =
671679
(fun this -> CE.class_infos this (this.class_expr this));
@@ -729,9 +737,6 @@ let default_mapper =
729737
~loc:(this.location this pmtd_loc)
730738
);
731739

732-
module_type_extension =
733-
(fun _this emty -> match emty with _ -> .);
734-
735740
module_binding =
736741
(fun this {pmb_name; pmb_expr; pmb_attributes; pmb_loc} ->
737742
Mb.mk (map_loc this pmb_name) (this.module_expr this pmb_expr)

parsing/depend.ml

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ and add_binding_op bv bv' pbop =
341341

342342
and add_modtype bv mty =
343343
match Extensions.Module_type.get_desc mty with
344-
| Extension _ -> .
344+
| Extension emty -> add_modtype_extension bv emty
345345
| Regular desc ->
346346
match desc with
347347
Pmty_ident l -> add bv l
@@ -373,6 +373,11 @@ and add_modtype bv mty =
373373
| Pmty_typeof m -> add_module_expr bv m
374374
| Pmty_extension e -> handle_extension e
375375

376+
and add_modtype_extension bv : Extensions.Module_type.t -> _ = function
377+
| Emty_strengthen { mty; mod_id } ->
378+
add_modtype bv mty;
379+
add_module_path bv mod_id
380+
376381
and add_module_alias bv l =
377382
(* If we are in delayed dependencies mode, we delay the dependencies
378383
induced by "Lident s" *)
@@ -386,7 +391,7 @@ and add_module_alias bv l =
386391

387392
and add_modtype_binding bv mty =
388393
match Extensions.Module_type.get_desc mty with
389-
| Extension _ -> .
394+
| Extension emty -> add_modtype_extension_binding bv emty
390395
| Regular desc ->
391396
match desc with
392397
Pmty_alias l ->
@@ -398,6 +403,13 @@ and add_modtype_binding bv mty =
398403
| _ ->
399404
add_modtype bv mty; bound
400405

406+
and add_modtype_extension_binding bv : Extensions.Module_type.t -> _ = function
407+
| Emty_strengthen { mty; mod_id } ->
408+
(* treat like a [with] constraint *)
409+
add_modtype bv mty;
410+
add_module_path bv mod_id;
411+
bound
412+
401413
and add_signature bv sg =
402414
ignore (add_signature_binding bv sg)
403415

parsing/extensions.ml

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -297,13 +297,13 @@ module Immutable_arrays = struct
297297

298298
let of_pat expr = match expr.ppat_desc with
299299
| Ppat_array elts -> Iapat_immutable_array elts
300-
| _ -> failwith "Malformed immutable array expression"
300+
| _ -> failwith "Malformed immutable array pattern"
301301
end
302302

303303
(** Strengthening *)
304304
module Strengthen = struct
305305
type nonrec module_type =
306-
| Smt_strengthen of Parsetree.module_type * Longident.t Location.loc
306+
{ mty : Parsetree.module_type; mod_id : Longident.t Location.loc }
307307

308308
let extension_string = Language_extension.to_string Strengthen
309309

@@ -317,15 +317,16 @@ module Strengthen = struct
317317
]}
318318
*)
319319

320-
let mty_of ~loc = function
321-
| Smt_strengthen (mty, lid) ->
322-
(* See Note [Wrapping with make_extension] *)
323-
Module_type.make_extension ~loc [extension_string] @@
320+
let mty_of ~loc { mty; mod_id } =
321+
(* See Note [Wrapping with make_extension] *)
322+
Module_type.make_extension ~loc [extension_string] @@
324323
Ast_helper.Mty.functor_ (Named (Location.mknoloc None, mty))
325-
(Ast_helper.Mty.ident lid)
324+
(Ast_helper.Mty.ident mod_id)
326325

327326
let of_mty mty = match mty.pmty_desc with
328-
| Pmty_functor(Named(_, mty), {pmty_desc = Pmty_ident
327+
| Pmty_functor(Named(_, mty), {pmty_desc = Pmty_ident mod_id}) ->
328+
{ mty; mod_id }
329+
| _ -> failwith "Malformed strengthen module type"
329330
end
330331

331332
(******************************************************************************)

parsing/extensions.mli

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ end
8282
(** The ASTs for module type strengthening. *)
8383
module Strengthen : sig
8484
type module_type =
85-
| Smt_strengthen of Parsetree.module_type * Longident.t Location.loc
85+
{ mty : Parsetree.module_type; mod_id : Longident.t Location.loc }
8686

8787
val mty_of : loc:Location.t -> module_type -> Parsetree.module_type_desc
8888
end

parsing/pprintast.ml

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1129,7 +1129,7 @@ and module_type ctxt f x =
11291129
(attributes ctxt) x.pmty_attributes
11301130
end else
11311131
match Extensions.Module_type.get_desc x with
1132-
| Extension _ -> .
1132+
| Extension emty -> module_type_extension ctxt f emty
11331133
| Regular desc -> match desc with
11341134
| Pmty_functor (Unit, mt2) ->
11351135
pp f "@[<hov2>functor () ->@ %a@]" (module_type ctxt) mt2
@@ -1149,6 +1149,12 @@ and module_type ctxt f x =
11491149
(list (with_constraint ctxt) ~sep:"@ and@ ") l
11501150
| _ -> module_type1 ctxt f x
11511151

1152+
and module_type_extension ctxt f : Extensions.Module_type.t -> _ = function
1153+
| Emty_strengthen { mty; mod_id } ->
1154+
pp f "@[<hov2>%a@ with@ %a@]"
1155+
(module_type1 ctxt) mty
1156+
longident_loc mod_id
1157+
11521158
and with_constraint ctxt f = function
11531159
| Pwith_type (li, ({ptype_params= ls ;_} as td)) ->
11541160
let ls = List.map fst ls in
@@ -1174,7 +1180,7 @@ and with_constraint ctxt f = function
11741180
and module_type1 ctxt f x =
11751181
if x.pmty_attributes <> [] then module_type ctxt f x
11761182
else match Extensions.Module_type.get_desc x with
1177-
| Extension _ -> .
1183+
| Extension emty -> module_type_extension1 ctxt f emty
11781184
| Regular desc -> match desc with
11791185
| Pmty_ident li ->
11801186
pp f "%a" longident_loc li;
@@ -1188,6 +1194,9 @@ and module_type1 ctxt f x =
11881194
| Pmty_extension e -> extension ctxt f e
11891195
| _ -> paren true (module_type ctxt) f x
11901196

1197+
and module_type_extension1 ctxt f : Extensions.Module_type.t -> _ = function
1198+
| Emty_strengthen _ as emty -> paren true (module_type_extension ctxt) f emty
1199+
11911200
and signature ctxt f x = list ~sep:"@\n" (signature_item ctxt) f x
11921201

11931202
and signature_item ctxt f x : unit =

typing/typemod.ml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -836,7 +836,7 @@ let map_ext fn exts =
836836

837837
let rec approx_modtype env smty =
838838
match Extensions.Module_type.get_desc smty with
839-
| Extension _ -> .
839+
| Extension emty -> approx_modtype_extension env emty
840840
| Regular desc ->
841841
match desc with
842842
Pmty_ident lid ->
@@ -895,6 +895,9 @@ let rec approx_modtype env smty =
895895
| Pmty_extension ext ->
896896
raise (Error_forward (Builtin_attributes.error_of_extension ext))
897897

898+
and approx_modtype_extension _env : Extensions.Module_type.t -> _ = function
899+
| Emty_strengthen { mty=_; mod_id=_ } -> failwith "strengthen not yet implemented"
900+
898901
and approx_module_declaration env pmd =
899902
{
900903
Types.md_type = approx_modtype env pmd.pmd_type;
@@ -1374,7 +1377,7 @@ and transl_modtype_functor_arg env sarg =
13741377
and transl_modtype_aux env smty =
13751378
let loc = smty.pmty_loc in
13761379
match Extensions.Module_type.get_desc smty with
1377-
| Extension _ -> .
1380+
| Extension emty -> transl_modtype_extension_aux env emty
13781381
| Regular desc ->
13791382
match desc with
13801383
Pmty_ident lid ->
@@ -1437,6 +1440,9 @@ and transl_modtype_aux env smty =
14371440
| Pmty_extension ext ->
14381441
raise (Error_forward (Builtin_attributes.error_of_extension ext))
14391442

1443+
and transl_modtype_extension_aux _env : Extensions.Module_type.t -> _ = function
1444+
| Emty_strengthen { mty=_ ; mod_id=_ } -> failwith "Strengthen not yet implemented"
1445+
14401446
and transl_with ~loc env remove_aliases (rev_tcstrs,sg) constr =
14411447
let lid, with_info = match constr with
14421448
| Pwith_type (l,decl) ->l , With_type decl

0 commit comments

Comments
 (0)