From b07ae0fa8852c4ae12156c9246f31369a3da8f59 Mon Sep 17 00:00:00 2001 From: Christoph Knittel Date: Tue, 3 Nov 2026 18:34:12 +0000 Subject: [PATCH] Explain why a variant's inline record cannot be coerced Why: A coercion that fails on an inline record field said only that one variant is not a subtype of the other; since #9086 that includes an immutable field coerced to a mutable one. Plain records name the field. The record messages for a missing field and an @as mismatch also named the two records the wrong way round. Diagnostic modified (inline record coercion): Example: type v1 = A({x: float}) type v2 = A({mutable x: float}) let toV2 = (a: v1) => (a :> v2) Before (diagnostic): Type v1 is not a subtype of v2 After (diagnostic): Type v1 is not a subtype of v2 The record v1.A cannot be coerced to the record v2.A because: - The field x is mutable in record v2.A, but not in record v1.A. A coercion can't make a field writable. Diagnostic modified (missing record field): Example: type x = {x: int} type y = {x: int, z: int} let toY = (a: x) => (a :> y) Before (diagnostic): - The field z is missing in record y, but present in record x After (diagnostic): - The field z is missing in record x, but present in record y Review: Why only the first failing constructor? The error has one context; the first constructor whose fields fail is reported. Review: A different field type in an inline record still gives no reason; acceptance there is unchanged. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Kbf3gGhHjzBTXqSo8Dckpb Signed-off-by: Christoph Knittel --- .../inline-record-coercion-reason.polish.md | 1 + compiler/ml/ctype.ml | 79 +++++++++++++------ compiler/ml/printtyp.ml | 6 +- ...cion_as_payload_mismatch_both.res.expected | 2 +- ...cion_as_payload_mismatch_left.res.expected | 2 +- ...ion_as_payload_mismatch_right.res.expected | 2 +- ...record_coercion_missing_field.res.expected | 2 +- ...ion_inline_record_as_mismatch.res.expected | 13 +++ ...e_record_immutable_to_mutable.res.expected | 13 +++ ...n_inline_record_missing_field.res.expected | 13 +++ ...line_record_optional_mismatch.res.expected | 13 +++ ...ine_record_second_constructor.res.expected | 13 +++ ...ant_coercion_inline_record_as_mismatch.res | 6 ++ ...ion_inline_record_immutable_to_mutable.res | 4 + ...t_coercion_inline_record_missing_field.res | 4 + ...ercion_inline_record_optional_mismatch.res | 4 + ...rcion_inline_record_second_constructor.res | 4 + 17 files changed, 150 insertions(+), 31 deletions(-) create mode 100644 changes/inline-record-coercion-reason.polish.md create mode 100644 tests/build_tests/super_errors/expected/variant_coercion_inline_record_as_mismatch.res.expected create mode 100644 tests/build_tests/super_errors/expected/variant_coercion_inline_record_immutable_to_mutable.res.expected create mode 100644 tests/build_tests/super_errors/expected/variant_coercion_inline_record_missing_field.res.expected create mode 100644 tests/build_tests/super_errors/expected/variant_coercion_inline_record_optional_mismatch.res.expected create mode 100644 tests/build_tests/super_errors/expected/variant_coercion_inline_record_second_constructor.res.expected create mode 100644 tests/build_tests/super_errors/fixtures/variant_coercion_inline_record_as_mismatch.res create mode 100644 tests/build_tests/super_errors/fixtures/variant_coercion_inline_record_immutable_to_mutable.res create mode 100644 tests/build_tests/super_errors/fixtures/variant_coercion_inline_record_missing_field.res create mode 100644 tests/build_tests/super_errors/fixtures/variant_coercion_inline_record_optional_mismatch.res create mode 100644 tests/build_tests/super_errors/fixtures/variant_coercion_inline_record_second_constructor.res diff --git a/changes/inline-record-coercion-reason.polish.md b/changes/inline-record-coercion-reason.polish.md new file mode 100644 index 0000000000..253e7e6218 --- /dev/null +++ b/changes/inline-record-coercion-reason.polish.md @@ -0,0 +1 @@ +- A variant coercion that fails on a missing, optional or `@as`-renamed inline record field now says which field is wrong, and record coercion errors no longer swap the two record names for a missing field or a different `@as` name. diff --git a/compiler/ml/ctype.ml b/compiler/ml/ctype.ml index 1e71fa12a7..4116d748fe 100644 --- a/compiler/ml/ctype.ml +++ b/compiler/ml/ctype.ml @@ -3531,6 +3531,13 @@ let coerces_into_private_type env t1 t2 = | None -> true) | Some _ | None -> false +(* Whether a variant constructor coerces to the same-named constructor of the + target variant, and if not, whether the error can say why *) +type constructor_coercion = + | Coerces + | Fails_with of subtype_context + | Fails_unexplained + let rec subtype_rec env trace t1 t2 cstrs = let t1 = repr t1 in let t2 = repr t2 in @@ -3759,7 +3766,7 @@ let rec subtype_rec env trace t1 t2 cstrs = c2 |> List.iteri (fun position (c : Types.constructor_declaration) -> Hashtbl.add constructor_map (Ident.name c.cd_id) (c, position)); - let field_subtype_violations = + let constructor_coercions = c1 |> List.mapi (fun position1 (c : Types.constructor_declaration) -> @@ -3784,18 +3791,30 @@ let rec subtype_rec env trace t1 t2 cstrs = | [] -> ( try let lst = subtype_list env trace tl1 tl2 cstrs in - if List.length lst = List.length cstrs then None - else - Some - [ (* TODO(subtype-errors) Variant constructor inline record mismatch *) ] + if List.length lst = List.length cstrs then + Coerces + else Fails_unexplained + (* TODO(subtype-errors) Variant constructor inline record field type mismatch *) with _ -> - Some - [ (* TODO(subtype-errors) Variant constructor inline record mismatch *) ] + Fails_unexplained + (* TODO(subtype-errors) Variant constructor inline record field type mismatch *) ) - | violations -> Some violations + | violations -> + (* The inline record types are named like the + compiler prints them, e.g. [v1.A]. *) + let inline_record_name p = + Path.Pdot (p, Ident.name c.cd_id, Path.nopos) + in + Fails_with + (Record_fields_mismatch + { + left_record_name = inline_record_name p1; + right_record_name = inline_record_name p2; + issues = violations; + }) else - Some - [ (* TODO(subtype-errors) Variant constructor representation mismatch*) ] + Fails_unexplained + (* TODO(subtype-errors) Variant constructor representation mismatch*) | ( {Types.cd_args = Cstr_tuple tl1}, Some ({Types.cd_args = Cstr_tuple tl2}, position2) ) -> @@ -3806,22 +3825,34 @@ let rec subtype_rec env trace t1 t2 cstrs = then try let lst = subtype_list env trace tl1 tl2 cstrs in - if List.length lst = List.length cstrs then None - else - Some - [ (* TODO(subtype-errors) Variant constructor tuple mismatch *) ] - with _ -> - Some - [ (* TODO(subtype-errors) Variant constructor tuple mismatch *) ] + if List.length lst = List.length cstrs then Coerces + else Fails_unexplained + (* TODO(subtype-errors) Variant constructor tuple mismatch *) + with _ -> Fails_unexplained + (* TODO(subtype-errors) Variant constructor tuple mismatch *) else - Some - [ (* TODO(subtype-errors) Variant constructor tuple mismatch *) ] - | _ -> - Some [ (* TODO(subtype-errors) Variant other issue *) ]) - |> List.filter_map Fun.id + Fails_unexplained + (* TODO(subtype-errors) Variant constructor tuple mismatch *) + | _ -> Fails_unexplained + (* TODO(subtype-errors) Variant other issue *)) in - if field_subtype_violations = [] then cstrs - else (trace, t1, t2, !(current_univar_pairs ()), None) :: cstrs) + if + List.for_all + (function + | Coerces -> true + | Fails_with _ | Fails_unexplained -> false) + constructor_coercions + then cstrs + else + (* Report the first failure the error can explain *) + let context = + List.find_map + (function + | Fails_with context -> Some context + | Coerces | Fails_unexplained -> None) + constructor_coercions + in + (trace, t1, t2, !(current_univar_pairs ()), context) :: cstrs) | ( (p1, _, {type_kind = Type_record (fields1, repr1)}), (p2, _, {type_kind = Type_record (fields2, repr2)}) ) -> let same_repr = diff --git a/compiler/ml/printtyp.ml b/compiler/ml/printtyp.ml index 8d3b29e866..030d84ea8d 100644 --- a/compiler/ml/printtyp.ml +++ b/compiler/ml/printtyp.ml @@ -1729,8 +1729,8 @@ let print_record_field_subtype_violation ppf " is configured to be @{\"%s\"@} (via the @as attribute)" as_name | None -> fprintf ppf " is @{\"%s\"@}" label); fprintf ppf " in record @{%s@}, but in record @{%s@}" - (Path.name right_record_name) - (Path.name left_record_name); + (Path.name left_record_name) + (Path.name right_record_name); (match right_as with | Some as_name -> fprintf ppf @@ -1743,8 +1743,8 @@ let print_record_field_subtype_violation ppf "The field @{%s@} is missing in record @{%s@}, but present \ in record @{%s@}" label - (Path.name right_record_name) (Path.name left_record_name) + (Path.name right_record_name) | Mutable_mismatch {label} -> fprintf ppf "The field @{%s@} is mutable in record @{%s@}, but not in \ diff --git a/tests/build_tests/super_errors/expected/record_coercion_as_payload_mismatch_both.res.expected b/tests/build_tests/super_errors/expected/record_coercion_as_payload_mismatch_both.res.expected index bac13d93eb..78e0b8caf4 100644 --- a/tests/build_tests/super_errors/expected/record_coercion_as_payload_mismatch_both.res.expected +++ b/tests/build_tests/super_errors/expected/record_coercion_as_payload_mismatch_both.res.expected @@ -10,4 +10,4 @@ Type x is not a subtype of y The record x cannot be coerced to the record y because: - - Field x runtime representation is configured to be "z" (via the @as attribute) in record y, but in record x it is configured to be "w" (via the @as attribute). Runtime representations must match. \ No newline at end of file + - Field x runtime representation is configured to be "z" (via the @as attribute) in record x, but in record y it is configured to be "w" (via the @as attribute). Runtime representations must match. \ No newline at end of file diff --git a/tests/build_tests/super_errors/expected/record_coercion_as_payload_mismatch_left.res.expected b/tests/build_tests/super_errors/expected/record_coercion_as_payload_mismatch_left.res.expected index 4b0a538ad5..a39f50ef39 100644 --- a/tests/build_tests/super_errors/expected/record_coercion_as_payload_mismatch_left.res.expected +++ b/tests/build_tests/super_errors/expected/record_coercion_as_payload_mismatch_left.res.expected @@ -10,4 +10,4 @@ Type x is not a subtype of y The record x cannot be coerced to the record y because: - - Field x runtime representation is configured to be "z" (via the @as attribute) in record y, but in record x it is "x". Runtime representations must match. \ No newline at end of file + - Field x runtime representation is configured to be "z" (via the @as attribute) in record x, but in record y it is "x". Runtime representations must match. \ No newline at end of file diff --git a/tests/build_tests/super_errors/expected/record_coercion_as_payload_mismatch_right.res.expected b/tests/build_tests/super_errors/expected/record_coercion_as_payload_mismatch_right.res.expected index 202528c634..2974ee1535 100644 --- a/tests/build_tests/super_errors/expected/record_coercion_as_payload_mismatch_right.res.expected +++ b/tests/build_tests/super_errors/expected/record_coercion_as_payload_mismatch_right.res.expected @@ -10,4 +10,4 @@ Type x is not a subtype of y The record x cannot be coerced to the record y because: - - Field x runtime representation is "x" in record y, but in record x it is configured to be "z" (via the @as attribute). Runtime representations must match. \ No newline at end of file + - Field x runtime representation is "x" in record x, but in record y it is configured to be "z" (via the @as attribute). Runtime representations must match. \ No newline at end of file diff --git a/tests/build_tests/super_errors/expected/record_coercion_missing_field.res.expected b/tests/build_tests/super_errors/expected/record_coercion_missing_field.res.expected index 8c2bb96ab9..43f9457b53 100644 --- a/tests/build_tests/super_errors/expected/record_coercion_missing_field.res.expected +++ b/tests/build_tests/super_errors/expected/record_coercion_missing_field.res.expected @@ -10,4 +10,4 @@ Type x is not a subtype of y The record x cannot be coerced to the record y because: - - The field z is missing in record y, but present in record x \ No newline at end of file + - The field z is missing in record x, but present in record y \ No newline at end of file diff --git a/tests/build_tests/super_errors/expected/variant_coercion_inline_record_as_mismatch.res.expected b/tests/build_tests/super_errors/expected/variant_coercion_inline_record_as_mismatch.res.expected new file mode 100644 index 0000000000..dd3f8a824b --- /dev/null +++ b/tests/build_tests/super_errors/expected/variant_coercion_inline_record_as_mismatch.res.expected @@ -0,0 +1,13 @@ + + We've found a bug for you! + /.../fixtures/variant_coercion_inline_record_as_mismatch.res:6:26-34 + + 4 │ } + 5 │ + 6 │ let toV2 = (a: M.v1) => (a :> M.v2) + 7 │ + + Type M.v1 is not a subtype of M.v2 + + The record M.v1.A cannot be coerced to the record M.v2.A because: + - Field x runtime representation is configured to be "z" (via the @as attribute) in record M.v1.A, but in record M.v2.A it is "x". Runtime representations must match. \ No newline at end of file diff --git a/tests/build_tests/super_errors/expected/variant_coercion_inline_record_immutable_to_mutable.res.expected b/tests/build_tests/super_errors/expected/variant_coercion_inline_record_immutable_to_mutable.res.expected new file mode 100644 index 0000000000..e56d1a92d4 --- /dev/null +++ b/tests/build_tests/super_errors/expected/variant_coercion_inline_record_immutable_to_mutable.res.expected @@ -0,0 +1,13 @@ + + We've found a bug for you! + /.../fixtures/variant_coercion_inline_record_immutable_to_mutable.res:4:24-30 + + 2 │ type v2 = A({mutable x: float}) + 3 │ + 4 │ let toV2 = (a: v1) => (a :> v2) + 5 │ + + Type v1 is not a subtype of v2 + + The record v1.A cannot be coerced to the record v2.A because: + - The field x is mutable in record v2.A, but not in record v1.A. A coercion can't make a field writable. \ No newline at end of file diff --git a/tests/build_tests/super_errors/expected/variant_coercion_inline_record_missing_field.res.expected b/tests/build_tests/super_errors/expected/variant_coercion_inline_record_missing_field.res.expected new file mode 100644 index 0000000000..b6dfdca96b --- /dev/null +++ b/tests/build_tests/super_errors/expected/variant_coercion_inline_record_missing_field.res.expected @@ -0,0 +1,13 @@ + + We've found a bug for you! + /.../fixtures/variant_coercion_inline_record_missing_field.res:4:24-30 + + 2 │ type v2 = A({x: float, y: int}) + 3 │ + 4 │ let toV2 = (a: v1) => (a :> v2) + 5 │ + + Type v1 is not a subtype of v2 + + The record v1.A cannot be coerced to the record v2.A because: + - The field y is missing in record v1.A, but present in record v2.A \ No newline at end of file diff --git a/tests/build_tests/super_errors/expected/variant_coercion_inline_record_optional_mismatch.res.expected b/tests/build_tests/super_errors/expected/variant_coercion_inline_record_optional_mismatch.res.expected new file mode 100644 index 0000000000..096172ebc9 --- /dev/null +++ b/tests/build_tests/super_errors/expected/variant_coercion_inline_record_optional_mismatch.res.expected @@ -0,0 +1,13 @@ + + We've found a bug for you! + /.../fixtures/variant_coercion_inline_record_optional_mismatch.res:4:24-30 + + 2 │ type v2 = B | A({x?: float}) + 3 │ + 4 │ let toV2 = (a: v1) => (a :> v2) + 5 │ + + Type v1 is not a subtype of v2 + + The record v1.A cannot be coerced to the record v2.A because: + - The field x is not optional in record v1.A, but is optional in record v2.A \ No newline at end of file diff --git a/tests/build_tests/super_errors/expected/variant_coercion_inline_record_second_constructor.res.expected b/tests/build_tests/super_errors/expected/variant_coercion_inline_record_second_constructor.res.expected new file mode 100644 index 0000000000..c65d296ef1 --- /dev/null +++ b/tests/build_tests/super_errors/expected/variant_coercion_inline_record_second_constructor.res.expected @@ -0,0 +1,13 @@ + + We've found a bug for you! + /.../fixtures/variant_coercion_inline_record_second_constructor.res:4:24-30 + + 2 │ type v2 = A(int) | B({x: float}) | C({mutable y: int}) | D + 3 │ + 4 │ let toV2 = (a: v1) => (a :> v2) + 5 │ + + Type v1 is not a subtype of v2 + + The record v1.C cannot be coerced to the record v2.C because: + - The field y is mutable in record v2.C, but not in record v1.C. A coercion can't make a field writable. \ No newline at end of file diff --git a/tests/build_tests/super_errors/fixtures/variant_coercion_inline_record_as_mismatch.res b/tests/build_tests/super_errors/fixtures/variant_coercion_inline_record_as_mismatch.res new file mode 100644 index 0000000000..9df1fb8885 --- /dev/null +++ b/tests/build_tests/super_errors/fixtures/variant_coercion_inline_record_as_mismatch.res @@ -0,0 +1,6 @@ +module M = { + type v1 = A({@as("z") x: float}) | B({y: int}) + type v2 = A({x: float}) | B({y: int}) +} + +let toV2 = (a: M.v1) => (a :> M.v2) diff --git a/tests/build_tests/super_errors/fixtures/variant_coercion_inline_record_immutable_to_mutable.res b/tests/build_tests/super_errors/fixtures/variant_coercion_inline_record_immutable_to_mutable.res new file mode 100644 index 0000000000..41a15e046d --- /dev/null +++ b/tests/build_tests/super_errors/fixtures/variant_coercion_inline_record_immutable_to_mutable.res @@ -0,0 +1,4 @@ +type v1 = A({x: float}) +type v2 = A({mutable x: float}) + +let toV2 = (a: v1) => (a :> v2) diff --git a/tests/build_tests/super_errors/fixtures/variant_coercion_inline_record_missing_field.res b/tests/build_tests/super_errors/fixtures/variant_coercion_inline_record_missing_field.res new file mode 100644 index 0000000000..212729a44f --- /dev/null +++ b/tests/build_tests/super_errors/fixtures/variant_coercion_inline_record_missing_field.res @@ -0,0 +1,4 @@ +type v1 = A({x: float}) +type v2 = A({x: float, y: int}) + +let toV2 = (a: v1) => (a :> v2) diff --git a/tests/build_tests/super_errors/fixtures/variant_coercion_inline_record_optional_mismatch.res b/tests/build_tests/super_errors/fixtures/variant_coercion_inline_record_optional_mismatch.res new file mode 100644 index 0000000000..6aeef1a83b --- /dev/null +++ b/tests/build_tests/super_errors/fixtures/variant_coercion_inline_record_optional_mismatch.res @@ -0,0 +1,4 @@ +type v1 = B | A({x: float}) +type v2 = B | A({x?: float}) + +let toV2 = (a: v1) => (a :> v2) diff --git a/tests/build_tests/super_errors/fixtures/variant_coercion_inline_record_second_constructor.res b/tests/build_tests/super_errors/fixtures/variant_coercion_inline_record_second_constructor.res new file mode 100644 index 0000000000..1d63e537f8 --- /dev/null +++ b/tests/build_tests/super_errors/fixtures/variant_coercion_inline_record_second_constructor.res @@ -0,0 +1,4 @@ +type v1 = A(int) | B({x: float}) | C({y: int}) +type v2 = A(int) | B({x: float}) | C({mutable y: int}) | D + +let toV2 = (a: v1) => (a :> v2)