Skip to content

Commit b354825

Browse files
cknittclaude
andcommitted
Keep the module type comparison around a permutation of its fields
Why: Codex review of #9140: when two module types have the same fields in another order, the innermost symptom is the permutation, which shows no declarations, and leaving out the module type comparison around it left only "Illegal permutation of structure fields". A comparison is now left out only when a later symptom shows the declarations that differ. The parser gave a module type declaration in a signature no location, unlike one in a structure, so the expected side printed without a position. It now spans from the name to the end, as in a structure. Diagnostic modified (fields of a module type in another order): Example: module M: { module type T = { let a: int let b: int } } = { module type T = { let b: int let a: int } } Before (diagnostic): Signature mismatch: In module type T: Illegal permutation of structure fields After (diagnostic): Signature mismatch: Actual declaration (include_modtype_permutation.res:7:15-10:3): module type T = { let b: int let a: int } Expected declaration (include_modtype_permutation.res:2:15-5:3): module type T = { let a: int let b: int } In module type T: Illegal permutation of structure fields Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Kbf3gLpf7RN1MiRcubPgRn Signed-off-by: Christoph Knittel <ck@cca.io>
1 parent e819c19 commit b354825

3 files changed

Lines changed: 33 additions & 3 deletions

File tree

‎compiler/ml/includemod.ml‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -681,10 +681,22 @@ let include_err ~show_context ppf (cxt, env, err) =
681681
differ. The modules on the way are left out, as the items at the end say
682682
what differs, each under the position it is at. *)
683683
let report_error ppf errs =
684+
(* Whether a symptom shows the declarations that differ. A permutation of
685+
fields doesn't, so the comparison of the module types around it stays. *)
686+
let shows_declarations (_, _, symptom) =
687+
match symptom with
688+
| Missing_field _ | Value_descriptions _ | Type_declarations _
689+
| Extension_constructors _ | Module_types _ | Modtype_infos _ ->
690+
true
691+
| Modtype_permutation | Interface_mismatch _ | Unbound_modtype_path _
692+
| Unbound_module_path _ | Invalid_module_alias _ ->
693+
false
694+
in
684695
let rec shown = function
685696
| [] -> []
686-
| [err] -> [err]
687-
| (_, _, (Module_types _ | Modtype_infos _)) :: rest -> shown rest
697+
| (_, _, (Module_types _ | Modtype_infos _)) :: rest
698+
when List.exists shows_declarations rest ->
699+
shown rest
688700
| err :: rest -> err :: shown rest
689701
in
690702
let rec print ppf previous_cxt = function

‎compiler/syntax/src/res_core.ml‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7244,6 +7244,7 @@ and parse_module_declaration_or_alias ~attrs p =
72447244

72457245
and parse_module_type_declaration ~attrs ~start_pos p =
72467246
Parser.expect Typ p;
7247+
let name_start = Parser.start_pos p in
72477248
let module_name =
72487249
match Parser.peek p with
72497250
| Uident ident ->
@@ -7265,7 +7266,12 @@ and parse_module_type_declaration ~attrs ~start_pos p =
72657266
Some (parse_module_type p)
72667267
| _ -> None
72677268
in
7268-
let module_decl = Ast_helper.Mtd.mk ~attrs ?typ module_name in
7269+
(* From the name, like a module type declaration in a structure *)
7270+
let module_decl =
7271+
Ast_helper.Mtd.mk ~attrs
7272+
~loc:(mk_loc name_start (Parser.position p))
7273+
?typ module_name
7274+
in
72697275
Ast_helper.Sig.modtype ~loc:(mk_loc start_pos (Parser.position p)) module_decl
72707276

72717277
and parse_sign_let_desc ~attrs p =

‎tests/build_tests/super_errors/expected/include_modtype_permutation.res.expected‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,18 @@
1313

1414
Signature mismatch:
1515

16+
Actual declaration (/.../fixtures/include_modtype_permutation.res:7:15-10:3):
17+
module type T = {
18+
let b: int
19+
let a: int
20+
}
21+
22+
Expected declaration (/.../fixtures/include_modtype_permutation.res:2:15-5:3):
23+
module type T = {
24+
let a: int
25+
let b: int
26+
}
27+
1628
In module type T:
1729

1830
Illegal permutation of structure fields

0 commit comments

Comments
 (0)