Skip to content

Commit e238013

Browse files
committed
Alternative approach
1 parent 0ba6d8a commit e238013

File tree

4 files changed

+22
-11
lines changed

4 files changed

+22
-11
lines changed

jscomp/core/lam.ml

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -468,10 +468,16 @@ end
468468

469469
let prim ~primitive:(prim : Lam_primitive.t) ~args loc : t =
470470
let default () : t = Lprim { primitive = prim; args; loc } in
471+
let extract_const_as c def =
472+
(match c with
473+
| Lam_constant.Pt_constructor({cstr_name = ({tag_type = Some(Int(v))})}) -> Int32.of_int v
474+
| _ -> def) in
471475
match args with
472476
| [ Lconst a ] -> (
473477
match (prim, a) with
474-
| Pnegint, Const_int { i } -> Lift.int (Int32.neg i)
478+
| Pnegint, Const_int { i; comment = pointer_info} ->
479+
let ii = extract_const_as pointer_info i in
480+
Lift.int (Int32.neg ii)
475481
(* | Pfloatofint, ( (Const_int a)) *)
476482
(* -> Lift.float (float_of_int a) *)
477483
| Pintoffloat, Const_float a ->
@@ -503,8 +509,10 @@ let prim ~primitive:(prim : Lam_primitive.t) ~args loc : t =
503509
| _ -> assert false)
504510
| ( ( Paddint | Psubint | Pmulint | Pdivint | Pmodint | Pandint | Porint
505511
| Pxorint | Plslint | Plsrint | Pasrint ),
506-
Const_int { i = aa },
507-
Const_int { i = bb } ) -> (
512+
Const_int { i = aa; comment = aa_pointer_info },
513+
Const_int { i = bb; comment = bb_pointer_info } ) -> (
514+
let aa = extract_const_as aa_pointer_info aa in
515+
let bb = extract_const_as bb_pointer_info bb in
508516
(* WE SHOULD keep it as [int], to preserve types *)
509517
let int_ = Lift.int in
510518
match prim with

jscomp/core/lam_constant_convert.ml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,6 @@ let rec convert_constant (const : Lambda.structured_constant) : Lam_constant.t =
5050
Const_int { i = Int32.of_int i; comment = Pt_assertfalse }
5151
| Pt_constructor { name; const; non_const; attrs } ->
5252
let tag_type = Ast_untagged_variants.process_tag_type attrs in
53-
let i = match tag_type with
54-
| Some(Ast_untagged_variants.Int(v)) -> v
55-
| _ -> i in
5653
Const_int
5754
{
5855
i = Int32.of_int i;

jscomp/test/VariantCoercion.js

Lines changed: 7 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

jscomp/test/VariantCoercion.res

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,13 +124,15 @@ module CoerceFromPolyvariantToVariant = {
124124
module CoerceVariantBinaryOp = {
125125
type flag = | @as(0) A | @as(2) B
126126

127-
let x = 0->lor((B :> int))
127+
let x1 = 0->lor((B :> int))
128+
let x2 = -(B :> int)
128129

129130
type flagStr = | @as("one") One | @as("two") Two
130131

131132
let y = (One :> string)->String.length
132133

133134
type flagFloat = | @as(1.5) X | @as(2.0) Y
134135

135-
let z = (X :> float) +. (Y :> float) +. 1.5
136+
let z1 = (X :> float) +. (Y :> float) +. 1.5
137+
let z2 = int_of_float((X :> float))
136138
}

0 commit comments

Comments
 (0)