Skip to content

Remove @return(undefined_to_opt) and %undefined_to_opt primitive #7462

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
12 changes: 3 additions & 9 deletions compiler/core/lam.ml
Original file line number Diff line number Diff line change
@@ -298,8 +298,8 @@ let rec apply ?(ap_transformed_jsx = false) fn args (ap_info : ap_info) : t =
Lprim
{
primitive =
( Pundefined_to_opt | Pnull_to_opt | Pnull_undefined_to_opt
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removing the primitive to convert undefined to option makes sense, as the undefined type is de-emphasized (no Stdlib_undefined module), so it would be weird to special case this conversion in the compiler.

| Pis_null | Pis_null_undefined | Ptypeof ) as wrap;
( Pnull_to_opt | Pnull_undefined_to_opt | Pis_null
| Pis_null_undefined | Ptypeof ) as wrap;
args =
[Lprim ({primitive = _; args = inner_args} as primitive_call)];
};
@@ -442,11 +442,7 @@ let rec seq (a : t) b : t =
match a with
| Lprim {primitive = Pmakeblock _; args = x :: xs} ->
seq (Ext_list.fold_left xs x seq) b
| Lprim
{
primitive = Pnull_to_opt | Pundefined_to_opt | Pnull_undefined_to_opt;
args = [a];
} ->
| Lprim {primitive = Pnull_to_opt | Pnull_undefined_to_opt; args = [a]} ->
seq a b
| _ -> Lsequence (a, b)

@@ -719,8 +715,6 @@ let result_wrap loc (result_type : External_ffi_types.return_wrapper) result =
| Return_null_to_opt -> prim ~primitive:Pnull_to_opt ~args:[result] loc
| Return_null_undefined_to_opt ->
prim ~primitive:Pnull_undefined_to_opt ~args:[result] loc
| Return_undefined_to_opt ->
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removing @return(undefined_to_opt) sounds good, as it does not really have a clear use case.

prim ~primitive:Pundefined_to_opt ~args:[result] loc
| Return_unset | Return_identity -> result

