Skip to content

Commit 1437815

Browse files
authored
Fix variant coercion with as attribute (#7058)
* Use value from as attribute if available * Add tests * Update CHANGELOG * Fix formatting
1 parent 57121b2 commit 1437815

File tree

4 files changed

+63
-1
lines changed

4 files changed

+63
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
- Fix bigint max, min https://github.com/rescript-lang/rescript-compiler/pull/7088
3434
- Fix parsing issue with nested variant pattern type spreads. https://github.com/rescript-lang/rescript-compiler/pull/7080
3535
- Fix JSX settings inheritance: only 'version' propagates to dependencies, preserving their 'mode' and 'module'. https://github.com/rescript-lang/rescript-compiler/pull/7094
36+
- Fix variant cast to int. https://github.com/rescript-lang/rescript-compiler/pull/7058
3637

3738
#### :nail_care: Polish
3839

compiler/core/lam_constant_convert.ml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ let rec convert_constant (const : Lambda.structured_constant) : Lam_constant.t =
4848
| Pt_assertfalse -> Const_int {i = Int32.of_int i; comment = Pt_assertfalse}
4949
| Pt_constructor {name; const; non_const; attrs} ->
5050
let tag_type = Ast_untagged_variants.process_tag_type attrs in
51+
let i =
52+
match tag_type with
53+
| Some (Ast_untagged_variants.Int v) -> v
54+
| _ -> i
55+
in
5156
Const_int
5257
{
5358
i = Int32.of_int i;

tests/tests/src/VariantCoercion.js

Lines changed: 27 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/tests/src/VariantCoercion.res

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,3 +120,33 @@ module CoerceFromPolyvariantToVariant = {
120120
let withUnboxedCatchAll: withUnboxedCatchAll = #One
121121
let withUnboxedCatchAllP = (withUnboxedCatchAll :> withUnboxedCatchAllP)
122122
}
123+
124+
module CoerceVariantBinaryOp = {
125+
type flag = | @as(0) A | @as(2) B
126+
127+
let x = 0->lor((B :> int))
128+
129+
let v = B
130+
let f1 = () =>
131+
switch v {
132+
| A => "a"
133+
| B => "b"
134+
}
135+
let f2 = () =>
136+
switch (v :> int) {
137+
| 2 => "b"
138+
| _ => "a"
139+
}
140+
141+
for x in 1 to (B :> int) {
142+
Js.log(x)
143+
}
144+
145+
type flagStr = | @as("one") One | @as("two") Two
146+
147+
let y = (One :> string)->String.length
148+
149+
type flagFloat = | @as(1.5) X | @as(2.0) Y
150+
151+
let z = (X :> float) +. (Y :> float) +. 1.5
152+
}

0 commit comments

Comments
 (0)