Skip to content

Commit 2c0a4eb

Browse files
cknittcristianoc
andauthored
Remove @return(undefined_to_opt) and %undefined_to_opt primitive (#7462)
* Remove @return(undefined_to_opt) and %undefined_to_opt primitive * Respect conversion semantics from undefined to option. * Fix rewatch test * CHANGELOG --------- Co-authored-by: Cristiano Calcagno <cristianoc@users.noreply.github.com>
1 parent 38459ba commit 2c0a4eb

Some content is hidden

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

45 files changed

+134
-158
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@
2323

2424
- Better error message for when trying to await something that is not a promise. https://github.com/rescript-lang/rescript/pull/7561
2525

26+
#### :house: Internal
27+
28+
- Remove `@return(undefined_to_opt)` and `%undefined_to_opt` primitive. https://github.com/rescript-lang/rescript/pull/7462
29+
2630
# 12.0.0-alpha.14
2731

2832
#### :boom: Breaking Change

compiler/core/lam.ml

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -298,8 +298,8 @@ let rec apply ?(ap_transformed_jsx = false) fn args (ap_info : ap_info) : t =
298298
Lprim
299299
{
300300
primitive =
301-
( Pundefined_to_opt | Pnull_to_opt | Pnull_undefined_to_opt
302-
| Pis_null | Pis_null_undefined | Ptypeof ) as wrap;
301+
( Pnull_to_opt | Pnull_undefined_to_opt | Pis_null
302+
| Pis_null_undefined | Ptypeof ) as wrap;
303303
args =
304304
[Lprim ({primitive = _; args = inner_args} as primitive_call)];
305305
};
@@ -442,11 +442,7 @@ let rec seq (a : t) b : t =
442442
match a with
443443
| Lprim {primitive = Pmakeblock _; args = x :: xs} ->
444444
seq (Ext_list.fold_left xs x seq) b
445-
| Lprim
446-
{
447-
primitive = Pnull_to_opt | Pundefined_to_opt | Pnull_undefined_to_opt;
448-
args = [a];
449-
} ->
445+
| Lprim {primitive = Pnull_to_opt | Pnull_undefined_to_opt; args = [a]} ->
450446
seq a b
451447
| _ -> Lsequence (a, b)
452448

@@ -719,8 +715,6 @@ let result_wrap loc (result_type : External_ffi_types.return_wrapper) result =
719715
| Return_null_to_opt -> prim ~primitive:Pnull_to_opt ~args:[result] loc
720716
| Return_null_undefined_to_opt ->
721717
prim ~primitive:Pnull_undefined_to_opt ~args:[result] loc
722-
| Return_undefined_to_opt ->
723-
prim ~primitive:Pundefined_to_opt ~args:[result] loc
724718
| Return_unset | Return_identity -> result
725719

726720
let handle_bs_non_obj_ffi ?(transformed_jsx = false)

compiler/core/lam_analysis.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ let rec no_side_effects (lam : Lam.t) : bool =
4646
| _ -> false)
4747
| Pcreate_extension _ | Ptypeof | Pis_null | Pis_not_none | Psome
4848
| Psome_not_nest | Pis_undefined | Pis_null_undefined | Pnull_to_opt
49-
| Pundefined_to_opt | Pnull_undefined_to_opt | Pjs_fn_make _
50-
| Pjs_fn_make_unit | Pjs_object_create _ | Pimport
49+
| Pnull_undefined_to_opt | Pjs_fn_make _ | Pjs_fn_make_unit
50+
| Pjs_object_create _ | Pimport
5151
(* TODO: check *)
5252
| Pmakeblock _
5353
(* whether it's mutable or not *)

compiler/core/lam_compile_primitive.ml

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -95,13 +95,6 @@ let translate output_prefix loc (cxt : Lam_compile_context.t)
9595
| Var _ | Undefined _ | Null -> Js_of_lam_option.null_to_opt e
9696
| _ -> E.runtime_call Primitive_modules.option "fromNull" args)
9797
| _ -> assert false)
98-
| Pundefined_to_opt -> (
99-
match args with
100-
| [e] -> (
101-
match e.expression_desc with
102-
| Var _ | Undefined _ | Null -> Js_of_lam_option.undef_to_opt e
103-
| _ -> E.runtime_call Primitive_modules.option "fromUndefined" args)
104-
| _ -> assert false)
10598
| Pnull_undefined_to_opt -> (
10699
match args with
107100
| [e] -> (

compiler/core/lam_convert.ml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,6 @@ let lam_prim ~primitive:(p : Lambda.primitive) ~args loc : Lam.t =
197197
| Pisnullable -> prim ~primitive:Pis_null_undefined ~args loc
198198
| Pnull_to_opt -> prim ~primitive:Pnull_to_opt ~args loc
199199
| Pnullable_to_opt -> prim ~primitive:Pnull_undefined_to_opt ~args loc
200-
| Pundefined_to_opt -> prim ~primitive:Pundefined_to_opt ~args loc
201200
| Pis_not_none -> prim ~primitive:Pis_not_none ~args loc
202201
| Pval_from_option -> prim ~primitive:Pval_from_option ~args loc
203202
| Pval_from_option_not_nest ->

compiler/core/lam_pass_collect.ml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,6 @@ let collect_info (meta : Lam_stats.t) (lam : Lam.t) =
6666
collect v
6767
| Lprim {primitive = Pnull_to_opt; args = [(Lvar _ as l)]; _} ->
6868
Hash_ident.replace meta.ident_tbl ident (OptionalBlock (l, Null))
69-
| Lprim {primitive = Pundefined_to_opt; args = [(Lvar _ as l)]; _} ->
70-
Hash_ident.replace meta.ident_tbl ident (OptionalBlock (l, Undefined))
7169
| Lprim {primitive = Pnull_undefined_to_opt; args = [(Lvar _ as l)]} ->
7270
Hash_ident.replace meta.ident_tbl ident
7371
(OptionalBlock (l, Null_undefined))

compiler/core/lam_pass_deep_flatten.ml

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,7 @@ let deep_flatten (lam : Lam.t) : Lam.t =
123123
id,
124124
(Lprim
125125
{
126-
primitive =
127-
Pnull_to_opt | Pundefined_to_opt | Pnull_undefined_to_opt;
126+
primitive = Pnull_to_opt | Pnull_undefined_to_opt;
128127
args = [Lvar _];
129128
} as arg),
130129
body ) ->
@@ -134,9 +133,7 @@ let deep_flatten (lam : Lam.t) : Lam.t =
134133
id,
135134
Lprim
136135
{
137-
primitive =
138-
(Pnull_to_opt | Pundefined_to_opt | Pnull_undefined_to_opt) as
139-
primitive;
136+
primitive = (Pnull_to_opt | Pnull_undefined_to_opt) as primitive;
140137
args = [arg];
141138
},
142139
body ) ->

