Skip to content

Commit c282587

Browse files
committed
remove intenral usage of %assert
1 parent f5e9b82 commit c282587

Some content is hidden

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

44 files changed

+275
-350
lines changed

Diff for: jscomp/main/builtin_cmj_datasets.ml

+4-4
Large diffs are not rendered by default.

Diff for: jscomp/others/belt_List.ml

+5-20
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ let head x =
9292

9393
let headExn x =
9494
match x with
95-
| [] -> [%assert "headExn"]
95+
| [] -> raise Not_found
9696
| x::_ -> x
9797

9898
let tail x =
@@ -102,7 +102,7 @@ let tail x =
102102

103103
let tailExn x =
104104
match x with
105-
| [] -> [%assert "tailExn"]
105+
| [] -> raise Not_found
106106
| _::t -> t
107107

108108
let add xs x = x :: xs
@@ -116,14 +116,14 @@ let rec nthAux x n =
116116
let rec nthAuxAssert x n =
117117
match x with
118118
| h::t -> if n = 0 then h else nthAuxAssert t (n - 1)
119-
| _ -> [%assert "getExn"]
119+
| _ -> raise Not_found
120120

121121
let get x n =
122122
if n < 0 then None
123123
else nthAux x n
124124

125125
let getExn x n =
126-
if n < 0 then [%assert "getExn"]
126+
if n < 0 then raise Not_found
127127
else nthAuxAssert x n
128128

129129
let rec partitionAux p cell precX precY =
@@ -447,22 +447,7 @@ let shuffle xs =
447447
2. raise JS exception, how to pattern match
448448
*)
449449

450-
(* let fromJson j f = *)
451-
(* match J.decodeArray j with *)
452-
(* | Some arr -> *)
453-
(* let len = Belt_Array.length arr in *)
454-
(* if len = 0 then [] *)
455-
(* else *)
456-
(* let head = (mutableCell (f (A.getUnsafe arr 0) [@bs]) []) in *)
457-
(* let cell = ref head in *)
458-
(* for i = 1 to len - 1 do *)
459-
(* let next = mutableCell (f (A.getUnsafe arr i) [@bs]) [] in *)
460-
(* unsafeMutateTail.unsafeMutateTail !cell next ; *)
461-
(* cell .contents<- next *)
462-
(* done ; *)
463-
(* head *)
464-
(* | None -> *)
465-
(* [%assert "Not array when decoding list"] *)
450+
466451

467452
let rec reverseConcat l1 l2 =
468453
match l1 with

Diff for: jscomp/others/belt_MutableQueue.ml

+2-2
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ let peekUndefined q =
7171

7272
let peekExn q =
7373
match q.first with
74-
| None -> [%assert "Belt.Queue.Empty"]
74+
| None -> raise Not_found
7575
| Some v -> v.content
7676

7777
let pop q =
@@ -92,7 +92,7 @@ let pop q =
9292

9393
let popExn q = (* TO fix *)
9494
match q.first with
95-
| None -> [%assert "Empty"]
95+
| None -> raise Not_found
9696
| Some x ->
9797
let next = x.next in
9898
if next = None then

Diff for: jscomp/others/belt_Option.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ let forEach opt f = forEachU opt (fun[@bs] x -> f x)
3030

3131
let getExn = function
3232
| Some x -> x
33-
| None -> [%assert "getExn"]
33+
| None -> raise Not_found
3434

3535
external getUnsafe : 'a option -> 'a = "%identity"
3636

Diff for: jscomp/others/belt_Result.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ type ('a,'b) t = Ok of 'a | Error of 'b
2626

2727
let getExn = function
2828
| Ok x -> x
29-
| Error _ -> [%assert "getExn"]
29+
| Error _ -> raise Not_found
3030

3131
let mapWithDefaultU opt default f = match opt with
3232
| Ok x -> (f x [@bs])

Diff for: jscomp/others/belt_internalAVLset.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -529,7 +529,7 @@ let rec getUndefined (n : _ t) x ~cmp =
529529

530530
let rec getExn (n : _ t) x ~cmp =
531531
match n with
532-
None -> [%assert "getExn0"]
532+
None -> raise Not_found
533533
| Some t (* Node(l, v, r, _) *) ->
534534
let v = t.value in
535535
let c = (Belt_Id.getCmpInternal cmp) x v [@bs] in

