Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changes/inline-record-coercion-reason.polish.md
Original file line number Diff line number Diff line change
@@ -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.
79 changes: 55 additions & 24 deletions compiler/ml/ctype.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) ->
Expand All @@ -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) )
->
Expand All @@ -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 =
Expand Down
6 changes: 3 additions & 3 deletions compiler/ml/printtyp.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1729,8 +1729,8 @@ let print_record_field_subtype_violation ppf
" is configured to be @{<info>\"%s\"@} (via the @as attribute)" as_name
| None -> fprintf ppf " is @{<info>\"%s\"@}" label);
fprintf ppf " in record @{<info>%s@}, but in record @{<info>%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
Expand All @@ -1743,8 +1743,8 @@ let print_record_field_subtype_violation ppf
"The field @{<info>%s@} is missing in record @{<info>%s@}, but present \
in record @{<info>%s@}"
label
(Path.name right_record_name)
(Path.name left_record_name)
(Path.name right_record_name)
| Mutable_mismatch {label} ->
fprintf ppf
"The field @{<info>%s@} is mutable in record @{<info>%s@}, but not in \
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
- 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.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
- 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.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
- 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.
Original file line number Diff line number Diff line change
Expand Up @@ -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
- The field z is missing in record x, but present in record y
Original file line number Diff line number Diff line change
@@ -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.
Original file line number Diff line number Diff line change
@@ -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.
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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.
Original file line number Diff line number Diff line change
@@ -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)
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
type v1 = A({x: float})
type v2 = A({mutable x: float})

let toV2 = (a: v1) => (a :> v2)
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
type v1 = A({x: float})
type v2 = A({x: float, y: int})

let toV2 = (a: v1) => (a :> v2)
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
type v1 = B | A({x: float})
type v2 = B | A({x?: float})

let toV2 = (a: v1) => (a :> v2)
Original file line number Diff line number Diff line change
@@ -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)
Loading