Skip to content

Commit 6c353bb

Browse files
cknittclaude
andcommitted
Compare runtime annotations and untagged payloads across signatures
Why: Interface inclusion ignored `@instanceOf`/`@typeOf`. An interface could claim a class its implementation does not have, or hide a private superclass manifest; the implementation and its clients then compiled different checks for the same values, and #8938 came back in the clients. Functor arguments, `with type` constraints and module type ascriptions had the same hole. When an interface states a class or `typeof` for a type, through its own annotation or the type it abbreviates, the implementation must state the same one, superclasses included. An interface that states none hides it. The payload kind of every untagged case is compared too, so a variant declared in both is recognized the same way on both sides. Diagnostic added (hidden superclass): Example: // Classes.res @instanceof("Animal") type animal @instanceof("Dog") type dog = private animal // Classes.resi @instanceof("Animal") type animal @instanceof("Dog") type dog After (diagnostic): Type declarations do not match: type dog = animal is not included in type dog Their runtime representations differ: the first declaration states instances of Dog, a subclass of Animal, the second declaration states instances of Dog. Diagnostic added (claim the implementation does not make): Example: // Claim.res type t = option<string> // Claim.resi @typeof("string") type t After (diagnostic): Their runtime representations differ: the first declaration states no runtime class or typeof, the second declaration states values whose typeof is "string". Review: Why compare payload kinds when the payload types are already compared? Equal payload types are classified alike only when both sides see the same annotations; an interface that hides one (stating none, which is allowed) gives its variant another layout. Review: Why not compare `@notUndefined` too? It is unchanged here and has the same hole on master; the implied never-undefined of the new annotations is covered, because the classification includes it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Fc7LEuyEfynjaKCVEBPmTF Signed-off-by: Christoph Knittel <ck@cca.io>
1 parent fa730fd commit 6c353bb

13 files changed

Lines changed: 145 additions & 1 deletion

File tree

‎compiler/ml/ast_untagged_variants.ml‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,17 @@ let runtime_annotation_to_string = function
307307
| Type_of block_type ->
308308
Printf.sprintf "@%s(\"%s\")" type_of (typeof_name block_type)
309309

