Skip to content

Commit c65c6c0

Browse files
authored
Add support for the bitwise NOT(~) unary operator (#7418)
1 parent 80c96a1 commit c65c6c0

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+178
-78
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
- Add shift (`<<`, `>>`, `>>>`) operators for `int` and `bigint`. https://github.com/rescript-lang/rescript/pull/7183
2323
- Add bitwise AND (`&`) operator for `int` and `bigint`. https://github.com/rescript-lang/rescript/pull/7415
24+
- Add bitwise NOT (`~`) operator for `int` and `bigint`. https://github.com/rescript-lang/rescript/pull/7418
2425
- Significantly reduced the download size by splitting binaries into optional platform-specific dependencies (e.g, `@rescript/linux-x64`). https://github.com/rescript-lang/rescript/pull/7395
2526
- JSX: do not error on ref as prop anymore (which is allowed in React 19). https://github.com/rescript-lang/rescript/pull/7420
2627

compiler/core/j.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ and expression_desc =
8888
| Typeof of expression
8989
| In of expression * expression (* prop in obj *)
9090
| Js_not of expression (* !v *)
91+
| Js_bnot of expression (* ~v *)
9192
(* TODO: Add some primitives so that [js inliner] can do a better job *)
9293
| Seq of expression * expression
9394
| Cond of expression * expression * expression

compiler/core/js_analyzer.ml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ let rec no_side_effect_expression_desc (x : J.expression_desc) =
110110
no_side_effect call_expr
111111
&& Ext_list.for_all strings no_side_effect
112112
&& Ext_list.for_all values no_side_effect
113-
| Js_not e -> no_side_effect e
113+
| Js_not e | Js_bnot e -> no_side_effect e
114114
| In (prop, obj) -> no_side_effect prop && no_side_effect obj
115115
| Cond (a, b, c) -> no_side_effect a && no_side_effect b && no_side_effect c
116116
| Call ({expression_desc = Str {txt = "Array.isArray"}}, [e], _) ->
@@ -228,8 +228,8 @@ let rec eq_expression ({expression_desc = x0} : J.expression)
228228
eq_expression_list ls0 ls1 && flag0 = flag1 && eq_expression tag0 tag1
229229
| _ -> false)
230230
| Length _ | Is_null_or_undefined _ | String_append _ | Typeof _ | Js_not _
231-
| In _ | Cond _ | FlatCall _ | New _ | Fun _ | Raw_js_code _ | Array _
232-
| Caml_block_tag _ | Object _ | Tagged_template _ | Await _ ->
231+
| Js_bnot _ | In _ | Cond _ | FlatCall _ | New _ | Fun _ | Raw_js_code _
232+
| Array _ | Caml_block_tag _ | Object _ | Tagged_template _ | Await _ ->
233233
false
234234
| Spread _ -> false
235235

compiler/core/js_dump.ml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,8 @@ let rec exp_need_paren ?(arrow = false) (e : J.expression) =
166166
| Length _ | Call _ | Caml_block_tag _ | Seq _ | Static_index _ | Cond _
167167
| Bin _ | Is_null_or_undefined _ | String_index _ | Array_index _
168168
| String_append _ | Var _ | Undefined _ | Null | Str _ | Array _
169-
| Caml_block _ | FlatCall _ | Typeof _ | Number _ | Js_not _ | In _ | Bool _
170-
| New _ ->
169+
| Caml_block _ | FlatCall _ | Typeof _ | Number _ | Js_not _ | Js_bnot _
170+
| In _ | Bool _ | New _ ->
171171
false
172172
| Await _ -> false
173173
| Spread _ -> false
@@ -677,6 +677,10 @@ and expression_desc cxt ~(level : int) f x : cxt =
677677
P.cond_paren_group f (level > 13) (fun _ ->
678678
P.string f "!";
679679
expression ~level:13 cxt f e)
680+
| Js_bnot e ->
681+
P.cond_paren_group f (level > 13) (fun _ ->
682+
P.string f "~";
683+
expression ~level:13 cxt f e)
680684
| In (prop, obj) ->
681685
P.cond_paren_group f (level > 12) (fun _ ->
682686
let cxt = expression ~level:0 cxt f prop in

compiler/core/js_exp_make.ml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1616,6 +1616,11 @@ let int32_mul ?comment (e1 : J.expression) (e2 : J.expression) : J.expression =
16161616
let unchecked_int32_mul ?comment e1 e2 : J.expression =
16171617
{comment; expression_desc = Bin (Mul, e1, e2)}
16181618

1619+
let int_bnot ?comment (e : t) : J.expression =
1620+
match e.expression_desc with
1621+
| Number (Int {i}) -> int ?comment (Int32.lognot i)
1622+
| _ -> {comment; expression_desc = Js_bnot e}
1623+
16191624
let int32_pow ?comment (e1 : t) (e2 : t) : J.expression =
16201625
match (e1.expression_desc, e2.expression_desc) with
16211626
| Number (Int {i = i1}), Number (Int {i = i2}) ->

compiler/core/js_exp_make.mli

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,8 @@ val float_equal : ?comment:string -> t -> t -> t
210210

211211
val int_equal : ?comment:string -> t -> t -> t
212212

213+
val int_bnot : ?comment:string -> t -> t
214+
213215
val string_equal : ?comment:string -> t -> t -> t
214216

215217
val eq_null_undefined_boolean : ?comment:string -> t -> t -> t

compiler/core/js_fold.ml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,9 @@ class fold =
103103
| Js_not _x0 ->
104104
let _self = _self#expression _x0 in
105105
_self
106+
| Js_bnot _x0 ->
107+
let _self = _self#expression _x0 in
108+
_self
106109
| In (_x0, _x1) ->
107110
let _self = _self#expression _x0 in
108111
let _self = _self#expression _x1 in

compiler/core/js_op.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ type binop =
3838
| Le
3939
| Gt
4040
| Ge
41+
| Bnot
4142
| Bor
4243
| Bxor
4344
| Band

compiler/core/js_op_util.ml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ let op_prec (op : Js_op.binop) =
3939
| Bxor -> (6, 6, 6)
4040
| Band -> (7, 7, 7)
4141
| Lsl | Lsr | Asr -> (10, 10, 11)
42-
| Plus | Minus -> (11, 11, 12)
42+
| Bnot | Plus | Minus -> (11, 11, 12)
4343
| Mul | Div | Mod -> (12, 12, 13)
4444
| Pow -> (13, 14, 12)
4545

@@ -55,6 +55,7 @@ let op_int_prec (op : Js_op.int_op) =
5555

5656
let op_str (op : Js_op.binop) =
5757
match op with
58+
| Bnot -> "~"
5859
| Bor -> "|"
5960
| Bxor -> "^"
6061
| Band -> "&"

compiler/core/js_record_fold.ml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,9 @@ let expression_desc : 'a. ('a, expression_desc) fn =
109109
| Js_not _x0 ->
110110
let st = _self.expression _self st _x0 in
111111
st
112+
| Js_bnot _x0 ->
113+
let st = _self.expression _self st _x0 in
114+
st
112115
| In (_x0, _x1) ->
113116
let st = _self.expression _self st _x0 in
114117
let st = _self.expression _self st _x1 in

0 commit comments

Comments
 (0)