Skip to content
Open
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
2 changes: 2 additions & 0 deletions changes/coercion-error-explanations.fix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- Fix wrong or missing explanations in coercion errors involving variants and `unknown`.
- Allow coercing an unboxed variant with an `int` payload to `int`.
264 changes: 178 additions & 86 deletions compiler/typing/ctype.ml

Large diffs are not rendered by default.

24 changes: 23 additions & 1 deletion compiler/typing/ctype.mli
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,39 @@ type subtype_context =
| Coercion_target_variant_not_unboxed of {
variant_name: Path.t;
primitive: Path.t;
} (** Coercing a primitive to a variant that is not unboxed. *)
}
(** Coercing a primitive to a variant that has a catch-all case for it,
but is not unboxed. *)
| Coercion_target_variant_does_not_cover_type of {
variant_name: Path.t;
primitive: Path.t;
unboxed: bool;
}
(** Coercing a primitive to a variant that does not have a catch-all case. *)
| Coercion_with_bool_or_unit of {typename: Path.t}
(** Coercing [bool] or [unit] to or from another type. *)
| Coercion_from_unknown (** Coercing [unknown] to another type. *)
| Coercion_to_unknown (** Coercing another type to [unknown]. *)
| Variant_constructor_runtime_representation_mismatch of {
variant_name: Path.t;
issues: Variant_coercion.variant_runtime_representation_issue list;
}
(** A variant constructor's runtime representation does not match the target variant. *)
| Variant_constructor_tag_mismatch of {
left_variant_name: Path.t;
right_variant_name: Path.t;
constructor_name: string;
left_tag: Variant_runtime.literal_tag;
right_tag: Variant_runtime.literal_tag;
} (** A constructor has different runtime tags in the two variants. *)
| Variant_constructor_payload_mismatch of {
left_variant_name: Path.t;
right_variant_name: Path.t;
constructor_name: string;
payloads: (type_expr list * type_expr list) option;
}
(** A constructor's payload does not coerce to its payload in the other
variant. [payloads] are both payloads when they are tuples. *)
| Variant_configurations_mismatch of {
left_variant_name: Path.t;
right_variant_name: Path.t;
Expand Down
13 changes: 2 additions & 11 deletions compiler/typing/parmatch.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2358,23 +2358,14 @@ and shadowed_argument_liveness ~shadowing arg =
| Some shadowed -> Never_matches [shadowed]
| None -> payload_liveness arg)

let literal_to_string : Variant_runtime.literal_tag -> string = function
| String s -> "\"" ^ s ^ "\""
| Int i -> string_of_int i
| Float f -> f
| BigInt b -> b ^ "n"
| Bool b -> string_of_bool b
| Null -> "null"
| Undefined -> "undefined"

let shadowed_payload_reason shadowed =
shadowed |> List.sort_uniq compare
|> List.map (fun {payload_constructor; value; literal_constructor} ->
Printf.sprintf
"%s is the runtime value of constructor %s, so it is always matched as \
%s, never as %s."
(literal_to_string value) literal_constructor literal_constructor
payload_constructor)
(Variant_runtime.literal_to_string value)
literal_constructor literal_constructor payload_constructor)
|> String.concat "\n "

(********************************)
Expand Down
6 changes: 3 additions & 3 deletions compiler/typing/predef.ml
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ and path_bigint = Pident ident_bigint

and path_string = Pident ident_string

and path_unkonwn = Pident ident_unknown
and path_unknown = Pident ident_unknown

and path_promise = Pident ident_promise

Expand Down Expand Up @@ -146,7 +146,7 @@ let type_bigint () = Domain.DLS.get type_bigint_key
let type_string_key = predefined_type path_string
let type_string () = Domain.DLS.get type_string_key

let type_unknown_key = predefined_type path_unkonwn
let type_unknown_key = predefined_type path_unknown
let type_unknown () = Domain.DLS.get type_unknown_key