let handle_bs_non_obj_ffi ?(transformed_jsx = false)
4 changes: 2 additions & 2 deletions compiler/core/lam_analysis.ml
Original file line number Diff line number Diff line change
@@ -46,8 +46,8 @@ let rec no_side_effects (lam : Lam.t) : bool =
| _ -> false)
| Pcreate_extension _ | Ptypeof | Pis_null | Pis_not_none | Psome
| Psome_not_nest | Pis_undefined | Pis_null_undefined | Pnull_to_opt
| Pundefined_to_opt | Pnull_undefined_to_opt | Pjs_fn_make _
| Pjs_fn_make_unit | Pjs_object_create _ | Pimport
| Pnull_undefined_to_opt | Pjs_fn_make _ | Pjs_fn_make_unit
| Pjs_object_create _ | Pimport
(* TODO: check *)
| Pmakeblock _
(* whether it's mutable or not *)
7 changes: 0 additions & 7 deletions compiler/core/lam_compile_primitive.ml
Original file line number Diff line number Diff line change
@@ -95,13 +95,6 @@ let translate output_prefix loc (cxt : Lam_compile_context.t)
| Var _ | Undefined _ | Null -> Js_of_lam_option.null_to_opt e
| _ -> E.runtime_call Primitive_modules.option "fromNull" args)
| _ -> assert false)
| Pundefined_to_opt -> (
match args with
| [e] -> (
match e.expression_desc with
| Var _ | Undefined _ | Null -> Js_of_lam_option.undef_to_opt e
| _ -> E.runtime_call Primitive_modules.option "fromUndefined" args)
| _ -> assert false)
| Pnull_undefined_to_opt -> (
match args with
| [e] -> (
1 change: 0 additions & 1 deletion compiler/core/lam_convert.ml
Original file line number Diff line number Diff line change
@@ -197,7 +197,6 @@ let lam_prim ~primitive:(p : Lambda.primitive) ~args loc : Lam.t =
| Pisnullable -> prim ~primitive:Pis_null_undefined ~args loc
| Pnull_to_opt -> prim ~primitive:Pnull_to_opt ~args loc
| Pnullable_to_opt -> prim ~primitive:Pnull_undefined_to_opt ~args loc
| Pundefined_to_opt -> prim ~primitive:Pundefined_to_opt ~args loc
| Pis_not_none -> prim ~primitive:Pis_not_none ~args loc
| Pval_from_option -> prim ~primitive:Pval_from_option ~args loc
| Pval_from_option_not_nest ->
2 changes: 0 additions & 2 deletions compiler/core/lam_pass_collect.ml
Original file line number Diff line number Diff line change
@@ -73,8 +73,6 @@ let collect_info (meta : Lam_stats.t) (lam : Lam.t) =
(FunctionId {arity = Lam_arity.info [arity] false; lambda = None})
| Lprim {primitive = Pnull_to_opt; args = [(Lvar _ as l)]; _} ->
Hash_ident.replace meta.ident_tbl ident (OptionalBlock (l, Null))
| Lprim {primitive = Pundefined_to_opt; args = [(Lvar _ as l)]; _} ->
Hash_ident.replace meta.ident_tbl ident (OptionalBlock (l, Undefined))
| Lprim {primitive = Pnull_undefined_to_opt; args = [(Lvar _ as l)]} ->
Hash_ident.replace meta.ident_tbl ident
(OptionalBlock (l, Null_undefined))
7 changes: 2 additions & 5 deletions compiler/core/lam_pass_deep_flatten.ml
Original file line number Diff line number Diff line change
@@ -123,8 +123,7 @@ let deep_flatten (lam : Lam.t) : Lam.t =
id,
(Lprim
{
primitive =
Pnull_to_opt | Pundefined_to_opt | Pnull_undefined_to_opt;
primitive = Pnull_to_opt | Pnull_undefined_to_opt;
args = [Lvar _];
} as arg),
body ) ->
@@ -134,9 +133,7 @@ let deep_flatten (lam : Lam.t) : Lam.t =
id,
Lprim
{
primitive =
(Pnull_to_opt | Pundefined_to_opt | Pnull_undefined_to_opt) as
primitive;
primitive = (Pnull_to_opt | Pnull_undefined_to_opt) as primitive;
args = [arg];
},
body ) ->
13 changes: 6 additions & 7 deletions compiler/core/lam_primitive.ml
Original file line number Diff line number Diff line change
@@ -161,7 +161,6 @@ type t =
play safe first
*)
| Pjs_fn_method
| Pundefined_to_opt
| Pnull_to_opt
| Pnull_undefined_to_opt
| Pis_null
@@ -224,12 +223,12 @@ let eq_primitive_approx (lhs : t) (rhs : t) =
| Pawait
(* etc *)
| Pjs_apply | Pjs_runtime_apply | Pval_from_option | Pval_from_option_not_nest
| Pundefined_to_opt | Pnull_to_opt | Pnull_undefined_to_opt | Pis_null
| Pis_not_none | Psome | Psome_not_nest | Pis_undefined | Pis_null_undefined
| Pimport | Ptypeof | Pfn_arity | Pis_poly_var_block | Pdebugger | Pinit_mod
| Pupdate_mod | Pduprecord | Pmakearray | Parraylength | Parrayrefu
| Parraysetu | Parrayrefs | Parraysets | Pjs_fn_make_unit | Pjs_fn_method
| Phash | Phash_mixstring | Phash_mixint | Phash_finalmix ->
| Pnull_to_opt | Pnull_undefined_to_opt | Pis_null | Pis_not_none | Psome
| Psome_not_nest | Pis_undefined | Pis_null_undefined | Pimport | Ptypeof
| Pfn_arity | Pis_poly_var_block | Pdebugger | Pinit_mod | Pupdate_mod
| Pduprecord | Pmakearray | Parraylength | Parrayrefu | Parraysetu
| Parrayrefs | Parraysets | Pjs_fn_make_unit | Pjs_fn_method | Phash
| Phash_mixstring | Phash_mixint | Phash_finalmix ->
rhs = lhs
| Pcreate_extension a -> (
match rhs with
1 change: 0 additions & 1 deletion compiler/core/lam_primitive.mli
Original file line number Diff line number Diff line change
@@ -152,7 +152,6 @@ type t =
| Pjs_fn_make of int
| Pjs_fn_make_unit
| Pjs_fn_method
| Pundefined_to_opt
| Pnull_to_opt
| Pnull_undefined_to_opt
| Pis_null
1 change: 0 additions & 1 deletion compiler/core/lam_print.ml
Original file line number Diff line number Diff line change
@@ -61,7 +61,6 @@ let primitive ppf (prim : Lam_primitive.t) =
| Praw_js_code _ -> fprintf ppf "[raw]"
| Ptypeof -> fprintf ppf "typeof"
| Pnull_to_opt -> fprintf ppf "[null->opt]"
| Pundefined_to_opt -> fprintf ppf "[undefined->opt]"
| Pnull_undefined_to_opt -> fprintf ppf "[null/undefined->opt]"
| Pis_null -> fprintf ppf "[?null]"
| Pis_not_none -> fprintf ppf "[?is-not-none]"
18 changes: 0 additions & 18 deletions compiler/frontend/ast_attributes.ml
Original file line number Diff line number Diff line change
@@ -279,24 +279,6 @@ let set : attr = ({txt = "set"; loc = locg}, Ast_payload.empty)
let internal_expansive : attr =
({txt = "internal.expansive"; loc = locg}, Ast_payload.empty)

let bs_return_undefined : attr =
( {txt = "return"; loc = locg},
PStr
[
{
pstr_desc =
Pstr_eval
( {
pexp_desc =
Pexp_ident {txt = Lident "undefined_to_opt"; loc = locg};
pexp_loc = locg;
pexp_attributes = [];
},
[] );
pstr_loc = locg;
};
] )

let is_gentype (attr : attr) =
match attr with
| {Location.txt = "genType" | "gentype"; _}, _ -> true
2 changes: 0 additions & 2 deletions compiler/frontend/ast_attributes.mli
Original file line number Diff line number Diff line change
@@ -59,8 +59,6 @@ val get_index : attr

val set : attr

val bs_return_undefined : attr

val internal_expansive : attr
(* val deprecated : string -> attr *)

3 changes: 1 addition & 2 deletions compiler/frontend/ast_derive_abstract.ml
Original file line number Diff line number Diff line change
@@ -54,8 +54,7 @@ let is_abstract (xs : Ast_payload.action list) =
(** For this attributes, its type was wrapped as an option,
so we can still reuse existing frame work
*)
let get_optional_attrs =
[Ast_attributes.get; Ast_attributes.bs_return_undefined]
let get_optional_attrs = [Ast_attributes.get]

let get_attrs = []

4 changes: 1 addition & 3 deletions compiler/frontend/ast_external_process.ml
Original file line number Diff line number Diff line change
@@ -190,7 +190,6 @@ let init_st =

let return_wrapper loc (txt : string) : External_ffi_types.return_wrapper =
match txt with
| "undefined_to_opt" -> Return_undefined_to_opt
| "null_to_opt" -> Return_null_to_opt
| "nullable" | "null_undefined_to_opt" -> Return_null_undefined_to_opt
| "identity" -> Return_identity
@@ -407,8 +406,7 @@ let check_return_wrapper loc (wrapper : External_ffi_types.return_wrapper)
| Return_unset ->
if Ast_core_type.is_unit result_type then Return_replaced_with_unit
else wrapper
| Return_undefined_to_opt | Return_null_to_opt | Return_null_undefined_to_opt
->
| Return_null_to_opt | Return_null_undefined_to_opt ->
if Ast_core_type.is_user_option result_type then wrapper
else Bs_syntaxerr.err loc Expect_opt_in_bs_return_to_opt
| Return_replaced_with_unit -> assert false
1 change: 0 additions & 1 deletion compiler/frontend/external_ffi_types.ml
Original file line number Diff line number Diff line change
@@ -98,7 +98,6 @@ type external_spec =
type return_wrapper =
| Return_unset
| Return_identity
| Return_undefined_to_opt
| Return_null_to_opt
| Return_null_undefined_to_opt
| Return_replaced_with_unit
1 change: 0 additions & 1 deletion compiler/frontend/external_ffi_types.mli
Original file line number Diff line number Diff line change
@@ -74,7 +74,6 @@ type external_spec =
type return_wrapper =
| Return_unset
| Return_identity
| Return_undefined_to_opt
| Return_null_to_opt
| Return_null_undefined_to_opt
| Return_replaced_with_unit
1 change: 0 additions & 1 deletion compiler/ml/lambda.ml
Original file line number Diff line number Diff line change
@@ -295,7 +295,6 @@ type primitive =
(* js *)
| Pcurry_apply of int
| Pjscomp of comparison
| Pundefined_to_opt
| Pnull_to_opt
| Pnullable_to_opt
| Pis_not_none
1 change: 0 additions & 1 deletion compiler/ml/lambda.mli
Original file line number Diff line number Diff line change
@@ -264,7 +264,6 @@ type primitive =
(* js *)
| Pcurry_apply of int
| Pjscomp of comparison
| Pundefined_to_opt
| Pnull_to_opt
| Pnullable_to_opt
| Pis_not_none
1 change: 0 additions & 1 deletion compiler/ml/printlambda.ml
Original file line number Diff line number Diff line change
@@ -252,7 +252,6 @@ let primitive ppf = function
| Pjscomp Cle -> fprintf ppf "<="
| Pjscomp Cgt -> fprintf ppf ">"
| Pjscomp Cge -> fprintf ppf ">="
| Pundefined_to_opt -> fprintf ppf "undefined_to_opt"
| Pnull_to_opt -> fprintf ppf "null_to_opt"
| Pnullable_to_opt -> fprintf ppf "nullable_to_opt"
| Pis_not_none -> fprintf ppf "#is_not_none"
2 changes: 0 additions & 2 deletions compiler/ml/translcore.ml
Original file line number Diff line number Diff line change
@@ -372,7 +372,6 @@ let primitives_table =
("%null", Pnull);
("%undefined", Pundefined);
("%is_nullable", Pisnullable);
("%undefined_to_opt", Pundefined_to_opt);
("%null_to_opt", Pnull_to_opt);
("%nullable_to_opt", Pnullable_to_opt);
("%function_arity", Pfn_arity);
@@ -397,7 +396,6 @@ let primitives_table =
("#is_nullable", Pisnullable);
("#null_to_opt", Pnull_to_opt);
("#nullable_to_opt", Pnullable_to_opt);
("#undefined_to_opt", Pundefined_to_opt);
("#makemutablelist", Pmakelist Mutable);
("#import", Pimport);
(* FIXME: Deprecated *)
4 changes: 4 additions & 0 deletions lib/es6/Js.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@


import * as Primitive_option from "./Primitive_option.js";

let Null;

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

let $$WeakMap;

let undefinedToOption = Primitive_option.fromUndefined;

export {
Null,
Undefined,
@@ -101,5 +104,6 @@ export {
$$WeakSet,
$$Map,
$$WeakMap,
undefinedToOption,
}
/* No side effect */
5 changes: 2 additions & 3 deletions lib/es6/Js_array.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@


import * as Primitive_option from "./Primitive_option.js";

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

function find(arg1, obj) {
return Primitive_option.fromUndefined(obj.find(arg1));
return obj.find(arg1);
}

function findi(arg1, obj) {
return Primitive_option.fromUndefined(obj.find(arg1));
return obj.find(arg1);
}

function findIndex(arg1, obj) {
21 changes: 15 additions & 6 deletions lib/es6/Js_undefined.js
Original file line number Diff line number Diff line change
@@ -11,22 +11,25 @@ function testAny(x) {
}

function getExn(f) {
if (f !== undefined) {
return f;
let x = Primitive_option.fromUndefined(f);
if (x !== undefined) {
return Primitive_option.valFromOption(x);
}
throw new Error("Js.Undefined.getExn");
}

function bind(x, f) {
if (x !== undefined) {
return f(x);
let x$1 = Primitive_option.fromUndefined(x);
if (x$1 !== undefined) {
return f(Primitive_option.valFromOption(x$1));
}

}

function iter(x, f) {
if (x !== undefined) {
return f(x);
let x$1 = Primitive_option.fromUndefined(x);
if (x$1 !== undefined) {
return f(Primitive_option.valFromOption(x$1));
}

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

let from_opt = fromOption;

let toOption = Primitive_option.fromUndefined;

let to_opt = Primitive_option.fromUndefined;

export {
test,
testAny,
@@ -48,5 +55,7 @@ export {
iter,
fromOption,
from_opt,
toOption,
to_opt,
}
/* No side effect */
4 changes: 4 additions & 0 deletions lib/js/Js.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';

let Primitive_option = require("./Primitive_option.js");

let Null;

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

let $$WeakMap;

let undefinedToOption = Primitive_option.fromUndefined;

exports.Null = Null;
exports.Undefined = Undefined;
exports.Nullable = Nullable;
@@ -100,4 +103,5 @@ exports.$$Set = $$Set;
exports.$$WeakSet = $$WeakSet;
exports.$$Map = $$Map;
exports.$$WeakMap = $$WeakMap;
exports.undefinedToOption = undefinedToOption;
/* No side effect */
5 changes: 2 additions & 3 deletions lib/js/Js_array.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict';

let Primitive_option = require("./Primitive_option.js");

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

function find(arg1, obj) {
return Primitive_option.fromUndefined(obj.find(arg1));
return obj.find(arg1);
}

function findi(arg1, obj) {
return Primitive_option.fromUndefined(obj.find(arg1));
return obj.find(arg1);
}

function findIndex(arg1, obj) {
Loading