Skip to content

Fix variant coercion with as attribute #7058

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
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 @@ -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

Expand Down
5 changes: 5 additions & 0 deletions compiler/core/lam_constant_convert.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
28 changes: 27 additions & 1 deletion tests/tests/src/VariantCoercion.js

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

30 changes: 30 additions & 0 deletions tests/tests/src/VariantCoercion.res
Original file line number Diff line number Diff line change
Expand Up @@ -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
}