Diff for: jscomp/others/belt_internalAVLtree.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -604,7 +604,7 @@ let rec getUndefined n x ~cmp =
604604
let rec getExn n x ~cmp =
605605
match n with
606606
None ->
607-
[%assert "getExn0"]
607+
raise Not_found
608608
| Some n (* Node(l, v, d, r, _)*) ->
609609
let v = n.key in
610610
let c = (Belt_Id.getCmpInternal cmp) x v [@bs] in

Diff for: jscomp/others/belt_internalMapInt.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ let rec getUndefined n (x : key) =
4646

4747
let rec getExn n (x : key) =
4848
match n with
49-
| None -> [%assert "getExn"]
49+
| None -> raise Not_found
5050
| Some n ->
5151
let v = n.N.key in
5252
if x = v then n.N.value

Diff for: jscomp/others/belt_internalMapString.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ let rec getUndefined n (x : key) =
4646

4747
let rec getExn n (x : key) =
4848
match n with
49-
| None -> [%assert "getExn"]
49+
| None -> raise Not_found
5050
| Some n ->
5151
let v = n.N.key in
5252
if x = v then n.N.value

Diff for: jscomp/others/belt_internalSetInt.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ let rec getUndefined (n :t) (x : value) =
8282

8383
let rec getExn (n :t) (x : value) =
8484
match n with
85-
| None -> [%assert "getExn"]
85+
| None -> raise Not_found
8686
| Some t ->
8787
let v = t.value in
8888
if x = v then v

Diff for: jscomp/others/belt_internalSetString.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ let rec getUndefined (n :t) (x : value) =
8282

8383
let rec getExn (n :t) (x : value) =
8484
match n with
85-
| None -> [%assert "getExn"]
85+
| None -> raise Not_found
8686
| Some t ->
8787
let v = t.value in
8888
if x = v then v

Diff for: jscomp/others/internal_map.cppo.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ let rec getUndefined n (x : key) =
4949

5050
let rec getExn n (x : key) =
5151
match n with
52-
| None -> [%assert "getExn"]
52+
| None -> raise Not_found
5353
| Some n ->
5454
let v = n.N.key in
5555
if x = v then n.N.value

Diff for: jscomp/others/internal_set.cppo.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ let rec getUndefined (n :t) (x : value) =
8787

8888
let rec getExn (n :t) (x : value) =
8989
match n with
90-
| None -> [%assert "getExn"]
90+
| None -> raise Not_found
9191
| Some t ->
9292
let v = t.value in
9393
if x = v then v

Diff for: jscomp/syntax/ast_derive_js_mapper.ml

+3-6
Original file line numberDiff line numberDiff line change
@@ -165,12 +165,9 @@ let raiseWhenNotFound x =
165165
txt = Longident.Ldot (jsMapperRt,"raiseWhenNotFound")})
166166
x
167167
let assertExp e =
168-
Exp.extension
169-
({Asttypes.loc = noloc; txt = "assert"},
170-
(PStr
171-
[Str.eval e ]
172-
)
173-
)
168+
Exp.assert_
169+
e
170+
174171
let derivingName = "jsConverter"
175172

176173
(* let notApplicable loc =

Diff for: jscomp/syntax/ast_exp_extension.ml

-59
Original file line numberDiff line numberDiff line change
@@ -88,65 +88,6 @@ let handle_extension record_as_js_object e (self : Bs_ast_mapper.mapper)
8888
Location.raise_errorf
8989
~loc "expect a boolean expression in the payload"
9090
)
91-
| "bs.assert" | "assert" ->
92-
(
93-
match payload with
94-
| PStr [ {pstr_desc = Pstr_eval( e,_)}] ->
95-
96-
let locString =
97-
if loc.loc_ghost then
98-
"ASSERT FAILURE"
99-
else
100-
let loc_start = loc.loc_start in
101-
let (file, lnum, cnum) = Location.get_pos_info loc_start in
102-
let file = Filename.basename file in
103-
let enum =
104-
loc.Location.loc_end.Lexing.pos_cnum -
105-
loc_start.Lexing.pos_cnum + cnum in
106-
Printf.sprintf "File %S, line %d, characters %d-%d"
107-
file lnum cnum enum in
108-
let raiseWithString locString =
109-
Ast_compatible.app1 ~loc
110-
(Exp.ident ~loc {loc; txt =
111-
Ldot(Ldot (Lident "Js","Exn"),"raiseError")})
112-
(Ast_compatible.const_exp_string locString)
113-
in
114-
(match e.pexp_desc with
115-
| Pexp_construct({txt = Lident "false"},None) ->
116-
(* The backend will convert [assert false] into a nop later *)
117-
if !Clflags.no_assert_false then
118-
Exp.assert_ ~loc
119-
(Exp.construct ~loc {txt = Lident "false";loc} None)
120-
else
121-
(raiseWithString locString)
122-
| Pexp_constant (
123-
Pconst_string
124-
(r, _)) ->
125-
if !Clflags.noassert then
126-
Exp.assert_ ~loc (Exp.construct ~loc {txt = Lident "true"; loc} None)
127-
(* Need special handling to make it type check*)
128-
else
129-
raiseWithString r
130-
| _ ->
131-
let e = self.expr self e in
132-
if !Clflags.noassert then
133-
(* pass down so that it still type check, but the backend will
134-
make it a nop
135-
*)
136-
Exp.assert_ ~loc e
137-
else
138-
Exp.ifthenelse ~loc
139-
(Ast_compatible.app1 ~loc
140-
(Exp.ident {loc ; txt = Ldot(Lident "Pervasives","not")})
141-
e
142-
)
143-
(raiseWithString locString)
144-
None
145-
)
146-
| _ ->
147-
Location.raise_errorf
148-
~loc "expect a boolean expression in the payload"
149-
)
15091
| "bs.node" | "node" ->
15192
let strip s =
15293
match s with

