Skip to content
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

WIP: Remove assert keyword #7098

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions compiler/core/lam_analysis.ml
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ let rec no_side_effects (lam : Lam.t) : bool =
(* TODO *)
| Praw_js_code _
(* byte swap *)
| Parraysets | Parraysetu | Poffsetref _ | Praise | Plazyforce | Psetfield _
->
| Parraysets | Parraysetu | Poffsetref _ | Praise | Passert | Plazyforce
| Psetfield _ ->
false)
| Llet (_, _, arg, body) -> no_side_effects arg && no_side_effects body
| Lswitch (_, _) -> false
Expand Down
27 changes: 27 additions & 0 deletions compiler/core/lam_compile.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1523,6 +1523,33 @@ let compile output_prefix =
| Fld_module {name = field} ->
compile_external_field ~dynamic_import lambda_cxt id field
| _ -> assert false)
| {primitive = Passert; args = [e]; _} -> (
match
compile_lambda {lambda_cxt with continuation = NeedValue Not_tail} e
with
| {block; value = Some v} ->
let loc_start = prim_info.loc.loc_start in
let payload =
E.array Js_op.Immutable
[
E.str (loc_start.pos_fname |> Filename.basename);
E.int (Int32.of_int loc_start.pos_lnum);
E.int (Int32.of_int (loc_start.pos_cnum - loc_start.pos_bol));
]
in

let block_expr =
Js_exp_make.make_block payload Blk_extension
(E.str "Assert_failure" :: [payload])
Immutable
in

let else_ = S.throw_stmt block_expr in

Js_output.make
[S.if_ v block ~else_:[else_]]
~value:E.undefined ~output_finished:False
| {value = None} -> assert false)
| {primitive = Praise; args = [e]; _} -> (
match
compile_lambda {lambda_cxt with continuation = NeedValue Not_tail} e
Expand Down
1 change: 1 addition & 0 deletions compiler/core/lam_compile_primitive.ml
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,7 @@ let translate output_prefix loc (cxt : Lam_compile_context.t)
| _ -> assert false)
(* only when Lapply -> expand = true*)
| Praise -> assert false (* handled before here *)
| Passert -> assert false
(* Runtime encoding relevant *)
| Parraylength -> E.array_length (Ext_list.singleton_exn args)
| Psetfield (i, field_info) -> (
Expand Down
1 change: 1 addition & 0 deletions compiler/core/lam_convert.ml
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ let lam_prim ~primitive:(p : Lambda.primitive) ~args loc : Lam.t =
| Pduprecord -> prim ~primitive:Pduprecord ~args loc
| Plazyforce -> prim ~primitive:Plazyforce ~args loc
| Praise _ -> prim ~primitive:Praise ~args loc
| Passert -> prim ~primitive:Passert ~args loc
| Pobjcomp x -> prim ~primitive:(Pobjcomp x) ~args loc
| Pobjorder -> prim ~primitive:Pobjorder ~args loc
| Pobjmin -> prim ~primitive:Pobjmin ~args loc
Expand Down
3 changes: 2 additions & 1 deletion compiler/core/lam_primitive.ml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ type t =
| Pjs_object_create of External_arg_spec.obj_params
(* Exceptions *)
| Praise
| Passert
(* object primitives *)
| Pobjcomp of Lam_compat.comparison
| Pobjorder
Expand Down Expand Up @@ -193,7 +194,7 @@ let eq_tag_info (x : Lam_tag_info.t) y = x = y

let eq_primitive_approx (lhs : t) (rhs : t) =
match lhs with
| Pwrap_exn | Praise
| Pwrap_exn | Praise | Passert
(* generic comparison *)
| Pobjorder | Pobjmin | Pobjmax | Pobjtag | Pobjsize
(* bool primitives *)
Expand Down
1 change: 1 addition & 0 deletions compiler/core/lam_primitive.mli
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ type t =
}
| Pjs_object_create of External_arg_spec.obj_params
| Praise
| Passert
(* object primitives *)
| Pobjcomp of Lam_compat.comparison
| Pobjorder
Expand Down
1 change: 1 addition & 0 deletions compiler/core/lam_print.ml
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ let primitive ppf (prim : Lam_primitive.t) =
| Pjs_call {prim_name} -> fprintf ppf "%s[js]" prim_name
| Pjs_object_create _ -> fprintf ppf "[js.obj]"
| Praise -> fprintf ppf "raise"
| Passert -> fprintf ppf "assert"
| Pobjcomp Ceq -> fprintf ppf "=="
| Pobjcomp Cneq -> fprintf ppf "!="
| Pobjcomp Clt -> fprintf ppf "<"
Expand Down
1 change: 1 addition & 0 deletions compiler/ml/lambda.ml
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ type primitive =
| Pccall of Primitive.description
(* Exceptions *)
| Praise of raise_kind
| Passert
(* object operations *)
| Pobjcomp of comparison
| Pobjorder
Expand Down
1 change: 1 addition & 0 deletions compiler/ml/lambda.mli
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ type primitive =
| Pccall of Primitive.description
(* Exceptions *)
| Praise of raise_kind
| Passert
(* object primitives *)
| Pobjcomp of comparison
| Pobjorder
Expand Down
1 change: 1 addition & 0 deletions compiler/ml/predef.mli
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ val path_promise : Path.t
val path_uncurried : Path.t

val path_match_failure : Path.t

val path_assert_failure : Path.t
val path_undefined_recursive_module : Path.t

