Skip to content

Commit 7a08865

Browse files
authored
flambda-backend: Make -error_size directly about length of printed error (#2700)
1 parent 4c0a469 commit 7a08865

File tree

7 files changed

+165
-17
lines changed

7 files changed

+165
-17
lines changed

testsuite/tests/typing-modules/inclusion_errors_elision.ml

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,46 @@ Lines 9-13, characters 15-3:
2626
11 | type a and b and c and d and e and f and g and h
2727
12 | end
2828
13 | end
29+
Error: Signature mismatch:
30+
Modules do not match:
31+
sig
32+
module B :
33+
sig type a and b and c and d and e and f and g and h end
34+
end
35+
is not included in
36+
S
37+
In module B:
38+
Modules do not match:
39+
sig
40+
type a = B.a
41+
and b = B.b
42+
and c = B.c
43+
and d = B.d
44+
and e = B.e
45+
and f = B.f
46+
and g = B.g
47+
and h = B.h
48+
end
49+
is not included in
50+
(module A)
51+
|}]
52+
53+
module C : S = struct
54+
module B = struct
55+
type a and b and c and d and e and f and g and h
56+
and a_type_with_extremely_long_long_long_long_long_long_long_long_name
57+
and a_type_with_extremely_long_long_long_long_long_long_long_long_name0
58+
end
59+
end
60+
[%%expect {|
61+
Lines 1-7, characters 15-3:
62+
1 | ...............struct
63+
2 | module B = struct
64+
3 | type a and b and c and d and e and f and g and h
65+
4 | and a_type_with_extremely_long_long_long_long_long_long_long_long_name
66+
5 | and a_type_with_extremely_long_long_long_long_long_long_long_long_name0
67+
6 | end
68+
7 | end
2969
Error: Signature mismatch:
3070
...
3171
In module B:
@@ -39,6 +79,10 @@ Error: Signature mismatch:
3979
and f = B.f
4080
and g = B.g
4181
and h = B.h
82+
and a_type_with_extremely_long_long_long_long_long_long_long_long_name =
83+
B.a_type_with_extremely_long_long_long_long_long_long_long_long_name
84+
and a_type_with_extremely_long_long_long_long_long_long_long_long_name0 =
85+
B.a_type_with_extremely_long_long_long_long_long_long_long_long_name0
4286
end
4387
is not included in
4488
(module A)
@@ -72,6 +116,69 @@ Lines 11-17, characters 15-3:
72116
15 | end
73117
16 | end
74118
17 | end
119+
Error: Signature mismatch:
120+
Modules do not match:
121+
sig
122+
module type B =
123+
sig
124+
module C :
125+
sig type a and b and c and d and e and f and g and h end
126+
end
127+
end
128+
is not included in
129+
S
130+
Module type declarations do not match:
131+
module type B =
132+
sig
133+
module C :
134+
sig type a and b and c and d and e and f and g and h end
135+
end
136+
does not match
137+
module type B = sig module C = A end
138+
At position module type B = <here>
139+
Module types do not match:
140+
sig
141+
module C :
142+
sig type a and b and c and d and e and f and g and h end
143+
end
144+
is not equal to
145+
sig module C = A end
146+
At position module type B = sig module C : <here> end
147+
Modules do not match:
148+
sig
149+
type a = C.a
150+
and b = C.b
151+
and c = C.c
152+
and d = C.d
153+
and e = C.e
154+
and f = C.f
155+
and g = C.g
156+
and h = C.h
157+
end
158+
is not included in
159+
(module A)
160+
|}]
161+
162+
module D : S = struct
163+
module type B = sig
164+
module C: sig
165+
type a and b and c and d and e and f and g and h
166+
and a_type_with_extremely_long_long_long_long_long_long_long_long_name
167+
and a_type_with_extremely_long_long_long_long_long_long_long_long_name0
168+
end
169+
end
170+
end
171+
[%%expect{|
172+
Lines 1-9, characters 15-3:
173+
1 | ...............struct
174+
2 | module type B = sig
175+
3 | module C: sig
176+
4 | type a and b and c and d and e and f and g and h
177+
5 | and a_type_with_extremely_long_long_long_long_long_long_long_long_name
178+
6 | and a_type_with_extremely_long_long_long_long_long_long_long_long_name0
179+
7 | end
180+
8 | end
181+
9 | end
75182
Error: Signature mismatch:
76183
...
77184
...
@@ -87,6 +194,10 @@ Error: Signature mismatch:
87194
and f = C.f
88195
and g = C.g
89196
and h = C.h
197+
and a_type_with_extremely_long_long_long_long_long_long_long_long_name =
198+
C.a_type_with_extremely_long_long_long_long_long_long_long_long_name
199+
and a_type_with_extremely_long_long_long_long_long_long_long_long_name0 =
200+
C.a_type_with_extremely_long_long_long_long_long_long_long_long_name0
90201
end
91202
is not included in
92203
(module A)

testsuite/tests/typing-objects-bugs/pr4824a_bad.compilers.reference

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ File "pr4824a_bad.ml", line 10, characters 2-45:
22
10 | struct class c x = object val x = x end end
33
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
44
Error: Signature mismatch:
5-
...
5+
Modules do not match:
6+
sig class c : 'a -> object val x : 'a end end
7+
is not included in
8+
sig class c : 'a -> object val x : 'b end end
69
Class declarations do not match:
710
class c : 'a -> object val x : 'a end
811
does not match

testsuite/tests/typing-sigsubst/test_locations.compilers.reference

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,14 @@ File "test_loc_type_eq.ml", line 1, characters 49-76:
22
1 | module M : Test_functor.S with type elt = unit = Test_functor.Apply (String)
33
^^^^^^^^^^^^^^^^^^^^^^^^^^^
44
Error: Signature mismatch:
5-
...
5+
Modules do not match:
6+
sig
7+
type elt = String.t
8+
type t = Test_functor.Apply(String).t
9+
val create : elt -> t
10+
end
11+
is not included in
12+
sig type elt = unit type t val create : elt -> t end
613
Type declarations do not match:
714
type elt = String.t
815
is not included in
@@ -35,7 +42,14 @@ File "test_loc_type_subst.ml", line 1, characters 50-77:
3542
1 | module M : Test_functor.S with type elt := unit = Test_functor.Apply (String)
3643
^^^^^^^^^^^^^^^^^^^^^^^^^^^
3744
Error: Signature mismatch:
38-
...
45+
Modules do not match:
46+
sig
47+
type elt = String.t
48+
type t = Test_functor.Apply(String).t
49+
val create : elt -> t
50+
end
51+
is not included in
52+
sig type t val create : unit -> t end
3953
Values do not match:
4054
val create : elt -> t
4155
is not included in

typing/includemod_errorprinter.ml

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -182,15 +182,9 @@ end
182182

183183
module Err = Includemod.Error
184184

185-
let buffer = ref Bytes.empty
186-
let is_big obj =
185+
let is_big p =
187186
let size = !Clflags.error_size in
188-
size > 0 &&
189-
begin
190-
if Bytes.length !buffer < size then buffer := Bytes.create size;
191-
try ignore (Marshal.to_buffer !buffer 0 size obj []); false
192-
with _ -> true
193-
end
187+
size > 0 && Misc.is_print_longer_than size p
194188

195189
let show_loc msg ppf loc =
196190
let pos = loc.Location.loc_start in
@@ -575,11 +569,11 @@ let with_context ?loc ctx printer diff =
575569
let dwith_context ?loc ctx printer =
576570
Location.msg ?loc "%a%t" Context.pp (List.rev ctx) printer
577571

578-
let dwith_context_and_elision ?loc ctx printer diff =
579-
if is_big (diff.got,diff.expected) then
572+
let dwith_context_and_elision ?loc ctx print_diff =
573+
if is_big print_diff then
580574
Location.msg ?loc "..."
581575
else
582-
dwith_context ?loc ctx (printer diff)
576+
dwith_context ?loc ctx print_diff
583577

584578
(* Merge sub msgs into one printer *)
585579
let coalesce msgs =
@@ -750,7 +744,7 @@ let rec module_type ~expansion_token ~eqmode ~env ~before ~ctx diff =
750744
It is thus better to avoid eliding the current error message.
751745
*)
752746
dwith_context ctx (inner diff)
753-
| _ -> dwith_context_and_elision ctx inner diff
747+
| _ -> dwith_context_and_elision ctx (inner diff)
754748
in
755749
let before = next :: before in
756750
module_type_symptom ~eqmode ~expansion_token ~env ~before ~ctx
@@ -819,7 +813,7 @@ and sigitem ~expansion_token ~env ~before ~ctx (name,s) = match s with
819813
module_type_decl ~expansion_token ~env ~before ~ctx name diff
820814
and module_type_decl ~expansion_token ~env ~before ~ctx id diff =
821815
let next =
822-
dwith_context_and_elision ctx (module_type_declarations id) diff in
816+
dwith_context_and_elision ctx (module_type_declarations id diff) in
823817
let before = next :: before in
824818
match diff.symptom with
825819
| Not_less_than mts ->

utils/clflags.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ and no_auto_link = ref false (* -noautolink *)
110110
and dllpaths = ref ([] : string list) (* -dllpath *)
111111
and make_package = ref false (* -pack *)
112112
and for_package = ref (None: string option) (* -for-pack *)
113-
and error_size = ref 500 (* -error-size *)
113+
and error_size = ref 256 (* -error-size *)
114114
and float_const_prop = ref true (* -no-float-const-prop *)
115115
and transparent_modules = ref false (* -trans-mod *)
116116
let unique_ids = ref true (* -d(no-)unique-ds *)

utils/misc.ml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1099,6 +1099,28 @@ let output_of_print print =
10991099
in
11001100
output
11011101

1102+
let is_print_longer_than size p =
1103+
let exception Limit_exceeded in
1104+
let limit = ref size in
1105+
let count_down len =
1106+
limit := !limit - len;
1107+
if !limit < 0 then raise Limit_exceeded
1108+
in
1109+
let out_string _ _ len = count_down len in
1110+
let out_newline () = count_down 1 in
1111+
let out_spaces n = count_down n in
1112+
let out_flush _ = () in
1113+
let out_indent _ = () in
1114+
let out_functions : Format.formatter_out_functions = {
1115+
out_string;
1116+
out_flush;
1117+
out_newline;
1118+
out_spaces;
1119+
out_indent}
1120+
in
1121+
let ppf = Format.formatter_of_out_functions out_functions in
1122+
try p ppf; false
1123+
with Limit_exceeded -> true
11021124

11031125
type filepath = string
11041126

utils/misc.mli

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -630,6 +630,10 @@ val output_of_print :
630630
Note that naively using [Format.formatter_of_out_channel] typechecks but
631631
doesn't work because it fails to flush the formatter. *)
632632

633+
val is_print_longer_than: int -> (Format.formatter -> unit) -> bool
634+
(** Returns [true] if the printed string is longer than the given integer. Stops
635+
early if so. Spaces and newlines are counted, but indentation is not. *)
636+
633637
(** {1 Displaying configuration variables} *)
634638

635639
val show_config_and_exit : unit -> unit

0 commit comments

Comments
 (0)