Skip to content

Commit 4666595

Browse files
committed
Add test for pattern matching exceptions.
1 parent b87df8c commit 4666595

File tree

4 files changed

+66
-8
lines changed

4 files changed

+66
-8
lines changed

jscomp/test/variant.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
'use strict';
22

33
var Block = require("../../lib/js/block.js");
4+
var Curry = require("../../lib/js/curry.js");
45
var Caml_obj = require("../../lib/js/caml_obj.js");
6+
var Caml_exceptions = require("../../lib/js/caml_exceptions.js");
7+
var Caml_js_exceptions = require("../../lib/js/caml_js_exceptions.js");
58
var Caml_builtin_exceptions = require("../../lib/js/caml_builtin_exceptions.js");
69

710
function foo(param) {
@@ -100,6 +103,39 @@ function rollback_path(subst, p) {
100103
}
101104
}
102105

106+
var EA1 = Caml_exceptions.create("Variant.EA1");
107+
108+
var EA2 = Caml_exceptions.create("Variant.EA2");
109+
110+
var EB = Caml_exceptions.create("Variant.EB");
111+
112+
var EC = Caml_exceptions.create("Variant.EC");
113+
114+
var ED = Caml_exceptions.create("Variant.ED");
115+
116+
function fooExn(f) {
117+
try {
118+
return Curry._1(f, /* () */0);
119+
}
120+
catch (raw_exn){
121+
var exn = Caml_js_exceptions.internalToOCamlException(raw_exn);
122+
if (exn === EA1) {
123+
return 1;
124+
} else if (exn === EA2) {
125+
return 2;
126+
} else if (exn[0] === EB) {
127+
return exn[1];
128+
} else if (exn[0] === EC) {
129+
return exn[1] + exn[2] | 0;
130+
} else if (exn[0] === ED) {
131+
var match = exn[1];
132+
return match[0] + match[1] | 0;
133+
} else {
134+
throw exn;
135+
}
136+
}
137+
}
138+
103139
var a1 = /* A1 */0;
104140

105141
var a2 = /* A2 */1;
@@ -129,4 +165,10 @@ exports.Path = Path;
129165
exports.Make = Make;
130166
exports.M = M;
131167
exports.rollback_path = rollback_path;
168+
exports.EA1 = EA1;
169+
exports.EA2 = EA2;
170+
exports.EB = EB;
171+
exports.EC = EC;
172+
exports.ED = ED;
173+
exports.fooExn = fooExn;
132174
/* No side effect */

jscomp/test/variant.ml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,17 @@ let rollback_path subst p =
5555
match p with
5656
Pident _ | Papply _ -> "Pident | Papply"
5757
| Pdot _ -> "Pdot"
58+
59+
60+
exception EA1
61+
exception EA2
62+
exception EB of int
63+
exception EC of int * int
64+
exception ED of pair
65+
66+
let fooExn f = try f () with
67+
| EA1 -> 1
68+
| EA2 -> 2
69+
| EB n -> n
70+
| EC (n,m) -> n+m
71+
| ED (n,m) -> n+m

lib/4.02.3/unstable/js_compiler.ml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89177,7 +89177,7 @@ let make_clusters ({cases=cases ; actions=actions} as s) n_clusters k sw_names =
8917789177
end ;
8917889178
if i > 0 then zyva (i-1) (ir-1) in
8917989179

89180-
zyva (len-1) (n_clusters-1);
89180+
zyva (len-1) (n_clusters-1) ;
8918189181
let acts = Array.make !index (fun _ -> assert false) in
8918289182
Hashtbl.iter (fun _ (i,act) -> acts.(i) <- act) t ;
8918389183
{cases = r ; actions = acts}
@@ -92414,12 +92414,13 @@ and do_compile_matching repr partial ctx arg pmh = match pmh with
9241492414
| _ -> assert false)
9241592415
| {type_kind = Type_abstract; type_manifest = None} ->
9241692416
(* Format.eprintf "XXX Type_abstract@."; *)
92417-
Some {consts=[||]; blocks=[||]}
92417+
assert false
9241892418
| {type_kind = Type_record _} ->
9241992419
(* Format.eprintf "XXX Type_record@."; *)
92420-
Some {consts=[||]; blocks=[||]}
92420+
assert false
9242192421
| {type_kind = Type_open } ->
92422-
(* Format.eprintf "XXX Type_open@."; *)
92422+
(* Exceptions *)
92423+
(* Format.eprintf "XXX Type_open %s.@." cstr.cstr_name; *)
9242392424
Some {consts=[||]; blocks=[||]} in
9242492425

9242592426
let names = match (Btype.repr pat.pat_type).desc with

lib/4.02.3/whole_compiler.ml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76697,7 +76697,7 @@ let make_clusters ({cases=cases ; actions=actions} as s) n_clusters k sw_names =
7669776697
end ;
7669876698
if i > 0 then zyva (i-1) (ir-1) in
7669976699

76700-
zyva (len-1) (n_clusters-1);
76700+
zyva (len-1) (n_clusters-1) ;
7670176701
let acts = Array.make !index (fun _ -> assert false) in
7670276702
Hashtbl.iter (fun _ (i,act) -> acts.(i) <- act) t ;
7670376703
{cases = r ; actions = acts}
@@ -79934,12 +79934,13 @@ and do_compile_matching repr partial ctx arg pmh = match pmh with
7993479934
| _ -> assert false)
7993579935
| {type_kind = Type_abstract; type_manifest = None} ->
7993679936
(* Format.eprintf "XXX Type_abstract@."; *)
79937-
Some {consts=[||]; blocks=[||]}
79937+
assert false
7993879938
| {type_kind = Type_record _} ->
7993979939
(* Format.eprintf "XXX Type_record@."; *)
79940-
Some {consts=[||]; blocks=[||]}
79940+
assert false
7994179941
| {type_kind = Type_open } ->
79942-
(* Format.eprintf "XXX Type_open@."; *)
79942+
(* Exceptions *)
79943+
(* Format.eprintf "XXX Type_open %s.@." cstr.cstr_name; *)
7994379944
Some {consts=[||]; blocks=[||]} in
7994479945

7994579946
let names = match (Btype.repr pat.pat_type).desc with

0 commit comments

Comments
 (0)