Skip to content

Commit e1a552e

Browse files
committed
not dump the undefined value of optional field
in the inlined record
1 parent dc04ee0 commit e1a552e

10 files changed

+102
-74
lines changed

jscomp/core/js_dump.ml

+6
Original file line numberDiff line numberDiff line change
@@ -766,6 +766,12 @@ and expression_desc cxt ~(level : int) f x : cxt =
766766
in
767767
if p.num_nonconst = 1 && not !Js_config.debug then
768768
pp_comment_option f (Some p.name);
769+
let objs = if List.length p.optional_labels <> 0 then
770+
Ext_list.array_list_filter_map p.fields el (fun f x ->
771+
match x.expression_desc with
772+
| Undefined when List.mem f p.optional_labels -> None
773+
| _ -> Some (Js_op.Lit f, x))
774+
else objs in
769775
expression_desc cxt ~level f (Object objs)
770776
| Caml_block (el, _, tag, Blk_constructor p) ->
771777
let not_is_cons = p.name <> Literals.cons in

jscomp/main/builtin_cmj_datasets.ml

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

jscomp/ml/lambda.ml

+3-3
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ type record_repr =
4040

4141
type tag_info =
4242
| Blk_constructor of {name : string ; num_nonconst : int ; tag : int }
43-
| Blk_record_inlined of { name : string ; num_nonconst : int; tag : int; fields : string array; mutable_flag : Asttypes.mutable_flag }
43+
| Blk_record_inlined of { name : string ; num_nonconst : int; tag : int; optional_labels: string list; fields : string array; mutable_flag : Asttypes.mutable_flag }
4444
| Blk_tuple
4545
| Blk_poly_var of string
4646
| Blk_record of {fields : string array; mutable_flag : Asttypes.mutable_flag; record_repr : record_repr}
@@ -96,9 +96,9 @@ let blk_record_ext = ref (fun fields mutable_flag ->
9696
Blk_record_ext {fields = all_labels_info; mutable_flag }
9797
)
9898

