Skip to content

Commit 88c9a20

Browse files
authored
Differentiate exception extensions (rescript-lang#6954)
* Differentiate exception extensions * Update extension tests
1 parent 76e09cc commit 88c9a20

9 files changed

+92
-30
lines changed

jscomp/ml/ctype.ml

+1
Original file line numberDiff line numberDiff line change
@@ -4172,6 +4172,7 @@ let nondep_extension_constructor env mid ext =
41724172
ext_private = ext.ext_private;
41734173
ext_attributes = ext.ext_attributes;
41744174
ext_loc = ext.ext_loc;
4175+
ext_is_exception = ext.ext_is_exception;
41754176
}
41764177
with Not_found ->
41774178
clear_hash ();

jscomp/ml/env.ml

+1-2
Original file line numberDiff line numberDiff line change
@@ -1777,7 +1777,6 @@ and store_extension ~check id ext env =
17771777
if check && not loc.Location.loc_ghost &&
17781778
Warnings.is_active (Warnings.Unused_extension ("", false, false, false))
17791779
then begin
1780-
let is_exception = Path.same ext.ext_type_path Predef.path_exn in
17811780
let ty = Path.last ext.ext_type_path in
17821781
let n = Ident.name id in
17831782
let k = (ty, loc, n) in
@@ -1789,7 +1788,7 @@ and store_extension ~check id ext env =
17891788
if not (is_in_signature env) && not used.cu_positive then
17901789
Location.prerr_warning loc
17911790
(Warnings.Unused_extension
1792-
(n, is_exception, used.cu_pattern, used.cu_privatize)
1791+
(n, ext.ext_is_exception, used.cu_pattern, used.cu_privatize)
17931792
)
17941793
)
17951794
end;

jscomp/ml/predef.ml

+12-11
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ let common_initial_env add_type add_extension empty_env =
276276
type_variance = [Variance.covariant]}
277277
in
278278

