-
Notifications
You must be signed in to change notification settings - Fork 1
/
typemod.ml
2899 lines (2757 loc) · 105 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
module String = Misc.Stdlib.String
module Sig_component_kind = struct
type t =
| Value
| Type
| Module
| Module_type
| Extension_constructor
| Class
| Class_type
let to_string = function
| Value -> "value"
| Type -> "type"
| Module -> "module"
| Module_type -> "module type"
| Extension_constructor -> "extension constructor"
| Class -> "class"
| Class_type -> "class type"
(** Whether the name of a component of that kind can appear in a type. *)
let can_appear_in_types = function
| Value
| Extension_constructor ->
false
| Type
| Module
| Module_type
| Class
| Class_type ->
true
end
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.error list
| 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.error list
| With_makes_applicative_functor_ill_typed of
Longident.t * Path.t * Includemod.error list
| 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_class of Ident.t * class_declaration
| 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
| Badly_formed_signature of string * Typedecl.error
| Cannot_hide_id of hiding_error
| Invalid_type_subst_rhs
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
(* 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
| Some env -> path, env
| None ->
let md = Env.find_module path env in
ignore (extract_sig_open env lid.loc md.md_type);
assert false
let initial_env ~loc ~safe_string ~initially_opened_module
~open_implicit_modules =
let env =
if safe_string then
Env.initial_safe_string
else
Env.initial_unsafe_string
in
let open_module env m =
let open Asttypes in
let lid = {loc; txt = Longident.parse m } in
snd (type_open_ Override env lid.loc lid)
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.rev_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)
(* Record a module type *)
let rm node =
Stypes.record (Stypes.Ti_mod node);
node
(* 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 rec add_rec_types env = function
Sig_type(id, decl, Trec_next, _) :: rem ->
add_rec_types (Env.add_type ~check:true id decl env) rem
| _ -> env
let check_type_decl env loc id row_id newdecl decl rs rem =
let env = Env.add_type ~check:true id newdecl env in
let env =
match row_id with
| None -> env
| Some id -> Env.add_type ~check:false id newdecl env
in
let env = if rs = Trec_not then env else add_rec_types env rem in
Includemod.type_declarations ~loc env id newdecl decl;
Typedecl.check_coherence env loc (Path.Pident id) newdecl
let update_rec_next rs rem =
match rs with
Trec_next -> rem
| Trec_first | Trec_not ->
match rem with
Sig_type (id, decl, Trec_next, priv) :: rem ->
Sig_type (id, decl, rs, priv) :: rem
| Sig_module (id, pres, mty, Trec_next, priv) :: rem ->
Sig_module (id, pres, mty, rs, priv) :: rem
| _ -> rem
let make_variance p n i =
let open Variance in
set May_pos p (set May_neg n (set May_weak n (set Inj i 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 *)
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 env signature ~loc ~lid =
let iterator =
let env, super = iterator_with_env env in
{ 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
try retype_applicative_functor_type ~loc env funct arg
with Includemod.Error explanation ->
raise(Error(loc, env,
With_makes_applicative_functor_ill_typed
(lid.txt, referenced_path, explanation)))
)
);
}
in
iterator.Btype.it_signature iterator signature;
Btype.unmark_iterators.Btype.it_signature Btype.unmark_iterators signature
(* 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 (Btype.repr hd).desc with
| Tvar _ -> List.memq hd tl || loop tl
| _ -> true
in
loop
;;
let merge_constraint initial_env remove_aliases loc sg constr =
let lid =
match constr with
| Pwith_type (lid, _) | Pwith_module (lid, _)
| Pwith_typesubst (lid, _) | Pwith_modsubst (lid, _) -> lid
in
let destructive_substitution =
match constr with
| Pwith_type _ | Pwith_module _ -> false
| Pwith_typesubst _ | Pwith_modsubst _ -> true
in
let real_ids = ref [] in
let rec merge env sg namelist row_id =
match (sg, namelist, constr) with
([], _, _) ->
raise(Error(loc, env, With_no_component lid.txt))
| (Sig_type(id, decl, rs, priv) :: rem, [s],
Pwith_type (_, ({ptype_kind = Ptype_abstract} as sdecl)))
when Ident.name id = s && Typedecl.is_fixed_type sdecl ->
let decl_row =
{ type_params =
List.map (fun _ -> Btype.newgenvar()) sdecl.ptype_params;
type_arity = List.length sdecl.ptype_params;
type_kind = Type_abstract;
type_private = Private;
type_manifest = None;
type_variance =
List.map
(fun (_, v) ->
let (c, n) =
match v with
| Covariant -> true, false
| Contravariant -> false, true
| Invariant -> false, false
in
make_variance (not n) (not c) false
)
sdecl.ptype_params;
type_loc = sdecl.ptype_loc;
type_is_newtype = false;
type_expansion_scope = Btype.lowest_level;
type_attributes = [];
type_immediate = Unknown;
type_unboxed = unboxed_false_default_false;
}
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 tdecl = Typedecl.transl_with_constraint
initial_env id (Some(Pident id_row)) decl sdecl in
let newdecl = tdecl.typ_type in
check_type_decl env sdecl.ptype_loc id row_id newdecl decl rs rem;
let decl_row = {decl_row with type_params = newdecl.type_params} in
let rs' = if rs = Trec_first then Trec_not else rs in
(Pident id, lid, Twith_type tdecl),
Sig_type(id_row, decl_row, rs', priv)
:: Sig_type(id, newdecl, rs, priv)
:: rem
| (Sig_type(id, decl, rs, priv) :: rem , [s], Pwith_type (_, sdecl))
when Ident.name id = s ->
let tdecl =
Typedecl.transl_with_constraint initial_env id None decl sdecl in
let newdecl = tdecl.typ_type in
check_type_decl env sdecl.ptype_loc id row_id newdecl decl rs rem;
(Pident id, lid, Twith_type tdecl),
Sig_type(id, newdecl, rs, priv) :: rem
| (Sig_type(id, _, _, _) :: rem, [s], (Pwith_type _ | Pwith_typesubst _))
when Ident.name id = s ^ "#row" ->
merge env rem namelist (Some id)
| (Sig_type(id, decl, rs, _priv) :: rem, [s], Pwith_typesubst (_, sdecl))
when Ident.name id = s ->
(* Check as for a normal with constraint, but discard definition *)
let tdecl =
Typedecl.transl_with_constraint initial_env id None decl sdecl in
let newdecl = tdecl.typ_type in
check_type_decl env sdecl.ptype_loc id row_id newdecl decl rs rem;
real_ids := [Pident id];
(Pident id, lid, Twith_typesubst tdecl),
update_rec_next rs rem
| (Sig_module(id, pres, md, rs, priv) :: rem, [s], Pwith_module (_, lid'))
when Ident.name id = s ->
let path, md' = Env.lookup_module ~loc lid'.txt initial_env in
let mty = md'.md_type in
let mty = Mtype.scrape_for_type_of ~remove_aliases env mty in
let md'' = { md' with md_type = mty } in
let newmd = Mtype.strengthen_decl ~aliasable:false env md'' path in
ignore(Includemod.modtypes ~loc env newmd.md_type md.md_type);
(Pident id, lid, Twith_module (path, lid')),
Sig_module(id, pres, newmd, rs, priv) :: rem
| (Sig_module(id, _, md, rs, _) :: rem, [s], Pwith_modsubst (_, lid'))
when Ident.name id = s ->
let path, md' = Env.lookup_module ~loc lid'.txt initial_env in
let aliasable = not (Env.is_functor_arg path env) in
let newmd = Mtype.strengthen_decl ~aliasable env md' path in
ignore(Includemod.modtypes ~loc env newmd.md_type md.md_type);
real_ids := [Pident id];
(Pident id, lid, Twith_modsubst (path, lid')),
update_rec_next rs rem
| (Sig_module(id, _, ({md_type = Mty_alias _} as md), _, _) as item :: rem,
s :: namelist, (Pwith_module _ | Pwith_type _))
when Ident.name id = s ->
let ((path, _, tcstr), _) =
merge env (extract_sig env loc md.md_type) namelist None
in
let path = path_concat id path in
real_ids := path :: !real_ids;
(path, lid, tcstr), item :: rem
| (Sig_module(id, _, md, rs, priv) :: rem, s :: namelist, _)
when Ident.name id = s ->
let ((path, _path_loc, tcstr), newsg) =
merge env (extract_sig env loc md.md_type) namelist None
in
let path = path_concat id path in
real_ids := path :: !real_ids;
let newmd = {md with md_type=Mty_signature newsg} in
let item = Sig_module(id, Mp_present, newmd, rs, priv) in
(path, lid, tcstr),
item :: rem
| (item :: rem, _, _) ->
let (cstr, items) = merge (Env.add_item item env) rem namelist row_id
in
cstr, item :: items
in
try
let names = Longident.flatten lid.txt in
let (tcstr, sg) = merge initial_env sg names None in
if destructive_substitution then (
match List.rev !real_ids with
| [] -> assert false
| last :: rest ->
(* 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);
match rest with
| [] -> ()
| _ :: _ ->
check_usage_of_path_of_substituted_item
rest initial_env sg ~loc ~lid;
);
let sg =
match tcstr with
| (_, _, Twith_typesubst tdecl) ->
let how_to_extend_subst =
let sdecl =
match constr with
| Pwith_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 = List.fold_left how_to_extend_subst Subst.identity !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 =
List.fold_left
(fun s path -> Subst.add_module_path path real_path s)
Subst.identity
!real_ids
in
(* See explanation in the [Twith_typesubst] case above. *)
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, _info) =
Env.lookup_modtype ~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_module (_, lid') ->
(* Lookup the module to make sure that it is not recursive.
(GPR#1626) *)
ignore (Env.lookup_module ~use:false ~loc:lid'.loc lid'.txt env)
| Pwith_modsubst (_, lid') ->
ignore (Env.lookup_module ~use: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;
}
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_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);
Sig_type(decl.clsty_typesharp_id, decl.clsty_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;
}
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 info = [
| `Exported
| `From_open
| `Shadowable of Ident.t * Location.t
| `Substituted_away of 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 -> Types.signature_item -> unit
val simplify: Env.t -> t -> Types.signature -> Types.signature
end = struct
type bound_info = [
| `Exported
| `Shadowable of Ident.t * Location.t
]
type info = [
| `From_open
| `Substituted_away of 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;
}
type names_infos = (string, bound_info) Hashtbl.t
type names = {
values: names_infos;
types: names_infos;
modules: names_infos;
modtypes: names_infos;
typexts: names_infos;
classes: names_infos;
class_types: names_infos;
}
let new_names () = {
values = Hashtbl.create 16;
types = Hashtbl.create 16;
modules = Hashtbl.create 16;
modtypes = Hashtbl.create 16;
typexts = Hashtbl.create 16;
classes = Hashtbl.create 16;
class_types = Hashtbl.create 16;
}
type t = {
bound: names;
to_be_removed: to_be_removed;
}
let create () = {
bound = new_names ();
to_be_removed = {
subst = Subst.identity;
hide = Ident.Map.empty;
};
}
let check cl loc (tbl : names_infos) id (info : info) to_be_removed =
match info with
| `Substituted_away s ->
to_be_removed.subst <- Subst.compose s to_be_removed.subst
| `From_open ->
to_be_removed.hide <-
Ident.Map.add id (cl, loc, From_open) to_be_removed.hide
| #bound_info as bound_info ->
let name = Ident.name id in
match Hashtbl.find_opt tbl name with
| None -> Hashtbl.add tbl name bound_info
| Some (`Shadowable (shadowed_id, shadowed_loc)) ->
Hashtbl.replace tbl name bound_info;
let reason = Shadowed_by (id, loc) in
to_be_removed.hide <-
Ident.Map.add shadowed_id (cl, shadowed_loc, reason)
to_be_removed.hide
| Some `Exported ->
raise(Error(loc, Env.empty, Repeated_name(cl, name)))
let check_value ?info t loc id =
let info =
match info with
| Some i -> i
| None -> `Shadowable (id, loc)
in
check Sig_component_kind.Value loc t.bound.values id info t.to_be_removed
let check_type ?(info=`Exported) t loc id =
check Sig_component_kind.Type loc t.bound.types id info t.to_be_removed
let check_module ?(info=`Exported) t loc id =
check Sig_component_kind.Module loc t.bound.modules id info t.to_be_removed
let check_modtype ?(info=`Exported) t loc id =
check Sig_component_kind.Module_type loc t.bound.modtypes id info
t.to_be_removed
let check_typext ?(info=`Exported) t loc id =
check Sig_component_kind.Extension_constructor loc t.bound.typexts id info
t.to_be_removed
let check_class ?(info=`Exported) t loc id =
check Sig_component_kind.Class loc t.bound.classes id info t.to_be_removed
let check_class_type ?(info=`Exported) t loc id =
check Sig_component_kind.Class_type loc t.bound.class_types id info
t.to_be_removed
let check_sig_item ?info names loc component =
let info id loc =
match info with
| None -> `Shadowable (id, loc)
| Some i -> i
in
match component with
| Sig_type(id, _, _, _) ->
check_type names loc id ~info:(info id loc)
| Sig_module(id, _, _, _, _) ->
check_module names loc id ~info:(info id loc)
| Sig_modtype(id, _, _) ->
check_modtype names loc id ~info:(info id loc)
| Sig_typext(id, _, _, _) ->
check_typext names loc id ~info:(info id loc)
| Sig_value (id, _, _) ->
check_value names loc id ~info:(info id loc)
| Sig_class (id, _, _, _) ->
check_class names loc id ~info:(info id loc)
| Sig_class_type (id, _, _, _) ->
check_class_type names loc id ~info:(info id loc)
(* We usually require name uniqueness of signature components (e.g. types,
modules, etc), however in some situation reusing the name is allowed: if
the component is a value or an extension, or if the name is introduced by
an include.