Skip to content

Commit 422ee0f

Browse files
author
Roman Leshchinskiy
committed
Improve error messages
1 parent 820d020 commit 422ee0f

File tree

3 files changed

+37
-14
lines changed

3 files changed

+37
-14
lines changed

ocaml/testsuite/tests/typing-modules/strengthening.ml

+12-4
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,12 @@ module type S2 = sig
2727
module N : T with M
2828
end
2929
[%%expect{|
30-
Line 1:
31-
Error: Modules do not match: sig end is not included in T
30+
Line 4, characters 13-21:
31+
4 | module N : T with M
32+
^^^^^^^^
33+
Error: In this strengthened module type, the type of M
34+
does not match the underlying type
35+
Modules do not match: sig end is not included in T
3236
|}]
3337

3438
module type S3 = sig
@@ -68,8 +72,12 @@ module Must_be_subtype = struct
6872
module type S = sig type t1 type t2 end with M
6973
end
7074
[%%expect{|
71-
Line 1:
72-
Error: Modules do not match:
75+
Line 3, characters 18-48:
76+
3 | module type S = sig type t1 type t2 end with M
77+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
78+
Error: In this strengthened module type, the type of M
79+
does not match the underlying type
80+
Modules do not match:
7381
sig type t1 end
7482
is not included in
7583
sig type t1 type t2 end

ocaml/typing/typemod.ml

+24-10
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ type error =
8686
| Unpackable_local_modtype_subst of Path.t
8787
| With_cannot_remove_packed_modtype of Path.t * module_type
8888
| Toplevel_nonvalue of string * sort
89+
| Strengthening_mismatch of Longident.t * Includemod.explanation
8990

9091
exception Error of Location.t * Env.t * error
9192
exception Error_forward of Location.error
@@ -1472,18 +1473,23 @@ and transl_modtype_jane_syntax_aux ~loc env = function
14721473
| Jane_syntax.Module_type.Jmty_strengthen { mty ; mod_id } ->
14731474
let tmty = transl_modtype_aux env mty in
14741475
let path, md =
1475-
Env.lookup_module ~use:false ~loc mod_id.txt env
1476+
Env.lookup_module ~use:false ~loc:mod_id.loc mod_id.txt env
14761477
in
14771478
let aliasable = not (Env.is_functor_arg path env) in
1478-
ignore
1479-
(Includemod.modtypes ~loc env
1480-
~mark:Includemod.Mark_both md.md_type tmty.mty_type);
1481-
mkmty
1482-
(Tmty_strengthen (tmty, path, mod_id))
1483-
(Mty_strengthen (tmty.mty_type, path, Aliasability.aliasable aliasable))
1484-
env
1485-
loc
1486-
[]
1479+
try
1480+
ignore
1481+
(Includemod.modtypes ~loc env
1482+
~mark:Includemod.Mark_both md.md_type tmty.mty_type);
1483+
mkmty
1484+
(Tmty_strengthen (tmty, path, mod_id))
1485+
(Mty_strengthen
1486+
(tmty.mty_type, path, Aliasability.aliasable aliasable))
1487+
env
1488+
loc
1489+
[]
1490+
with Includemod.Error explanation ->
1491+
raise(Error(loc, env, Strengthening_mismatch(mod_id.txt, explanation)))
1492+
;
14871493

14881494
and transl_with ~loc env remove_aliases (rev_tcstrs,sg) constr =
14891495
let lid, with_info = match constr with
@@ -3661,6 +3667,14 @@ let report_error ~loc _env = function
36613667
Location.errorf ~loc
36623668
"@[Top-level module bindings must have layout value, but@ \
36633669
%s has layout@ %a.@]" id Sort.format sort
3670+
| Strengthening_mismatch(lid, explanation) ->
3671+
let main = Includemod_errorprinter.err_msgs explanation in
3672+
Location.errorf ~loc
3673+
"@[<v>\
3674+
@[In this strengthened module type, the type of %a@ \
3675+
does not match the underlying type@]@ \
3676+
%t@]"
3677+
longident lid main
36643678

36653679
let report_error env ~loc err =
36663680
Printtyp.wrap_printing_env ~error:true env

ocaml/typing/typemod.mli

+1
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ type error =
145145
| Unpackable_local_modtype_subst of Path.t
146146
| With_cannot_remove_packed_modtype of Path.t * module_type
147147
| Toplevel_nonvalue of string * sort
148+
| Strengthening_mismatch of Longident.t * Includemod.explanation
148149

149150
exception Error of Location.t * Env.t * error
150151
exception Error_forward of Location.error

0 commit comments

Comments
 (0)