Diff for: jscomp/test/ast_abstract_test.js

+27-3
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,15 @@ function bToJs(param) {
111111

112112
function bFromJs(param) {
113113
if (!(param <= 3 && 0 <= param)) {
114-
throw new Error("ASSERT FAILURE");
114+
throw {
115+
RE_EXN_ID: "Assert_failure",
116+
_1: [
117+
"_none_",
118+
1,
119+
-1
120+
],
121+
Error: new Error()
122+
};
115123
}
116124
return param - 0 | 0;
117125
}
@@ -138,7 +146,15 @@ function cToJs(param) {
138146

139147
function cFromJs(param) {
140148
if (!(param <= 6 && 3 <= param)) {
141-
throw new Error("ASSERT FAILURE");
149+
throw {
150+
RE_EXN_ID: "Assert_failure",
151+
_1: [
152+
"_none_",
153+
1,
154+
-1
155+
],
156+
Error: new Error()
157+
};
142158
}
143159
return param - 3 | 0;
144160
}
@@ -163,7 +179,15 @@ function hToJs(param) {
163179

164180
function hFromJs(param) {
165181
if (!(param <= 1 && 0 <= param)) {
166-
throw new Error("ASSERT FAILURE");
182+
throw {
183+
RE_EXN_ID: "Assert_failure",
184+
_1: [
185+
"_none_",
186+
1,
187+
-1
188+
],
189+
Error: new Error()
190+
};
167191
}
168192
return param - 0 | 0;
169193
}

Diff for: jscomp/test/ast_mapper_defensive_test.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,15 @@ function aToJs(param) {
3434

3535
function aFromJs(param) {
3636
if (!(param <= 2 && 0 <= param)) {
37-
throw new Error("ASSERT FAILURE");
37+
throw {
38+
RE_EXN_ID: "Assert_failure",
39+
_1: [
40+
"_none_",
41+
1,
42+
-1
43+
],
44+
Error: new Error()
45+
};
3846
}
3947
return param - 0 | 0;
4048
}

Diff for: jscomp/test/bs_array_test.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,15 @@ function addone(x) {
390390

391391
function makeMatrixExn(sx, sy, init) {
392392
if (!(sx >= 0 && sy >= 0)) {
393-
throw new Error("File \"bs_array_test.ml\", line 109, characters 4-10");
393+
throw {
394+
RE_EXN_ID: "Assert_failure",
395+
_1: [
396+
"bs_array_test.ml",
397+
109,
398+
2
399+
],
400+
Error: new Error()
401+
};
394402
}
395403
var res = new Array(sx);
396404
for(var x = 0; x < sx; ++x){

Diff for: jscomp/test/bs_array_test.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ let addone = fun [@bs] x -> x + 1
106106

107107
let makeMatrixExn sx sy init =
108108
let open A in
109-
[%assert sx >=0 && sy >=0 ];
109+
assert (sx >=0 && sy >=0 );
110110
let res = makeUninitializedUnsafe sx in
111111
for x = 0 to sx - 1 do
112112
let initY = makeUninitializedUnsafe sy in

0 commit comments

Comments
 (0)