diff --git a/changes/coercion-error-explanations.fix.md b/changes/coercion-error-explanations.fix.md new file mode 100644 index 0000000000..1cfe1984fd --- /dev/null +++ b/changes/coercion-error-explanations.fix.md @@ -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`. diff --git a/compiler/typing/ctype.ml b/compiler/typing/ctype.ml index 7b0834be7e..91069c0a9c 100644 --- a/compiler/typing/ctype.ml +++ b/compiler/typing/ctype.ml @@ -65,11 +65,28 @@ type subtype_context = | Coercion_target_variant_does_not_cover_type of { variant_name: Path.t; primitive: Path.t; + unboxed: bool; } + | Coercion_with_bool_or_unit of {typename: Path.t} + | Coercion_from_unknown + | Coercion_to_unknown | Variant_constructor_runtime_representation_mismatch of { variant_name: Path.t; issues: Variant_coercion.variant_runtime_representation_issue list; } + | 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; + } + | 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; + } | Variant_configurations_mismatch of { left_variant_name: Path.t; right_variant_name: Path.t; @@ -3646,6 +3663,44 @@ let coerces_into_private_type env t1 t2 = | None -> true) | Some _ | None -> false +(* The explanation when [bool] or [unit] is coerced to or from another type, + or a variant and an abstract type are coerced to each other, like + [Date.t :> t] *) +let other_type_coercion_context env t1 t2 = + let kind t = + match extract_concrete_typedecl_opt env t with + | Some (_, p, _) when Variant_coercion.is_bool_or_unit p -> `Bool_or_unit p + | Some (p, _, {type_kind = Type_variant _ as kind}) -> `Variant (p, kind) + | Some _ -> `Other + | None -> ( + match (repr (expand_head env t)).desc with + | Tconstr (p, _, _) -> `Abstract p + | _ -> `Other) + in + match (kind t1, kind t2) with + | `Bool_or_unit typename, _ | _, `Bool_or_unit typename -> + Some (Coercion_with_bool_or_unit {typename}) + | `Abstract left_typename, `Variant (right_typename, right_type_kind) -> + Some + (Different_type_kinds + { + left_typename; + right_typename; + left_type_kind = Type_abstract; + right_type_kind; + }) + | `Variant (left_typename, left_type_kind), `Abstract right_typename -> + Some + (Different_type_kinds + { + left_typename; + right_typename; + left_type_kind; + right_type_kind = Type_abstract; + }) + | (`Abstract _ | `Variant _ | `Other), (`Abstract _ | `Variant _ | `Other) -> + None + (* 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 = @@ -3792,6 +3847,13 @@ let rec subtype_rec env trace t1 t2 cstrs = (trace, t1, t2, !(current_univar_pairs ()), None) :: cstrs) | Tconstr (p1, _, _), _ when generic_private_abbrev env p1 -> subtype_rec env trace (expand_abbrev_opt env t1) t2 cstrs + | Tconstr (p, [], _), _ when Path.same p Predef.path_unknown -> + (* Only [unknown] itself coerces to and from [unknown] *) + (trace, t1, t2, !(current_univar_pairs ()), Some Coercion_from_unknown) + :: cstrs + | _, Tconstr (p, [], _) when Path.same p Predef.path_unknown -> + (trace, t1, t2, !(current_univar_pairs ()), Some Coercion_to_unknown) + :: cstrs | _, Tconstr _ when coerces_into_private_type env t1 t2 -> (* Only the module defining a private record or variant builds its values; a coercion from another type would build one anywhere *) @@ -3822,30 +3884,33 @@ let rec subtype_rec env trace t1 t2 cstrs = Variant_coercion.can_try_coerce_variant_to_primitive_opt (extract_concrete_typedecl_opt env t2) with - | Some (p, _, false) -> - (* Not @unboxed *) - ( trace, - t1, - t2, - !(current_univar_pairs ()), - Some - (Coercion_target_variant_not_unboxed - {variant_name = p; primitive = path}) ) - :: cstrs - | Some (p, constructors, true) -> - if + | Some (p, constructors, unboxed) -> ( + (* A catch-all case like [String(string)] holds the primitive, and + only an unboxed variant represents it as the primitive itself *) + let covered = Variant_coercion.variant_has_case_covering_type constructors ~path_is_same_fn:(fun p -> Path.same p path) - then cstrs - else + in + match (covered, unboxed) with + | true, true -> cstrs + | true, false -> ( trace, t1, t2, !(current_univar_pairs ()), Some - (Coercion_target_variant_does_not_cover_type + (Coercion_target_variant_not_unboxed {variant_name = p; primitive = path}) ) :: cstrs + | false, _ -> + ( trace, + t1, + t2, + !(current_univar_pairs ()), + Some + (Coercion_target_variant_does_not_cover_type + {variant_name = p; primitive = path; unboxed}) ) + :: cstrs) | None -> (* Unclear when this case actually happens. *) ( trace, @@ -3893,7 +3958,17 @@ let rec subtype_rec env trace t1 t2 cstrs = || Variant_coercion.is_bool_or_unit p2 -> (* No other type coerces to or from [bool] or [unit], and the variant errors below would describe them as variants *) - (trace, t1, t2, !(current_univar_pairs ()), None) :: cstrs + ( trace, + t1, + t2, + !(current_univar_pairs ()), + Some + (Coercion_with_bool_or_unit + { + typename = + (if Variant_coercion.is_bool_or_unit p1 then p1 else p2); + }) ) + :: cstrs | ( ( p1, _, { @@ -3930,19 +4005,14 @@ let rec subtype_rec env trace t1 t2 cstrs = {left_variant_name = p1; right_variant_name = p2; issue}) ) :: cstrs | Ok () -> - let c1_len = List.length c1 in - if c1_len > List.length c2 then - let c1_constructor_names = - c1 |> List.map (fun c -> c.cd_id.name) - in - let c2_constructor_names = - c2 |> List.map (fun c -> c.cd_id.name) - in - let incompatible_constructor_names = - c1_constructor_names - |> List.filter (fun name -> - not (List.mem name c2_constructor_names)) - in + let c2_constructor_names = c2 |> List.map (fun c -> c.cd_id.name) in + let incompatible_constructor_names = + c1 + |> List.filter_map (fun c -> + if List.mem c.cd_id.name c2_constructor_names then None + else Some c.cd_id.name) + in + if incompatible_constructor_names <> [] then ( trace, t1, t2, @@ -3958,7 +4028,7 @@ let rec subtype_rec env trace t1 t2 cstrs = }) ) :: cstrs else - let constructor_map = Hashtbl.create c1_len in + let constructor_map = Hashtbl.create (List.length c2) in c2 |> List.iteri (fun position (c : Types.constructor_declaration) -> Hashtbl.add constructor_map (Ident.name c.cd_id) (c, position)); @@ -3966,71 +4036,88 @@ let rec subtype_rec env trace t1 t2 cstrs = c1 |> List.mapi (fun position1 (c : Types.constructor_declaration) -> + let constructor_name = Ident.name c.cd_id in + let payload_mismatch payloads = + Fails_with + (Variant_constructor_payload_mismatch + { + left_variant_name = p1; + right_variant_name = p2; + constructor_name; + payloads; + }) + in + (* The tags agree, or the error names both *) + let check_tags position2 coerce_payloads = + let left_tag = + Variant_runtime.constructor_tag layout1 position1 + in + let right_tag = + Variant_runtime.constructor_tag layout2 position2 + in + if + Variant_coercion.variant_representation_matches + left_tag right_tag + then coerce_payloads () + else + Fails_with + (Variant_constructor_tag_mismatch + { + left_variant_name = p1; + right_variant_name = p2; + constructor_name; + left_tag; + right_tag; + }) + in + (* Whether payload types [tl1] coerce to [tl2] *) + let payloads_coerce tl1 tl2 = + match subtype_list env trace tl1 tl2 cstrs with + | lst -> List.length lst = List.length cstrs + | exception _ -> false + in match - ( c, - Hashtbl.find_opt constructor_map (Ident.name c.cd_id) - ) + (c, Hashtbl.find_opt constructor_map constructor_name) with | ( {Types.cd_args = Cstr_record fields1}, Some ({Types.cd_args = Cstr_record fields2}, position2) ) -> - if - Variant_coercion.variant_representation_matches - (Variant_runtime.constructor_tag layout1 position1) - (Variant_runtime.constructor_tag layout2 position2) - then - let violations, tl1, tl2 = - Record_coercion.check_record_fields fields1 fields2 - in - match violations with - | [] -> ( - try - let lst = subtype_list env trace tl1 tl2 cstrs in - if List.length lst = List.length cstrs then - Coerces - else Fails_unexplained - (* TODO(subtype-errors) Variant constructor inline record field type mismatch *) - with _ -> - Fails_unexplained - (* TODO(subtype-errors) Variant constructor inline record field type mismatch *) - ) - | 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) + check_tags position2 (fun () -> + let violations, tl1, tl2 = + Record_coercion.check_record_fields fields1 + fields2 in - Fails_with - (Record_fields_mismatch - { - left_record_name = inline_record_name p1; - right_record_name = inline_record_name p2; - issues = violations; - }) - else - Fails_unexplained - (* TODO(subtype-errors) Variant constructor representation mismatch*) + match violations with + | [] -> + if payloads_coerce tl1 tl2 then Coerces + else payload_mismatch None + | violations -> + (* The inline record types are named like the + compiler prints them, e.g. [v1.A]. *) + let inline_record_name p = + Path.Pdot (p, constructor_name, Path.nopos) + in + Fails_with + (Record_fields_mismatch + { + left_record_name = inline_record_name p1; + right_record_name = inline_record_name p2; + issues = violations; + })) | ( {Types.cd_args = Cstr_tuple tl1}, Some ({Types.cd_args = Cstr_tuple tl2}, position2) ) -> - if - Variant_coercion.variant_representation_matches - (Variant_runtime.constructor_tag layout1 position1) - (Variant_runtime.constructor_tag layout2 position2) - then - try - let lst = subtype_list env trace tl1 tl2 cstrs in - 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 - Fails_unexplained - (* TODO(subtype-errors) Variant constructor tuple mismatch *) - | _ -> Fails_unexplained - (* TODO(subtype-errors) Variant other issue *)) + check_tags position2 (fun () -> + if payloads_coerce tl1 tl2 then Coerces + else payload_mismatch (Some (tl1, tl2))) + | _, Some _ -> + (* An inline record on one side, a tuple on the + other *) + payload_mismatch None + | _, None -> + (* Reported above as a missing constructor *) + Fails_unexplained) in if List.for_all @@ -4112,7 +4199,12 @@ let rec subtype_rec env trace t1 t2 cstrs = }) ) :: cstrs | exception Not_found -> - (trace, t1, t2, !(current_univar_pairs ()), None) :: cstrs) + ( trace, + t1, + t2, + !(current_univar_pairs ()), + other_type_coercion_context env t1 t2 ) + :: cstrs) (* | (_, Tconstr(p2, _, _)) when generic_private_abbrev false env p2 -> subtype_rec env trace t1 (expand_abbrev_opt env t2) cstrs *) | Tobject f1, Tobject f2 diff --git a/compiler/typing/ctype.mli b/compiler/typing/ctype.mli index d60e3a42eb..d27916e00a 100644 --- a/compiler/typing/ctype.mli +++ b/compiler/typing/ctype.mli @@ -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; diff --git a/compiler/typing/parmatch.ml b/compiler/typing/parmatch.ml index 45aec5876b..82897c5509 100644 --- a/compiler/typing/parmatch.ml +++ b/compiler/typing/parmatch.ml @@ -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 " (********************************) diff --git a/compiler/typing/predef.ml b/compiler/typing/predef.ml index cc7ea27ce3..34e4044e8c 100644 --- a/compiler/typing/predef.ml +++ b/compiler/typing/predef.ml @@ -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 @@ -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 = @@ -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 () = diff --git a/compiler/typing/predef.mli b/compiler/typing/predef.mli index 31c3d3aba4..53b956b8fc 100644 --- a/compiler/typing/predef.mli +++ b/compiler/typing/predef.mli @@ -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 diff --git a/compiler/typing/printtyp.ml b/compiler/typing/printtyp.ml index 418724ac87..9959ccf45b 100644 --- a/compiler/typing/printtyp.ml +++ b/compiler/typing/printtyp.ml @@ -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 @{%s@} of unboxed variant @{%s@} has a \ + payload of type @{%a@}, which is not compatible with \ + @{%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 @{%s@} of variant @{%s@} has " constructor_name (Path.name variant_name); @@ -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 @{%s@}." - (Path.name expected_typename); - fprintf ppf - "@,\ - @ Fix this by making sure all constructors in variant @{%s@} has \ - a runtime representation of @{%s@}." - (Path.name variant_name) (Path.name expected_typename) let print_variant_configuration_issue ppf @@ -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 @{%s@} is not unboxed, so it cannot be \ - coerced to a @{%s@}. @ Fix this by adding the \ - @{@unboxed@} attribute to the variant @{%s@}." + "@ The variant @{%s@} has a catch-all case for @{%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 @{%s@} %shas no catch-all case for \ + @{%s@}, like @{%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 @{%s@} is unboxed, but has no catch-all case \ - for the primitive @{%s@}, and therefore does not cover all \ - values of type @{%s@}. @ Fix this by adding a catch-all for \ - @{%s@} to @{%s@}, like @{%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) + "@ @{%s@} cannot be coerced to or from another type." + (Path.name typename) + | Coercion_from_unknown -> + fprintf ppf + "@ A value of type @{unknown@} cannot be coerced to another \ + type. Check its type at runtime first, for example with \ + @{Type.Classify.classify@}." + | Coercion_to_unknown -> + fprintf ppf "@ No other type can be coerced to @{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 \ + @{%s@} have a runtime representation of @{%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 @{%s@} is @{%s@} at runtime in \ + @{%s@}, but @{%s@} in @{%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 @{%a@} of @{%s@} cannot be \ + coerced to @{%a@} of @{%s@}." + constructor tl1 + (Path.name left_variant_name) + constructor tl2 + (Path.name right_variant_name) + | None -> + fprintf ppf + "@ The payload of the constructor @{%s@} in @{%s@} \ + cannot be coerced to its payload in @{%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 @@ -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 @{%s@} and @{%s@} are different:" - (Path.name left_typename) (Path.name right_typename); - fprintf ppf "@ - @{%s@} is %s" (Path.name left_typename) + left_typename right_typename; + fprintf ppf "@ - @{%s@} is %s" left_typename (type_kind_to_string left_type_kind); - fprintf ppf "@ - @{%s@} is %s" (Path.name right_typename) + fprintf ppf "@ - @{%s@} is %s" right_typename (type_kind_to_string right_type_kind) | Record_fields_mismatch {left_record_name; right_record_name; issues} -> diff --git a/compiler/typing/variant_coercion.ml b/compiler/typing/variant_coercion.ml index 314fa791dc..befbe507ce 100644 --- a/compiler/typing/variant_coercion.ml +++ b/compiler/typing/variant_coercion.ml @@ -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; @@ -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) = @@ -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 *) diff --git a/compiler/typing/variant_runtime.ml b/compiler/typing/variant_runtime.ml index 3cd877e9e0..a9fadd9dc8 100644 --- a/compiler/typing/variant_runtime.ml +++ b/compiler/typing/variant_runtime.ml @@ -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. *) diff --git a/compiler/typing/variant_runtime.mli b/compiler/typing/variant_runtime.mli index 7c2e656f39..d1b08734b9 100644 --- a/compiler/typing/variant_runtime.mli +++ b/compiler/typing/variant_runtime.mli @@ -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. *) diff --git a/tests/compiler_tests/src/VariantCoercion.mjs b/tests/compiler_tests/src/VariantCoercion.mjs index 5d298c9451..1679ccf21e 100644 --- a/tests/compiler_tests/src/VariantCoercion.mjs +++ b/tests/compiler_tests/src/VariantCoercion.mjs @@ -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"; diff --git a/tests/compiler_tests/src/VariantCoercion.res b/tests/compiler_tests/src/VariantCoercion.res index c00ffad29a..674b2391c8 100644 --- a/tests/compiler_tests/src/VariantCoercion.res +++ b/tests/compiler_tests/src/VariantCoercion.res @@ -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 = { diff --git a/tests/error_tests/single/expected/coercion_string_to_unit.res.expected b/tests/error_tests/single/expected/coercion_string_to_unit.res.expected index e83f5c5624..53a29cadb4 100644 --- a/tests/error_tests/single/expected/coercion_string_to_unit.res.expected +++ b/tests/error_tests/single/expected/coercion_string_to_unit.res.expected @@ -5,4 +5,6 @@ 1 │ let u: unit = ("a" :> unit) 2 │ - Type string is not a subtype of unit \ No newline at end of file + Type string is not a subtype of unit + + unit cannot be coerced to or from another type. \ No newline at end of file diff --git a/tests/error_tests/single/expected/coercion_string_to_unknown.res.expected b/tests/error_tests/single/expected/coercion_string_to_unknown.res.expected new file mode 100644 index 0000000000..6dc4a7077f --- /dev/null +++ b/tests/error_tests/single/expected/coercion_string_to_unknown.res.expected @@ -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. \ No newline at end of file diff --git a/tests/error_tests/single/expected/coercion_unit_to_int.res.expected b/tests/error_tests/single/expected/coercion_unit_to_int.res.expected index 9be7b11fe0..5f88db0716 100644 --- a/tests/error_tests/single/expected/coercion_unit_to_int.res.expected +++ b/tests/error_tests/single/expected/coercion_unit_to_int.res.expected @@ -5,4 +5,6 @@ 1 │ let i: int = (() :> int) 2 │ - Type unit is not a subtype of int \ No newline at end of file + Type unit is not a subtype of int + + unit cannot be coerced to or from another type. \ No newline at end of file diff --git a/tests/error_tests/single/expected/coercion_unit_to_string.res.expected b/tests/error_tests/single/expected/coercion_unit_to_string.res.expected index f7ba693108..6ca33af93b 100644 --- a/tests/error_tests/single/expected/coercion_unit_to_string.res.expected +++ b/tests/error_tests/single/expected/coercion_unit_to_string.res.expected @@ -5,4 +5,6 @@ 1 │ let s: string = (() :> string) 2 │ - Type unit is not a subtype of string \ No newline at end of file + Type unit is not a subtype of string + + unit cannot be coerced to or from another type. \ No newline at end of file diff --git a/tests/error_tests/single/expected/coercion_unit_to_unknown.res.expected b/tests/error_tests/single/expected/coercion_unit_to_unknown.res.expected index f735acd0fd..cc4b2993b3 100644 --- a/tests/error_tests/single/expected/coercion_unit_to_unknown.res.expected +++ b/tests/error_tests/single/expected/coercion_unit_to_unknown.res.expected @@ -5,4 +5,6 @@ 1 │ let u: unknown = (() :> unknown) 2 │ - Type unit is not a subtype of unknown \ No newline at end of file + Type unit is not a subtype of unknown + + No other type can be coerced to unknown. \ No newline at end of file diff --git a/tests/error_tests/single/expected/coercion_unknown_to_string.res.expected b/tests/error_tests/single/expected/coercion_unknown_to_string.res.expected new file mode 100644 index 0000000000..b680334914 --- /dev/null +++ b/tests/error_tests/single/expected/coercion_unknown_to_string.res.expected @@ -0,0 +1,10 @@ + + We've found a bug for you! + /.../fixtures/coercion_unknown_to_string.res:1:26-36 + + 1 │ let f = (u: unknown) => (u :> string) + 2 │ + + Type unknown is not a subtype of string + + A value of type unknown cannot be coerced to another type. Check its type at runtime first, for example with Type.Classify.classify. \ No newline at end of file diff --git a/tests/error_tests/single/expected/variant_coercion_abstract_to_variant.res.expected b/tests/error_tests/single/expected/variant_coercion_abstract_to_variant.res.expected new file mode 100644 index 0000000000..0fcda1d21c --- /dev/null +++ b/tests/error_tests/single/expected/variant_coercion_abstract_to_variant.res.expected @@ -0,0 +1,13 @@ + + We've found a bug for you! + /.../fixtures/variant_coercion_abstract_to_variant.res:2:25-30 + + 1 │ @unboxed type t = String(string) | Int(int) + 2 │ let g = (d: Date.t) => (d :> t) + 3 │ + + Type Date.t is not a subtype of t + + The types of Date.t and t are different: + - Date.t is an abstract type + - t is a variant \ No newline at end of file diff --git a/tests/error_tests/single/expected/variant_coercion_bigint_as.res.expected b/tests/error_tests/single/expected/variant_coercion_bigint_as.res.expected index 43d6ab665a..a7f6fb03bd 100644 --- a/tests/error_tests/single/expected/variant_coercion_bigint_as.res.expected +++ b/tests/error_tests/single/expected/variant_coercion_bigint_as.res.expected @@ -12,4 +12,4 @@ The constructor Two of variant x has no @as payload, which makes it a string at runtime. That runtime representation is not compatible with the expected runtime representation of bigint. - Fix this by making sure all constructors in variant x has a runtime representation of bigint. \ No newline at end of file + Fix this by making sure all constructors in variant x have a runtime representation of bigint. \ No newline at end of file diff --git a/tests/error_tests/single/expected/variant_coercion_bool_to_bool_payload.res.expected b/tests/error_tests/single/expected/variant_coercion_bool_to_bool_payload.res.expected new file mode 100644 index 0000000000..78e59ad11d --- /dev/null +++ b/tests/error_tests/single/expected/variant_coercion_bool_to_bool_payload.res.expected @@ -0,0 +1,11 @@ + + We've found a bug for you! + /.../fixtures/variant_coercion_bool_to_bool_payload.res:2:23-28 + + 1 │ @unboxed type t = Bool(bool) | S(string) + 2 │ let f = (v: bool) => (v :> t) + 3 │ + + Type bool is not a subtype of t + + bool cannot be coerced to or from another type. \ No newline at end of file diff --git a/tests/error_tests/single/expected/variant_coercion_bool_to_variant.res.expected b/tests/error_tests/single/expected/variant_coercion_bool_to_variant.res.expected new file mode 100644 index 0000000000..f6dc325fe4 --- /dev/null +++ b/tests/error_tests/single/expected/variant_coercion_bool_to_variant.res.expected @@ -0,0 +1,11 @@ + + We've found a bug for you! + /.../fixtures/variant_coercion_bool_to_variant.res:2:23-28 + + 1 │ @unboxed type t = String(string) | Int(int) + 2 │ let g = (b: bool) => (b :> t) + 3 │ + + Type bool is not a subtype of t + + bool cannot be coerced to or from another type. \ No newline at end of file diff --git a/tests/error_tests/single/expected/variant_coercion_float_as.res.expected b/tests/error_tests/single/expected/variant_coercion_float_as.res.expected index c14bc3d41c..ac5b25b332 100644 --- a/tests/error_tests/single/expected/variant_coercion_float_as.res.expected +++ b/tests/error_tests/single/expected/variant_coercion_float_as.res.expected @@ -12,4 +12,4 @@ The constructor Two of variant x has no @as payload, which makes it a string at runtime. That runtime representation is not compatible with the expected runtime representation of float. - Fix this by making sure all constructors in variant x has a runtime representation of float. \ No newline at end of file + Fix this by making sure all constructors in variant x have a runtime representation of float. \ No newline at end of file diff --git a/tests/error_tests/single/expected/variant_coercion_float_without_catch_all.res.expected b/tests/error_tests/single/expected/variant_coercion_float_without_catch_all.res.expected new file mode 100644 index 0000000000..9130dd4bfc --- /dev/null +++ b/tests/error_tests/single/expected/variant_coercion_float_without_catch_all.res.expected @@ -0,0 +1,11 @@ + + We've found a bug for you! + /.../fixtures/variant_coercion_float_without_catch_all.res:2:24-29 + + 1 │ @unboxed type t = String(string) | Int(int) + 2 │ let f = (v: float) => (v :> t) + 3 │ + + Type float is not a subtype of t + + The variant t has no catch-all case for float, like Float(float). \ No newline at end of file diff --git a/tests/error_tests/single/expected/variant_coercion_int_as.res.expected b/tests/error_tests/single/expected/variant_coercion_int_as.res.expected index 7b68b06bd8..710aa8d404 100644 --- a/tests/error_tests/single/expected/variant_coercion_int_as.res.expected +++ b/tests/error_tests/single/expected/variant_coercion_int_as.res.expected @@ -12,4 +12,4 @@ The constructor Two of variant x has no @as payload, which makes it a string at runtime. That runtime representation is not compatible with the expected runtime representation of int. - Fix this by making sure all constructors in variant x has a runtime representation of int. \ No newline at end of file + Fix this by making sure all constructors in variant x have a runtime representation of int. \ No newline at end of file diff --git a/tests/error_tests/single/expected/variant_coercion_string_as.res.expected b/tests/error_tests/single/expected/variant_coercion_string_as.res.expected index 885d44a90b..19fc102dc4 100644 --- a/tests/error_tests/single/expected/variant_coercion_string_as.res.expected +++ b/tests/error_tests/single/expected/variant_coercion_string_as.res.expected @@ -12,4 +12,4 @@ The constructor Two of variant x has an @as payload that gives it the runtime type of int. That runtime representation is not compatible with the expected runtime representation of string. - Fix this by making sure all constructors in variant x has a runtime representation of string. \ No newline at end of file + Fix this by making sure all constructors in variant x have a runtime representation of string. \ No newline at end of file diff --git a/tests/error_tests/single/expected/variant_coercion_string_to_non_unboxed_variant.res.expected b/tests/error_tests/single/expected/variant_coercion_string_to_non_unboxed_variant.res.expected new file mode 100644 index 0000000000..7627defe89 --- /dev/null +++ b/tests/error_tests/single/expected/variant_coercion_string_to_non_unboxed_variant.res.expected @@ -0,0 +1,11 @@ + + We've found a bug for you! + /.../fixtures/variant_coercion_string_to_non_unboxed_variant.res:2:25-30 + + 1 │ type a = One | S(string) + 2 │ let f = (s: string) => (s :> a) + 3 │ + + Type string is not a subtype of a + + The variant a has a catch-all case for string, but is not unboxed. \ No newline at end of file diff --git a/tests/error_tests/single/expected/variant_coercion_string_to_variant_no_payload.res.expected b/tests/error_tests/single/expected/variant_coercion_string_to_variant_no_payload.res.expected index ad4f073b3a..57df9ef73e 100644 --- a/tests/error_tests/single/expected/variant_coercion_string_to_variant_no_payload.res.expected +++ b/tests/error_tests/single/expected/variant_coercion_string_to_variant_no_payload.res.expected @@ -9,5 +9,4 @@ Type string is not a subtype of x - The variant x is unboxed, but has no catch-all case for the primitive string, and therefore does not cover all values of type string. - Fix this by adding a catch-all for x to string, like String(string). \ No newline at end of file + The variant x has no catch-all case for string, like String(string). \ No newline at end of file diff --git a/tests/error_tests/single/expected/variant_coercion_string_to_variant_without_catch_all.res.expected b/tests/error_tests/single/expected/variant_coercion_string_to_variant_without_catch_all.res.expected new file mode 100644 index 0000000000..2ece8d4966 --- /dev/null +++ b/tests/error_tests/single/expected/variant_coercion_string_to_variant_without_catch_all.res.expected @@ -0,0 +1,11 @@ + + We've found a bug for you! + /.../fixtures/variant_coercion_string_to_variant_without_catch_all.res:2:25-30 + + 1 │ type a = One | Two + 2 │ let f = (s: string) => (s :> a) + 3 │ + + Type string is not a subtype of a + + The variant a is not unboxed and has no catch-all case for string, like String(string). \ No newline at end of file diff --git a/tests/error_tests/single/expected/variant_coercion_string_unboxed.res.expected b/tests/error_tests/single/expected/variant_coercion_string_unboxed.res.expected index a152598031..cd8c065c7d 100644 --- a/tests/error_tests/single/expected/variant_coercion_string_unboxed.res.expected +++ b/tests/error_tests/single/expected/variant_coercion_string_unboxed.res.expected @@ -7,4 +7,6 @@ 6 │ let y = (x :> string) 7 │ - Type x is not a subtype of string \ No newline at end of file + Type x is not a subtype of string + + The constructor Other of unboxed variant x has a payload of type float, which is not compatible with string. \ No newline at end of file diff --git a/tests/error_tests/single/expected/variant_coercion_unboxed_array_payload.res.expected b/tests/error_tests/single/expected/variant_coercion_unboxed_array_payload.res.expected new file mode 100644 index 0000000000..250a1f245d --- /dev/null +++ b/tests/error_tests/single/expected/variant_coercion_unboxed_array_payload.res.expected @@ -0,0 +1,11 @@ + + We've found a bug for you! + /.../fixtures/variant_coercion_unboxed_array_payload.res:2:20-30 + + 1 │ @unboxed type t = Strings(array) | @as("none") None + 2 │ let g = (v: t) => (v :> string) + 3 │ + + Type t is not a subtype of string + + The constructor Strings of unboxed variant t has a payload of type array, which is not compatible with string. \ No newline at end of file diff --git a/tests/error_tests/single/expected/variant_coercion_variant_to_bool.res.expected b/tests/error_tests/single/expected/variant_coercion_variant_to_bool.res.expected new file mode 100644 index 0000000000..551597cc88 --- /dev/null +++ b/tests/error_tests/single/expected/variant_coercion_variant_to_bool.res.expected @@ -0,0 +1,11 @@ + + We've found a bug for you! + /.../fixtures/variant_coercion_variant_to_bool.res:2:20-28 + + 1 │ @unboxed type t = String(string) | Int(int) + 2 │ let g = (v: t) => (v :> bool) + 3 │ + + Type t is not a subtype of bool + + bool cannot be coerced to or from another type. \ No newline at end of file diff --git a/tests/error_tests/single/expected/variant_pattern_type_spreads_not_subtype.res.expected b/tests/error_tests/single/expected/variant_pattern_type_spreads_not_subtype.res.expected index 48c8eaf690..3ae4d22a2d 100644 --- a/tests/error_tests/single/expected/variant_pattern_type_spreads_not_subtype.res.expected +++ b/tests/error_tests/single/expected/variant_pattern_type_spreads_not_subtype.res.expected @@ -8,4 +8,7 @@ 8 │ | Four => Console.log("four") 9 │ | Five => Console.log("five") - Type c is not a subtype of b \ No newline at end of file + Type c is not a subtype of b + + c has 2 constructors that b does not have: Six, Seven + Therefore, it is not possible for b to represent c. \ No newline at end of file diff --git a/tests/error_tests/single/expected/variant_to_variant_coercion.res.expected b/tests/error_tests/single/expected/variant_to_variant_coercion.res.expected index 75a0fc30b6..adcc5ec397 100644 --- a/tests/error_tests/single/expected/variant_to_variant_coercion.res.expected +++ b/tests/error_tests/single/expected/variant_to_variant_coercion.res.expected @@ -7,4 +7,6 @@ 6 │ let y = (x :> y) 7 │ - Type x is not a subtype of y \ No newline at end of file + Type x is not a subtype of y + + The constructor One(bool) of x cannot be coerced to One(string) of y. \ No newline at end of file diff --git a/tests/error_tests/single/expected/variant_to_variant_coercion_as.res.expected b/tests/error_tests/single/expected/variant_to_variant_coercion_as.res.expected index 04a3f55798..848edaae7b 100644 --- a/tests/error_tests/single/expected/variant_to_variant_coercion_as.res.expected +++ b/tests/error_tests/single/expected/variant_to_variant_coercion_as.res.expected @@ -7,4 +7,6 @@ 6 │ let y = (x :> y) 7 │ - Type x is not a subtype of y \ No newline at end of file + Type x is not a subtype of y + + The constructor One is "one" at runtime in x, but "One" in y. \ No newline at end of file diff --git a/tests/error_tests/single/expected/variant_to_variant_coercion_constructor_as.res.expected b/tests/error_tests/single/expected/variant_to_variant_coercion_constructor_as.res.expected new file mode 100644 index 0000000000..be33914acc --- /dev/null +++ b/tests/error_tests/single/expected/variant_to_variant_coercion_constructor_as.res.expected @@ -0,0 +1,12 @@ + + We've found a bug for you! + /.../fixtures/variant_to_variant_coercion_constructor_as.res:3:20-25 + + 1 │ type t = A | @as("x") B + 2 │ type u = B + 3 │ let f = (v: u) => (v :> t) + 4 │ + + Type u is not a subtype of t + + The constructor B is "B" at runtime in u, but "x" in t. \ No newline at end of file diff --git a/tests/error_tests/single/expected/variant_to_variant_coercion_missing_constructor.res.expected b/tests/error_tests/single/expected/variant_to_variant_coercion_missing_constructor.res.expected new file mode 100644 index 0000000000..7c20854b41 --- /dev/null +++ b/tests/error_tests/single/expected/variant_to_variant_coercion_missing_constructor.res.expected @@ -0,0 +1,13 @@ + + We've found a bug for you! + /.../fixtures/variant_to_variant_coercion_missing_constructor.res:3:20-25 + + 1 │ type t = A | B + 2 │ type u = B | C + 3 │ let f = (v: u) => (v :> t) + 4 │ + + Type u is not a subtype of t + + u has 1 constructor that t does not have: C + Therefore, it is not possible for t to represent u. \ No newline at end of file diff --git a/tests/error_tests/single/expected/variant_to_variant_coercion_payload.res.expected b/tests/error_tests/single/expected/variant_to_variant_coercion_payload.res.expected new file mode 100644 index 0000000000..2e2ae5a639 --- /dev/null +++ b/tests/error_tests/single/expected/variant_to_variant_coercion_payload.res.expected @@ -0,0 +1,12 @@ + + We've found a bug for you! + /.../fixtures/variant_to_variant_coercion_payload.res:3:20-25 + + 1 │ type t = A | B(int) + 2 │ type u = B(string) + 3 │ let f = (v: u) => (v :> t) + 4 │ + + Type u is not a subtype of t + + The constructor B(string) of u cannot be coerced to B(int) of t. \ No newline at end of file diff --git a/tests/error_tests/single/expected/variant_to_variant_coercion_payload_kind.res.expected b/tests/error_tests/single/expected/variant_to_variant_coercion_payload_kind.res.expected new file mode 100644 index 0000000000..bbe01a2959 --- /dev/null +++ b/tests/error_tests/single/expected/variant_to_variant_coercion_payload_kind.res.expected @@ -0,0 +1,12 @@ + + We've found a bug for you! + /.../fixtures/variant_to_variant_coercion_payload_kind.res:3:20-25 + + 1 │ type t = A | B({x: int}) + 2 │ type u = B(int) + 3 │ let f = (v: u) => (v :> t) + 4 │ + + Type u is not a subtype of t + + The payload of the constructor B in u cannot be coerced to its payload in t. \ No newline at end of file diff --git a/tests/error_tests/single/fixtures/coercion_string_to_unknown.res b/tests/error_tests/single/fixtures/coercion_string_to_unknown.res new file mode 100644 index 0000000000..aa5398de30 --- /dev/null +++ b/tests/error_tests/single/fixtures/coercion_string_to_unknown.res @@ -0,0 +1 @@ +let f = (s: string) => (s :> unknown) diff --git a/tests/error_tests/single/fixtures/coercion_unknown_to_string.res b/tests/error_tests/single/fixtures/coercion_unknown_to_string.res new file mode 100644 index 0000000000..4e2f65e4eb --- /dev/null +++ b/tests/error_tests/single/fixtures/coercion_unknown_to_string.res @@ -0,0 +1 @@ +let f = (u: unknown) => (u :> string) diff --git a/tests/error_tests/single/fixtures/variant_coercion_abstract_to_variant.res b/tests/error_tests/single/fixtures/variant_coercion_abstract_to_variant.res new file mode 100644 index 0000000000..1aab3e5434 --- /dev/null +++ b/tests/error_tests/single/fixtures/variant_coercion_abstract_to_variant.res @@ -0,0 +1,2 @@ +@unboxed type t = String(string) | Int(int) +let g = (d: Date.t) => (d :> t) diff --git a/tests/error_tests/single/fixtures/variant_coercion_bool_to_bool_payload.res b/tests/error_tests/single/fixtures/variant_coercion_bool_to_bool_payload.res new file mode 100644 index 0000000000..fad2a613f0 --- /dev/null +++ b/tests/error_tests/single/fixtures/variant_coercion_bool_to_bool_payload.res @@ -0,0 +1,2 @@ +@unboxed type t = Bool(bool) | S(string) +let f = (v: bool) => (v :> t) diff --git a/tests/error_tests/single/fixtures/variant_coercion_bool_to_variant.res b/tests/error_tests/single/fixtures/variant_coercion_bool_to_variant.res new file mode 100644 index 0000000000..b20e4eaf80 --- /dev/null +++ b/tests/error_tests/single/fixtures/variant_coercion_bool_to_variant.res @@ -0,0 +1,2 @@ +@unboxed type t = String(string) | Int(int) +let g = (b: bool) => (b :> t) diff --git a/tests/error_tests/single/fixtures/variant_coercion_float_without_catch_all.res b/tests/error_tests/single/fixtures/variant_coercion_float_without_catch_all.res new file mode 100644 index 0000000000..3ee2d571a0 --- /dev/null +++ b/tests/error_tests/single/fixtures/variant_coercion_float_without_catch_all.res @@ -0,0 +1,2 @@ +@unboxed type t = String(string) | Int(int) +let f = (v: float) => (v :> t) diff --git a/tests/error_tests/single/fixtures/variant_coercion_string_to_non_unboxed_variant.res b/tests/error_tests/single/fixtures/variant_coercion_string_to_non_unboxed_variant.res new file mode 100644 index 0000000000..197d85b924 --- /dev/null +++ b/tests/error_tests/single/fixtures/variant_coercion_string_to_non_unboxed_variant.res @@ -0,0 +1,2 @@ +type a = One | S(string) +let f = (s: string) => (s :> a) diff --git a/tests/error_tests/single/fixtures/variant_coercion_string_to_variant_without_catch_all.res b/tests/error_tests/single/fixtures/variant_coercion_string_to_variant_without_catch_all.res new file mode 100644 index 0000000000..518d4a4485 --- /dev/null +++ b/tests/error_tests/single/fixtures/variant_coercion_string_to_variant_without_catch_all.res @@ -0,0 +1,2 @@ +type a = One | Two +let f = (s: string) => (s :> a) diff --git a/tests/error_tests/single/fixtures/variant_coercion_unboxed_array_payload.res b/tests/error_tests/single/fixtures/variant_coercion_unboxed_array_payload.res new file mode 100644 index 0000000000..84adf296fb --- /dev/null +++ b/tests/error_tests/single/fixtures/variant_coercion_unboxed_array_payload.res @@ -0,0 +1,2 @@ +@unboxed type t = Strings(array) | @as("none") None +let g = (v: t) => (v :> string) diff --git a/tests/error_tests/single/fixtures/variant_coercion_variant_to_bool.res b/tests/error_tests/single/fixtures/variant_coercion_variant_to_bool.res new file mode 100644 index 0000000000..bb0b9ce5ae --- /dev/null +++ b/tests/error_tests/single/fixtures/variant_coercion_variant_to_bool.res @@ -0,0 +1,2 @@ +@unboxed type t = String(string) | Int(int) +let g = (v: t) => (v :> bool) diff --git a/tests/error_tests/single/fixtures/variant_to_variant_coercion_constructor_as.res b/tests/error_tests/single/fixtures/variant_to_variant_coercion_constructor_as.res new file mode 100644 index 0000000000..49e48ab3f5 --- /dev/null +++ b/tests/error_tests/single/fixtures/variant_to_variant_coercion_constructor_as.res @@ -0,0 +1,3 @@ +type t = A | @as("x") B +type u = B +let f = (v: u) => (v :> t) diff --git a/tests/error_tests/single/fixtures/variant_to_variant_coercion_missing_constructor.res b/tests/error_tests/single/fixtures/variant_to_variant_coercion_missing_constructor.res new file mode 100644 index 0000000000..e00be0df26 --- /dev/null +++ b/tests/error_tests/single/fixtures/variant_to_variant_coercion_missing_constructor.res @@ -0,0 +1,3 @@ +type t = A | B +type u = B | C +let f = (v: u) => (v :> t) diff --git a/tests/error_tests/single/fixtures/variant_to_variant_coercion_payload.res b/tests/error_tests/single/fixtures/variant_to_variant_coercion_payload.res new file mode 100644 index 0000000000..61e7f055b8 --- /dev/null +++ b/tests/error_tests/single/fixtures/variant_to_variant_coercion_payload.res @@ -0,0 +1,3 @@ +type t = A | B(int) +type u = B(string) +let f = (v: u) => (v :> t) diff --git a/tests/error_tests/single/fixtures/variant_to_variant_coercion_payload_kind.res b/tests/error_tests/single/fixtures/variant_to_variant_coercion_payload_kind.res new file mode 100644 index 0000000000..0902510047 --- /dev/null +++ b/tests/error_tests/single/fixtures/variant_to_variant_coercion_payload_kind.res @@ -0,0 +1,3 @@ +type t = A | B({x: int}) +type u = B(int) +let f = (v: u) => (v :> t)