From 3c8ef684adfd7e9b2dfa3fa904c4bd6a74c25c5f Mon Sep 17 00:00:00 2001 From: Shulhi Sapli Date: Sat, 28 Sep 2024 17:06:37 +0800 Subject: [PATCH 1/4] Use value from as attribute if available --- compiler/core/lam_constant_convert.ml | 36 +++++++++++++++------------ 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/compiler/core/lam_constant_convert.ml b/compiler/core/lam_constant_convert.ml index 6f132e9453..f7ea3050e1 100644 --- a/compiler/core/lam_constant_convert.ml +++ b/compiler/core/lam_constant_convert.ml @@ -42,22 +42,26 @@ let rec convert_constant (const : Lambda.structured_constant) : Lam_constant.t = | Const_false -> Const_js_false | Const_true -> Const_js_true | Const_pointer (i, p) -> ( - match p with - | Pt_module_alias -> Const_module_alias - | Pt_shape_none -> Lam_constant.lam_none - | Pt_assertfalse -> Const_int {i = Int32.of_int i; comment = Pt_assertfalse} - | Pt_constructor {name; const; non_const; attrs} -> - let tag_type = Ast_untagged_variants.process_tag_type attrs in - Const_int - { - i = Int32.of_int i; - comment = - Pt_constructor {cstr_name = {name; tag_type}; const; non_const}; - } - | Pt_variant {name} -> - if Ext_string.is_valid_hash_number name then - Const_int {i = Ext_string.hash_number_as_i32_exn name; comment = None} - else Const_pointer name) + match p with + | Pt_module_alias -> Const_module_alias + | Pt_shape_none -> Lam_constant.lam_none + | Pt_assertfalse -> + Const_int { i = Int32.of_int i; comment = Pt_assertfalse } + | Pt_constructor { name; const; non_const; attrs } -> + let tag_type = Ast_untagged_variants.process_tag_type attrs in + let i = match tag_type with + | Some(Ast_untagged_variants.Int(v)) -> v + | _ -> i in + Const_int + { + i = Int32.of_int i; + comment = Pt_constructor { cstr_name={name; tag_type}; const; non_const }; + } + | Pt_variant { name } -> + if Ext_string.is_valid_hash_number name then + Const_int + { i = Ext_string.hash_number_as_i32_exn name; comment = None } + else Const_pointer name) | Const_float_array s -> assert false | Const_immstring s -> Const_string {s; unicode = false} | Const_block (t, xs) -> ( From fcff3ba133a58e40f423f32dda3b577198f9a521 Mon Sep 17 00:00:00 2001 From: Shulhi Sapli Date: Sat, 28 Sep 2024 17:22:13 +0800 Subject: [PATCH 2/4] Add tests --- tests/tests/src/VariantCoercion.js | 28 ++++++++++++++++++++++++++- tests/tests/src/VariantCoercion.res | 30 +++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 1 deletion(-) diff --git a/tests/tests/src/VariantCoercion.js b/tests/tests/src/VariantCoercion.js index 1cea8b731b..5913561e11 100644 --- a/tests/tests/src/VariantCoercion.js +++ b/tests/tests/src/VariantCoercion.js @@ -82,6 +82,31 @@ let CoerceFromPolyvariantToVariant = { withUnboxedCatchAllP: "One" }; +function f1() { + return "b"; +} + +function f2() { + return "b"; +} + +for (let x$1 = 1; x$1 <= 2; ++x$1) { + console.log(x$1); +} + +let y = "one".length; + +let z = 1.5 + 2.0 + 1.5; + +let CoerceVariantBinaryOp = { + x: 2, + v: 2, + f1: f1, + f2: f2, + y: y, + z: z +}; + let a$2 = "Three"; let b = "Three"; @@ -107,4 +132,5 @@ exports.CoerceFromIntToVariant = CoerceFromIntToVariant; exports.CoerceFromFloatToVariant = CoerceFromFloatToVariant; exports.CoerceFromBigintToVariant = CoerceFromBigintToVariant; exports.CoerceFromPolyvariantToVariant = CoerceFromPolyvariantToVariant; -/* No side effect */ +exports.CoerceVariantBinaryOp = CoerceVariantBinaryOp; +/* Not a pure module */ diff --git a/tests/tests/src/VariantCoercion.res b/tests/tests/src/VariantCoercion.res index f8f0826f67..c1e197be55 100644 --- a/tests/tests/src/VariantCoercion.res +++ b/tests/tests/src/VariantCoercion.res @@ -120,3 +120,33 @@ module CoerceFromPolyvariantToVariant = { let withUnboxedCatchAll: withUnboxedCatchAll = #One let withUnboxedCatchAllP = (withUnboxedCatchAll :> withUnboxedCatchAllP) } + +module CoerceVariantBinaryOp = { + type flag = | @as(0) A | @as(2) B + + let x = 0->lor((B :> int)) + + let v = B + let f1 = () => + switch v { + | A => "a" + | B => "b" + } + let f2 = () => + switch (v :> int) { + | 2 => "b" + | _ => "a" + } + + for x in 1 to (B :> int) { + Js.log(x) + } + + type flagStr = | @as("one") One | @as("two") Two + + let y = (One :> string)->String.length + + type flagFloat = | @as(1.5) X | @as(2.0) Y + + let z = (X :> float) +. (Y :> float) +. 1.5 +} From cadb22c3efafda1adf804e5a154339b3ce97f98f Mon Sep 17 00:00:00 2001 From: Shulhi Sapli Date: Thu, 10 Oct 2024 18:43:07 +0800 Subject: [PATCH 3/4] Update CHANGELOG --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2d71a3d889..9f163672b6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -33,6 +33,7 @@ - Fix bigint max, min https://github.com/rescript-lang/rescript-compiler/pull/7088 - Fix parsing issue with nested variant pattern type spreads. https://github.com/rescript-lang/rescript-compiler/pull/7080 - Fix JSX settings inheritance: only 'version' propagates to dependencies, preserving their 'mode' and 'module'. https://github.com/rescript-lang/rescript-compiler/pull/7094 +- Fix variant cast to int. https://github.com/rescript-lang/rescript-compiler/pull/7058 #### :nail_care: Polish From 2fa737d30ac649577796a749db33be2ab84b10be Mon Sep 17 00:00:00 2001 From: Shulhi Sapli Date: Thu, 10 Oct 2024 19:23:12 +0800 Subject: [PATCH 4/4] Fix formatting --- compiler/core/lam_constant_convert.ml | 41 ++++++++++++++------------- 1 file changed, 21 insertions(+), 20 deletions(-) diff --git a/compiler/core/lam_constant_convert.ml b/compiler/core/lam_constant_convert.ml index f7ea3050e1..ad0e7de05a 100644 --- a/compiler/core/lam_constant_convert.ml +++ b/compiler/core/lam_constant_convert.ml @@ -42,26 +42,27 @@ let rec convert_constant (const : Lambda.structured_constant) : Lam_constant.t = | Const_false -> Const_js_false | Const_true -> Const_js_true | Const_pointer (i, p) -> ( - match p with - | Pt_module_alias -> Const_module_alias - | Pt_shape_none -> Lam_constant.lam_none - | Pt_assertfalse -> - Const_int { i = Int32.of_int i; comment = Pt_assertfalse } - | Pt_constructor { name; const; non_const; attrs } -> - let tag_type = Ast_untagged_variants.process_tag_type attrs in - let i = match tag_type with - | Some(Ast_untagged_variants.Int(v)) -> v - | _ -> i in - Const_int - { - i = Int32.of_int i; - comment = Pt_constructor { cstr_name={name; tag_type}; const; non_const }; - } - | Pt_variant { name } -> - if Ext_string.is_valid_hash_number name then - Const_int - { i = Ext_string.hash_number_as_i32_exn name; comment = None } - else Const_pointer name) + match p with + | Pt_module_alias -> Const_module_alias + | Pt_shape_none -> Lam_constant.lam_none + | Pt_assertfalse -> Const_int {i = Int32.of_int i; comment = Pt_assertfalse} + | Pt_constructor {name; const; non_const; attrs} -> + let tag_type = Ast_untagged_variants.process_tag_type attrs in + let i = + match tag_type with + | Some (Ast_untagged_variants.Int v) -> v + | _ -> i + in + Const_int + { + i = Int32.of_int i; + comment = + Pt_constructor {cstr_name = {name; tag_type}; const; non_const}; + } + | Pt_variant {name} -> + if Ext_string.is_valid_hash_number name then + Const_int {i = Ext_string.hash_number_as_i32_exn name; comment = None} + else Const_pointer name) | Const_float_array s -> assert false | Const_immstring s -> Const_string {s; unicode = false} | Const_block (t, xs) -> (