Skip to content

Commit 39d4f3d

Browse files
cristianoczth
authored andcommitted
Make lbl_all mutable.
With lbl_all mutable, it can be extended when new fields are used in pattern matching. This handles examples with multiple fields: ``` type myDict = {name?:string, anyOtherField?: int} let tst = (d: myDict) => switch d { | {a:i, b:j} => i + j | _ => 0 } ```
1 parent 09eaef5 commit 39d4f3d

File tree

3 files changed

+10
-8
lines changed

3 files changed

+10
-8
lines changed

jscomp/ml/typecore.ml

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -789,7 +789,7 @@ module NameChoice(Name : sig
789789
val get_type: t -> type_expr
790790
val get_descrs: Env.type_descriptions -> t list
791791

792-
val set_name: t -> string -> t
792+
val add_with_name: t -> string -> t
793793
val unbound_name_error: Env.t -> Longident.t loc -> 'a
794794

795795
end) = struct
@@ -813,7 +813,7 @@ end) = struct
813813
try
814814
let x = List.find (fun nd -> get_name nd = s) descrs in
815815
if s = "anyOtherField"
816-
then set_name x s_
816+
then add_with_name x s_
817817
else x
818818
with Not_found ->
819819
let names = List.map get_name descrs in
@@ -894,13 +894,15 @@ module Label = NameChoice (struct
894894
type t = label_description
895895
let type_kind = "record"
896896
let get_name lbl = lbl.lbl_name
897-
let set_name lbl name =
897+
let add_with_name lbl name =
898898
let l =
899899
{lbl with
900900
lbl_name = name;
901+
lbl_pos = Array.length lbl.lbl_all;
901902
lbl_repres = Record_optional_labels [name]} in
902-
let lbl_all = lbl.lbl_all in
903-
lbl_all.(Array.length lbl_all - 1) <- l; (* assume "anyOtherField" is the last label *)
903+
let lbl_all_list = Array.to_list lbl.lbl_all @ [l] in
904+
let lbl_all = Array.of_list lbl_all_list in
905+
Ext_array.iter lbl_all (fun lbl -> lbl.lbl_all <- lbl_all);
904906
l
905907
let get_type lbl = lbl.lbl_res
906908
let get_descrs = snd
@@ -1059,7 +1061,7 @@ module Constructor = NameChoice (struct
10591061
let get_name cstr = cstr.cstr_name
10601062
let get_type cstr = cstr.cstr_res
10611063

1062-
let set_name _cstr _name = assert false
1064+
let add_with_name _cstr _name = assert false
10631065
let get_descrs = fst
10641066
let unbound_name_error = Typetexp.unbound_constructor_error
10651067
end)

jscomp/ml/types.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ type label_description =
303303
lbl_arg: type_expr; (* Type of the argument *)
304304
lbl_mut: mutable_flag; (* Is this a mutable field? *)
305305
lbl_pos: int; (* Position in block *)
306-
lbl_all: label_description array; (* All the labels in this type *)
306+
mutable lbl_all: label_description array; (* All the labels in this type *)
307307
lbl_repres: record_representation; (* Representation for this record *)
308308
lbl_private: private_flag; (* Read-only field? *)
309309
lbl_loc: Location.t;

jscomp/ml/types.mli

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,7 @@ type label_description =
449449
lbl_arg: type_expr; (* Type of the argument *)
450450
lbl_mut: mutable_flag; (* Is this a mutable field? *)
451451
lbl_pos: int; (* Position in block *)
452-
lbl_all: label_description array; (* All the labels in this type *)
452+
mutable lbl_all: label_description array; (* All the labels in this type *)
453453
lbl_repres: record_representation; (* Representation for this record *)
454454
lbl_private: private_flag; (* Read-only field? *)
455455
lbl_loc: Location.t;

0 commit comments

Comments
 (0)