Skip to content

Commit 594ff28

Browse files
cknittclaude
andcommitted
Compare constructor representations in interfaces and coercions alike
Why: An interface or a coercion accepted a tagged constructor for an `@unboxed` one: inclusion compared only the tags of tagged constructors, and the coercion check only the declared `@as` (#9038). The coercion error also said that the wrong variant was unboxed. `Variant_runtime.same_representation` compares two constructors: constant with the same literal, tagged with the same tag, or both untagged. Interface inclusion and the coercion check use it. Type declarations in messages print the `@unboxed` of an untagged constructor of a type that is not `@unboxed` itself, and an interface mismatch names the constructor that is `@unboxed` in only one declaration. The predefined `unknown`, an `@unboxed` variant, now has the untagged layout a declaration of it gets, so `@unboxed type rec t = unknown = Unknown(_): t` still agrees with it. Diagnostic added (interface with an @unboxed constructor): Example: module M: { type t = | @unboxed A(int) | B({x: int}) } = { type t = | A(int) | B({x: int}) } Before (diagnostic): none; clients read {TAG: "A", _0: 3} as B After (diagnostic): Type declarations do not match: type t = A(int) | B({x: int}) is not included in type t = @unboxed A(int) | B({x: int}) Their internal representations differ: case A is @unboxed only in the second declaration. Diagnostic added (coercion between representations): Example: type a = | @unboxed A(int) | B({x: int}) type b = | A(int) | B({x: int}) | C let x: b = (A(3): a :> b) Before (diagnostic): none; the coerced 3 matches C After (diagnostic): Type a is not a subtype of b Diagnostic modified (unboxed configurations): Example: @unboxed type x = One(bool) | Two type y = One(bool) | Two Before (diagnostic): - Variant x is not unboxed. - Variant y is unboxed. After (diagnostic): - Variant x is unboxed. - Variant y is not unboxed. Fixes #9038 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01CMkrYaktctdKc4M41WF36t Signed-off-by: Christoph Knittel <ck@cca.io>
1 parent d48d577 commit 594ff28

14 files changed

Lines changed: 121 additions & 44 deletions
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- Reject interfaces and coercions in which an `@unboxed` constructor meets a tagged one, and name the unboxed variant correctly in the coercion error.

‎compiler/ml/ctype.ml‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3727,9 +3727,9 @@ let rec subtype_rec env trace t1 t2 cstrs =
37273727
({Types.cd_args = Cstr_record fields2}, position2)
37283728
) ->
37293729
if
3730-
Variant_coercion.variant_representation_matches
3731-
(Variant_runtime.constructor_tag layout1 position1)
3732-
(Variant_runtime.constructor_tag layout2 position2)
3730+
Variant_runtime.same_representation
3731+
(Variant_runtime.constructor_at layout1 position1)
3732+
(Variant_runtime.constructor_at layout2 position2)
37333733
then
37343734
let violations, tl1, tl2 =
37353735
Record_coercion.check_record_fields fields1 fields2
@@ -3754,9 +3754,9 @@ let rec subtype_rec env trace t1 t2 cstrs =
37543754
Some ({Types.cd_args = Cstr_tuple tl2}, position2) )
37553755
->
37563756
if
3757-
Variant_coercion.variant_representation_matches
3758-
(Variant_runtime.constructor_tag layout1 position1)
3759-
(Variant_runtime.constructor_tag layout2 position2)
3757+
Variant_runtime.same_representation
3758+
(Variant_runtime.constructor_at layout1 position1)
3759+
(Variant_runtime.constructor_at layout2 position2)
37603760
then
37613761
try
37623762
let lst = subtype_list env trace tl1 tl2 cstrs in

‎compiler/ml/includecore.ml‎

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,9 @@ type type_mismatch =
184184
| Immediate
185185
| Tag_name
186186
| Variant_representation of Ident.t
187+
| Constructor_unboxed of Ident.t * bool
188+
(** The case is untagged in only one declaration; [true] means the
189+
second *)
187190

