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 diff --git a/compiler/core/lam_constant_convert.ml b/compiler/core/lam_constant_convert.ml index 6f132e9453..ad0e7de05a 100644 --- a/compiler/core/lam_constant_convert.ml +++ b/compiler/core/lam_constant_convert.ml @@ -48,6 +48,11 @@ let rec convert_constant (const : Lambda.structured_constant) : Lam_constant.t = | 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; 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 +}