compiler/core/lam_primitive.ml

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,6 @@ type t =
161161
play safe first
162162
*)
163163
| Pjs_fn_method
164-
| Pundefined_to_opt
165164
| Pnull_to_opt
166165
| Pnull_undefined_to_opt
167166
| Pis_null
@@ -224,12 +223,12 @@ let eq_primitive_approx (lhs : t) (rhs : t) =
224223
| Pawait
225224
(* etc *)
226225
| Pjs_apply | Pjs_runtime_apply | Pval_from_option | Pval_from_option_not_nest
227-
| Pundefined_to_opt | Pnull_to_opt | Pnull_undefined_to_opt | Pis_null
228-
| Pis_not_none | Psome | Psome_not_nest | Pis_undefined | Pis_null_undefined
229-
| Pimport | Ptypeof | Pfn_arity | Pis_poly_var_block | Pdebugger | Pinit_mod
230-
| Pupdate_mod | Pduprecord | Pmakearray | Parraylength | Parrayrefu
231-
| Parraysetu | Parrayrefs | Parraysets | Pjs_fn_make_unit | Pjs_fn_method
232-
| Phash | Phash_mixstring | Phash_mixint | Phash_finalmix ->
226+
| Pnull_to_opt | Pnull_undefined_to_opt | Pis_null | Pis_not_none | Psome
227+
| Psome_not_nest | Pis_undefined | Pis_null_undefined | Pimport | Ptypeof
228+
| Pfn_arity | Pis_poly_var_block | Pdebugger | Pinit_mod | Pupdate_mod
229+
| Pduprecord | Pmakearray | Parraylength | Parrayrefu | Parraysetu
230+
| Parrayrefs | Parraysets | Pjs_fn_make_unit | Pjs_fn_method | Phash
231+
| Phash_mixstring | Phash_mixint | Phash_finalmix ->
233232
rhs = lhs
234233
| Pcreate_extension a -> (
235234
match rhs with

compiler/core/lam_primitive.mli

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,6 @@ type t =
152152
| Pjs_fn_make of int
153153
| Pjs_fn_make_unit
154154
| Pjs_fn_method
155-
| Pundefined_to_opt
156155
| Pnull_to_opt
157156
| Pnull_undefined_to_opt
158157
| Pis_null

compiler/core/lam_print.ml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ let primitive ppf (prim : Lam_primitive.t) =
6161
| Praw_js_code _ -> fprintf ppf "[raw]"
6262
| Ptypeof -> fprintf ppf "typeof"
6363
| Pnull_to_opt -> fprintf ppf "[null->opt]"
64-
| Pundefined_to_opt -> fprintf ppf "[undefined->opt]"
6564
| Pnull_undefined_to_opt -> fprintf ppf "[null/undefined->opt]"
6665
| Pis_null -> fprintf ppf "[?null]"
6766
| Pis_not_none -> fprintf ppf "[?is-not-none]"

compiler/frontend/ast_attributes.ml

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -279,24 +279,6 @@ let set : attr = ({txt = "set"; loc = locg}, Ast_payload.empty)
279279
let internal_expansive : attr =
280280
({txt = "internal.expansive"; loc = locg}, Ast_payload.empty)
281281

282-
let bs_return_undefined : attr =
283-
( {txt = "return"; loc = locg},
284-
PStr
285-
[
286-
{
287-
pstr_desc =
288-
Pstr_eval
289-
( {
290-
pexp_desc =
291-
Pexp_ident {txt = Lident "undefined_to_opt"; loc = locg};
292-
pexp_loc = locg;
293-
pexp_attributes = [];
294-
},
295-
[] );
296-
pstr_loc = locg;
297-
};
298-
] )
299-
300282
let is_gentype (attr : attr) =
301283
match attr with
302284
| {Location.txt = "genType" | "gentype"; _}, _ -> true

compiler/frontend/ast_attributes.mli

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,6 @@ val get_index : attr
5959

6060
val set : attr
6161

62-
val bs_return_undefined : attr
63-
6462
val internal_expansive : attr
6563
(* val deprecated : string -> attr *)
6664

compiler/frontend/ast_derive_abstract.ml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,7 @@ let is_abstract (xs : Ast_payload.action list) =
5454
(** For this attributes, its type was wrapped as an option,
5555
so we can still reuse existing frame work
5656
*)
57-
let get_optional_attrs =
58-
[Ast_attributes.get; Ast_attributes.bs_return_undefined]
57+
let get_optional_attrs = [Ast_attributes.get]
5958

6059
let get_attrs = []
6160

compiler/frontend/ast_external_process.ml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,6 @@ let init_st =
190190

191191
let return_wrapper loc (txt : string) : External_ffi_types.return_wrapper =
192192
match txt with
193-
| "undefined_to_opt" -> Return_undefined_to_opt
194193
| "null_to_opt" -> Return_null_to_opt
195194
| "nullable" | "null_undefined_to_opt" -> Return_null_undefined_to_opt
196195
| "identity" -> Return_identity
@@ -395,8 +394,7 @@ let check_return_wrapper loc (wrapper : External_ffi_types.return_wrapper)
395394
| Return_unset ->
396395
if Ast_core_type.is_unit result_type then Return_replaced_with_unit
397396
else wrapper
398-
| Return_undefined_to_opt | Return_null_to_opt | Return_null_undefined_to_opt
399-
->
397+
| Return_null_to_opt | Return_null_undefined_to_opt ->
400398
if Ast_core_type.is_user_option result_type then wrapper
401399
else Bs_syntaxerr.err loc Expect_opt_in_bs_return_to_opt
402400
| Return_replaced_with_unit -> assert false

compiler/frontend/external_ffi_types.ml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,6 @@ type external_spec =
9898
type return_wrapper =
9999
| Return_unset
100100
| Return_identity
101-
| Return_undefined_to_opt
102101
| Return_null_to_opt
103102
| Return_null_undefined_to_opt
104103
| Return_replaced_with_unit

compiler/frontend/external_ffi_types.mli

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ type external_spec =
7474
type return_wrapper =
7575
| Return_unset
7676
| Return_identity
77-
| Return_undefined_to_opt
7877
| Return_null_to_opt
7978
| Return_null_undefined_to_opt
8079
| Return_replaced_with_unit

compiler/ml/lambda.ml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,6 @@ type primitive =
295295
(* js *)
296296
| Pcurry_apply of int
297297
| Pjscomp of comparison
298-
| Pundefined_to_opt
299298
| Pnull_to_opt
300299
| Pnullable_to_opt
301300
| Pis_not_none

compiler/ml/lambda.mli

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,6 @@ type primitive =
264264
(* js *)
265265
| Pcurry_apply of int
266266
| Pjscomp of comparison
267-
| Pundefined_to_opt
268267
| Pnull_to_opt
269268
| Pnullable_to_opt
270269
| Pis_not_none

compiler/ml/printlambda.ml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,6 @@ let primitive ppf = function
252252
| Pjscomp Cle -> fprintf ppf "<="
253253
| Pjscomp Cgt -> fprintf ppf ">"
254254
| Pjscomp Cge -> fprintf ppf ">="
255-
| Pundefined_to_opt -> fprintf ppf "undefined_to_opt"
256255
| Pnull_to_opt -> fprintf ppf "null_to_opt"
257256
| Pnullable_to_opt -> fprintf ppf "nullable_to_opt"
258257
| Pis_not_none -> fprintf ppf "#is_not_none"

compiler/ml/translcore.ml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,6 @@ let primitives_table =
372372
("%null", Pnull);
373373
("%undefined", Pundefined);
374374
("%is_nullable", Pisnullable);
375-
("%undefined_to_opt", Pundefined_to_opt);
376375
("%null_to_opt", Pnull_to_opt);
377376
("%nullable_to_opt", Pnullable_to_opt);
378377
("%function_arity", Pfn_arity);
@@ -397,7 +396,6 @@ let primitives_table =
397396
("#is_nullable", Pisnullable);
398397
("#null_to_opt", Pnull_to_opt);
399398
("#nullable_to_opt", Pnullable_to_opt);
400-
("#undefined_to_opt", Pundefined_to_opt);
401399
("#makemutablelist", Pmakelist Mutable);
402400
("#import", Pimport);
403401
(* FIXME: Deprecated *)

lib/es6/Js.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11

22

3+
import * as Primitive_option from "./Primitive_option.js";
34

45
let Null;
56

@@ -67,6 +68,8 @@ let $$Map;
6768

6869
let $$WeakMap;
6970

71+
let undefinedToOption = Primitive_option.fromUndefined;
72+
7073
export {
7174
Null,
7275
Undefined,
@@ -101,5 +104,6 @@ export {
101104
$$WeakSet,
102105
$$Map,
103106
$$WeakMap,
107+
undefinedToOption,
104108
}
105109
/* No side effect */

lib/es6/Js_array.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11

22

3-
import * as Primitive_option from "./Primitive_option.js";
43

54
function copyWithin(to_, obj) {
65
return obj.copyWithin(to_);
@@ -115,11 +114,11 @@ function filteri(arg1, obj) {
115114
}
116115

117116
function find(arg1, obj) {
118-
return Primitive_option.fromUndefined(obj.find(arg1));
117+
return obj.find(arg1);
119118
}
120119

121120
function findi(arg1, obj) {
122-
return Primitive_option.fromUndefined(obj.find(arg1));
121+
return obj.find(arg1);
123122
}
124123

125124
function findIndex(arg1, obj) {

lib/es6/Js_undefined.js

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,25 @@ function testAny(x) {
1111
}
1212

1313
function getExn(f) {
14-
if (f !== undefined) {
15-
return f;
14+
let x = Primitive_option.fromUndefined(f);
15+
if (x !== undefined) {
16+
return Primitive_option.valFromOption(x);
1617
}
1718
throw new Error("Js.Undefined.getExn");
1819
}
1920

2021
function bind(x, f) {
21-
if (x !== undefined) {
22-
return f(x);
22+
let x$1 = Primitive_option.fromUndefined(x);
23+
if (x$1 !== undefined) {
24+
return f(Primitive_option.valFromOption(x$1));
2325
}
2426

2527
}
2628

2729
function iter(x, f) {
28-
if (x !== undefined) {
29-
return f(x);
30+
let x$1 = Primitive_option.fromUndefined(x);
31+
if (x$1 !== undefined) {
32+
return f(Primitive_option.valFromOption(x$1));
3033
}
3134

3235
}
@@ -40,6 +43,10 @@ function fromOption(x) {
4043

4144
let from_opt = fromOption;
4245

46+
let toOption = Primitive_option.fromUndefined;
47+
48+
let to_opt = Primitive_option.fromUndefined;
49+
4350
export {
4451
test,
4552
testAny,
@@ -48,5 +55,7 @@ export {
4855
iter,
4956
fromOption,
5057
from_opt,
58+
toOption,
59+
to_opt,
5160
}
5261
/* No side effect */

lib/js/Js.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
'use strict';
22

3+
let Primitive_option = require("./Primitive_option.js");
34

45
let Null;
56

@@ -67,6 +68,8 @@ let $$Map;
6768

6869
let $$WeakMap;
6970

71+
let undefinedToOption = Primitive_option.fromUndefined;
72+
7073
exports.Null = Null;
7174
exports.Undefined = Undefined;
7275
exports.Nullable = Nullable;
@@ -100,4 +103,5 @@ exports.$$Set = $$Set;
100103
exports.$$WeakSet = $$WeakSet;
101104
exports.$$Map = $$Map;
102105
exports.$$WeakMap = $$WeakMap;
106+
exports.undefinedToOption = undefinedToOption;
103107
/* No side effect */

0 commit comments

Comments
 (0)