let predefined_types =
Expand All @@ -159,7 +159,7 @@ let predefined_types =
(type_exn_key, path_exn);
(type_bigint_key, path_bigint);
(type_string_key, path_string);
(type_unknown_key, path_unkonwn);
(type_unknown_key, path_unknown);
]

let reset_for_request () =
Expand Down
1 change: 1 addition & 0 deletions compiler/typing/predef.mli
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ val path_result : Path.t
val path_dict : Path.t

val path_bigint : Path.t
val path_unknown : Path.t
val path_promise : Path.t
val path_tagged_template : Path.t

Expand Down
114 changes: 91 additions & 23 deletions compiler/typing/printtyp.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1613,7 +1613,13 @@ let print_variant_runtime_representation_issue ppf variant_name
constructor_name (Path.name variant_name)
(Ast_untagged_variants.literal_tag_to_user_visible_string as_payload)
(Path.name expected_typename)
| Mismatched_unboxed_payload _ -> ()
| Mismatched_unboxed_payload {constructor_name; expected_typename; payload} ->
fprintf ppf
"The constructor @{<info>%s@} of unboxed variant @{<info>%s@} has a \
payload of type @{<info>%a@}, which is not compatible with \
@{<info>%s@}."
constructor_name (Path.name variant_name) type_expr payload
(Path.name expected_typename)
| Mismatched_as_payload {constructor_name; expected_typename; as_payload} ->
fprintf ppf "The constructor @{<info>%s@} of variant @{<info>%s@} has "
constructor_name (Path.name variant_name);
Expand All @@ -1630,12 +1636,6 @@ let print_variant_runtime_representation_issue ppf variant_name
fprintf ppf
"@ That runtime representation is not compatible with the expected \
runtime representation of @{<info>%s@}."
(Path.name expected_typename);
fprintf ppf
"@,\
@ Fix this by making sure all constructors in variant @{<info>%s@} has \
a runtime representation of @{<info>%s@}."
(Path.name variant_name)
(Path.name expected_typename)

