@@ -422,17 +422,57 @@ module Analyser =
422
422
let comments = Record. (doc typedtree) pos_end l in
423
423
Odoc_type. Cstr_record (List. map (record comments) l)
424
424
425
+ (* Given a constraint "with type M.N.t := foo", this function adds "M" ->
426
+ "with type N.t := foo" to acc, ie it build the constraint to put on the
427
+ first element of the path being modified.
428
+ When filter_out_erased_items_from_signature finds "M", it applies the
429
+ constraint on its module type. *)
430
+ let constraint_for_subitem =
431
+ let split_longident p =
432
+ match Longident. flatten p with
433
+ | [] -> assert false
434
+ | hd :: tl -> hd, Longident. unflatten tl
435
+ in
436
+ fun acc s rebuild_constraint ->
437
+ match split_longident s.txt with
438
+ | hd , None -> Name.Map. add hd `Removed acc
439
+ | hd , Some p ->
440
+ let constraint_ = rebuild_constraint { s with txt = p } in
441
+ match Name.Map. find hd acc with
442
+ | exception Not_found ->
443
+ Name.Map. add hd (`Constrained [constraint_]) acc
444
+ | `Constrained old ->
445
+ Name.Map. add hd (`Constrained (constraint_ :: old)) acc
446
+ | `Removed -> acc
447
+
425
448
let erased_names_of_constraints constraints acc =
426
449
List. fold_right (fun constraint_ acc ->
427
450
match constraint_ with
428
451
| Parsetree. Pwith_type _ | Parsetree. Pwith_module _ -> acc
429
- | Parsetree. Pwith_typesubst {Parsetree. ptype_name= s}
430
- | Parsetree. Pwith_modsubst (s , _ ) ->
431
- Name.Set. add s.txt acc)
452
+ | Parsetree. Pwith_typesubst (s , typedecl ) ->
453
+ constraint_for_subitem acc s (fun s -> Parsetree. Pwith_typesubst (s, typedecl))
454
+ | Parsetree. Pwith_modsubst (s , modpath ) ->
455
+ constraint_for_subitem acc s (fun s -> Parsetree. Pwith_modsubst (s, modpath)))
432
456
constraints acc
433
457
458
+ let is_erased ident map =
459
+ match Name.Map. find ident map with
460
+ | exception Not_found -> false
461
+ | `Removed -> true
462
+ | `Constrained _ -> false
463
+
464
+ let apply_constraint module_type constraints =
465
+ match module_type.Parsetree. pmty_desc with
466
+ | Parsetree. Pmty_alias _ -> module_type
467
+ | _ ->
468
+ { Parsetree.
469
+ pmty_desc = Parsetree. Pmty_with (module_type, List. rev constraints);
470
+ pmty_loc = module_type.Parsetree. pmty_loc;
471
+ pmty_attributes = []
472
+ }
473
+
434
474
let filter_out_erased_items_from_signature erased signature =
435
- if Name.Set . is_empty erased then signature
475
+ if Name.Map . is_empty erased then signature
436
476
else List. fold_right (fun sig_item acc ->
437
477
let take_item psig_desc = { sig_item with Parsetree. psig_desc } :: acc in
438
478
match sig_item.Parsetree. psig_desc with
@@ -446,14 +486,24 @@ module Analyser =
446
486
| Parsetree. Psig_class _
447
487
| Parsetree. Psig_class_type _ as tp -> take_item tp
448
488
| Parsetree. Psig_type (rf , types ) ->
449
- (match List. filter (fun td -> not (Name.Set. mem td.Parsetree. ptype_name.txt erased)) types with
489
+ (match List. filter (fun td -> not (is_erased td.Parsetree. ptype_name.txt erased)) types with
450
490
| [] -> acc
451
491
| types -> take_item (Parsetree. Psig_type (rf, types)))
452
- | Parsetree. Psig_module {Parsetree. pmd_name= name}
492
+ | Parsetree. Psig_module ({Parsetree. pmd_name= name;
493
+ pmd_type= module_type} as r) as m ->
494
+ begin match Name.Map. find name.txt erased with
495
+ | exception Not_found -> take_item m
496
+ | `Removed -> acc
497
+ | `Constrained constraints ->
498
+ take_item
499
+ (Parsetree. Psig_module
500
+ { r with Parsetree. pmd_type =
501
+ apply_constraint module_type constraints })
502
+ end
453
503
| Parsetree. Psig_modtype {Parsetree. pmtd_name =name } as m ->
454
- if Name.Set. mem name.txt erased then acc else take_item m
504
+ if is_erased name.txt erased then acc else take_item m
455
505
| Parsetree. Psig_recmodule mods ->
456
- (match List. filter (fun pmd -> not (Name.Set. mem pmd.Parsetree. pmd_name.txt erased)) mods with
506
+ (match List. filter (fun pmd -> not (is_erased pmd.Parsetree. pmd_name.txt erased)) mods with
457
507
| [] -> acc
458
508
| mods -> take_item (Parsetree. Psig_recmodule mods)))
459
509
signature []
@@ -1336,7 +1386,7 @@ module Analyser =
1336
1386
1337
1387
(* * Return a module_type_kind from a Parsetree.module_type and a Types.module_type *)
1338
1388
and analyse_module_type_kind
1339
- ?(erased = Name.Set . empty) env current_module_name module_type sig_module_type =
1389
+ ?(erased = Name.Map . empty) env current_module_name module_type sig_module_type =
1340
1390
match module_type.Parsetree. pmty_desc with
1341
1391
Parsetree. Pmty_ident longident ->
1342
1392
let name =
@@ -1432,7 +1482,7 @@ module Analyser =
1432
1482
1433
1483
(* * analyse of a Parsetree.module_type and a Types.module_type.*)
1434
1484
and analyse_module_kind
1435
- ?(erased = Name.Set . empty) env current_module_name module_type sig_module_type =
1485
+ ?(erased = Name.Map . empty) env current_module_name module_type sig_module_type =
1436
1486
match module_type.Parsetree. pmty_desc with
1437
1487
| Parsetree. Pmty_ident _longident ->
1438
1488
let k = analyse_module_type_kind env current_module_name module_type sig_module_type in
@@ -1502,7 +1552,7 @@ module Analyser =
1502
1552
raise (Failure " Parsetree.Pmty_functor _ but not Types.Mty_functor _" )
1503
1553
)
1504
1554
| Parsetree. Pmty_with (module_type2 , constraints ) ->
1505
- (* of module_type * (Longident.t * with_constraint) list*)
1555
+ (* of module_type * (Longident.t * with_constraint) list*)
1506
1556
(
1507
1557
let loc_start = Loc. end_ module_type2.Parsetree. pmty_loc in
1508
1558
let loc_end = Loc. end_ module_type.Parsetree. pmty_loc in
0 commit comments