Skip to content

Commit 98ff97a

Browse files
cknittclaude
andcommitted
Match fields and constructors by name in signature mismatch errors
Why: The inclusion check compared the fields of two records and the constructors of two variants by position and reported only the first difference. A field missing in the middle, two swapped fields or a constructor added at the front all read as "Fields number N have different names", and the other differences stayed hidden. Upstream OCaml 4.13 lines the declarations up with an edit distance (#9130); names are unique within a declaration, so lining them up by name is enough here. Includecore.align lines the two lists up by name. Declarations with the same names in the same order, as nearly all are, are paired directly. Otherwise a name in only one declaration is missing or extra, one name lacking on each side after the same common entry is a renaming (paired in order when that gap holds as many on each side), and common names in another order are reported once, in the expected order. Each entry in both is compared on its own, at its own position in each variant layout, so every difference is reported. The differences of an inline record are grouped under its constructor. The declarations are printed as before; the blocks below show the reasons under them. Diagnostic modified (field missing in the middle): Example: module M: { type t = {name: string, age: int, email: string} } = { type t = {name: string, email: string} } Before (diagnostic): Fields number 2 have different names, email and age. After (diagnostic): The field age is only present in the expected declaration. Diagnostic modified (fields in another order): Example: module M: { type t = {a: int, b: int, c: int, d: int} } = { type t = {c: int, a: int, b: int, e: int} } Before (diagnostic): Fields number 1 have different names, c and a. After (diagnostic): The field d is only present in the expected declaration. The field e is only present in the actual declaration. The fields are in another order in the expected declaration: a, b, c. Diagnostic modified (several differing fields): Example: module M: { type t = {id: int, mutable name: string, age: int, @as("e-mail") email?: string} } = { type t = {id: string, name: string, age: float, email: string} } Before (diagnostic): The mutability of field name is different. After (diagnostic): The types for field id are not equal. The mutability of field name is different. The types for field age are not equal. The optional attribute of field email is different. The field email has the runtime name "email" in the actual declaration, but "e-mail" in the expected declaration. Diagnostic modified (constructor added and renamed): Example: module M: { type t = Red | Green | Blue | Gray } = { type t = Black | Red | Green | Blue | Grey } Before (diagnostic): Constructors number 1 have different names, Black and Red. After (diagnostic): The constructor Grey is named Gray in the expected declaration. The constructor Black is only present in the actual declaration. Diagnostic modified (inline record of a constructor): Example: module M: { type t = Person({name: string, age: int}) | Anonymous } = { type t = Person({age: int, name: string, nick: string}) | Anonymous } Before (diagnostic): Fields number 0 have different names, age and name. After (diagnostic): The records of constructor Person differ: The field nick is only present in the actual declaration. The fields are in another order in the expected declaration: name, age. Review: Does the check accept anything it rejected before, or the other way round? No. A declaration passes only when every name is in both, in the same order, with no difference per entry, which is what the positional walk required; only the messages change. Review: Why is the type of an optional field not reported when only its optional attribute differs? The type of an optional field is an option of the declared type, so it differs whenever the attribute does; the attribute message already says so. 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 6176c4d commit 98ff97a

21 files changed

Lines changed: 456 additions & 113 deletions
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- Report every difference between the record fields or variant constructors of an implementation and its interface, matched by name: missing, extra, renamed and reordered entries, and say which side is the actual and which the expected declaration.

‎compiler/ml/includecore.ml‎

Lines changed: 241 additions & 107 deletions
Large diffs are not rendered by default.

‎compiler/ml/includecore.mli‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,16 @@ type type_mismatch =
3131
| Field_mutable of Ident.t
3232
| Field_optional of Ident.t
3333
| Field_arity of Ident.t
34-
| Field_names of int * string * string
34+
| Field_renamed of string * string
35+
| Field_runtime_name of Ident.t * string * string
3536
| Field_missing of bool * Ident.t
37+
| Field_order of string list
3638
| Constructor_type of Ident.t
3739
| Constructor_arity of Ident.t
38-
| Constructor_names of int * string * string
40+
| Constructor_renamed of string * string
3941
| Constructor_missing of bool * Ident.t
42+
| Constructor_order of string list
43+
| Constructor_record of Ident.t * type_mismatch list
4044
| Record_representation of record_representation * record_representation
4145
| Unboxed_representation of bool
4246
| Immediate

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,5 @@
1717
Expected declaration (/.../fixtures/OptionalInlineImplIntf.res:2:3-23):
1818
type t = A({x?: int})
1919

20-
The optional attribute of field x is different.
20+
The records of constructor A differ:
21+
The optional attribute of field x is different.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
2+
We've found a bug for you!
3+
/.../fixtures/inclusion_constructor_extra.res:5:5-7:1
4+
5+
3 │ module M: {
6+
4 │ type t = Red | Green | Blue | Gray
7+
5 │ } = {
8+
6 │  type t = Black | Red | Green | Blue | Grey
9+
7 │ }
10+
8 │
11+
12+
Signature mismatch:
13+
14+
Actual declaration (/.../fixtures/inclusion_constructor_extra.res:6:3-44):
15+
type t = Black | Red | Green | Blue | Grey
16+
17+
Expected declaration (/.../fixtures/inclusion_constructor_extra.res:4:3-36):
18+
type t = Red | Green | Blue | Gray
19+
20+
The constructor Grey is named Gray in the expected declaration.
21+
The constructor Black is only present in the actual declaration.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
We've found a bug for you!
3+
/.../fixtures/inclusion_constructor_order.res:5:5-7:1
4+
5+
3 │ module M: {
6+
4 │ type t = A(int) | B(string) | C
7+
5 │ } = {
8+
6 │  type t = B(int) | A(int) | C
9+
7 │ }
10+
8 │
11+
12+
Signature mismatch:
13+
14+
Actual declaration (/.../fixtures/inclusion_constructor_order.res:6:3-30):
15+
type t = B(int) | A(int) | C
16+
17+
Expected declaration (/.../fixtures/inclusion_constructor_order.res:4:3-33):
18+
type t = A(int) | B(string) | C
19+
20+
The types for constructor B are not equal.
21+
The constructors are in another order in the expected declaration:
22+
A, B, C.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
We've found a bug for you!
3+
/.../fixtures/inclusion_field_missing.res:5:5-7:1
4+
5+
3 │ module M: {
6+
4 │ type t = {name: string, age: int, email: string}
7+
5 │ } = {
8+
6 │  type t = {name: string, email: string}
9+
7 │ }
10+
8 │
11+
12+
Signature mismatch:
13+
14+
Actual declaration (/.../fixtures/inclusion_field_missing.res:6:3-40):
15+
type t = {name: string, email: string}
16+
17+
Expected declaration (/.../fixtures/inclusion_field_missing.res:4:3-50):
18+
type t = {name: string, age: int, email: string}
19+
20+
The field age is only present in the expected declaration.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
2+
We've found a bug for you!
3+
/.../fixtures/inclusion_field_order.res:5:5-7:1
4+
5+
3 │ module M: {
6+
4 │ type t = {a: int, b: int, c: int, d: int}
7+
5 │ } = {
8+
6 │  type t = {c: int, a: int, b: int, e: int}
9+
7 │ }
10+
8 │
11+
12+
Signature mismatch:
13+
14+
Actual declaration (/.../fixtures/inclusion_field_order.res:6:3-43):
15+
type t = {c: int, a: int, b: int, e: int}
16+
17+
Expected declaration (/.../fixtures/inclusion_field_order.res:4:3-43):
18+
type t = {a: int, b: int, c: int, d: int}
19+
20+
The field d is only present in the expected declaration.
21+
The field e is only present in the actual declaration.
22+
The fields are in another order in the expected declaration:
23+
a, b, c.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
We've found a bug for you!
3+
/.../fixtures/inclusion_field_renamed.res:4:5-6:1
4+
5+
2 │ module M: {
6+
3 │ type t = {name: string, email: string, age: int}
7+
4 │ } = {
8+
5 │  type t = {name: string, mail: string, age: int}
9+
6 │ }
10+
7 │
11+
12+
Signature mismatch:
13+
14+
Actual declaration (/.../fixtures/inclusion_field_renamed.res:5:3-49):
15+
type t = {name: string, mail: string, age: int}
16+
17+
Expected declaration (/.../fixtures/inclusion_field_renamed.res:3:3-50):
18+
type t = {name: string, email: string, age: int}
19+
20+
The field mail is named email in the expected declaration.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
2+
We've found a bug for you!
3+
/.../fixtures/inclusion_field_several.res:4:5-6:1
4+
5+
2 │ module M: {
6+
3 │ type t = {id: int, mutable name: string, age: int, @as("e-mail") email
7+
│ ?: string}
8+
4 │ } = {
9+
5 │  type t = {id: string, name: string, age: float, email: string}
10+
6 │ }
11+
7 │
12+
13+
Signature mismatch:
14+
15+
Actual declaration (/.../fixtures/inclusion_field_several.res:5:3-64):
16+
type t = {id: string, name: string, age: float, email: string}
17+
18+
Expected declaration (/.../fixtures/inclusion_field_several.res:3:3-82):
19+
type t = {id: int, mutable name: string, age: int, @as("e-mail") email?: string}
20+
21+
The types for field id are not equal.
22+
The mutability of field name is different.
23+
The types for field age are not equal.
24+
The optional attribute of field email is different.
25+
The field email has the runtime name "email" in the actual declaration,
26+
but "e-mail" in the expected declaration.

0 commit comments

Comments
 (0)