279-
let add_extension id l =
279+
let add_exception id l =
280280
add_extension id
281281
{ ext_type_path = path_exn;
282282
ext_type_params = [];
@@ -286,19 +286,20 @@ let common_initial_env add_type add_extension empty_env =
286286
ext_loc = Location.none;
287287
ext_attributes = [{Asttypes.txt="ocaml.warn_on_literal_pattern";
288288
loc=Location.none},
289-
Parsetree.PStr[]] }
289+
Parsetree.PStr[]];
290+
ext_is_exception = true }
290291
in
291-
add_extension ident_match_failure
292+
add_exception ident_match_failure
292293
[newgenty (Ttuple[type_string; type_int; type_int])] (
293-
add_extension ident_invalid_argument [type_string] (
294-
add_extension ident_js_error [type_unknown] (
295-
add_extension ident_failure [type_string] (
296-
add_extension ident_not_found [] (
297-
add_extension ident_end_of_file [] (
298-
add_extension ident_division_by_zero [] (
299-
add_extension ident_assert_failure
294+
add_exception ident_invalid_argument [type_string] (
295+
add_exception ident_js_error [type_unknown] (
296+
add_exception ident_failure [type_string] (
297+
add_exception ident_not_found [] (
298+
add_exception ident_end_of_file [] (
299+
add_exception ident_division_by_zero [] (
300+
add_exception ident_assert_failure
300301
[newgenty (Ttuple[type_string; type_int; type_int])] (
301-
add_extension ident_undefined_recursive_module
302+
add_exception ident_undefined_recursive_module
302303
[newgenty (Ttuple[type_string; type_int; type_int])] (
303304
add_type ident_int64 decl_abstr (
304305
add_type ident_bigint decl_abstr (

jscomp/ml/subst.ml

+2-1
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,8 @@ let extension_constructor s ext =
313313
ext_ret_type = may_map (typexp s) ext.ext_ret_type;
314314
ext_private = ext.ext_private;
315315
ext_attributes = attrs s ext.ext_attributes;
316-
ext_loc = if s.for_saving then Location.none else ext.ext_loc; }
316+
ext_loc = if s.for_saving then Location.none else ext.ext_loc;
317+
ext_is_exception = ext.ext_is_exception; }
317318
in
318319
cleanup_types ();
319320
ext

jscomp/ml/typedecl.ml

+8-6
Original file line numberDiff line numberDiff line change
@@ -1556,21 +1556,23 @@ let transl_extension_constructor env type_path type_params
15561556
in
15571557
args, ret_type, Text_rebind(path, lid)
15581558
in
1559+
let is_exception = Path.same type_path Predef.path_exn in
15591560
let ext =
1560-
{ ext_type_path = type_path;
1561+
{ Types.ext_type_path = type_path;
15611562
ext_type_params = typext_params;
15621563
ext_args = args;
15631564
ext_ret_type = ret_type;
15641565
ext_private = priv;
1565-
Types.ext_loc = sext.pext_loc;
1566-
Types.ext_attributes = sext.pext_attributes; }
1566+
ext_loc = sext.pext_loc;
1567+
ext_attributes = sext.pext_attributes;
1568+
ext_is_exception = is_exception; }
15671569
in
1568-
{ ext_id = id;
1570+
{ Typedtree.ext_id = id;
15691571
ext_name = sext.pext_name;
15701572
ext_type = ext;
15711573
ext_kind = kind;
1572-
Typedtree.ext_loc = sext.pext_loc;
1573-
Typedtree.ext_attributes = sext.pext_attributes; }
1574+
ext_loc = sext.pext_loc;
1575+
ext_attributes = sext.pext_attributes; }
15741576

15751577
let transl_extension_constructor env type_path type_params
15761578
typext_params priv sext =

jscomp/ml/types.ml

+2-1
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,8 @@ type extension_constructor =
198198
ext_ret_type: type_expr option;
199199
ext_private: private_flag;
200200
ext_loc: Location.t;
201-
ext_attributes: Parsetree.attributes; }
201+
ext_attributes: Parsetree.attributes;
202+
ext_is_exception: bool; }
202203

203204
and type_transparence =
204205
Type_public (* unrestricted expansion *)

jscomp/ml/types.mli

+1
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,7 @@ type extension_constructor =
350350
ext_private: private_flag;
351351
ext_loc: Location.t;
352352
ext_attributes: Parsetree.attributes;
353+
ext_is_exception: bool;
353354
}
354355

355356
and type_transparence =

jscomp/test/record_extension_test.js

+54-6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

jscomp/test/record_extension_test.res

+11-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* for o in jscomp/test/*test.js ; do npx mocha $o ; done */*/
1+
/* for o in jscomp/test/*test.js ; do npx mocha $o ; done */ */
22

33
let suites: ref<Mt.pair_suites> = ref(list{})
44
let test_id = ref(0)
@@ -7,15 +7,19 @@ let eq = (loc, x, y) => Mt.eq_suites(~test_id, ~suites, loc, x, y)
77
/* Record_extension */
88
type t0 = ..
99
type t0 += Inline_record({x: int, y: string})
10+
type t0 += SinglePayload(string) | TuplePayload(int, string)
1011

1112
let f = x =>
1213
switch x {
1314
| Inline_record({x, y}) => Some(x + int_of_string(y))
15+
| SinglePayload(v) => Some(int_of_string(v))
16+
| TuplePayload(v0, v1) => Some(v0 + int_of_string(v1))
1417
| _ => None
1518
}
16-
let v0 = Inline_record({x: 3, y: "4"})
1719

18-
eq(__LOC__, f(v0), Some(7))
20+
eq(__LOC__, f(Inline_record({x: 3, y: "4"})), Some(7))
21+
eq(__LOC__, f(SinglePayload("1")), Some(1))
22+
eq(__LOC__, f(TuplePayload(1, "2")), Some(3))
1923

2024
/* Record_unboxed */
2125
type t1 = | @unboxed A({x: int})
@@ -52,4 +56,8 @@ let u = f =>
5256
| _ => -1
5357
}
5458

59+
eq(__LOC__, u(() => raise(A({name: 1, x: 1}))), 2)
60+
eq(__LOC__, u(() => raise(B(1, 2))), 3)
61+
eq(__LOC__, u(() => raise(C({name: 4}))), 4)
62+
5563
let () = Mt.from_pair_suites(__LOC__, suites.contents)

0 commit comments

Comments
 (0)