188191
let report_type_mismatch0 first second decl ppf err =
189192
let pr fmt = Format.fprintf ppf fmt in
@@ -216,6 +219,13 @@ let report_type_mismatch0 first second decl ppf err =
216219
| Tag_name -> pr "Their @tag annotations differ"
217220
| Variant_representation s ->
218221
pr "The internal representations for case %s are not equal" (Ident.name s)
222+
| Constructor_unboxed (s, b) ->
223+
pr
224+
"Their internal representations differ:@ case %s is @@unboxed only in %s \
225+
%s"
226+
(Ident.name s)
227+
(if b then second else first)
228+
decl
219229

220230
let report_type_mismatch first second decl ppf =
221231
List.iter (fun err ->
@@ -265,23 +275,19 @@ and compare_variants ~loc env params1 params2 ~layout1 ~layout2 n
265275
let r =
266276
if r <> [] then r
267277
else
268-
match Ast_untagged_variants.is_nullary_variant cd1.cd_args with
269-
| true ->
270-
let tag_type1 = Variant_runtime.constructor_tag layout1 (n - 1) in
271-
let tag_type2 = Variant_runtime.constructor_tag layout2 (n - 1) in
272-
if tag_type1 <> tag_type2 then [Variant_representation cd1.cd_id]
273-
else []
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)
278+
(* A tagged case with a payload carries its tag in the object; an
279+
untagged one is its payload, whatever its [@as], also when only
280+
some constructors are [@unboxed] *)
281+
let case1 = Variant_runtime.constructor_at layout1 (n - 1)
282+
and case2 = Variant_runtime.constructor_at layout2 (n - 1) in
283+
if Variant_runtime.same_representation case1 case2 then r
284+
else
285+
match (case1, case2) with
286+
| Block (Tagged _), Block (Untagged _) ->
287+
[Constructor_unboxed (cd1.cd_id, true)]
288+
| Block (Untagged _), Block (Tagged _) ->
289+
[Constructor_unboxed (cd1.cd_id, false)]
290+
| _ -> [Variant_representation cd1.cd_id]
285291
in
286292
if r <> [] then r
287293
else

‎compiler/ml/includecore.mli‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ type type_mismatch =
3838
| Immediate
3939
| Tag_name
4040
| Variant_representation of Ident.t
41+
| Constructor_unboxed of Ident.t * bool
42+
(** The case is untagged in only one declaration; [true] means the
43+
second *)
4144

4245
val value_descriptions :
4346
loc:Location.t ->

‎compiler/ml/predef.ml‎

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -340,17 +340,32 @@ let common_initial_env add_type add_extension empty_env =
340340
type_params = [];
341341
type_arity = 0;
342342
type_kind =
343-
plain_variant
344-
[
345-
{
346-
cd_id = ident_ctor_unknown;
347-
cd_runtime_tag = None;
348-
cd_args = Cstr_tuple [tvar];
349-
cd_res = Some (type_unknown ());
350-
cd_loc = Location.none;
351-
cd_attributes = [];
352-
};
353-
];
343+
(* An [@unboxed] variant whose only case is its payload *)
344+
(let layout = Variant_runtime.pending_layout () in
345+
Variant_runtime.complete_layout layout
346+
(Variant_runtime.make_layout
347+
~configuration:{unboxed = true; tag_name = None}
348+
[|
349+
Block
350+
(Untagged
351+
{
352+
tag =
353+
{name = Ident.name ident_ctor_unknown; literal = None};
354+
block_type = UnknownType;
355+
});
356+
|]);
357+
Type_variant
358+
( [
359+
{
360+
cd_id = ident_ctor_unknown;
361+
cd_runtime_tag = None;
362+
cd_args = Cstr_tuple [tvar];
363+
cd_res = Some (type_unknown ());
364+
cd_loc = Location.none;
365+
cd_attributes = [];
366+
};
367+
],
368+
layout ));
354369
type_representation = Types.Unboxed;
355370
}
356371
and decl_promise =