let print_variant_configuration_issue ppf
Expand Down Expand Up @@ -1774,29 +1774,94 @@ let report_subtyping_error ppf env tr1 txt1 tr2 ctx =
| Generic {error_code} -> fprintf ppf "Error: %s" error_code
| Coercion_target_variant_not_unboxed {variant_name; primitive} ->
fprintf ppf
"@ The variant @{<info>%s@} is not unboxed, so it cannot be \
coerced to a @{<info>%s@}. @ Fix this by adding the \
@{<info>@unboxed@} attribute to the variant @{<info>%s@}."
"@ The variant @{<info>%s@} has a catch-all case for @{<info>%s@}, \
but is not unboxed."
(Path.name variant_name) (Path.name primitive)
| Coercion_target_variant_does_not_cover_type
{variant_name; primitive; unboxed} ->
let primitive = Path.name primitive in
fprintf ppf
"@ The variant @{<info>%s@} %shas no catch-all case for \
@{<info>%s@}, like @{<info>%s(%s)@}."
(Path.name variant_name)
| Coercion_target_variant_does_not_cover_type {variant_name; primitive}
->
(if unboxed then "" else "is not unboxed and ")
primitive
(String.capitalize_ascii primitive)
primitive
| Coercion_with_bool_or_unit {typename} ->
fprintf ppf
"@ The variant @{<info>%s@} is unboxed, but has no catch-all case \
for the primitive @{<info>%s@}, and therefore does not cover all \
values of type @{<info>%s@}. @ Fix this by adding a catch-all for \
@{<info>%s@} to @{<info>%s@}, like @{<info>%s(%s)@}."
(Path.name variant_name) (Path.name primitive) (Path.name primitive)
(Path.name variant_name) (Path.name primitive)
(String.capitalize_ascii (Path.name primitive))
(Path.name primitive)
"@ @{<info>%s@} cannot be coerced to or from another type."
(Path.name typename)
| Coercion_from_unknown ->
fprintf ppf
"@ A value of type @{<info>unknown@} cannot be coerced to another \
type. Check its type at runtime first, for example with \
@{<info>Type.Classify.classify@}."
| Coercion_to_unknown ->
fprintf ppf "@ No other type can be coerced to @{<info>unknown@}."
| Variant_constructor_runtime_representation_mismatch
{variant_name; issues} ->
List.iter
(fun issue ->
fprintf ppf "@ ";
print_variant_runtime_representation_issue ppf variant_name issue)
issues;
(* Said once, however many constructors have the wrong literal *)
List.find_map
(function
| Variant_coercion.Mismatched_as_payload {expected_typename} ->
Some expected_typename
| _ -> None)
issues
|> Option.iter (fun expected_typename ->
fprintf ppf
"@,\
@ Fix this by making sure all constructors in variant \
@{<info>%s@} have a runtime representation of @{<info>%s@}."
(Path.name variant_name)
(Path.name expected_typename))
| Variant_constructor_tag_mismatch
{
left_variant_name;
right_variant_name;
constructor_name;
left_tag;
right_tag;
} ->
fprintf ppf
"@ The constructor @{<info>%s@} is @{<info>%s@} at runtime in \
@{<info>%s@}, but @{<info>%s@} in @{<info>%s@}."
constructor_name
(Variant_runtime.literal_to_string left_tag)
(Path.name left_variant_name)
(Variant_runtime.literal_to_string right_tag)
(Path.name right_variant_name)
| Variant_constructor_payload_mismatch
{left_variant_name; right_variant_name; constructor_name; payloads}
-> (
match payloads with
| Some (tl1, tl2) ->
let constructor ppf tl =
fprintf ppf "%s(%a)" constructor_name
(Format.pp_print_list
~pp_sep:(fun ppf () -> fprintf ppf ", ")
type_expr)
tl
in
fprintf ppf
"@ The constructor @{<info>%a@} of @{<info>%s@} cannot be \
coerced to @{<info>%a@} of @{<info>%s@}."
constructor tl1
(Path.name left_variant_name)
constructor tl2
(Path.name right_variant_name)
| None ->
fprintf ppf
"@ The payload of the constructor @{<info>%s@} in @{<info>%s@} \
cannot be coerced to its payload in @{<info>%s@}."
constructor_name
(Path.name left_variant_name)
(Path.name right_variant_name))
| Variant_configurations_mismatch
{left_variant_name; right_variant_name; issue} ->
print_variant_configuration_issue ppf issue ~left_variant_name
Expand All @@ -1809,12 +1874,15 @@ let report_subtyping_error ppf env tr1 txt1 tr2 ctx =
| Type_variant _ -> "a variant"
| Type_open -> "an open type"
in
(* Named like types are printed, e.g. [Date.t] for [Stdlib_Date.t] *)
let left_typename = string_of_path left_typename in
let right_typename = string_of_path right_typename in
fprintf ppf
"@ The types of @{<info>%s@} and @{<info>%s@} are different:"
(Path.name left_typename) (Path.name right_typename);
fprintf ppf "@ - @{<info>%s@} is %s" (Path.name left_typename)
left_typename right_typename;
fprintf ppf "@ - @{<info>%s@} is %s" left_typename
(type_kind_to_string left_type_kind);
fprintf ppf "@ - @{<info>%s@} is %s" (Path.name right_typename)
fprintf ppf "@ - @{<info>%s@} is %s" right_typename
(type_kind_to_string right_type_kind)
| Record_fields_mismatch {left_record_name; right_record_name; issues}
->
Expand Down
27 changes: 10 additions & 17 deletions compiler/typing/variant_coercion.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ type variant_runtime_representation_issue =
| Mismatched_unboxed_payload of {
constructor_name: string;
expected_typename: Path.t;
payload: Types.type_expr;
}
| Mismatched_as_payload of {
constructor_name: string;
Expand Down Expand Up @@ -32,9 +33,6 @@ let can_coerce_primitive (path : Path.t) =
|| Path.same path Predef.path_float
|| Path.same path Predef.path_bigint

let check_paths_same p1 p2 target_path =
Path.same p1 target_path && Path.same p2 target_path

let variant_has_case_covering_type
(constructors : Types.constructor_declaration list) ~path_is_same_fn =
let has_catch_all_string_case (c : Types.constructor_declaration) =
Expand All @@ -56,25 +54,20 @@ let variant_has_same_runtime_representation_as_target ~(target_path : Path.t)
let as_payload = c.cd_runtime_tag in

match args with
| Cstr_tuple [{desc = Tconstr (p, [], _)}] when unboxed ->
(* Unboxed type, and the constructor has a single item payload.*)
let path_same = check_paths_same p target_path in
if
(* unboxed String(string) :> string *)
path_same Predef.path_string
(* unboxed Number(float) :> float *)
|| path_same Predef.path_float
||
(* unboxed BigInt(bigint) :> bigint *)
path_same Predef.path_bigint
then None
else
| Cstr_tuple [payload] when unboxed -> (
(* An unboxed constructor is its payload at runtime, like
[String(string) :> string]. The target is one of the primitives of
[can_coerce_primitive]. *)
match payload.desc with
| Tconstr (p, [], _) when Path.same p target_path -> None
| _ ->
Some
(Mismatched_unboxed_payload
{
constructor_name = Ident.name c.cd_id;
expected_typename = target_path;
})
payload;
}))
| Cstr_tuple [] -> (
(* Check that @as payloads match with the target path to coerce to.
No @as means the default encoding, which is string *)
Expand Down
9 changes: 9 additions & 0 deletions compiler/typing/variant_runtime.ml
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,15 @@ let literal_value_key = function

let same_literal_value a b = literal_value_key a = literal_value_key b

let literal_to_string = function
| String s -> "\"" ^ s ^ "\""
| Int i -> string_of_int i
| Float f -> f
| BigInt b -> b ^ "n"
| Bool b -> string_of_bool b
| Null -> "null"
| Undefined -> "undefined"

(** Information used to recognize a constructor during matching. A literal
identifies a constant or an object's tag; a payload shape identifies a
value represented directly by its payload. *)
Expand Down
3 changes: 3 additions & 0 deletions compiler/typing/variant_runtime.mli
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ val literal_value_key : literal_tag -> string

val same_literal_value : literal_tag -> literal_tag -> bool

val literal_to_string : literal_tag -> string
(** The literal as ReScript source writes it, like ["x"], [1] or [1n]. *)

(** Information used to recognize a constructor during matching. A literal
identifies a constant or an object's tag; a payload shape identifies a
value represented directly by its payload. *)
Expand Down
6 changes: 5 additions & 1 deletion tests/compiler_tests/src/VariantCoercion.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ let CoerceWithPayload = {
c: 100,
cc: 2,
d: 100,
dd: 2
dd: 2,
e: 100,
ee: 2,
f: 100,
ff: 2
};

let a$1 = "hello";
Expand Down
6 changes: 6 additions & 0 deletions tests/compiler_tests/src/VariantCoercion.res
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ module CoerceWithPayload = {
let cc: floats = Second
let d: float = (c :> float)
let dd: float = (cc :> float)

@unboxed type ints = Int(int) | @as(1) First | @as(2) Second | @as(3) Third
let e: ints = Int(100)
let ee: ints = Second
let f: int = (e :> int)
let ff: int = (ee :> int)
}

module CoerceFromStringToVariant = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@
1 │ let u: unit = ("a" :> unit)
2 │

Type string is not a subtype of unit
Type string is not a subtype of unit

unit cannot be coerced to or from another type.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

We've found a bug for you!
/.../fixtures/coercion_string_to_unknown.res:1:25-36

1 │ let f = (s: string) => (s :> unknown)
2 │

Type string is not a subtype of unknown

No other type can be coerced to unknown.
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@
1 │ let i: int = (() :> int)
2 │

Type unit is not a subtype of int
Type unit is not a subtype of int

unit cannot be coerced to or from another type.
Loading
Loading