Skip to content

Commit 44309a2

Browse files
cknittclaude
andauthored
Compare the @as tags of variant cases with payloads across signatures (#9009)
Why: Interface inclusion compared the `@as` literal of nullary cases only. An implementation could swap the tags of two cases with payloads, and its clients then matched `M.A(_)` as `C` (#8956). The tags of tagged cases with a payload are compared as those of nullary cases are. An untagged case's `@as` does not change its representation and is not compared. Type declarations in messages now print the `@as` of a tagged case with a payload too, so the mismatch shows. Diagnostic added: Example: module M: { type t = A({x: int}) | C({x: int}) } = { type t = | @as("C") A({x: int}) | @as("A") C({x: int}) } After (diagnostic): Type declarations do not match: type t = @as("C") A({x: int}) | @as("A") C({x: int}) is not included in type t = A({x: int}) | C({x: int}) The internal representations for case A are not equal. Fixes: #8956 Claude-Session: https://claude.ai/code/session_01CMkrYaktctdKc4M41WF36t Signed-off-by: Christoph Knittel <ck@cca.io> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 35b1e48 commit 44309a2

6 files changed

Lines changed: 66 additions & 21 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- Reject an implementation whose variant cases with a payload have other `@as` tags than the interface states, and print those tags in type declarations.

‎compiler/ml/UntaggedVariants.md‎

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -339,8 +339,10 @@ value. An inline-record field whose runtime name equals the tag field is an
339339
error.
340340

341341
An interface and its implementation must agree on `@unboxed`, on `@tag`, and
342-
on the literal of every nullary case. They are not compared on the `@as` of
343-
cases with a payload (see Known bugs).
342+
on the literal of every nullary case and of every tagged case with a payload.
343+
The `@as` of an untagged case with a payload does not change its
344+
representation (rule 2) and is not compared. Type declarations in messages
345+
print the `@as` of nullary and tagged cases.
344346

345347
- Implementation: `variant_layout.ml:layout_from_type_variant`,
346348
`ast_untagged_variants.ml:process_tag_name`, `check_tag_field_conflicts`;
@@ -349,8 +351,8 @@ cases with a payload (see Known bugs).
349351
`variant_tag_overlaps_with_field.res`, `UntaggedInvalidVariantTagAnnotation.res`,
350352
`UntaggedImplIntf.res`; `tests/build_tests/super_errors_multi/fixtures/`
351353
`Iface_unboxed_variant_mismatch`, `Iface_tag_name_mismatch`,
352-
`Iface_variant_representation_mismatch`.
353-
- Shaped by #6498, #6513, #6669, #7875.
354+
`Iface_variant_representation_mismatch`; `variant_payload_tag_mismatch.res`.
355+
- Shaped by #6498, #6513, #6669, #7875, #8956.
354356

355357
## Known bugs
356358

@@ -376,20 +378,6 @@ let f = v => switch v { | I8(_) => "i8" | U8(_) => "u8" }
376378
Console.log(f(I8(Uint8Array.fromArray([1])))) // u8
377379
```
378380

379-
**Payload-case tags are not compared across a signature.** Rule 14 compares
380-
only the literals of nullary cases.
381-
382-
```rescript
383-
module M: {
384-
type t = A({x: int}) | C({x: int})
385-
let a: t
386-
} = {
387-
type t = | @as("C") A({x: int}) | @as("A") C({x: int})
388-
let a = A({x: 1})
389-
}
390-
Console.log(switch M.a { | M.A(_) => "A" | M.C(_) => "C" }) // C
391-
```
392-
393381
## Open questions
394382

395383
These are the decisions a general mechanism for recognizing payloads by class

‎compiler/ml/includecore.ml‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,17 @@ and compare_variants ~loc env params1 params2 ~layout1 ~layout2 n
271271
let tag_type2 = Variant_runtime.constructor_tag layout2 (n - 1) in
272272
if tag_type1 <> tag_type2 then [Variant_representation cd1.cd_id]
273273
else []
274-
| false -> r
274+
| false -> (
275+
(* A tagged case with a payload carries its tag in the object;
276+
an untagged one is its payload, whatever its [@as] *)
277+
match
278+
( Variant_runtime.constructor_at layout1 (n - 1),
279+
Variant_runtime.constructor_at layout2 (n - 1) )
280+
with
281+
| Block (Tagged {tag = tag1}), Block (Tagged {tag = tag2})
282+
when tag1.literal <> tag2.literal ->
283+
[Variant_representation cd1.cd_id]
284+
| _ -> r)
275285
in
276286
if r <> [] then r
277287
else

‎compiler/ml/printtyp.ml‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -909,9 +909,17 @@ and tree_of_constructor_arguments ?printing_context = function
909909

910910
and tree_of_constructor ?printing_context ~layout ~position cd =
911911
let name = Ident.name cd.cd_id in
912-
let nullary = Ast_untagged_variants.is_nullary_variant cd.cd_args in
912+
(* The [@as] of a nullary case or of a tagged case with a payload sets its
913+
runtime tag; an untagged case is its payload *)
914+
let tagged =
915+
Ast_untagged_variants.is_nullary_variant cd.cd_args
916+
||
917+
match Variant_runtime.constructor_at layout position with
918+
| Block (Tagged _) -> true
919+
| Block (Untagged _) | Constant _ -> false
920+
in
913921
let repr =
914-
if not nullary then None
922+
if not tagged then None
915923
else
916924
let as_annotation = function
917925
| Variant_runtime.Null -> "@as(null)"
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/variant_payload_tag_mismatch.res:5:5-8:1
4+
5+
3 │ type t = A({x: int}) | C({x: int})
6+
4 │ let a: t
7+
5 │ } = {
8+
6 │  type t = | @as("C") A({x: int}) | @as("A") C({x: int})
9+
7 │  let a = A({x: 1})
10+
8 │ }
11+
9 │ Console.log(
12+
10 │ switch M.a {
13+
14+
Signature mismatch:
15+
...
16+
Type declarations do not match:
17+
type t = @as("C") A({x: int}) | @as("A") C({x: int})
18+
is not included in
19+
type t = A({x: int}) | C({x: int})
20+
/.../fixtures/variant_payload_tag_mismatch.res:3:3-36:
21+
Expected declaration
22+
/.../fixtures/variant_payload_tag_mismatch.res:6:3-56:
23+
Actual declaration
24+
The internal representations for case A are not equal.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Payload cases swapped by @as across a signature (#8956)
2+
module M: {
3+
type t = A({x: int}) | C({x: int})
4+
let a: t
5+
} = {
6+
type t = | @as("C") A({x: int}) | @as("A") C({x: int})
7+
let a = A({x: 1})
8+
}
9+
Console.log(
10+
switch M.a {
11+
| M.A(_) => "A"
12+
| M.C(_) => "C"
13+
},
14+
)

0 commit comments

Comments
 (0)