99-
let blk_record_inlined = ref (fun fields name num_nonconst ~tag mutable_flag ->
99+
let blk_record_inlined = ref (fun fields name num_nonconst optional_labels ~tag mutable_flag ->
100100
let fields = fields |> Array.map (fun (x,_) -> x.Types.lbl_name) in
101-
Blk_record_inlined {fields; name; num_nonconst; tag; mutable_flag}
101+
Blk_record_inlined {fields; name; num_nonconst; tag; mutable_flag; optional_labels}
102102
)
103103

104104
let ref_tag_info : tag_info =

jscomp/ml/lambda.mli

+2-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ type record_repr =
4040

4141
type tag_info =
4242
| Blk_constructor of {name : string ; num_nonconst : int; tag : int}
43-
| Blk_record_inlined of { name : string ; num_nonconst : int ; tag : int; fields : string array; mutable_flag : mutable_flag}
43+
| Blk_record_inlined of { name : string ; num_nonconst : int ; tag : int; optional_labels: string list; fields : string array; mutable_flag : mutable_flag }
4444
| Blk_tuple
4545
| Blk_poly_var of string
4646
| Blk_record of {fields : string array; mutable_flag : mutable_flag; record_repr : record_repr }
@@ -85,6 +85,7 @@ val blk_record_inlined :
8585
(Types.label_description* Typedtree.record_label_definition) array ->
8686
string ->
8787
int ->
88+
string list ->
8889
tag:int ->
8990
mutable_flag ->
9091
tag_info

jscomp/ml/translcore.ml

+4-4
Original file line numberDiff line numberDiff line change
@@ -1138,10 +1138,10 @@ and transl_record loc env fields repres opt_init_expr =
11381138
| Record_optional_labels _ ->
11391139
Lconst
11401140
(Const_block (!Lambda.blk_record fields mut Record_optional, cl))
1141-
| Record_inlined { tag; name; num_nonconsts } ->
1141+
| Record_inlined { tag; name; num_nonconsts; optional_labels } ->
11421142
Lconst
11431143
(Const_block
1144-
( !Lambda.blk_record_inlined fields name num_nonconsts ~tag
1144+
( !Lambda.blk_record_inlined fields name num_nonconsts optional_labels ~tag
11451145
mut,
11461146
cl ))
11471147
| Record_unboxed _ ->
@@ -1160,10 +1160,10 @@ and transl_record loc env fields repres opt_init_expr =
11601160
ll,
11611161
loc )
11621162
| Record_float_unused -> assert false
1163-
| Record_inlined { tag; name; num_nonconsts } ->
1163+
| Record_inlined { tag; name; num_nonconsts; optional_labels } ->
11641164
Lprim
11651165
( Pmakeblock
1166-
(!Lambda.blk_record_inlined fields name num_nonconsts ~tag
1166+
(!Lambda.blk_record_inlined fields name num_nonconsts optional_labels ~tag
11671167
mut),
11681168
ll,
11691169
loc )

jscomp/test/record_regression.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,7 @@ var h = newrecord$2;
8181
var h10 = newrecord$3;
8282

8383
var foo1 = /* Foo */{
84-
name: "foo",
85-
age: undefined
84+
name: "foo"
8685
};
8786

8887
var foo2 = /* Foo */{

lib/4.06.1/unstable/all_ounit_tests.ml

+5-4
Original file line numberDiff line numberDiff line change
@@ -49750,7 +49750,7 @@ type record_repr =
4975049750

4975149751
type tag_info =
4975249752
| Blk_constructor of {name : string ; num_nonconst : int; tag : int}
49753-
| Blk_record_inlined of { name : string ; num_nonconst : int ; tag : int; fields : string array; mutable_flag : mutable_flag}
49753+
| Blk_record_inlined of { name : string ; num_nonconst : int ; tag : int; optional_labels: string list; fields : string array; mutable_flag : mutable_flag }
4975449754
| Blk_tuple
4975549755
| Blk_poly_var of string
4975649756
| Blk_record of {fields : string array; mutable_flag : mutable_flag; record_repr : record_repr }
@@ -49795,6 +49795,7 @@ val blk_record_inlined :
4979549795
(Types.label_description* Typedtree.record_label_definition) array ->
4979649796
string ->
4979749797
int ->
49798+
string list ->
4979849799
tag:int ->
4979949800
mutable_flag ->
4980049801
tag_info
@@ -50145,7 +50146,7 @@ type record_repr =
5014550146

5014650147
type tag_info =
5014750148
| Blk_constructor of {name : string ; num_nonconst : int ; tag : int }
50148-
| Blk_record_inlined of { name : string ; num_nonconst : int; tag : int; fields : string array; mutable_flag : Asttypes.mutable_flag }
50149+
| Blk_record_inlined of { name : string ; num_nonconst : int; tag : int; optional_labels: string list; fields : string array; mutable_flag : Asttypes.mutable_flag }
5014950150
| Blk_tuple
5015050151
| Blk_poly_var of string
5015150152
| Blk_record of {fields : string array; mutable_flag : Asttypes.mutable_flag; record_repr : record_repr}
@@ -50201,9 +50202,9 @@ let blk_record_ext = ref (fun fields mutable_flag ->
5020150202
Blk_record_ext {fields = all_labels_info; mutable_flag }
5020250203
)
5020350204

50204-
let blk_record_inlined = ref (fun fields name num_nonconst ~tag mutable_flag ->
50205+
let blk_record_inlined = ref (fun fields name num_nonconst optional_labels ~tag mutable_flag ->
5020550206
let fields = fields |> Array.map (fun (x,_) -> x.Types.lbl_name) in
50206-
Blk_record_inlined {fields; name; num_nonconst; tag; mutable_flag}
50207+
Blk_record_inlined {fields; name; num_nonconst; tag; mutable_flag; optional_labels}
5020750208
)
5020850209

5020950210
let ref_tag_info : tag_info =

lib/4.06.1/unstable/js_compiler.ml

+24-17
Large diffs are not rendered by default.

lib/4.06.1/unstable/js_playground_compiler.ml

+24-17
Large diffs are not rendered by default.

lib/4.06.1/whole_compiler.ml

+24-17
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)