‎compiler/ml/printtyp.ml‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -919,7 +919,10 @@ and tree_of_constructor ?printing_context ~layout ~position cd =
919919
| Block (Untagged _) | Constant _ -> false
920920
in
921921
let repr =
922-
if not tagged then None
922+
if not tagged then
923+
(* An untagged case of a type that is not [@unboxed] as a whole *)
924+
if (Variant_runtime.configuration layout).unboxed then None
925+
else Some "@unboxed"
923926
else
924927
let as_annotation = function
925928
| Variant_runtime.Null -> "@as(null)"
@@ -1604,7 +1607,7 @@ let print_variant_configuration_issue ppf
16041607
let print_unboxed_status ppf unboxed name =
16051608
fprintf ppf "@ - Variant @{<info>%s@} is @{<error>%s@}unboxed."
16061609
(Path.name name)
1607-
(if unboxed then "not " else "")
1610+
(if unboxed then "" else "not ")
16081611
in
16091612
print_unboxed_status ppf left_unboxed left_variant_name;
16101613
print_unboxed_status ppf right_unboxed right_variant_name;

‎compiler/ml/variant_coercion.ml‎

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -140,12 +140,6 @@ let can_try_coerce_variant_to_primitive_opt p =
140140
| None -> None
141141
| Some p -> can_try_coerce_variant_to_primitive p
142142

143-
let variant_representation_matches tag1 tag2 =
144-
match (tag1, tag2) with
145-
| None, None -> true
146-
| Some s1, Some s2 when s1 = s2 -> true
147-
| _ -> false
148-
149143
type variant_configuration_error =
150144
| Untagged of {left_is_unboxed: bool}
151145
| TagName of {left_tag: string option; right_tag: string option}

‎compiler/ml/variant_runtime.ml‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,14 @@ let constructor_tag layout position =
184184
| Constant tag -> tag.literal
185185
| Block (Tagged {tag} | Untagged {tag}) -> tag.literal
186186

187+
let same_representation (case1 : constructor_case) (case2 : constructor_case) =
188+
match (case1, case2) with
189+
| Constant tag1, Constant tag2 -> tag1.literal = tag2.literal
190+
| Block (Tagged {tag = tag1}), Block (Tagged {tag = tag2}) ->
191+
tag1.literal = tag2.literal
192+
| Block (Untagged _), Block (Untagged _) -> true
193+
| (Constant _ | Block (Tagged _ | Untagged _)), _ -> false
194+
187195
let representation ({variant; position} : constructor_reference) =
188196
constructor_at (get_layout variant) position
189197

‎compiler/ml/variant_runtime.mli‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,12 @@ val matching_facts : layout -> matching_facts
116116
val configuration : layout -> configuration
117117
val constructor_at : layout -> int -> constructor_case
118118
val constructor_tag : layout -> int -> literal_tag option
119+
120+
val same_representation : constructor_case -> constructor_case -> bool
121+
(** Whether two constructors are represented alike at run time: both constant
122+
with the same literal, both tagged with the same tag, or both untagged,
123+
whose payloads are compared by their types *)
124+
119125
val representation : constructor_reference -> constructor_case
120126
val length : layout -> int
121127
val num_constants : layout -> int
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
2+
We've found a bug for you!
3+
/.../fixtures/untagged_constructor_coercion_mismatch.res:3:14-26
4+
5+
1 │ type a = | @unboxed A(int) | B({x: int})
6+
2 │ type b = A(int) | B({x: int}) | C
7+
3 │ let x: b = ((A(3): a) :> b)
8+
4 │
9+
10+
Type a is not a subtype of b

0 commit comments

Comments
 (0)