Skip to content

Commit dd7ae60

Browse files
committed
Clean up optional implementation.
1 parent 075f7fc commit dd7ae60

File tree

6 files changed

+31
-32
lines changed

6 files changed

+31
-32
lines changed

jscomp/ml/typecore.ml

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1153,16 +1153,17 @@ and type_pat_aux ~constrs ~labels ~no_existentials ~mode ~explode ~env
11531153
match ld.lbl_repres with
11541154
| Record_optional_labels lbls -> Ext_list.mem_string lbls ld.lbl_name
11551155
| _ -> false in
1156-
let process_optional_label (id, ld, pat) =
1156+
let process_optional_label (ld, pat) =
11571157
let exp_optional_attr =
11581158
Ext_list.exists pat.ppat_attributes (fun ({txt },_) -> txt = "optional")
11591159
in
11601160
if label_is_optional ld && not exp_optional_attr then
1161-
let ppat_desc = Ppat_construct ({id with txt = Longident.Lident "Some"}, Some pat)
1162-
in {pat with ppat_desc}
1161+
let lid = mknoloc (Longident.(Ldot (Lident "*predef*", "Some"))) in
1162+
Ast_helper.Pat.construct ~loc:pat.ppat_loc lid (Some pat)
11631163
else pat
11641164
in
11651165
let type_label_pat (label_lid, label, sarg) k =
1166+
let sarg = process_optional_label (label, sarg) in
11661167
begin_def ();
11671168
let (vars, ty_arg, ty_res) = instance_label false label in
11681169
if vars = [] then end_def ();
@@ -1172,7 +1173,6 @@ and type_pat_aux ~constrs ~labels ~no_existentials ~mode ~explode ~env
11721173
raise(Error(label_lid.loc, !env,
11731174
Label_mismatch(label_lid.txt, trace)))
11741175
end;
1175-
let sarg = process_optional_label (label_lid, label, sarg) in
11761176
type_pat sarg ty_arg (fun arg ->
11771177
if vars <> [] then begin
11781178
end_def ();
@@ -1879,8 +1879,9 @@ and type_expect_ ?in_function ?(recarg=Rejected) env sexp ty_expected =
18791879
Ext_list.exists e.pexp_attributes (fun ({txt },_) -> txt = "optional")
18801880
in
18811881
if label_is_optional ld && not exp_optional_attr then
1882-
let pexp_desc = Pexp_construct ({id with txt = Longident.Lident "Some"}, Some e)
1883-
in (id, ld, {e with pexp_desc})
1882+
let lid = mknoloc (Longident.(Ldot (Lident "*predef*", "Some"))) in
1883+
let e = Ast_helper.Exp.construct ~loc:e.pexp_loc lid (Some e)
1884+
in (id, ld, e)
18841885
else (id, ld, e)
18851886
in
18861887
match sexp.pexp_desc with

jscomp/test/res_debug.js

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,7 @@ var newrecord = Caml_obj.obj_dup(v0);
1616
newrecord.x = 3;
1717

1818
function testMatch(v) {
19-
var y = v.y;
20-
if (y !== undefined) {
21-
return y;
22-
} else {
23-
return 42;
24-
}
19+
return v.y;
2520
}
2621

2722
var v2 = newrecord;

jscomp/test/res_debug.res

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,12 @@ let v1 : r = { x : 3
4444
, z : 3
4545
}
4646

47-
@@warning("-56") // Turn off match case unreachable
47+
//@@warning("-56") // Turn off match case unreachable
4848

4949
let testMatch = v =>
5050
switch v {
5151
| {y} => y
52-
| {y: @optional None} => 42
52+
// | {y: @optional None} => 42
5353
}
5454

5555
let h = '😊'

lib/4.06.1/unstable/js_compiler.ml

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40056,16 +40056,17 @@ and type_pat_aux ~constrs ~labels ~no_existentials ~mode ~explode ~env
4005640056
match ld.lbl_repres with
4005740057
| Record_optional_labels lbls -> Ext_list.mem_string lbls ld.lbl_name
4005840058
| _ -> false in
40059-
let process_optional_label (id, ld, pat) =
40059+
let process_optional_label (ld, pat) =
4006040060
let exp_optional_attr =
4006140061
Ext_list.exists pat.ppat_attributes (fun ({txt },_) -> txt = "optional")
4006240062
in
4006340063
if label_is_optional ld && not exp_optional_attr then
40064-
let ppat_desc = Ppat_construct ({id with txt = Longident.Lident "Some"}, Some pat)
40065-
in {pat with ppat_desc}
40064+
let lid = mknoloc (Longident.(Ldot (Lident "*predef*", "Some"))) in
40065+
Ast_helper.Pat.construct ~loc:pat.ppat_loc lid (Some pat)
4006640066
else pat
4006740067
in
4006840068
let type_label_pat (label_lid, label, sarg) k =
40069+
let sarg = process_optional_label (label, sarg) in
4006940070
begin_def ();
4007040071
let (vars, ty_arg, ty_res) = instance_label false label in
4007140072
if vars = [] then end_def ();
@@ -40075,7 +40076,6 @@ and type_pat_aux ~constrs ~labels ~no_existentials ~mode ~explode ~env
4007540076
raise(Error(label_lid.loc, !env,
4007640077
Label_mismatch(label_lid.txt, trace)))
4007740078
end;
40078-
let sarg = process_optional_label (label_lid, label, sarg) in
4007940079
type_pat sarg ty_arg (fun arg ->
4008040080
if vars <> [] then begin
4008140081
end_def ();
@@ -40782,8 +40782,9 @@ and type_expect_ ?in_function ?(recarg=Rejected) env sexp ty_expected =
4078240782
Ext_list.exists e.pexp_attributes (fun ({txt },_) -> txt = "optional")
4078340783
in
4078440784
if label_is_optional ld && not exp_optional_attr then
40785-
let pexp_desc = Pexp_construct ({id with txt = Longident.Lident "Some"}, Some e)
40786-
in (id, ld, {e with pexp_desc})
40785+
let lid = mknoloc (Longident.(Ldot (Lident "*predef*", "Some"))) in
40786+
let e = Ast_helper.Exp.construct ~loc:e.pexp_loc lid (Some e)
40787+
in (id, ld, e)
4078740788
else (id, ld, e)
4078840789
in
4078940790
match sexp.pexp_desc with

lib/4.06.1/unstable/js_playground_compiler.ml

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40056,16 +40056,17 @@ and type_pat_aux ~constrs ~labels ~no_existentials ~mode ~explode ~env
4005640056
match ld.lbl_repres with
4005740057
| Record_optional_labels lbls -> Ext_list.mem_string lbls ld.lbl_name
4005840058
| _ -> false in
40059-
let process_optional_label (id, ld, pat) =
40059+
let process_optional_label (ld, pat) =
4006040060
let exp_optional_attr =
4006140061
Ext_list.exists pat.ppat_attributes (fun ({txt },_) -> txt = "optional")
4006240062
in
4006340063
if label_is_optional ld && not exp_optional_attr then
40064-
let ppat_desc = Ppat_construct ({id with txt = Longident.Lident "Some"}, Some pat)
40065-
in {pat with ppat_desc}
40064+
let lid = mknoloc (Longident.(Ldot (Lident "*predef*", "Some"))) in
40065+
Ast_helper.Pat.construct ~loc:pat.ppat_loc lid (Some pat)
4006640066
else pat
4006740067
in
4006840068
let type_label_pat (label_lid, label, sarg) k =
40069+
let sarg = process_optional_label (label, sarg) in
4006940070
begin_def ();
4007040071
let (vars, ty_arg, ty_res) = instance_label false label in
4007140072
if vars = [] then end_def ();
@@ -40075,7 +40076,6 @@ and type_pat_aux ~constrs ~labels ~no_existentials ~mode ~explode ~env
4007540076
raise(Error(label_lid.loc, !env,
4007640077
Label_mismatch(label_lid.txt, trace)))
4007740078
end;
40078-
let sarg = process_optional_label (label_lid, label, sarg) in
4007940079
type_pat sarg ty_arg (fun arg ->
4008040080
if vars <> [] then begin
4008140081
end_def ();
@@ -40782,8 +40782,9 @@ and type_expect_ ?in_function ?(recarg=Rejected) env sexp ty_expected =
4078240782
Ext_list.exists e.pexp_attributes (fun ({txt },_) -> txt = "optional")
4078340783
in
4078440784
if label_is_optional ld && not exp_optional_attr then
40785-
let pexp_desc = Pexp_construct ({id with txt = Longident.Lident "Some"}, Some e)
40786-
in (id, ld, {e with pexp_desc})
40785+
let lid = mknoloc (Longident.(Ldot (Lident "*predef*", "Some"))) in
40786+
let e = Ast_helper.Exp.construct ~loc:e.pexp_loc lid (Some e)
40787+
in (id, ld, e)
4078740788
else (id, ld, e)
4078840789
in
4078940790
match sexp.pexp_desc with

lib/4.06.1/whole_compiler.ml

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -214587,16 +214587,17 @@ and type_pat_aux ~constrs ~labels ~no_existentials ~mode ~explode ~env
214587214587
match ld.lbl_repres with
214588214588
| Record_optional_labels lbls -> Ext_list.mem_string lbls ld.lbl_name
214589214589
| _ -> false in
214590-
let process_optional_label (id, ld, pat) =
214590+
let process_optional_label (ld, pat) =
214591214591
let exp_optional_attr =
214592214592
Ext_list.exists pat.ppat_attributes (fun ({txt },_) -> txt = "optional")
214593214593
in
214594214594
if label_is_optional ld && not exp_optional_attr then
214595-
let ppat_desc = Ppat_construct ({id with txt = Longident.Lident "Some"}, Some pat)
214596-
in {pat with ppat_desc}
214595+
let lid = mknoloc (Longident.(Ldot (Lident "*predef*", "Some"))) in
214596+
Ast_helper.Pat.construct ~loc:pat.ppat_loc lid (Some pat)
214597214597
else pat
214598214598
in
214599214599
let type_label_pat (label_lid, label, sarg) k =
214600+
let sarg = process_optional_label (label, sarg) in
214600214601
begin_def ();
214601214602
let (vars, ty_arg, ty_res) = instance_label false label in
214602214603
if vars = [] then end_def ();
@@ -214606,7 +214607,6 @@ and type_pat_aux ~constrs ~labels ~no_existentials ~mode ~explode ~env
214606214607
raise(Error(label_lid.loc, !env,
214607214608
Label_mismatch(label_lid.txt, trace)))
214608214609
end;
214609-
let sarg = process_optional_label (label_lid, label, sarg) in
214610214610
type_pat sarg ty_arg (fun arg ->
214611214611
if vars <> [] then begin
214612214612
end_def ();
@@ -215313,8 +215313,9 @@ and type_expect_ ?in_function ?(recarg=Rejected) env sexp ty_expected =
215313215313
Ext_list.exists e.pexp_attributes (fun ({txt },_) -> txt = "optional")
215314215314
in
215315215315
if label_is_optional ld && not exp_optional_attr then
215316-
let pexp_desc = Pexp_construct ({id with txt = Longident.Lident "Some"}, Some e)
215317-
in (id, ld, {e with pexp_desc})
215316+
let lid = mknoloc (Longident.(Ldot (Lident "*predef*", "Some"))) in
215317+
let e = Ast_helper.Exp.construct ~loc:e.pexp_loc lid (Some e)
215318+
in (id, ld, e)
215318215319
else (id, ld, e)
215319215320
in
215320215321
match sexp.pexp_desc with

0 commit comments

Comments
 (0)