forked from ocaml/ocaml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
typemod.ml
3342 lines (3183 loc) · 122 KB
/
typemod.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(**************************************************************************)
(* *)
(* OCaml *)
(* *)
(* Xavier Leroy, projet Cristal, INRIA Rocquencourt *)
(* *)
(* Copyright 1996 Institut National de Recherche en Informatique et *)
(* en Automatique. *)
(* *)
(* All rights reserved. This file is distributed under the terms of *)
(* the GNU Lesser General Public License version 2.1, with the *)
(* special exception on linking described in the file LICENSE. *)
(* *)
(**************************************************************************)
open Misc
open Longident
open Path
open Asttypes
open Parsetree
open Types
open Format
let () = Includemod_errorprinter.register ()
module Sig_component_kind = Shape.Sig_component_kind
module String = Misc.Stdlib.String
type hiding_error =
| Illegal_shadowing of {
shadowed_item_id: Ident.t;
shadowed_item_kind: Sig_component_kind.t;
shadowed_item_loc: Location.t;
shadower_id: Ident.t;
user_id: Ident.t;
user_kind: Sig_component_kind.t;
user_loc: Location.t;
}
| Appears_in_signature of {
opened_item_id: Ident.t;
opened_item_kind: Sig_component_kind.t;
user_id: Ident.t;
user_kind: Sig_component_kind.t;
user_loc: Location.t;
}
type error =
Cannot_apply of module_type
| Not_included of Includemod.explanation
| Cannot_eliminate_dependency of module_type
| Signature_expected
| Structure_expected of module_type
| With_no_component of Longident.t
| With_mismatch of Longident.t * Includemod.explanation
| With_makes_applicative_functor_ill_typed of
Longident.t * Path.t * Includemod.explanation
| With_changes_module_alias of Longident.t * Ident.t * Path.t
| With_cannot_remove_constrained_type
| Repeated_name of Sig_component_kind.t * string
| Non_generalizable of type_expr
| Non_generalizable_module of module_type
| Implementation_is_required of string
| Interface_not_compiled of string
| Not_allowed_in_functor_body
| Not_a_packed_module of type_expr
| Incomplete_packed_module of type_expr
| Scoping_pack of Longident.t * type_expr
| Recursive_module_require_explicit_type
| Apply_generative
| Cannot_scrape_alias of Path.t
| Cannot_scrape_package_type of Path.t
| Badly_formed_signature of string * Typedecl.error
| Cannot_hide_id of hiding_error
| Invalid_type_subst_rhs
| Unpackable_local_modtype_subst of Path.t
| With_cannot_remove_packed_modtype of Path.t * module_type
exception Error of Location.t * Env.t * error
exception Error_forward of Location.error
open Typedtree
let rec path_concat head p =
match p with
Pident tail -> Pdot (Pident head, Ident.name tail)
| Pdot (pre, s) -> Pdot (path_concat head pre, s)
| Papply _ -> assert false
| Pextra_ty (p, extra) -> Pextra_ty (path_concat head p, extra)
(* Extract a signature from a module type *)
let extract_sig env loc mty =
match Env.scrape_alias env mty with
Mty_signature sg -> sg
| Mty_alias path ->
raise(Error(loc, env, Cannot_scrape_alias path))
| _ -> raise(Error(loc, env, Signature_expected))
let extract_sig_open env loc mty =
match Env.scrape_alias env mty with
Mty_signature sg -> sg
| Mty_alias path ->
raise(Error(loc, env, Cannot_scrape_alias path))
| mty -> raise(Error(loc, env, Structure_expected mty))
(* Compute the environment after opening a module *)
let type_open_ ?used_slot ?toplevel ovf env loc lid =
let path = Env.lookup_module_path ~load:true ~loc:lid.loc lid.txt env in
match Env.open_signature ~loc ?used_slot ?toplevel ovf path env with
| Ok env -> path, env
| Error _ ->
let md = Env.find_module path env in
ignore (extract_sig_open env lid.loc md.md_type);
assert false
let initial_env ~loc ~initially_opened_module
~open_implicit_modules =
let env = Env.initial in
let open_module env m =
let open Asttypes in
let lexbuf = Lexing.from_string m in
let txt =
Location.init lexbuf (Printf.sprintf "command line argument: -open %S" m);
Parse.simple_module_path lexbuf in
snd (type_open_ Override env loc {txt;loc})
in
let add_units env units =
String.Set.fold
(fun name env ->
Env.add_persistent_structure (Ident.create_persistent name) env)
units
env
in
let units =
List.map Env.persistent_structures_of_dir (Load_path.get ())
in
let env, units =
match initially_opened_module with
| None -> (env, units)
| Some m ->
(* Locate the directory that contains [m], adds the units it
contains to the environment and open [m] in the resulting
environment. *)
let rec loop before after =
match after with
| [] -> None
| units :: after ->
if String.Set.mem m units then
Some (units, List.rev_append before after)
else
loop (units :: before) after
in
let env, units =
match loop [] units with
| None ->
(env, units)
| Some (units_containing_m, other_units) ->
(add_units env units_containing_m, other_units)
in
(open_module env m, units)
in
let env = List.fold_left add_units env units in
List.fold_left open_module env open_implicit_modules
let type_open_descr ?used_slot ?toplevel env sod =
let (path, newenv) =
Builtin_attributes.warning_scope sod.popen_attributes
(fun () ->
type_open_ ?used_slot ?toplevel sod.popen_override env sod.popen_loc
sod.popen_expr
)
in
let od =
{
open_expr = (path, sod.popen_expr);
open_bound_items = [];
open_override = sod.popen_override;
open_env = newenv;
open_attributes = sod.popen_attributes;
open_loc = sod.popen_loc;
}
in
(od, newenv)
(* Forward declaration, to be filled in by type_module_type_of *)
let type_module_type_of_fwd :
(Env.t -> Parsetree.module_expr ->
Typedtree.module_expr * Types.module_type) ref
= ref (fun _env _m -> assert false)
(* Additional validity checks on type definitions arising from
recursive modules *)
let check_recmod_typedecls env decls =
let recmod_ids = List.map fst decls in
List.iter
(fun (id, md) ->
List.iter
(fun path ->
Typedecl.check_recmod_typedecl env md.Types.md_loc recmod_ids
path (Env.find_type path env))
(Mtype.type_paths env (Pident id) md.Types.md_type))
decls
(* Merge one "with" constraint in a signature *)
let check_type_decl env sg loc id row_id newdecl decl =
let fresh_id = Ident.rename id in
let path = Pident fresh_id in
let sub = Subst.add_type id path Subst.identity in
let fresh_row_id, sub =
match row_id with
| None -> None, sub
| Some id ->
let fresh_row_id = Some (Ident.rename id) in
let sub = Subst.add_type id (Pident fresh_id) sub in
fresh_row_id, sub
in
let newdecl = Subst.type_declaration sub newdecl in
let decl = Subst.type_declaration sub decl in
let sg = List.map (Subst.signature_item Keep sub) sg in
let env = Env.add_type ~check:false fresh_id newdecl env in
let env =
match fresh_row_id with
| None -> env
| Some fresh_row_id -> Env.add_type ~check:false fresh_row_id newdecl env
in
let env = Env.add_signature sg env in
Includemod.type_declarations ~mark:Mark_both ~loc env fresh_id newdecl decl;
Typedecl.check_coherence env loc path newdecl
let make_variance p n i =
let open Variance in
set_if p May_pos (set_if n May_neg (set_if i Inj null))
let rec iter_path_apply p ~f =
match p with
| Pident _ -> ()
| Pdot (p, _) -> iter_path_apply p ~f
| Papply (p1, p2) ->
iter_path_apply p1 ~f;
iter_path_apply p2 ~f;
f p1 p2 (* after recursing, so we know both paths are well typed *)
| Pextra_ty _ -> assert false
let path_is_strict_prefix =
let rec list_is_strict_prefix l ~prefix =
match l, prefix with
| [], [] -> false
| _ :: _, [] -> true
| [], _ :: _ -> false
| s1 :: t1, s2 :: t2 ->
String.equal s1 s2 && list_is_strict_prefix t1 ~prefix:t2
in
fun path ~prefix ->
match Path.flatten path, Path.flatten prefix with
| `Contains_apply, _ | _, `Contains_apply -> false
| `Ok (ident1, l1), `Ok (ident2, l2) ->
Ident.same ident1 ident2
&& list_is_strict_prefix l1 ~prefix:l2
let iterator_with_env env =
let env = ref (lazy env) in
let super = Btype.type_iterators in
env, { super with
Btype.it_signature = (fun self sg ->
(* add all items to the env before recursing down, to handle recursive
definitions *)
let env_before = !env in
env := lazy (Env.add_signature sg (Lazy.force env_before));
super.Btype.it_signature self sg;
env := env_before
);
Btype.it_module_type = (fun self -> function
| Mty_functor (param, mty_body) ->
let env_before = !env in
begin match param with
| Unit -> ()
| Named (param, mty_arg) ->
self.Btype.it_module_type self mty_arg;
match param with
| None -> ()
| Some id ->
env := lazy (Env.add_module ~arg:true id Mp_present
mty_arg (Lazy.force env_before))
end;
self.Btype.it_module_type self mty_body;
env := env_before;
| mty ->
super.Btype.it_module_type self mty
)
}
let retype_applicative_functor_type ~loc env funct arg =
let mty_functor = (Env.find_module funct env).md_type in
let mty_arg = (Env.find_module arg env).md_type in
let mty_param =
match Env.scrape_alias env mty_functor with
| Mty_functor (Named (_, mty_param), _) -> mty_param
| _ -> assert false (* could trigger due to MPR#7611 *)
in
Includemod.check_modtype_inclusion ~loc env mty_arg arg mty_param
(* When doing a deep destructive substitution with type M.N.t := .., we change M
and M.N and so we have to check that uses of the modules other than just
extracting components from them still make sense. There are only two such
kinds of uses:
- applicative functor types: F(M).t might not be well typed anymore
- aliases: module A = M still makes sense but it doesn't mean the same thing
anymore, so it's forbidden until it's clear what we should do with it.
This function would be called with M.N.t and N.t to check for these uses. *)
let check_usage_of_path_of_substituted_item paths ~loc ~lid env super =
{ super with
Btype.it_signature_item = (fun self -> function
| Sig_module (id, _, { md_type = Mty_alias aliased_path; _ }, _, _)
when List.exists
(fun path -> path_is_strict_prefix path ~prefix:aliased_path)
paths
->
let e = With_changes_module_alias (lid.txt, id, aliased_path) in
raise(Error(loc, Lazy.force !env, e))
| sig_item ->
super.Btype.it_signature_item self sig_item
);
Btype.it_path = (fun referenced_path ->
iter_path_apply referenced_path ~f:(fun funct arg ->
if List.exists
(fun path -> path_is_strict_prefix path ~prefix:arg)
paths
then
let env = Lazy.force !env in
match retype_applicative_functor_type ~loc env funct arg with
| None -> ()
| Some explanation ->
raise(Error(loc, env,
With_makes_applicative_functor_ill_typed
(lid.txt, referenced_path, explanation)))
)
);
}
(* When doing a module type destructive substitution [with module type T = RHS]
where RHS is not a module type path, we need to check that the module type
T was not used as a path for a packed module
*)
let check_usage_of_module_types ~error ~paths ~loc env super =
let it_do_type_expr it ty = match get_desc ty with
| Tpackage (p, _) ->
begin match List.find_opt (Path.same p) paths with
| Some p -> raise (Error(loc,Lazy.force !env,error p))
| _ -> super.Btype.it_do_type_expr it ty
end
| _ -> super.Btype.it_do_type_expr it ty in
{ super with Btype.it_do_type_expr }
let do_check_after_substitution env ~loc ~lid paths unpackable_modtype sg =
let env, iterator = iterator_with_env env in
let last, rest = match List.rev paths with
| [] -> assert false
| last :: rest -> last, rest
in
(* The last item is the one that's removed. We don't need to check how
it's used since it's replaced by a more specific type/module. *)
assert (match last with Pident _ -> true | _ -> false);
let iterator = match rest with
| [] -> iterator
| _ :: _ ->
check_usage_of_path_of_substituted_item rest ~loc ~lid env iterator
in
let iterator = match unpackable_modtype with
| None -> iterator
| Some mty ->
let error p = With_cannot_remove_packed_modtype(p,mty) in
check_usage_of_module_types ~error ~paths ~loc env iterator
in
iterator.Btype.it_signature iterator sg;
Btype.(unmark_iterators.it_signature unmark_iterators) sg
let check_usage_after_substitution env ~loc ~lid paths unpackable_modtype sg =
match paths, unpackable_modtype with
| [_], None -> ()
| _ -> do_check_after_substitution env ~loc ~lid paths unpackable_modtype sg
(* After substitution one also needs to re-check the well-foundedness
of type declarations in recursive modules *)
let rec extract_next_modules = function
| Sig_module (id, _, mty, Trec_next, _) :: rem ->
let (id_mty_l, rem) = extract_next_modules rem in
((id, mty) :: id_mty_l, rem)
| sg -> ([], sg)
let check_well_formed_module env loc context mty =
(* Format.eprintf "@[check_well_formed_module@ %a@]@."
Printtyp.modtype mty; *)
let open Btype in
let iterator =
let rec check_signature env = function
| [] -> ()
| Sig_module (id, _, mty, Trec_first, _) :: rem ->
let (id_mty_l, rem) = extract_next_modules rem in
begin try
check_recmod_typedecls (Lazy.force env) ((id, mty) :: id_mty_l)
with Typedecl.Error (_, err) ->
raise (Error (loc, Lazy.force env,
Badly_formed_signature(context, err)))
end;
check_signature env rem
| _ :: rem ->
check_signature env rem
in
let env, super = iterator_with_env env in
{ super with
it_type_expr = (fun _self _ty -> ());
it_signature = (fun self sg ->
let env_before = !env in
let env = lazy (Env.add_signature sg (Lazy.force env_before)) in
check_signature env sg;
super.it_signature self sg);
}
in
iterator.it_module_type iterator mty
let () = Env.check_well_formed_module := check_well_formed_module
let type_decl_is_alias sdecl = (* assuming no explicit constraint *)
match sdecl.ptype_manifest with
| Some {ptyp_desc = Ptyp_constr (lid, stl)}
when List.length stl = List.length sdecl.ptype_params ->
begin
match
List.iter2 (fun x (y, _) ->
match x, y with
{ptyp_desc=Ptyp_var sx}, {ptyp_desc=Ptyp_var sy}
when sx = sy -> ()
| _, _ -> raise Exit)
stl sdecl.ptype_params;
with
| exception Exit -> None
| () -> Some lid
end
| _ -> None
let params_are_constrained =
let rec loop = function
| [] -> false
| hd :: tl ->
match get_desc hd with
| Tvar _ -> List.memq hd tl || loop tl
| _ -> true
in
loop
type with_info =
| With_type of Parsetree.type_declaration
| With_typesubst of Parsetree.type_declaration
| With_module of {
lid:Longident.t loc;
path:Path.t;
md:Types.module_declaration;
remove_aliases:bool
}
| With_modsubst of Longident.t loc * Path.t * Types.module_declaration
| With_modtype of Typedtree.module_type
| With_modtypesubst of Typedtree.module_type
let merge_constraint initial_env loc sg lid constr =
let destructive_substitution =
match constr with
| With_type _ | With_module _ | With_modtype _ -> false
| With_typesubst _ | With_modsubst _ | With_modtypesubst _ -> true
in
let real_ids = ref [] in
let unpackable_modtype = ref None in
let split_row_id s ghosts =
let srow = s ^ "#row" in
let rec split before = function
| Sig_type(id,_,_,_) :: rest when Ident.name id = srow ->
before, Some id, rest
| a :: rest -> split (a::before) rest
| [] -> before, None, []
in
split [] ghosts
in
let rec patch_item constr namelist outer_sig_env sg_for_env ~ghosts item =
let return ?(ghosts=ghosts) ~replace_by info =
Some (info, {Signature_group.ghosts; replace_by})
in
match item, namelist, constr with
| Sig_type(id, decl, rs, priv), [s],
With_type ({ptype_kind = Ptype_abstract} as sdecl)
when Ident.name id = s && Typedecl.is_fixed_type sdecl ->
let decl_row =
let arity = List.length sdecl.ptype_params in
{
type_params =
List.map (fun _ -> Btype.newgenvar()) sdecl.ptype_params;
type_arity = arity;
type_kind = Type_abstract;
type_private = Private;
type_manifest = None;
type_variance =
List.map
(fun (_, (v, i)) ->
let (c, n) =
match v with
| Covariant -> true, false
| Contravariant -> false, true
| NoVariance -> false, false
in
make_variance (not n) (not c) (i = Injective)
)
sdecl.ptype_params;
type_separability =
Types.Separability.default_signature ~arity;
type_loc = sdecl.ptype_loc;
type_is_newtype = false;
type_expansion_scope = Btype.lowest_level;
type_attributes = [];
type_immediate = Unknown;
type_unboxed_default = false;
type_uid = Uid.mk ~current_unit:(Env.get_unit_name ());
}
and id_row = Ident.create_local (s^"#row") in
let initial_env =
Env.add_type ~check:false id_row decl_row initial_env
in
let sig_env = Env.add_signature sg_for_env outer_sig_env in
let tdecl =
Typedecl.transl_with_constraint id ~fixed_row_path:(Pident id_row)
~sig_env ~sig_decl:decl ~outer_env:initial_env sdecl in
let newdecl = tdecl.typ_type in
let before_ghosts, row_id, after_ghosts = split_row_id s ghosts in
check_type_decl outer_sig_env sg_for_env sdecl.ptype_loc
id row_id newdecl decl;
let decl_row = {decl_row with type_params = newdecl.type_params} in
let rs' = if rs = Trec_first then Trec_not else rs in
let ghosts =
List.rev_append before_ghosts
(Sig_type(id_row, decl_row, rs', priv)::after_ghosts)
in
return ~ghosts
~replace_by:(Some (Sig_type(id, newdecl, rs, priv)))
(Pident id, lid, Twith_type tdecl)
| Sig_type(id, sig_decl, rs, priv) , [s],
(With_type sdecl | With_typesubst sdecl as constr)
when Ident.name id = s ->
let sig_env = Env.add_signature sg_for_env outer_sig_env in
let tdecl =
Typedecl.transl_with_constraint id
~sig_env ~sig_decl ~outer_env:initial_env sdecl in
let newdecl = tdecl.typ_type and loc = sdecl.ptype_loc in
let before_ghosts, row_id, after_ghosts = split_row_id s ghosts in
let ghosts = List.rev_append before_ghosts after_ghosts in
check_type_decl outer_sig_env sg_for_env loc
id row_id newdecl sig_decl;
begin match constr with
With_type _ ->
return ~ghosts
~replace_by:(Some(Sig_type(id, newdecl, rs, priv)))
(Pident id, lid, Twith_type tdecl)
| (* With_typesubst *) _ ->
real_ids := [Pident id];
return ~ghosts ~replace_by:None
(Pident id, lid, Twith_typesubst tdecl)
end
| Sig_modtype(id, mtd, priv), [s],
(With_modtype mty | With_modtypesubst mty)
when Ident.name id = s ->
let sig_env = Env.add_signature sg_for_env outer_sig_env in
let () = match mtd.mtd_type with
| None -> ()
| Some previous_mty ->
Includemod.check_modtype_equiv ~loc sig_env
id previous_mty mty.mty_type
in
if not destructive_substitution then
let mtd': modtype_declaration =
{
mtd_uid = Uid.mk ~current_unit:(Env.get_unit_name ());
mtd_type = Some mty.mty_type;
mtd_attributes = [];
mtd_loc = loc;
}
in
return
~replace_by:(Some(Sig_modtype(id, mtd', priv)))
(Pident id, lid, Twith_modtype mty)
else begin
let path = Pident id in
real_ids := [path];
begin match mty.mty_type with
| Mty_ident _ -> ()
| mty -> unpackable_modtype := Some mty
end;
return ~replace_by:None (Pident id, lid, Twith_modtypesubst mty)
end
| Sig_module(id, pres, md, rs, priv), [s],
With_module {lid=lid'; md=md'; path; remove_aliases}
when Ident.name id = s ->
let sig_env = Env.add_signature sg_for_env outer_sig_env in
let mty = md'.md_type in
let mty = Mtype.scrape_for_type_of ~remove_aliases sig_env mty in
let md'' = { md' with md_type = mty } in
let newmd = Mtype.strengthen_decl ~aliasable:false sig_env md'' path in
ignore(Includemod.modtypes ~mark:Mark_both ~loc sig_env
newmd.md_type md.md_type);
return
~replace_by:(Some(Sig_module(id, pres, newmd, rs, priv)))
(Pident id, lid, Twith_module (path, lid'))
| Sig_module(id, _, md, _rs, _), [s], With_modsubst (lid',path,md')
when Ident.name id = s ->
let sig_env = Env.add_signature sg_for_env outer_sig_env in
let aliasable = not (Env.is_functor_arg path sig_env) in
ignore
(Includemod.strengthened_module_decl ~loc ~mark:Mark_both
~aliasable sig_env md' path md);
real_ids := [Pident id];
return ~replace_by:None (Pident id, lid, Twith_modsubst (path, lid'))
| Sig_module(id, _, md, rs, priv) as item, s :: namelist, constr
when Ident.name id = s ->
let sig_env = Env.add_signature sg_for_env outer_sig_env in
let sg = extract_sig sig_env loc md.md_type in
let ((path, _, tcstr), newsg) = merge_signature sig_env sg namelist in
let path = path_concat id path in
real_ids := path :: !real_ids;
let item =
match md.md_type, constr with
Mty_alias _, (With_module _ | With_type _) ->
(* A module alias cannot be refined, so keep it
and just check that the constraint is correct *)
item
| _ ->
let newmd = {md with md_type = Mty_signature newsg} in
Sig_module(id, Mp_present, newmd, rs, priv)
in
return ~replace_by:(Some item) (path, lid, tcstr)
| _ -> None
and merge_signature env sg namelist =
match
Signature_group.replace_in_place (patch_item constr namelist env sg) sg
with
| Some (x,sg) -> x, sg
| None -> raise(Error(loc, env, With_no_component lid.txt))
in
try
let names = Longident.flatten lid.txt in
let (tcstr, sg) = merge_signature initial_env sg names in
if destructive_substitution then
check_usage_after_substitution ~loc ~lid initial_env !real_ids
!unpackable_modtype sg;
let sg =
match tcstr with
| (_, _, Twith_typesubst tdecl) ->
let how_to_extend_subst =
let sdecl =
match constr with
| With_typesubst sdecl -> sdecl
| _ -> assert false
in
match type_decl_is_alias sdecl with
| Some lid ->
let replacement, _ =
try Env.find_type_by_name lid.txt initial_env
with Not_found -> assert false
in
fun s path -> Subst.add_type_path path replacement s
| None ->
let body = Option.get tdecl.typ_type.type_manifest in
let params = tdecl.typ_type.type_params in
if params_are_constrained params
then raise(Error(loc, initial_env,
With_cannot_remove_constrained_type));
fun s path -> Subst.add_type_function path ~params ~body s
in
let sub = Subst.change_locs Subst.identity loc in
let sub = List.fold_left how_to_extend_subst sub !real_ids in
(* This signature will not be used directly, it will always be freshened
by the caller. So what we do with the scope doesn't really matter. But
making it local makes it unlikely that we will ever use the result of
this function unfreshened without issue. *)
Subst.signature Make_local sub sg
| (_, _, Twith_modsubst (real_path, _)) ->
let sub = Subst.change_locs Subst.identity loc in
let sub =
List.fold_left
(fun s path -> Subst.add_module_path path real_path s)
sub
!real_ids
in
(* See explanation in the [Twith_typesubst] case above. *)
Subst.signature Make_local sub sg
| (_, _, Twith_modtypesubst tmty) ->
let add s p = Subst.add_modtype_path p tmty.mty_type s in
let sub = Subst.change_locs Subst.identity loc in
let sub = List.fold_left add sub !real_ids in
Subst.signature Make_local sub sg
| _ ->
sg
in
check_well_formed_module initial_env loc "this instantiated signature"
(Mty_signature sg);
(tcstr, sg)
with Includemod.Error explanation ->
raise(Error(loc, initial_env, With_mismatch(lid.txt, explanation)))
(* Add recursion flags on declarations arising from a mutually recursive
block. *)
let map_rec fn decls rem =
match decls with
| [] -> rem
| d1 :: dl -> fn Trec_first d1 :: map_end (fn Trec_next) dl rem
let map_rec_type ~rec_flag fn decls rem =
match decls with
| [] -> rem
| d1 :: dl ->
let first =
match rec_flag with
| Recursive -> Trec_first
| Nonrecursive -> Trec_not
in
fn first d1 :: map_end (fn Trec_next) dl rem
let rec map_rec_type_with_row_types ~rec_flag fn decls rem =
match decls with
| [] -> rem
| d1 :: dl ->
if Btype.is_row_name (Ident.name d1.typ_id) then
fn Trec_not d1 :: map_rec_type_with_row_types ~rec_flag fn dl rem
else
map_rec_type ~rec_flag fn decls rem
(* Add type extension flags to extension constructors *)
let map_ext fn exts rem =
match exts with
| [] -> rem
| d1 :: dl -> fn Text_first d1 :: map_end (fn Text_next) dl rem
(* Auxiliary for translating recursively-defined module types.
Return a module type that approximates the shape of the given module
type AST. Retain only module, type, and module type
components of signatures. For types, retain only their arity,
making them abstract otherwise. *)
let rec approx_modtype env smty =
match smty.pmty_desc with
Pmty_ident lid ->
let path =
Env.lookup_modtype_path ~use:false ~loc:smty.pmty_loc lid.txt env
in
Mty_ident path
| Pmty_alias lid ->
let path =
Env.lookup_module_path ~use:false ~load:false
~loc:smty.pmty_loc lid.txt env
in
Mty_alias(path)
| Pmty_signature ssg ->
Mty_signature(approx_sig env ssg)
| Pmty_functor(param, sres) ->
let (param, newenv) =
match param with
| Unit -> Types.Unit, env
| Named (param, sarg) ->
let arg = approx_modtype env sarg in
match param.txt with
| None -> Types.Named (None, arg), env
| Some name ->
let rarg = Mtype.scrape_for_functor_arg env arg in
let scope = Ctype.create_scope () in
let (id, newenv) =
Env.enter_module ~scope ~arg:true name Mp_present rarg env
in
Types.Named (Some id, arg), newenv
in
let res = approx_modtype newenv sres in
Mty_functor(param, res)
| Pmty_with(sbody, constraints) ->
let body = approx_modtype env sbody in
List.iter
(fun sdecl ->
match sdecl with
| Pwith_type _
| Pwith_typesubst _
| Pwith_modtype _
| Pwith_modtypesubst _ -> ()
| Pwith_module (_, lid') ->
(* Lookup the module to make sure that it is not recursive.
(GPR#1626) *)
ignore (Env.lookup_module_path ~use:false ~load:false
~loc:lid'.loc lid'.txt env)
| Pwith_modsubst (_, lid') ->
ignore (Env.lookup_module_path ~use:false ~load:false
~loc:lid'.loc lid'.txt env))
constraints;
body
| Pmty_typeof smod ->
let (_, mty) = !type_module_type_of_fwd env smod in
mty
| Pmty_extension ext ->
raise (Error_forward (Builtin_attributes.error_of_extension ext))
and approx_module_declaration env pmd =
{
Types.md_type = approx_modtype env pmd.pmd_type;
md_attributes = pmd.pmd_attributes;
md_loc = pmd.pmd_loc;
md_uid = Uid.internal_not_actually_unique;
}
and approx_sig env ssg =
match ssg with
[] -> []
| item :: srem ->
match item.psig_desc with
| Psig_type (rec_flag, sdecls) ->
let decls = Typedecl.approx_type_decl sdecls in
let rem = approx_sig env srem in
map_rec_type ~rec_flag
(fun rs (id, info) -> Sig_type(id, info, rs, Exported)) decls rem
| Psig_typesubst _ -> approx_sig env srem
| Psig_module { pmd_name = { txt = None; _ }; _ } ->
approx_sig env srem
| Psig_module pmd ->
let scope = Ctype.create_scope () in
let md = approx_module_declaration env pmd in
let pres =
match md.Types.md_type with
| Mty_alias _ -> Mp_absent
| _ -> Mp_present
in
let id, newenv =
Env.enter_module_declaration ~scope (Option.get pmd.pmd_name.txt)
pres md env
in
Sig_module(id, pres, md, Trec_not, Exported) :: approx_sig newenv srem
| Psig_modsubst pms ->
let scope = Ctype.create_scope () in
let _, md =
Env.lookup_module ~use:false ~loc:pms.pms_manifest.loc
pms.pms_manifest.txt env
in
let pres =
match md.Types.md_type with
| Mty_alias _ -> Mp_absent
| _ -> Mp_present
in
let _, newenv =
Env.enter_module_declaration ~scope pms.pms_name.txt pres md env
in
approx_sig newenv srem
| Psig_recmodule sdecls ->
let scope = Ctype.create_scope () in
let decls =
List.filter_map
(fun pmd ->
Option.map (fun name ->
Ident.create_scoped ~scope name,
approx_module_declaration env pmd
) pmd.pmd_name.txt
)
sdecls
in
let newenv =
List.fold_left
(fun env (id, md) -> Env.add_module_declaration ~check:false
id Mp_present md env)
env decls
in
map_rec
(fun rs (id, md) -> Sig_module(id, Mp_present, md, rs, Exported))
decls
(approx_sig newenv srem)
| Psig_modtype d ->
let info = approx_modtype_info env d in
let scope = Ctype.create_scope () in
let (id, newenv) =
Env.enter_modtype ~scope d.pmtd_name.txt info env
in
Sig_modtype(id, info, Exported) :: approx_sig newenv srem
| Psig_modtypesubst d ->
let info = approx_modtype_info env d in
let scope = Ctype.create_scope () in
let (_id, newenv) =
Env.enter_modtype ~scope d.pmtd_name.txt info env
in
approx_sig newenv srem
| Psig_open sod ->
let _, env = type_open_descr env sod in
approx_sig env srem
| Psig_include sincl ->
let smty = sincl.pincl_mod in
let mty = approx_modtype env smty in
let scope = Ctype.create_scope () in
let sg, newenv = Env.enter_signature ~scope
(extract_sig env smty.pmty_loc mty) env in
sg @ approx_sig newenv srem
| Psig_class sdecls | Psig_class_type sdecls ->
let decls = Typeclass.approx_class_declarations env sdecls in
let rem = approx_sig env srem in
map_rec (fun rs decl ->
let open Typeclass in [
Sig_class_type(decl.clsty_ty_id, decl.clsty_ty_decl, rs,
Exported);
Sig_type(decl.clsty_obj_id, decl.clsty_obj_abbr, rs, Exported);
]
) decls [rem]
|> List.flatten
| _ ->
approx_sig env srem
and approx_modtype_info env sinfo =
{
mtd_type = Option.map (approx_modtype env) sinfo.pmtd_type;
mtd_attributes = sinfo.pmtd_attributes;
mtd_loc = sinfo.pmtd_loc;
mtd_uid = Uid.internal_not_actually_unique;
}
let approx_modtype env smty =
Warnings.without_warnings
(fun () -> approx_modtype env smty)
(* Auxiliaries for checking the validity of name shadowing in signatures and
structures.
If a shadowing is valid, we also record some information (its ident,
location where it first appears, etc) about the item that gets shadowed. *)
module Signature_names : sig
type t
type shadowable =
{
self: Ident.t;
group: Ident.t list;
(** group includes the element itself and all elements
that should be removed at the same time
*)
loc:Location.t;
}
type info = [
| `Exported
| `From_open
| `Shadowable of shadowable
| `Substituted_away of Subst.t
| `Unpackable_modtype_substituted_away of Ident.t * Subst.t
]
val create : unit -> t
val check_value : ?info:info -> t -> Location.t -> Ident.t -> unit
val check_type : ?info:info -> t -> Location.t -> Ident.t -> unit
val check_typext : ?info:info -> t -> Location.t -> Ident.t -> unit
val check_module : ?info:info -> t -> Location.t -> Ident.t -> unit
val check_modtype : ?info:info -> t -> Location.t -> Ident.t -> unit
val check_class : ?info:info -> t -> Location.t -> Ident.t -> unit
val check_class_type: ?info:info -> t -> Location.t -> Ident.t -> unit
val check_sig_item:
?info:info -> t -> Location.t -> Signature_group.rec_group -> unit
val simplify: Env.t -> t -> Types.signature -> Types.signature
end = struct
type shadowable =
{
self: Ident.t;
group: Ident.t list;
(** group includes the element itself and all elements
that should be removed at the same time
*)
loc:Location.t;
}
type bound_info = [
| `Exported
| `Shadowable of shadowable
]
type info = [
| `From_open
| `Substituted_away of Subst.t
| `Unpackable_modtype_substituted_away of Ident.t * Subst.t
| bound_info
]
type hide_reason =
| From_open
| Shadowed_by of Ident.t * Location.t
type to_be_removed = {
mutable subst: Subst.t;
mutable hide: (Sig_component_kind.t * Location.t * hide_reason) Ident.Map.t;
mutable unpackable_modtypes: Ident.Set.t;
}
type names_infos = (string, bound_info) Hashtbl.t