Skip to content

Coercing int and float unboxed variant catch all cases #6540

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 27, 2023
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 CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#### :rocket: New Feature

- GenType: support `@deriving(accessors)` outputs. https://github.com/rescript-lang/rescript-compiler/pull/6537
- Allow coercing ints and floats to unboxed variants that have a catch-all unboxed int or float case. https://github.com/rescript-lang/rescript-compiler/pull/6540

# 11.0.0-rc.8

Expand Down
10 changes: 5 additions & 5 deletions jscomp/ml/ctype.ml
Original file line number Diff line number Diff line change
Expand Up @@ -3959,20 +3959,20 @@ let rec subtype_rec env trace t1 t2 cstrs =
subtype_rec env trace (expand_abbrev_opt env t1) t2 cstrs
| (Tconstr(p1, [], _), Tconstr(p2, [], _)) when Path.same p1 Predef.path_int && Path.same p2 Predef.path_float ->
cstrs
| (Tconstr(path, [], _), Tconstr(_, [], _)) when Path.same path Predef.path_string &&
| (Tconstr(path, [], _), Tconstr(_, [], _)) when Variant_coercion.can_coerce_primitive path &&
extract_concrete_typedecl env t2 |> Variant_coercion.can_try_coerce_variant_to_primitive |> Option.is_some
->
(* type coercion for strings to elgible unboxed variants:
(* type coercion for primitives (int/float/string) to elgible unboxed variants:
- must be unboxed
- must have a constructor case with a string payload *)
- must have a constructor case with a supported and matching primitive payload *)
(match Variant_coercion.can_try_coerce_variant_to_primitive (extract_concrete_typedecl env t2) with
| Some (constructors, true) ->
if constructors |> Variant_coercion.variant_has_catch_all_string_case then
if Variant_coercion.variant_has_catch_all_case constructors (fun p -> Path.same p path) then
cstrs
else
(trace, t1, t2, !univar_pairs)::cstrs
| _ -> (trace, t1, t2, !univar_pairs)::cstrs)
| (Tconstr(_, [], _), Tconstr(path, [], _)) when Variant_coercion.can_coerce_path path &&
| (Tconstr(_, [], _), Tconstr(path, [], _)) when Variant_coercion.can_coerce_primitive path &&
extract_concrete_typedecl env t1 |> Variant_coercion.can_try_coerce_variant_to_primitive |> Option.is_some
->
(* type coercion for variants to primitives *)
Expand Down
9 changes: 6 additions & 3 deletions jscomp/ml/variant_coercion.ml
Original file line number Diff line number Diff line change
@@ -1,25 +1,28 @@
(* TODO: Improve error messages? Say why we can't coerce. *)

(* Right now we only allow coercing to primitives string/int/float *)
let can_coerce_path (path : Path.t) =
let can_coerce_primitive (path : Path.t) =
Path.same path Predef.path_string
|| Path.same path Predef.path_int
|| Path.same path Predef.path_float

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

let variant_has_catch_all_string_case (constructors : Types.constructor_declaration list) =
let variant_has_catch_all_case (constructors : Types.constructor_declaration list) path_is_same =
let has_catch_all_string_case (c : Types.constructor_declaration) =
let args = c.cd_args in
match args with
| Cstr_tuple [{desc = Tconstr (p, [], _)}] ->
Path.same p Predef.path_string
path_is_same p
| _ -> false
in

constructors |> List.exists has_catch_all_string_case

let variant_has_relevant_primitive_catch_all (constructors : Types.constructor_declaration list) =
variant_has_catch_all_case constructors can_coerce_primitive

(* Checks if every case of the variant has the same runtime representation as the target type. *)
let variant_has_same_runtime_representation_as_target ~(targetPath : Path.t)
~unboxed (constructors : Types.constructor_declaration list) =
Expand Down
20 changes: 20 additions & 0 deletions jscomp/test/VariantCoercion.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions jscomp/test/VariantCoercion.res
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,27 @@ module CoerceFromStringToVariant = {
let c = "Hi"
let cc: mixed = (c :> mixed)
}

module CoerceFromIntToVariant = {
@unboxed type ints = Int(int) | @as(1) First | @as(2) Second | @as(3) Third
let a = 100
let aa = 1
let b: ints = (a :> ints)
let bb: ints = (aa :> ints)

@unboxed type mixed = Int(int) | @as(1) One | @as(null) Null | Two
let c = 120
let cc: mixed = (c :> mixed)
}

module CoerceFromFloatToVariant = {
@unboxed type floats = Float(float) | @as(1.) First | @as(2.) Second | @as(3.) Third
let a = 100.
let aa = 1.
let b: floats = (a :> floats)
let bb: floats = (aa :> floats)

@unboxed type mixed = Float(float) | @as(1.) One | @as(null) Null | Two
let c = 120.
let cc: mixed = (c :> mixed)
}