Skip to content

Commit d96aea3

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. 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. Diagnostic modified (sides of a signature mismatch): Before (diagnostic): the second declaration is @notundefined, but the first declaration can hold undefined. After (diagnostic): the expected declaration is @notundefined, but the actual declaration can hold undefined. Matching modified (lining up two declarations, Includecore.align): Before (text): entry i of the actual declaration against entry i of the expected one, stopping at the first difference After (text): same names in the same order → each pair, no tables a name in both → compared, at its own position in each layout a name in one only → missing or extra one name lacking on each side after the same common entry → renamed (paired in order when the gap holds as many on each side) common names in another order → the expected order, once 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 "actual" and "expected"? The error already shows the two locations as "Actual declaration" and "Expected declaration"; the sentences now use the same words instead of "first" and "second". Type re-exports and extensions keep their own words ("the original definition", "this definition"). 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 25e3dfb commit d96aea3

35 files changed

Lines changed: 509 additions & 137 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: 243 additions & 109 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

‎compiler/ml/includemod.ml‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -556,7 +556,8 @@ let include_symptom env ppf = function
556556
"Type declarations do not match" (type_declaration id) d1
557557
"is not included in" (type_declaration id) d2 show_locs
558558
(d1.type_loc, d2.type_loc)
559-
(Includecore.report_type_mismatch "the first" "the second" "declaration")
559+
(Includecore.report_type_mismatch "the actual" "the expected"
560+
"declaration")
560561
errs
561562
| Extension_constructors (id, x1, x2) ->
562563
fprintf ppf

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,5 @@
2626
Expected declaration
2727
/.../fixtures/OptionalInlineImplIntf.res:4:3-22:
2828
Actual declaration
29-
The optional attribute of field x is different.
29+
The records of constructor A differ:
30+
The optional attribute of field x is different.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,4 @@
2727
/.../fixtures/UntaggedImplIntf.res:4:3-24:
2828
Actual declaration
2929
Their internal representations differ:
30-
the second declaration uses unboxed representation.
30+
the expected declaration uses unboxed representation.
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_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+
Type declarations do not match:
15+
type t = Black | Red | Green | Blue | Grey
16+
is not included in
17+
type t = Red | Green | Blue | Gray
18+
/.../fixtures/inclusion_constructor_extra.res:4:3-36:
19+
Expected declaration
20+
/.../fixtures/inclusion_constructor_extra.res:6:3-44:
21+
Actual declaration
22+
The constructor Grey is named Gray in the expected declaration.
23+
The constructor Black is only present in the actual declaration.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
Type declarations do not match:
15+
type t = B(int) | A(int) | C
16+
is not included in
17+
type t = A(int) | B(string) | C
18+
/.../fixtures/inclusion_constructor_order.res:4:3-33:
19+
Expected declaration
20+
/.../fixtures/inclusion_constructor_order.res:6:3-30:
21+
Actual declaration
22+
The types for constructor B are not equal.
23+
The constructors are in another order in the expected declaration:
24+
A, B, C.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+
Modules do not match:
14+
{
15+
type t = {name: string, email: string}
16+
}
17+
is not included in
18+
{
19+
type t = {name: string, age: int, email: string}
20+
}
21+
Type declarations do not match:
22+
type t = {name: string, email: string}
23+
is not included in
24+
type t = {name: string, age: int, email: string}
25+
/.../fixtures/inclusion_field_missing.res:4:3-50:
26+
Expected declaration
27+
/.../fixtures/inclusion_field_missing.res:6:3-40:
28+
Actual declaration
29+
The field age is only present in the expected declaration.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
Type declarations do not match:
15+
type t = {c: int, a: int, b: int, e: int}
16+
is not included in
17+
type t = {a: int, b: int, c: int, d: int}
18+
/.../fixtures/inclusion_field_order.res:4:3-43:
19+
Expected declaration
20+
/.../fixtures/inclusion_field_order.res:6:3-43:
21+
Actual declaration
22+
The field d is only present in the expected declaration.
23+
The field e is only present in the actual declaration.
24+
The fields are in another order in the expected declaration:
25+
a, b, c.

0 commit comments

Comments
 (0)