Skip to content

Commit f1f7da4

Browse files
rleshchinskiyRoman Leshchinskiy
and
Roman Leshchinskiy
authored
flambda-backend: Abbreviate module types when printing error messages (#1895)
* Abbreviate module types when printing error messages Module types can become huge and pretty-printing them in error messages seems to be very slow. In one case, we produced a ~400k lines error message which took about 4 minutes to generate. This PR caps the overall number of signature elements we print and the depth to which we unfold module types. In my somewhat limited testing, this seemed to be the best heuristic I tried, although it is far from perfect. We can adjust later based on user feedback. * Add comments --------- Co-authored-by: Roman Leshchinskiy <[email protected]>
1 parent e4566dd commit f1f7da4

File tree

6 files changed

+246
-139
lines changed

6 files changed

+246
-139
lines changed
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
(* TEST
2+
* expect
3+
*)
4+
5+
module Width = struct
6+
module type S1 = sig
7+
val a : int
8+
val b : int
9+
val c : int
10+
val d : int
11+
val e : int
12+
end
13+
14+
15+
module type F = functor
16+
(X :
17+
sig
18+
module M1 : sig include S1 end
19+
module M2 : sig include S1 end
20+
module M3 : sig include S1 end
21+
module M4 : sig include S1 end
22+
module M5 : sig include S1 end
23+
module M6 : sig include S1 end
24+
module M7 : sig include S1 end
25+
end
26+
) -> S1
27+
28+
module G(F:F)(X:functor() -> S1) = F(X)
29+
end
30+
31+
[%%expect{|
32+
Line 24, characters 37-41:
33+
24 | module G(F:F)(X:functor() -> S1) = F(X)
34+
^^^^
35+
Error: Modules do not match: functor () -> S1 is not included in
36+
sig
37+
module M1 :
38+
sig
39+
val a : int
40+
val b : int
41+
val c : int
42+
val d : int
43+
val e : int
44+
end
45+
module M2 :
46+
sig val a : int val b : int val c : int val d : int ... end
47+
module M3 : sig ... end
48+
module M4 : sig ... end
49+
module M5 : sig ... end
50+
module M6 : sig ... end
51+
module M7 : sig ... end
52+
end
53+
Modules do not match:
54+
functor () -> ...
55+
is not included in
56+
functor -> ...
57+
An extra argument is provided of module type ()
58+
|}]
59+
60+
module Depth = struct
61+
module type S = sig
62+
module M : sig
63+
module N : sig
64+
module O : sig
65+
module P : sig
66+
module Q : sig
67+
val x : int
68+
val y : int
69+
end
70+
end
71+
end
72+
end
73+
end
74+
end
75+
76+
module type F = functor(X:S) -> S
77+
78+
module F(F : F)(X : sig module M : sig end end) = F(X)
79+
end
80+
81+
[%%expect{|
82+
Line 19, characters 52-56:
83+
19 | module F(F : F)(X : sig module M : sig end end) = F(X)
84+
^^^^
85+
Error: Modules do not match: sig module M : sig end end is not included in
86+
S
87+
In module M:
88+
Modules do not match:
89+
sig end
90+
is not included in
91+
sig
92+
module N :
93+
sig
94+
module O : sig module P : sig module Q : sig ... end end end
95+
end
96+
end
97+
In module M: The module `N' is required but not provided
98+
|}]

testsuite/tests/typing-modules/functors.ml

Lines changed: 4 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -921,37 +921,14 @@ Error: Signature mismatch:
921921
sig
922922
module B :
923923
sig
924-
module C :
925-
sig
926-
module D :
927-
sig
928-
module E :
929-
sig
930-
module F :
931-
functor (X : sig type x end)
932-
(Y : sig type y' end) (W : sig type w end) ->
933-
sig end
934-
end
935-
end
936-
end
924+
module C : sig module D : sig module E : sig ... end end end
937925
end
938926
end
939927
is not included in
940928
sig
941929
module B :
942930
sig
943-
module C :
944-
sig
945-
module D :
946-
sig
947-
module E :
948-
sig
949-
module F :
950-
sig type x end -> sig type y end ->
951-
sig type z end -> sig type w end -> sig end
952-
end
953-
end
954-
end
931+
module C : sig module D : sig module E : sig ... end end end
955932
end
956933
end
957934
In module B:
@@ -966,8 +943,8 @@ Error: Signature mismatch:
966943
module E :
967944
sig
968945
module F :
969-
sig type x end -> sig type y end ->
970-
sig type z end -> sig type w end -> sig end
946+
sig ... end -> sig ... end -> sig ... end ->
947+
sig ... end -> sig end
971948
end
972949
end
973950
end

testsuite/tests/typing-modules/illegal_permutation.ml

Lines changed: 4 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -502,22 +502,7 @@ Error: Signature mismatch:
502502
sig
503503
module C :
504504
functor (X : sig end) (Y : sig end)
505-
(Z : sig
506-
module D :
507-
sig
508-
module E :
509-
sig
510-
module F :
511-
functor (X : sig end)
512-
(Arg : sig
513-
val two : int
514-
val one : int
515-
end)
516-
-> sig end
517-
end
518-
end
519-
end)
520-
-> sig end
505+
(Z : sig ... end) -> sig end
521506
end
522507
end
523508
end
@@ -532,22 +517,7 @@ Error: Signature mismatch:
532517
sig
533518
module C :
534519
functor (X : sig end) (Y : sig end)
535-
(Z : sig
536-
module D :
537-
sig
538-
module E :
539-
sig
540-
module F :
541-
functor (X : sig end)
542-
(Arg : sig
543-
val one : int
544-
val two : int
545-
end)
546-
-> sig end
547-
end
548-
end
549-
end)
550-
-> sig end
520+
(Z : sig ... end) -> sig end
551521
end
552522
end
553523
end
@@ -561,22 +531,7 @@ Error: Signature mismatch:
561531
sig
562532
module C :
563533
functor (X : sig end) (Y : sig end)
564-
(Z : sig
565-
module D :
566-
sig
567-
module E :
568-
sig
569-
module F :
570-
functor (X : sig end)
571-
(Arg : sig
572-
val two : int
573-
val one : int
574-
end)
575-
-> sig end
576-
end
577-
end
578-
end)
579-
-> sig end
534+
(Z : sig module D : sig ... end end) -> sig end
580535
end
581536
end
582537
end
@@ -589,22 +544,7 @@ Error: Signature mismatch:
589544
sig
590545
module C :
591546
functor (X : sig end) (Y : sig end)
592-
(Z : sig
593-
module D :
594-
sig
595-
module E :
596-
sig
597-
module F :
598-
functor (X : sig end)
599-
(Arg : sig
600-
val one : int
601-
val two : int
602-
end)
603-
-> sig end
604-
end
605-
end
606-
end)
607-
-> sig end
547+
(Z : sig module D : sig ... end end) -> sig end
608548
end
609549
end
610550
end

typing/includemod_errorprinter.ml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ let show_locs ppf (loc1, loc2) =
203203

204204

205205
let dmodtype mty =
206-
let tmty = Printtyp.tree_of_modtype mty in
206+
let tmty = Printtyp.tree_of_modtype ~abbrev:true mty in
207207
Format.dprintf "%a" !Oprint.out_module_type tmty
208208

209209
let space ppf () = Format.fprintf ppf "@ "
@@ -666,22 +666,22 @@ let module_types {Err.got=mty1; expected=mty2} =
666666
Format.dprintf
667667
"@[<hv 2>Modules do not match:@ \
668668
%a@;<1 -2>is not included in@ %a@]"
669-
!Oprint.out_module_type (Printtyp.tree_of_modtype mty1)
670-
!Oprint.out_module_type (Printtyp.tree_of_modtype mty2)
669+
!Oprint.out_module_type (Printtyp.tree_of_modtype ~abbrev:true mty1)
670+
!Oprint.out_module_type (Printtyp.tree_of_modtype ~abbrev:true mty2)
671671

672672
let eq_module_types {Err.got=mty1; expected=mty2} =
673673
Format.dprintf
674674
"@[<hv 2>Module types do not match:@ \
675675
%a@;<1 -2>is not equal to@ %a@]"
676-
!Oprint.out_module_type (Printtyp.tree_of_modtype mty1)
677-
!Oprint.out_module_type (Printtyp.tree_of_modtype mty2)
676+
!Oprint.out_module_type (Printtyp.tree_of_modtype ~abbrev:true mty1)
677+
!Oprint.out_module_type (Printtyp.tree_of_modtype ~abbrev:true mty2)
678678

679679
let module_type_declarations id {Err.got=d1 ; expected=d2} =
680680
Format.dprintf
681681
"@[<hv 2>Module type declarations do not match:@ \
682682
%a@;<1 -2>does not match@ %a@]"
683-
!Oprint.out_sig_item (Printtyp.tree_of_modtype_declaration id d1)
684-
!Oprint.out_sig_item (Printtyp.tree_of_modtype_declaration id d2)
683+
!Oprint.out_sig_item (Printtyp.tree_of_modtype_declaration ~abbrev:true id d1)
684+
!Oprint.out_sig_item (Printtyp.tree_of_modtype_declaration ~abbrev:true id d2)
685685

686686
let interface_mismatch ppf (diff: _ Err.diff) =
687687
Format.fprintf ppf

0 commit comments

Comments
 (0)