310+
(* What a declaration's annotations state, for error messages *)
311+
let describe_declared_block_type = function
312+
| Some (InstanceType (Class {name; ancestors = []})) -> "instances of " ^ name
313+
| Some (InstanceType (Class {name; ancestors})) ->
314+
"instances of " ^ name ^ ", a subclass of "
315+
^ String.concat " and " ancestors
316+
| Some (InstanceType Array) -> "arrays"
317+
| Some block_type ->
318+
"values whose typeof is \"" ^ typeof_name block_type ^ "\""
319+
| None -> "no runtime class or typeof"
320+
310321
(* The [@instanceOf] or [@typeOf] annotation in [attrs], validated. Only
311322
[Typedecl] passes [~loc]; later readers see attributes it accepted. *)
312323
let process_runtime_annotation ?(loc = Location.none)

‎compiler/ml/includecore.ml‎

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ type type_mismatch =
184184
| Immediate
185185
| Tag_name
186186
| Variant_representation of Ident.t
187+
| Runtime_annotation of string * string
187188

188189
let report_type_mismatch0 first second decl ppf err =
189190
let pr fmt = Format.fprintf ppf fmt in
@@ -216,6 +217,10 @@ let report_type_mismatch0 first second decl ppf err =
216217
| Tag_name -> pr "Their @tag annotations differ"
217218
| Variant_representation s ->
218219
pr "The internal representations for case %s are not equal" (Ident.name s)
220+
| Runtime_annotation (declared1, declared2) ->
221+
pr
222+
"Their runtime representations differ:@ %s %s states %s,@ %s %s states %s"
223+
first decl declared1 second decl declared2
219224

220225
let report_type_mismatch first second decl ppf =
221226
List.iter (fun err ->
@@ -271,7 +276,16 @@ and compare_variants ~loc env params1 params2 ~layout1 ~layout2 n
271276
let tag_type2 = Variant_runtime.constructor_tag layout2 (n - 1) in
272277
if tag_type1 <> tag_type2 then [Variant_representation cd1.cd_id]
273278
else []
274-
| false -> r
279+
| false -> (
280+
match
281+
( Variant_runtime.constructor_at layout1 (n - 1),
282+
Variant_runtime.constructor_at layout2 (n - 1) )
283+
with
284+
| ( Block (Untagged {block_type = block_type1}),
285+
Block (Untagged {block_type = block_type2}) )
286+
when block_type1 <> block_type2 ->
287+
[Variant_representation cd1.cd_id]
288+
| _ -> r)
275289
in
276290
if r <> [] then r
277291
else
@@ -317,6 +331,21 @@ and compare_records env params1_ params2_ n_
317331
in
318332
aux ~fast:true params1_ params2_ n_ labels1_ labels2_
319333

334+
(* Values are recognized by the class or [typeof] the second declaration
335+
states, so the first must state the same. Stating nothing hides it. *)
336+
let compare_runtime_annotations env decl1 decl2 =
337+
match Variant_layout.declaration_block_type ~env decl2 with
338+
| None -> []
339+
| Some _ as declared2 ->
340+
let declared1 = Variant_layout.declaration_block_type ~env decl1 in
341+
if declared1 = declared2 then []
342+
else
343+
[
344+
Runtime_annotation
345+
( Ast_untagged_variants.describe_declared_block_type declared1,
346+
Ast_untagged_variants.describe_declared_block_type declared2 );
347+
]
348+
320349
let type_declarations ?(equality = false) ~loc env name decl1 id decl2 =
321350
Builtin_attributes.check_deprecated_inclusion ~def:decl1.type_loc
322351
~use:decl2.type_loc loc decl1.type_attributes decl2.type_attributes name;
@@ -342,6 +371,9 @@ let type_declarations ?(equality = false) ~loc env name decl1 id decl2 =
342371
if Ctype.equal env false [ty1] [ty2] then [] else [Manifest]
343372
else [Constraint]
344373
in
374+
let err =
375+
if err <> [] then err else compare_runtime_annotations env decl1 decl2
376+
in
345377
if err <> [] then err
346378
else
347379
let err =

‎compiler/ml/includecore.mli‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ type type_mismatch =
3838
| Immediate
3939
| Tag_name
4040
| Variant_representation of Ident.t
41+
| Runtime_annotation of string * string
4142

4243
val value_descriptions :
4344
loc:Location.t ->

‎compiler/ml/variant_layout.ml‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,22 @@ let block_type_of_annotations = function
5858
let declared_block_type ~env t =
5959
block_type_of_annotations (runtime_annotations ~env t)
6060

61+
(* The block type a declaration states, through its own annotation or the
62+
ones of the type it abbreviates. *)
63+
let declaration_block_type ~env (decl : Types.type_declaration) =
64+
let own = process_runtime_annotation decl.type_attributes in
65+
let inner =
66+
match decl.type_manifest with
67+
| Some manifest -> runtime_annotations ~env manifest
68+
| None -> []
69+
in
70+
let inner =
71+
match inner with
72+
| annotation :: rest when Some annotation = own -> rest
73+
| _ -> inner
74+
in
75+
block_type_of_annotations (Option.to_list own @ inner)
76+
6177
(* An annotation on an abbreviation is a claim about the abbreviated type, so
6278
it must agree with what the compiler knows about that type. It can refine
6379
an abstract type, restate the annotation the abbreviated type has, or, on a
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
===== Claim.res =====
2+
3+
We've found a bug for you!
4+
/.../fixtures/Cross_runtime_annotation_claimed_by_interface/Claim.res:1:1-23
5+
6+
1 │ type t = option<string>
7+
2 │
8+
9+
The implementation /.../fixtures/Cross_runtime_annotation_claimed_by_interface/Claim.res
10+
does not match the interface /.../fixtures/Cross_runtime_annotation_claimed_by_interface/claim.cmi:
11+
Type declarations do not match:
12+
type t = option<string>
13+
is not included in
14+
type t
15+
/.../fixtures/Cross_runtime_annotation_claimed_by_interface/Claim.resi:1:19-24:
16+
Expected declaration
17+
/.../fixtures/Cross_runtime_annotation_claimed_by_interface/Claim.res:1:1-23:
18+
Actual declaration
19+
Their runtime representations differ:
20+
the first declaration states no runtime class or typeof,
21+
the second declaration states values whose typeof is "string".
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
===== B_user.res =====
2+
3+
We've found a bug for you!
4+
/.../fixtures/Cross_runtime_annotation_functor_argument/B_user.res:10:23-31
5+
6+
8 │ }
7+
9 │
8+
10 │ module Applied = Make(A_classes)
9+
11 │
10+
11+
Signature mismatch:
12+
...
13+
Type declarations do not match:
14+
type dog = A_classes.dog
15+
is not included in
16+
type dog
17+
/.../fixtures/Cross_runtime_annotation_functor_argument/B_user.res:4:24-31:
18+
Expected declaration
19+
/.../fixtures/Cross_runtime_annotation_functor_argument/A_classes.res:2:20-44:
20+
Actual declaration
21+
Their runtime representations differ:
22+
the first declaration states instances of Dog, a subclass of Animal,
23+
the second declaration states instances of Dog.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
===== Classes.res =====
2+
3+
We've found a bug for you!
4+
/.../fixtures/Cross_runtime_annotation_hidden_superclass/Classes.res:2:20-44
5+
6+
1 │ @instanceOf("Animal") type animal
7+
2 │ @instanceOf("Dog") type dog = private animal
8+
3 │
9+
10+
The implementation /.../fixtures/Cross_runtime_annotation_hidden_superclass/Classes.res
11+
does not match the interface /.../fixtures/Cross_runtime_annotation_hidden_superclass/classes.cmi:
12+
Type declarations do not match:
13+
type dog = animal
14+
is not included in
15+
type dog
16+
/.../fixtures/Cross_runtime_annotation_hidden_superclass/Classes.resi:2:20-27:
17+
Expected declaration
18+
/.../fixtures/Cross_runtime_annotation_hidden_superclass/Classes.res:2:20-44:
19+
Actual declaration
20+
Their runtime representations differ:
21+
the first declaration states instances of Dog, a subclass of Animal,
22+
the second declaration states instances of Dog.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
type t = option<string>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
@typeOf("string") type t
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
@instanceOf("Animal") type animal
2+
@instanceOf("Dog") type dog = private animal

0 commit comments

Comments
 (0)