Expand Down
1 change: 1 addition & 0 deletions compiler/ml/printlambda.ml
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ let primitive ppf = function
| Plazyforce -> fprintf ppf "force"
| Pccall p -> fprintf ppf "%s" p.prim_name
| Praise k -> fprintf ppf "%s" (Lambda.raise_kind k)
| Passert -> fprintf ppf "assert"
| Pobjcomp Ceq -> fprintf ppf "=="
| Pobjcomp Cneq -> fprintf ppf "!="
| Pobjcomp Clt -> fprintf ppf "<"
Expand Down
1 change: 1 addition & 0 deletions compiler/ml/translcore.ml
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ let primitives_table =
("%obj_get_field", Parrayrefu);
("%obj_set_field", Parraysetu);
("%raise", Praise Raise_regular);
("%assert", Passert);
(* bool primitives *)
("%sequand", Psequand);
("%sequor", Psequor);
Expand Down
6 changes: 0 additions & 6 deletions compiler/syntax/src/res_core.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2147,15 +2147,9 @@ and parse_unary_expr p =
* If you have `a + b`, `a` and `b` both represent
* the operands of the binary expression with opeartor `+` *)
and parse_operand_expr ~context p =
let start_pos = p.Parser.start_pos in
let attrs = ref (parse_attributes p) in
let expr =
match p.Parser.token with
| Assert ->
Parser.next p;
let expr = parse_expr p in
let loc = mk_loc start_pos p.prev_end_pos in
Ast_helper.Exp.assert_ ~loc expr
| Lident "async"
(* we need to be careful when we're in a ternary true branch:
`condition ? ternary-true-branch : false-branch`
Expand Down
18 changes: 9 additions & 9 deletions compiler/syntax/src/res_grammar.ml
Original file line number Diff line number Diff line change
Expand Up @@ -149,10 +149,10 @@ let is_atomic_typ_expr_start = function
| _ -> false

let is_expr_start = function
| Token.Assert | At | Await | Backtick | Bang | Codepoint _ | False | Float _
| For | Hash | If | Int _ | Lbrace | Lbracket | LessThan | Lident _ | List
| Lparen | Minus | MinusDot | Module | Percent | Plus | PlusDot | String _
| Switch | True | Try | Uident _ | Underscore (* _ => doThings() *)
| Token.At | Await | Backtick | Bang | Codepoint _ | False | Float _ | For
| Hash | If | Int _ | Lbrace | Lbracket | LessThan | Lident _ | List | Lparen
| Minus | MinusDot | Module | Percent | Plus | PlusDot | String _ | Switch
| True | Try | Uident _ | Underscore (* _ => doThings() *)
| While | Forwardslash | ForwardslashDot | Dict ->
true
| _ -> false
Expand Down Expand Up @@ -263,11 +263,11 @@ let is_attribute_start = function
let is_jsx_child_start = is_atomic_expr_start

let is_block_expr_start = function
| Token.Assert | At | Await | Backtick | Bang | Codepoint _ | Exception
| False | Float _ | For | Forwardslash | ForwardslashDot | Hash | If | Int _
| Lbrace | Lbracket | LessThan | Let | Lident _ | List | Lparen | Minus
| MinusDot | Module | Open | Percent | Plus | PlusDot | String _ | Switch
| True | Try | Uident _ | Underscore | While | Dict ->
| Token.At | Await | Backtick | Bang | Codepoint _ | Exception | False
| Float _ | For | Forwardslash | ForwardslashDot | Hash | If | Int _ | Lbrace
| Lbracket | LessThan | Let | Lident _ | List | Lparen | Minus | MinusDot
| Module | Open | Percent | Plus | PlusDot | String _ | Switch | True | Try
| Uident _ | Underscore | While | Dict ->
true
| _ -> false

Expand Down
9 changes: 3 additions & 6 deletions compiler/syntax/src/res_token.ml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ type t =
| LessThanSlash
| Hash
| HashEqual
| Assert
| Tilde
| Question
| If
Expand Down Expand Up @@ -167,7 +166,6 @@ let to_string = function
| Asterisk -> "*"
| AsteriskDot -> "*."
| Exponentiation -> "**"
| Assert -> "assert"
| Tilde -> "tilde"
| Question -> "?"
| If -> "if"
Expand Down Expand Up @@ -213,7 +211,6 @@ let to_string = function
let keyword_table = function
| "and" -> And
| "as" -> As
| "assert" -> Assert
| "await" -> Await
| "constraint" -> Constraint
| "else" -> Else
Expand Down Expand Up @@ -243,9 +240,9 @@ let keyword_table = function
[@@raises Not_found]

let is_keyword = function
| Await | And | As | Assert | Constraint | Else | Exception | External | False
| For | If | In | Include | Land | Let | List | Lor | Module | Mutable | Of
| Open | Private | Rec | Switch | True | Try | Typ | When | While | Dict ->
| Await | And | As | Constraint | Else | Exception | External | False | For
| If | In | Include | Land | Let | List | Lor | Module | Mutable | Of | Open
| Private | Rec | Switch | True | Try | Typ | When | While | Dict ->
true
| _ -> false

Expand Down
37 changes: 32 additions & 5 deletions lib/es6/Belt_List.js
Original file line number Diff line number Diff line change
Expand Up @@ -1235,16 +1235,43 @@ function partition(l, p) {
let b = p(h);
partitionAux(p, l.tl, nextX, nextY);
if (b) {
let tmp;
if (nextY) {
tmp = nextY.tl;
} else {
throw {
RE_EXN_ID: "Assert_failure",
_1: [
"Belt_List.res",
818,
20
],
Error: new Error()
};
}
return [
nextX,
nextY.tl
tmp
];
}
let tmp$1;
if (nextX) {
tmp$1 = nextX.tl;
} else {
return [
nextX.tl,
nextY
];
throw {
RE_EXN_ID: "Assert_failure",
_1: [
"Belt_List.res",
825,
20
],
Error: new Error()
};
}
return [
tmp$1,
nextY
];
}

function unzip(xs) {
Expand Down
Loading