forked from ocaml/ocaml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
includemod_errorprinter.ml
1023 lines (910 loc) · 35.2 KB
/
includemod_errorprinter.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 *)
(* *)
(* Florian Angeletti, projet Cambium, Inria Paris *)
(* *)
(* Copyright 2021 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. *)
(* *)
(**************************************************************************)
module Style = Misc.Style
module Fmt = Format_doc
module Context = struct
type pos =
| Module of Ident.t
| Modtype of Ident.t
| Arg of Types.functor_parameter
| Body of Types.functor_parameter
let path_of_context = function
Module id :: rem ->
let rec subm path = function
| [] -> path
| Module id :: rem -> subm (Path.Pdot (path, Ident.name id)) rem
| _ -> assert false
in subm (Path.Pident id) rem
| _ -> assert false
let rec context ppf = function
Module id :: rem ->
Fmt.fprintf ppf "@[<2>module %a%a@]" Printtyp.ident id args rem
| Modtype id :: rem ->
Fmt.fprintf ppf "@[<2>module type %a =@ %a@]"
Printtyp.ident id context_mty rem
| Body x :: rem ->
Fmt.fprintf ppf "(%s) ->@ %a" (argname x) context_mty rem
| Arg x :: rem ->
Fmt.fprintf ppf "(%s : %a) -> ..."
(argname x) context_mty rem
| [] ->
Fmt.fprintf ppf "<here>"
and context_mty ppf = function
(Module _ | Modtype _) :: _ as rem ->
Fmt.fprintf ppf "@[<2>sig@ %a@;<1 -2>end@]" context rem
| cxt -> context ppf cxt
and args ppf = function
Body x :: rem ->
Fmt.fprintf ppf "(%s)%a" (argname x) args rem
| Arg x :: rem ->
Fmt.fprintf ppf "(%s :@ %a) : ..." (argname x) context_mty rem
| cxt ->
Fmt.fprintf ppf " :@ %a" context_mty cxt
and argname = function
| Types.Unit -> ""
| Types.Named (None, _) -> "_"
| Types.Named (Some id, _) -> Ident.name id
let alt_pp ppf cxt =
if cxt = [] then () else
if List.for_all (function Module _ -> true | _ -> false) cxt then
Fmt.fprintf ppf ",@ in module %a"
(Style.as_inline_code Printtyp.path) (path_of_context cxt)
else
Fmt.fprintf ppf ",@ @[<hv 2>at position@ %a@]"
(Style.as_inline_code context) cxt
let pp ppf cxt =
if cxt = [] then () else
if List.for_all (function Module _ -> true | _ -> false) cxt then
Fmt.fprintf ppf "In module %a:@ "
(Style.as_inline_code Printtyp.path) (path_of_context cxt)
else
Fmt.fprintf ppf "@[<hv 2>At position@ %a@]@ "
(Style.as_inline_code context) cxt
end
module Runtime_coercion = struct
(** Extraction of a small change from a non-identity runtime coercion *)
(** When examining coercions, we only have runtime component indices,
we use thus a limited version of {!pos}. *)
type coerce_pos =
| Item of int
| InArg
| InBody
let either f x g y = match f x with
| None -> g y
| Some _ as v -> v
type change =
| Transposition of int * int
| Primitive_coercion of string
| Alias_coercion of Path.t
(** We extract a small change from a full coercion. *)
let rec first_change_under path (coerc:Typedtree.module_coercion) =
match coerc with
| Tcoerce_structure(c,_) ->
either
(first_item_transposition path 0) c
(first_non_id path 0) c
| Tcoerce_functor(arg,res) ->
either
(first_change_under (InArg::path)) arg
(first_change_under (InBody::path)) res
| Tcoerce_none -> None
| Tcoerce_alias _ | Tcoerce_primitive _ -> None
(* we search the first point which is not invariant at the current level *)
and first_item_transposition path pos = function
| [] -> None
| (n, _) :: q ->
if n < 0 || n = pos then
(* when n < 0, this is not a transposition but a kind coercion,
which will be covered in the first_non_id case *)
first_item_transposition path (pos+1) q
else
Some(List.rev path, Transposition (pos, n))
(* we search the first item with a non-identity inner coercion *)
and first_non_id path pos = function
| [] -> None
| (_, Typedtree.Tcoerce_none) :: q -> first_non_id path (pos + 1) q
| (_, Typedtree.Tcoerce_alias (_,p,_)) :: _ ->
Some (List.rev path, Alias_coercion p)
| (_, Typedtree.Tcoerce_primitive p) :: _ ->
let name = Primitive.byte_name p.pc_desc in
Some (List.rev path, Primitive_coercion name)
| (_,c) :: q ->
either
(first_change_under (Item pos :: path)) c
(first_non_id path (pos + 1)) q
let first_change c = first_change_under [] c
let rec runtime_item k = function
| [] -> raise Not_found
| item :: q ->
if not(Includemod.is_runtime_component item) then
runtime_item k q
else if k = 0 then
item
else
runtime_item (k-1) q
(* Find module type at position [path] and convert the [coerce_pos] path to
a [pos] path *)
let rec find env ctx path (mt:Types.module_type) = match mt, path with
| (Mty_ident p | Mty_alias p), _ ->
begin match (Env.find_modtype p env).mtd_type with
| None -> raise Not_found
| Some mt -> find env ctx path mt
end
| Mty_signature s , [] -> List.rev ctx, s
| Mty_signature s, Item k :: q ->
begin match runtime_item k s with
| Sig_module (id, _, md,_,_) ->
find env (Context.Module id :: ctx) q md.md_type
| _ -> raise Not_found
end
| Mty_functor(Named (_,mt) as arg,_), InArg :: q ->
find env (Context.Arg arg :: ctx) q mt
| Mty_functor(arg, mt), InBody :: q ->
find env (Context.Body arg :: ctx) q mt
| _ -> raise Not_found
let find env path mt = find env [] path mt
let item mt k = Includemod.item_ident_name (runtime_item k mt)
let pp_item ppf (id,_,kind) =
Fmt.fprintf ppf "%s %a"
(Includemod.kind_of_field_desc kind)
Style.inline_code (Ident.name id)
let illegal_permutation ctx_printer env ppf (mty,c) =
match first_change c with
| None | Some (_, (Primitive_coercion _ | Alias_coercion _)) ->
(* those kind coercions are not inversible, and raise an error earlier
when checking for module type equivalence *)
assert false
| Some (path, Transposition (k,l)) ->
try
let ctx, mt = find env path mty in
Fmt.fprintf ppf
"@[<hv 2>Illegal permutation of runtime components in a module type.@ \
@[For example%a,@]@ @[the %a@ and the %a are not in the same order@ \
in the expected and actual module types.@]@]"
ctx_printer ctx pp_item (item mt k) pp_item (item mt l)
with Not_found -> (* this should not happen *)
Fmt.fprintf ppf
"Illegal permutation of runtime components in a module type."
let in_package_subtype ctx_printer env mty c ppf =
match first_change c with
| None ->
(* The coercion looks like the identity but was not simplified to
[Tcoerce_none], this only happens when the two first-class module
types differ by runtime size *)
Fmt.fprintf ppf
"The two first-class module types differ by their runtime size."
| Some (path, c) ->
try
let ctx, mt = find env path mty in
match c with
| Primitive_coercion prim_name ->
Fmt.fprintf ppf
"@[The two first-class module types differ by a coercion of@ \
the primitive %a@ to a value%a.@]"
Style.inline_code prim_name
ctx_printer ctx
| Alias_coercion path ->
Fmt.fprintf ppf
"@[The two first-class module types differ by a coercion of@ \
a module alias %a@ to a module%a.@]"
(Style.as_inline_code Printtyp.path) path
ctx_printer ctx
| Transposition (k,l) ->
Fmt.fprintf ppf
"@[@[The two first-class module types do not share@ \
the same positions for runtime components.@]@ \
@[For example,%a@ the %a@ occurs at the expected position of@ \
the %a.@]@]"
ctx_printer ctx pp_item (item mt k) pp_item (item mt l)
with Not_found ->
Fmt.fprintf ppf
"@[The two packages types do not share@ \
the@ same@ positions@ for@ runtime@ components.@]"
end
module Err = Includemod.Error
let buffer = ref Bytes.empty
let is_big obj =
let size = !Clflags.error_size in
size > 0 &&
begin
if Bytes.length !buffer < size then buffer := Bytes.create size;
try ignore (Marshal.to_buffer !buffer 0 size obj []); false
with _ -> true
end
let show_loc msg ppf loc =
let pos = loc.Location.loc_start in
if List.mem pos.Lexing.pos_fname [""; "_none_"; "//toplevel//"] then ()
else Fmt.fprintf ppf "@\n@[<2>%a:@ %s@]" Location.Doc.loc loc msg
let show_locs ppf (loc1, loc2) =
show_loc "Expected declaration" ppf loc2;
show_loc "Actual declaration" ppf loc1
let dmodtype mty =
let tmty = Printtyp.tree_of_modtype mty in
Fmt.dprintf "%a" !Oprint.out_module_type tmty
let space ppf () = Fmt.fprintf ppf "@ "
(**
In order to display a list of functor arguments in a compact format,
we introduce a notion of shorthand for functor arguments.
The aim is to first present the lists of actual and expected types with
shorthands:
(X: $S1) (Y: $S2) (Z: An_existing_module_type) ...
does not match
(X: $T1) (Y: A_real_path) (Z: $T3) ...
and delay the full display of the module types corresponding to $S1, $S2,
$T1, and $T3 to the suberror message.
*)
module With_shorthand = struct
(** A item with a potential shorthand name *)
type 'a named = {
item: 'a;
name : string;
}
type 'a t =
| Original of 'a (** The shorthand has been discarded *)
| Synthetic of 'a named
(** The shorthand is potentially useful *)
type functor_param =
| Unit
| Named of (Ident.t option * Types.module_type t)
(** Shorthand generation *)
type kind =
| Got
| Expected
| Unneeded
type variant =
| App
| Inclusion
let elide_if_app ctx s = match ctx with
| App -> Unneeded
| Inclusion -> s
let make side pos =
match side with
| Got -> Fmt.asprintf "$S%d" pos
| Expected -> Fmt.asprintf "$T%d" pos
| Unneeded -> "..."
(** Add shorthands to a patch *)
open Diffing
let patch ctx p =
let add_shorthand side pos mty =
{name = (make side pos); item = mty }
in
let aux i d =
let pos = i + 1 in
let d = match d with
| Insert mty ->
Insert (add_shorthand Expected pos mty)
| Delete mty ->
Delete (add_shorthand (elide_if_app ctx Got) pos mty)
| Change (g, e, p) ->
Change
(add_shorthand Got pos g,
add_shorthand Expected pos e, p)
| Keep (g, e, p) ->
Keep (add_shorthand Got pos g,
add_shorthand (elide_if_app ctx Expected) pos e, p)
in
pos, d
in
List.mapi aux p
(** Shorthand computation from named item *)
let modtype (r : _ named) = match r.item with
| Types.Mty_ident _
| Types.Mty_alias _
| Types.Mty_signature []
-> Original r.item
| Types.Mty_signature _ | Types.Mty_functor _
-> Synthetic r
let functor_param (ua : _ named) = match ua.item with
| Types.Unit -> Unit
| Types.Named (from, mty) ->
Named (from, modtype { ua with item = mty })
(** Printing of arguments with shorthands *)
let pp ppx = function
| Original x -> ppx x
| Synthetic s -> Fmt.dprintf "%s" s.name
let pp_orig ppx = function
| Original x | Synthetic { item=x; _ } -> ppx x
let definition x = match functor_param x with
| Unit -> Fmt.dprintf "()"
| Named(_,short_mty) ->
match short_mty with
| Original mty -> dmodtype mty
| Synthetic {name; item = mty} ->
Fmt.dprintf
"%s@ =@ %t" name (dmodtype mty)
let param x = match functor_param x with
| Unit -> Fmt.dprintf "()"
| Named (_, short_mty) ->
pp dmodtype short_mty
let qualified_param x = match functor_param x with
| Unit -> Fmt.dprintf "()"
| Named (None, Original (Mty_signature []) ) ->
Fmt.dprintf "(sig end)"
| Named (None, short_mty) ->
pp dmodtype short_mty
| Named (Some p, short_mty) ->
Fmt.dprintf "(%s : %t)"
(Ident.name p) (pp dmodtype short_mty)
let definition_of_argument ua =
let arg, mty = ua.item in
match (arg: Err.functor_arg_descr) with
| Unit -> Fmt.dprintf "()"
| Empty_struct -> Fmt.dprintf "(struct end)"
| Named p ->
let mty = modtype { ua with item = mty } in
Fmt.dprintf
"%a@ :@ %t"
Printtyp.path p
(pp_orig dmodtype mty)
| Anonymous ->
let short_mty = modtype { ua with item = mty } in
begin match short_mty with
| Original mty -> dmodtype mty
| Synthetic {name; item=mty} ->
Fmt.dprintf "%s@ :@ %t" name (dmodtype mty)
end
let arg ua =
let arg, mty = ua.item in
match (arg: Err.functor_arg_descr) with
| Unit -> Fmt.dprintf "()"
| Empty_struct -> Fmt.dprintf "(struct end)"
| Named p -> fun ppf -> Printtyp.path ppf p
| Anonymous ->
let short_mty = modtype { ua with item=mty } in
pp dmodtype short_mty
end
module Functor_suberror = struct
open Err
let param_id x = match x.With_shorthand.item with
| Types.Named (Some _ as x,_) -> x
| Types.(Unit | Named(None,_)) -> None
(** Print the list of params with style *)
let pretty_params sep proj printer patch =
let elt (x,param) =
let sty = Diffing.(style @@ classify x) in
Fmt.dprintf "%a%t%a"
Fmt.pp_open_stag (Style.Style sty)
(printer param)
Fmt.pp_close_stag ()
in
let params = List.filter_map proj @@ List.map snd patch in
Printtyp.functor_parameters ~sep elt params
let expected d =
let extract: _ Diffing.change -> _ = function
| Insert mty
| Keep(_,mty,_)
| Change (_,mty,_) as x ->
Some (param_id mty,(x, mty))
| Delete _ -> None
in
pretty_params space extract With_shorthand.qualified_param d
let drop_inserted_suffix patch =
let rec drop = function
| Diffing.Insert _ :: q -> drop q
| rest -> List.rev rest in
drop (List.rev patch)
let prepare_patch ~drop ~ctx patch =
let drop_suffix x = if drop then drop_inserted_suffix x else x in
patch |> drop_suffix |> With_shorthand.patch ctx
module Inclusion = struct
let got d =
let extract: _ Diffing.change -> _ = function
| Delete mty
| Keep (mty,_,_)
| Change (mty,_,_) as x ->
Some (param_id mty,(x,mty))
| Insert _ -> None
in
pretty_params space extract With_shorthand.qualified_param d
let insert mty =
Fmt.dprintf
"An argument appears to be missing with module type@;<1 2>@[%t@]"
(With_shorthand.definition mty)
let delete mty =
Fmt.dprintf
"An extra argument is provided of module type@;<1 2>@[%t@]"
(With_shorthand.definition mty)
let ok x y =
Fmt.dprintf
"Module types %t and %t match"
(With_shorthand.param x)
(With_shorthand.param y)
let diff g e more =
let g = With_shorthand.definition g in
let e = With_shorthand.definition e in
Fmt.dprintf
"Module types do not match:@ @[%t@]@;<1 -2>does not include@ \
@[%t@]%t"
g e (more ())
let incompatible = function
| Types.Unit ->
Fmt.dprintf
"The functor was expected to be applicative at this position"
| Types.Named _ ->
Fmt.dprintf
"The functor was expected to be generative at this position"
let patch env got expected =
Includemod.Functor_inclusion_diff.diff env got expected
|> prepare_patch ~drop:false ~ctx:Inclusion
end
module App = struct
let patch env ~f ~args =
Includemod.Functor_app_diff.diff env ~f ~args
|> prepare_patch ~drop:true ~ctx:App
let got d =
let extract: _ Diffing.change -> _ = function
| Delete mty
| Keep (mty,_,_)
| Change (mty,_,_) as x ->
Some (None,(x,mty))
| Insert _ -> None
in
pretty_params space extract With_shorthand.arg d
let delete mty =
Fmt.dprintf
"The following extra argument is provided@;<1 2>@[%t@]"
(With_shorthand.definition_of_argument mty)
let insert = Inclusion.insert
let ok x y =
let pp_orig_name = match With_shorthand.functor_param y with
| With_shorthand.Named (_, Original mty) ->
Fmt.dprintf " %t" (dmodtype mty)
| _ -> ignore
in
Fmt.dprintf
"Module %t matches the expected module type%t"
(With_shorthand.arg x)
pp_orig_name
let diff g e more =
let g = With_shorthand.definition_of_argument g in
let e = With_shorthand.definition e in
Fmt.dprintf
"Modules do not match:@ @[%t@]@;<1 -2>\
is not included in@ @[%t@]%t"
g e (more ())
(** Specialized to avoid introducing shorthand names
for single change difference
*)
let single_diff g e more =
let _arg, mty = g.With_shorthand.item in
let e = match e.With_shorthand.item with
| Types.Unit -> Fmt.dprintf "()"
| Types.Named(_, mty) -> dmodtype mty
in
Fmt.dprintf
"Modules do not match:@ @[%t@]@;<1 -2>\
is not included in@ @[%t@]%t"
(dmodtype mty) e (more ())
let incompatible = function
| Unit ->
Fmt.dprintf
"The functor was expected to be applicative at this position"
| Named _ | Anonymous ->
Fmt.dprintf
"The functor was expected to be generative at this position"
| Empty_struct ->
(* an empty structure can be used in both applicative and generative
context *)
assert false
end
let subcase sub ~expansion_token env (pos, diff) =
Location.msg "%a%a%a%a@[<hv 2>%t@]%a"
Fmt.pp_print_tab ()
Fmt.pp_open_tbox ()
Diffing.prefix (pos, Diffing.classify diff)
Fmt.pp_set_tab ()
(Printtyp.wrap_printing_env env ~error:true
(fun () -> sub ~expansion_token env diff)
)
Fmt.pp_close_tbox ()
let onlycase sub ~expansion_token env (_, diff) =
Location.msg "%a@[<hv 2>%t@]"
Fmt.pp_print_tab ()
(Printtyp.wrap_printing_env env ~error:true
(fun () -> sub ~expansion_token env diff)
)
let params sub ~expansion_token env l =
let rec aux subcases = function
| [] -> subcases
| (_, Diffing.Keep _) as a :: q ->
aux (subcase sub ~expansion_token env a :: subcases) q
| a :: q ->
List.fold_left (fun acc x ->
(subcase sub ~expansion_token:false env x) :: acc
)
(subcase sub ~expansion_token env a :: subcases)
q
in
match l with
| [a] -> [onlycase sub ~expansion_token env a]
| l -> aux [] l
end
(** Construct a linear presentation of the error tree *)
open Err
(* Context helper functions *)
let with_context ?loc ctx printer diff =
Location.msg ?loc "%a%a" Context.pp (List.rev ctx)
printer diff
let dwith_context ?loc ctx printer =
Location.msg ?loc "%a%t" Context.pp (List.rev ctx) printer
let dwith_context_and_elision ?loc ctx printer diff =
if is_big (diff.got,diff.expected) then
Location.msg ?loc "..."
else
dwith_context ?loc ctx (printer diff)
(* Merge sub msgs into one printer *)
let coalesce msgs =
match List.rev msgs with
| [] -> ignore
| before ->
let ctx ppf =
Fmt.pp_print_list ~pp_sep:space
(fun ppf x -> Fmt.pp_doc ppf x.Location.txt)
ppf before in
ctx
let subcase_list l ppf = match l with
| [] -> ()
| _ :: _ ->
let pp_msg ppf lmsg = Fmt.pp_doc ppf lmsg.Location.txt in
Fmt.fprintf ppf "@;<1 -2>@[%a@]"
(Fmt.pp_print_list ~pp_sep:space pp_msg)
(List.rev l)
(* Printers for leaves *)
let core env id x =
match x with
| Err.Value_descriptions diff ->
Fmt.dprintf "@[<v>@[<hv>%s:@;<1 2>%a@ %s@;<1 2>%a@]%a%a@]"
"Values do not match"
!Oprint.out_sig_item
(Printtyp.tree_of_value_description id diff.got)
"is not included in"
!Oprint.out_sig_item
(Printtyp.tree_of_value_description id diff.expected)
(Includecore.report_value_mismatch
"the first" "the second" env) diff.symptom
show_locs (diff.got.val_loc, diff.expected.val_loc)
| Err.Type_declarations diff ->
Fmt.dprintf "@[<v>@[<hv>%s:@;<1 2>%a@ %s@;<1 2>%a@]%a%a@]"
"Type declarations do not match"
!Oprint.out_sig_item
(Printtyp.tree_of_type_declaration id diff.got Trec_first)
"is not included in"
!Oprint.out_sig_item
(Printtyp.tree_of_type_declaration id diff.expected Trec_first)
(Includecore.report_type_mismatch
"the first" "the second" "declaration" env) diff.symptom
show_locs (diff.got.type_loc, diff.expected.type_loc)
| Err.Extension_constructors diff ->
Fmt.dprintf "@[<v>@[<hv>%s:@;<1 2>%a@ %s@;<1 2>%a@]@ %a%a@]"
"Extension declarations do not match"
!Oprint.out_sig_item
(Printtyp.tree_of_extension_constructor id diff.got Text_first)
"is not included in"
!Oprint.out_sig_item
(Printtyp.tree_of_extension_constructor id diff.expected Text_first)
(Includecore.report_extension_constructor_mismatch
"the first" "the second" "declaration" env) diff.symptom
show_locs (diff.got.ext_loc, diff.expected.ext_loc)
| Err.Class_type_declarations diff ->
Fmt.dprintf
"@[<hv 2>Class type declarations do not match:@ \
%a@;<1 -2>does not match@ %a@]@ %a"
!Oprint.out_sig_item
(Printtyp.tree_of_cltype_declaration id diff.got Trec_first)
!Oprint.out_sig_item
(Printtyp.tree_of_cltype_declaration id diff.expected Trec_first)
(Includeclass.report_error_doc Type_scheme) diff.symptom
| Err.Class_declarations {got;expected;symptom} ->
let t1 = Printtyp.tree_of_class_declaration id got Trec_first in
let t2 = Printtyp.tree_of_class_declaration id expected Trec_first in
Fmt.dprintf
"@[<hv 2>Class declarations do not match:@ \
%a@;<1 -2>does not match@ %a@]@ %a"
!Oprint.out_sig_item t1
!Oprint.out_sig_item t2
(Includeclass.report_error_doc Type_scheme) symptom
let missing_field ppf item =
let id, loc, kind = Includemod.item_ident_name item in
Fmt.fprintf ppf "The %s %a is required but not provided%a"
(Includemod.kind_of_field_desc kind)
(Style.as_inline_code Printtyp.ident) id
(show_loc "Expected declaration") loc
let module_types {Err.got=mty1; expected=mty2} =
Fmt.dprintf
"@[<hv 2>Modules do not match:@ \
%a@;<1 -2>is not included in@ %a@]"
!Oprint.out_module_type (Printtyp.tree_of_modtype mty1)
!Oprint.out_module_type (Printtyp.tree_of_modtype mty2)
let eq_module_types {Err.got=mty1; expected=mty2} =
Fmt.dprintf
"@[<hv 2>Module types do not match:@ \
%a@;<1 -2>is not equal to@ %a@]"
!Oprint.out_module_type (Printtyp.tree_of_modtype mty1)
!Oprint.out_module_type (Printtyp.tree_of_modtype mty2)
let module_type_declarations id {Err.got=d1 ; expected=d2} =
Fmt.dprintf
"@[<hv 2>Module type declarations do not match:@ \
%a@;<1 -2>does not match@ %a@]"
!Oprint.out_sig_item (Printtyp.tree_of_modtype_declaration id d1)
!Oprint.out_sig_item (Printtyp.tree_of_modtype_declaration id d2)
let interface_mismatch ppf (diff: _ Err.diff) =
Fmt.fprintf ppf
"The implementation %a@ does not match the interface %a:@ "
Style.inline_code diff.got Style.inline_code diff.expected
let core_module_type_symptom (x:Err.core_module_type_symptom) =
match x with
| Not_an_alias | Not_an_identifier | Abstract_module_type
| Incompatible_aliases -> None
| Unbound_module_path path ->
Some(Fmt.dprintf "Unbound module %a"
(Style.as_inline_code Printtyp.path) path
)
(* Construct a linearized error message from the error tree *)
let rec module_type ~expansion_token ~eqmode ~env ~before ~ctx diff =
match diff.symptom with
| Invalid_module_alias _ (* the difference is non-informative here *)
| After_alias_expansion _ (* we print only the expanded module types *) ->
module_type_symptom ~eqmode ~expansion_token ~env ~before ~ctx
diff.symptom
| Functor Params d -> (* We jump directly to the functor param error *)
functor_params ~expansion_token ~env ~before ~ctx d
| _ ->
let inner = if eqmode then eq_module_types else module_types in
let next =
match diff.symptom with
| Mt_core _ ->
(* In those cases, the refined error messages for the current error
will at most add some minor comments on the current error.
It is thus better to avoid eliding the current error message.
*)
dwith_context ctx (inner diff)
| _ -> dwith_context_and_elision ctx inner diff
in
let before = next :: before in
module_type_symptom ~eqmode ~expansion_token ~env ~before ~ctx
diff.symptom
and module_type_symptom ~eqmode ~expansion_token ~env ~before ~ctx = function
| Mt_core core ->
begin match core_module_type_symptom core with
| None -> before
| Some msg -> Location.msg "%t" msg :: before
end
| Signature s -> signature ~expansion_token ~env ~before ~ctx s
| Functor f -> functor_symptom ~expansion_token ~env ~before ~ctx f
| After_alias_expansion diff ->
module_type ~eqmode ~expansion_token ~env ~before ~ctx diff
| Invalid_module_alias path ->
let printer =
Fmt.dprintf "Module %a cannot be aliased"
(Style.as_inline_code Printtyp.path) path
in
dwith_context ctx printer :: before
and functor_params ~expansion_token ~env ~before ~ctx {got;expected;_} =
let d = Functor_suberror.Inclusion.patch env got expected in
let actual = Functor_suberror.Inclusion.got d in
let expected = Functor_suberror.expected d in
let main =
Fmt.dprintf
"@[<hv 2>Modules do not match:@ \
@[%t@ -> ...@]@;<1 -2>is not included in@ \
@[%t@ -> ...@]@]"
actual expected
in
let msgs = dwith_context ctx main :: before in
let functor_suberrors =
if expansion_token then
Functor_suberror.params functor_arg_diff ~expansion_token env d
else []
in
functor_suberrors @ msgs
and functor_symptom ~expansion_token ~env ~before ~ctx = function
| Result res ->
module_type ~expansion_token ~eqmode:false ~env ~before ~ctx res
| Params d -> functor_params ~expansion_token ~env ~before ~ctx d
and signature ~expansion_token ~env:_ ~before ~ctx sgs =
Printtyp.wrap_printing_env ~error:true sgs.env (fun () ->
match sgs.missings, sgs.incompatibles with
| _ :: _ as missings, _ ->
if expansion_token then
let init_missings, last_missing = Misc.split_last missings in
List.map (Location.msg "%a" missing_field) init_missings
@ with_context ctx missing_field last_missing
:: before
else
before
| [], a :: _ -> sigitem ~expansion_token ~env:sgs.env ~before ~ctx a
| [], [] -> assert false
)
and sigitem ~expansion_token ~env ~before ~ctx (name,s) = match s with
| Core c ->
dwith_context ctx (core env name c) :: before
| Module_type diff ->
module_type ~expansion_token ~eqmode:false ~env ~before
~ctx:(Context.Module name :: ctx) diff
| Module_type_declaration diff ->
module_type_decl ~expansion_token ~env ~before ~ctx name diff
and module_type_decl ~expansion_token ~env ~before ~ctx id diff =
let next =
dwith_context_and_elision ctx (module_type_declarations id) diff in
let before = next :: before in
match diff.symptom with
| Not_less_than mts ->
let before =
Location.msg "The first module type is not included in the second"
:: before
in
module_type ~expansion_token ~eqmode:true ~before ~env
~ctx:(Context.Modtype id :: ctx) mts
| Not_greater_than mts ->
let before =
Location.msg "The second module type is not included in the first"
:: before in
module_type ~expansion_token ~eqmode:true ~before ~env
~ctx:(Context.Modtype id :: ctx) mts
| Incomparable mts ->
module_type ~expansion_token ~eqmode:true ~env ~before
~ctx:(Context.Modtype id :: ctx) mts.less_than
| Illegal_permutation c ->
begin match diff.got.Types.mtd_type with
| None -> assert false
| Some mty ->
with_context (Modtype id::ctx)
(Runtime_coercion.illegal_permutation Context.alt_pp env) (mty,c)
:: before
end
and functor_arg_diff ~expansion_token env (patch: _ Diffing.change) =
match patch with
| Insert mty -> Functor_suberror.Inclusion.insert mty
| Delete mty -> Functor_suberror.Inclusion.delete mty
| Keep (x, y, _) -> Functor_suberror.Inclusion.ok x y
| Change (_, _, Err.Incompatible_params (i,_)) ->
Functor_suberror.Inclusion.incompatible i
| Change (g, e, Err.Mismatch mty_diff) ->
let more () =
subcase_list @@
module_type_symptom ~eqmode:false ~expansion_token ~env ~before:[]
~ctx:[] mty_diff.symptom
in
Functor_suberror.Inclusion.diff g e more
let functor_app_diff ~expansion_token env (patch: _ Diffing.change) =
match patch with
| Insert mty -> Functor_suberror.App.insert mty
| Delete mty -> Functor_suberror.App.delete mty
| Keep (x, y, _) -> Functor_suberror.App.ok x y
| Change (_, _, Err.Incompatible_params (i,_)) ->
Functor_suberror.App.incompatible i
| Change (g, e, Err.Mismatch mty_diff) ->
let more () =
subcase_list @@
module_type_symptom ~eqmode:false ~expansion_token ~env ~before:[]
~ctx:[] mty_diff.symptom
in
Functor_suberror.App.diff g e more
let module_type_subst ~env id diff =
match diff.symptom with
| Not_less_than mts ->
module_type ~expansion_token:true ~eqmode:true ~before:[] ~env
~ctx:[Modtype id] mts
| Not_greater_than mts ->
module_type ~expansion_token:true ~eqmode:true ~before:[] ~env
~ctx:[Modtype id] mts
| Incomparable mts ->
module_type ~expansion_token:true ~eqmode:true ~env ~before:[]
~ctx:[Modtype id] mts.less_than
| Illegal_permutation c ->
let mty = diff.got in
let main =
with_context [Modtype id]
(Runtime_coercion.illegal_permutation Context.alt_pp env) (mty,c) in
[main]
let all env = function
| In_Compilation_unit diff ->
let first = Location.msg "%a" interface_mismatch diff in
signature ~expansion_token:true ~env ~before:[first] ~ctx:[] diff.symptom
| In_Type_declaration (id,reason) ->
[Location.msg "%t" (core env id reason)]
| In_Module_type diff ->
module_type ~expansion_token:true ~eqmode:false ~before:[] ~env ~ctx:[]
diff
| In_Module_type_substitution (id,diff) ->
module_type_subst ~env id diff
| In_Signature diff ->
signature ~expansion_token:true ~before:[] ~env ~ctx:[] diff
| In_Expansion cmts ->
match core_module_type_symptom cmts with
| None -> assert false
| Some main -> [Location.msg "%t" main]
(* General error reporting *)
let err_msgs ppf (env, err) =
Printtyp.wrap_printing_env ~error:true env
(fun () -> (coalesce @@ all env err) ppf)
let report_error_doc err =
Location.errorf
~loc:Location.(in_file !input_name)
~footnote:Printtyp.Conflicts.err_msg
"%a" err_msgs err
let report_apply_error_doc ~loc env (app_name, mty_f, args) =
let footnote = Printtyp.Conflicts.err_msg in
let d = Functor_suberror.App.patch env ~f:mty_f ~args in
match d with
(* We specialize the one change and one argument case to remove the
presentation of the functor arguments *)
| [ _, Change (_, _, Err.Incompatible_params (i,_)) ] ->
Location.errorf ~loc ~footnote "%t" (Functor_suberror.App.incompatible i)
| [ _, Change (g, e, Err.Mismatch mty_diff) ] ->
let more () =
subcase_list @@
module_type_symptom ~eqmode:false ~expansion_token:true ~env ~before:[]
~ctx:[] mty_diff.symptom
in
Location.errorf ~loc ~footnote "%t"
(Functor_suberror.App.single_diff g e more)
| _ ->
let not_functor =
List.for_all (function _, Diffing.Delete _ -> true | _ -> false) d
in
if not_functor then
match app_name with
| Includemod.Named_leftmost_functor lid ->
Location.errorf ~loc
"@[The module %a is not a functor, it cannot be applied.@]"
(Style.as_inline_code Printtyp.longident) lid
| Includemod.Anonymous_functor
| Includemod.Full_application_path _
(* The "non-functor application in term" case is directly handled in
[Env] and it is the only case where we have a full application
path at hand. Thus this case of the or-pattern is currently
unreachable and we don't try to specialize the corresponding error
message. *) ->
Location.errorf ~loc
"@[This module is not a functor, it cannot be applied.@]"
else
let intro ppf =
match app_name with
| Includemod.Anonymous_functor ->
Fmt.fprintf ppf "This functor application is ill-typed."
| Includemod.Full_application_path lid ->
Fmt.fprintf ppf "The functor application %a is ill-typed."
(Style.as_inline_code Printtyp.longident) lid
| Includemod.Named_leftmost_functor lid ->
Fmt.fprintf ppf
"This application of the functor %a is ill-typed."
(Style.as_inline_code Printtyp.longident) lid
in
let actual = Functor_suberror.App.got d in
let expected = Functor_suberror.expected d in
let sub =
List.rev @@