Skip to content

Commit 699c7d0

Browse files
authored
Refactor untagged variants (#6128)
* Refctor untagged variant files Move closer to the type checker, and encapsulate logic inside Ast_untagged_variants * rename * Move more to untagged variant ast * Move well-formedness check to the type checker. So errors appear on type definition, not on first use of pattern matching. CC @cknitt * Move type defs from lambda. * rename literal * Rename cstr_name to literal. * Check for duplicate literals. CC @cknitt * Apply duplicate literal check to normal variants too.
1 parent 2b6d237 commit 699c7d0

40 files changed

+298
-259
lines changed

jscomp/core/j.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ and case_clause = {
244244
comment : string option;
245245
}
246246

247-
and string_clause = Lambda.literal * case_clause
247+
and string_clause = Ast_untagged_variants.literal_type * case_clause
248248
and int_clause = int * case_clause
249249

250250
and statement_desc =

jscomp/core/js_dump.ml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -751,7 +751,7 @@ and expression_desc cxt ~(level : int) f x : cxt =
751751
| Caml_block (el, _, _, ((Blk_extension | Blk_record_ext _) as ext)) ->
752752
expression_desc cxt ~level f (exn_block_as_obj ~stack:false el ext)
753753
| Caml_block (el, _, tag, Blk_record_inlined p) ->
754-
let untagged = Ast_attributes.process_untagged p.attrs in
754+
let untagged = Ast_untagged_variants.process_untagged p.attrs in
755755
let objs =
756756
let tails =
757757
Ext_list.combine_array_append p.fields el
@@ -763,7 +763,7 @@ and expression_desc cxt ~(level : int) f x : cxt =
763763
| Lit n -> Ext_list.mem_string p.optional_labels n
764764
| Symbol_name -> false
765765
in
766-
let tag_name = match Ast_attributes.process_tag_name p.attrs with
766+
let tag_name = match Ast_untagged_variants.process_tag_name p.attrs with
767767
| None -> L.tag
768768
| Some s -> s in
769769
let tails =
@@ -779,17 +779,17 @@ and expression_desc cxt ~(level : int) f x : cxt =
779779
tails
780780
else
781781
(Js_op.Lit tag_name, (* TAG:xx for inline records *)
782-
match Ast_attributes.process_as_value p.attrs with
782+
match Ast_untagged_variants.process_literal_type p.attrs with
783783
| None -> E.str p.name
784784
| Some literal -> E.literal literal )
785785
:: tails
786786
in
787787
expression_desc cxt ~level f (Object objs)
788788
| Caml_block (el, _, tag, Blk_constructor p) ->
789789
let not_is_cons = p.name <> Literals.cons in
790-
let literal = Ast_attributes.process_as_value p.attrs in
791-
let untagged = Ast_attributes.process_untagged p.attrs in
792-
let tag_name = match Ast_attributes.process_tag_name p.attrs with
790+
let literal = Ast_untagged_variants.process_literal_type p.attrs in
791+
let untagged = Ast_untagged_variants.process_untagged p.attrs in
792+
let tag_name = match Ast_untagged_variants.process_tag_name p.attrs with
793793
| None -> L.tag
794794
| Some s -> s in
795795
let objs =
@@ -1210,7 +1210,7 @@ and statement_desc top cxt f (s : J.statement_desc) : cxt =
12101210
let cxt = P.paren_group f 1 (fun _ -> expression ~level:0 cxt f e) in
12111211
P.space f;
12121212
P.brace_vgroup f 1 (fun _ ->
1213-
let pp_as_value f (literal: Lambda.literal) =
1213+
let pp_as_value f (literal: Ast_untagged_variants.literal_type) =
12141214
let e = E.literal literal in
12151215
ignore @@ expression_desc cxt ~level:0 f e.expression_desc in
12161216
let cxt = loop_case_clauses cxt f pp_as_value cc in

jscomp/core/js_exp_make.ml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ let float_mod ?comment e1 e2 : J.expression =
336336
{ comment; expression_desc = Bin (Mod, e1, e2) }
337337

338338
let literal = function
339-
| Lambda.String s -> str s ~delim:DStarJ
339+
| Ast_untagged_variants.String s -> str s ~delim:DStarJ
340340
| Int i -> small_int i
341341
| Float f -> float f
342342
| Bool b -> bool b
@@ -775,7 +775,7 @@ let string_equal ?comment (e0 : t) (e1 : t) : t =
775775
let is_type_number ?comment (e : t) : t =
776776
string_equal ?comment (typeof e) (str "number")
777777

778-
let rec is_a_literal_case ~(literal_cases : Lambda.literal list) ~block_cases (e:t) : t =
778+
let rec is_a_literal_case ~(literal_cases : Ast_untagged_variants.literal_type list) ~block_cases (e:t) : t =
779779
let literals_overlaps_with_string () =
780780
Ext_list.exists literal_cases (function
781781
| String _ -> true
@@ -792,8 +792,8 @@ let rec is_a_literal_case ~(literal_cases : Lambda.literal list) ~block_cases (e
792792
let (!=) x y = bin NotEqEq x y in
793793
let (||) x y = bin Or x y in
794794
let (&&) x y = bin And x y in
795-
let is_literal_case (l:Lambda.literal) : t = e == (literal l) in
796-
let is_not_block_case (c:Lambda.block_type) : t = match c with
795+
let is_literal_case (l:Ast_untagged_variants.literal_type) : t = e == (literal l) in
796+
let is_not_block_case (c:Ast_untagged_variants.block_type) : t = match c with
797797
| StringType when literals_overlaps_with_string () = false (* No overlap *) ->
798798
(typeof e) != (str "string")
799799
| IntType when literals_overlaps_with_number () = false ->

jscomp/core/js_exp_make.mli

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ val assign_by_exp : t -> t -> t -> t
185185

186186
val assign : ?comment:string -> t -> t -> t
187187

188-
val literal : Lambda.literal -> t
188+
val literal : Ast_untagged_variants.literal_type -> t
189189

190190
val triple_equal : ?comment:string -> t -> t -> t
191191
(* TODO: reduce [triple_equal] use *)
@@ -204,7 +204,7 @@ val is_type_number : ?comment:string -> t -> t
204204

205205
val is_int_tag : ?has_null_undefined_other:(bool * bool * bool) -> t -> t
206206

207-
val is_a_literal_case : literal_cases:Lambda.literal list -> block_cases:Lambda.block_type list -> t -> t
207+
val is_a_literal_case : literal_cases:Ast_untagged_variants.literal_type list -> block_cases:Ast_untagged_variants.block_type list -> t -> t
208208

209209
val is_type_string : ?comment:string -> t -> t
210210

jscomp/core/js_of_lam_variant.ml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ let eval (arg : J.expression) (dispatches : (string * string) list) : E.t =
4040
[
4141
S.string_switch arg
4242
(Ext_list.map dispatches (fun (s, r) ->
43-
( Lambda.String s,
43+
( Ast_untagged_variants.String s,
4444
J.
4545
{
4646
switch_body = [ S.return_stmt (E.str r) ];
@@ -80,7 +80,7 @@ let eval_as_event (arg : J.expression)
8080
S.string_switch
8181
(E.poly_var_tag_access arg)
8282
(Ext_list.map dispatches (fun (s, r) ->
83-
( Lambda.String s,
83+
( Ast_untagged_variants.String s,
8484
J.
8585
{
8686
switch_body = [ S.return_stmt (E.str r) ];
@@ -108,7 +108,7 @@ let eval_as_int (arg : J.expression) (dispatches : (string * int) list) : E.t =
108108
[
109109
S.string_switch arg
110110
(Ext_list.map dispatches (fun (s, r) ->
111-
( Lambda.String s,
111+
( Ast_untagged_variants.String s,
112112
J.
113113
{
114114
switch_body =

jscomp/core/js_stmt_make.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ let int_switch ?(comment : string option)
129129

130130
let string_switch ?(comment : string option)
131131
?(declaration : (J.property * Ident.t) option) ?(default : J.block option)
132-
(e : J.expression) (clauses : (Lambda.literal * J.case_clause) list) : t =
132+
(e : J.expression) (clauses : (Ast_untagged_variants.literal_type * J.case_clause) list) : t =
133133
match e.expression_desc with
134134
| Str {txt} -> (
135135
let continuation =

jscomp/core/js_stmt_make.mli

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ val string_switch :
7777
?declaration:Lam_compat.let_kind * Ident.t ->
7878
?default:J.block ->
7979
J.expression ->
80-
(Lambda.literal * J.case_clause) list ->
80+
(Ast_untagged_variants.literal_type * J.case_clause) list ->
8181
t
8282

8383
val declare_variable :

jscomp/core/lam.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ module Types = struct
3939
sw_blocks_full : bool;
4040
sw_blocks : (int * t) list;
4141
sw_failaction : t option;
42-
sw_names : Lambda.switch_names option;
42+
sw_names : Ast_untagged_variants.switch_names option;
4343
}
4444

4545
and lfunction = {
@@ -116,7 +116,7 @@ module X = struct
116116
sw_blocks_full : bool;
117117
sw_blocks : (int * t) list;
118118
sw_failaction : t option;
119-
sw_names : Lambda.switch_names option;
119+
sw_names : Ast_untagged_variants.switch_names option;
120120
}
121121

122122
and prim_info = Types.prim_info = {

jscomp/core/lam.mli

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ type lambda_switch = {
3838
sw_blocks_full : bool;
3939
sw_blocks : (int * t) list;
4040
sw_failaction : t option;
41-
sw_names : Lambda.switch_names option;
41+
sw_names : Ast_untagged_variants.switch_names option;
4242
}
4343

4444
and apply = private { ap_func : t; ap_args : t list; ap_info : ap_info }

jscomp/core/lam_compile.ml

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -138,22 +138,22 @@ let default_action ~saturated failaction =
138138
| None -> Complete
139139
| Some x -> if saturated then Complete else Default x
140140

141-
let get_const_name i (sw_names : Lambda.switch_names option) =
141+
let get_const_name i (sw_names : Ast_untagged_variants.switch_names option) =
142142
match sw_names with None -> None | Some { consts } -> Some consts.(i)
143143

144-
let get_block i (sw_names : Lambda.switch_names option) =
144+
let get_block i (sw_names : Ast_untagged_variants.switch_names option) =
145145
match sw_names with None -> None | Some { blocks } -> Some blocks.(i)
146146

147-
let get_tag_name (sw_names : Lambda.switch_names option) =
147+
let get_tag_name (sw_names : Ast_untagged_variants.switch_names option) =
148148
match sw_names with
149149
| None -> Js_dump_lit.tag
150150
| Some { blocks } ->
151-
(match Array.find_opt (fun {Lambda.tag_name} -> tag_name <> None) blocks with
151+
(match Array.find_opt (fun {Ast_untagged_variants.tag_name} -> tag_name <> None) blocks with
152152
| Some {tag_name = Some s} -> s
153153
| _ -> Js_dump_lit.tag
154154
)
155155

156-
let get_block_cases (sw_names : Lambda.switch_names option) =
156+
let get_block_cases (sw_names : Ast_untagged_variants.switch_names option) =
157157
let res = ref [] in
158158
(match sw_names with
159159
| None -> res := []
@@ -165,25 +165,25 @@ let get_block_cases (sw_names : Lambda.switch_names option) =
165165
);
166166
!res
167167

168-
let get_literal_cases (sw_names : Lambda.switch_names option) =
168+
let get_literal_cases (sw_names : Ast_untagged_variants.switch_names option) =
169169
let res = ref [] in
170170
(match sw_names with
171171
| None -> res := []
172172
| Some { consts } ->
173173
Ext_array.iter consts (function
174-
| {literal = Some literal} -> res := literal :: !res
175-
| {name; literal = None} -> res := String name :: !res
174+
| {literal_type = Some literal} -> res := literal :: !res
175+
| {name; literal_type = None} -> res := String name :: !res
176176
)
177177
);
178178
!res
179179

180180

181-
let has_null_undefined_other (sw_names : Lambda.switch_names option) =
181+
let has_null_undefined_other (sw_names : Ast_untagged_variants.switch_names option) =
182182
let (null, undefined, other) = (ref false, ref false, ref false) in
183183
(match sw_names with
184184
| None -> ()
185185
| Some { consts; blocks } ->
186-
Ext_array.iter consts (fun x -> match x.literal with
186+
Ext_array.iter consts (fun x -> match x.literal_type with
187187
| Some Undefined -> undefined := true
188188
| Some Null -> null := true
189189
| _ -> other := true);
@@ -500,7 +500,7 @@ and compile_recursive_lets cxt id_args : Js_output.t =
500500

501501
and compile_general_cases :
502502
'a .
503-
('a -> Lambda.cstr_name option) ->
503+
('a -> Ast_untagged_variants.literal option) ->
504504
('a -> J.expression) ->
505505
('a option -> J.expression -> 'a option -> J.expression -> J.expression) ->
506506
Lam_compile_context.t ->
@@ -513,7 +513,7 @@ and compile_general_cases :
513513
('a * Lam.t) list ->
514514
default_case ->
515515
J.block =
516-
fun (get_cstr_name : _ -> Lambda.cstr_name option) (make_exp : _ -> J.expression)
516+
fun (get_cstr_name : _ -> Ast_untagged_variants.literal option) (make_exp : _ -> J.expression)
517517
(eq_exp : 'a option -> J.expression -> 'a option -> J.expression -> J.expression)
518518
(cxt : Lam_compile_context.t)
519519
(switch :
@@ -619,9 +619,9 @@ and compile_general_cases :
619619
and use_compile_literal_cases table get_name =
620620
List.fold_right (fun (i, lam) acc ->
621621
match get_name i, acc with
622-
| Some {Lambda.literal = Some literal}, Some string_table ->
622+
| Some {Ast_untagged_variants.literal_type = Some literal}, Some string_table ->
623623
Some ((literal, lam) :: string_table)
624-
| Some {name; literal = None}, Some string_table -> Some ((String name, lam) :: string_table)
624+
| Some {name; literal_type = None}, Some string_table -> Some ((String name, lam) :: string_table)
625625
| _, _ -> None
626626
) table (Some [])
627627
and compile_cases ?(untagged=false) cxt (switch_exp : E.t) table default get_name =
@@ -634,7 +634,7 @@ and compile_cases ?(untagged=false) cxt (switch_exp : E.t) table default get_nam
634634
compile_general_cases get_name
635635
(fun i -> match get_name i with
636636
| None -> E.small_int i
637-
| Some {literal = Some(String s)} -> E.str s
637+
| Some {literal_type = Some(String s)} -> E.str s
638638
| Some {name} -> E.str name)
639639
(fun _ x _ y -> E.int_equal x y) cxt
640640
(fun ?default ?declaration e clauses ->
@@ -671,9 +671,9 @@ and compile_switch (switch_arg : Lam.t) (sw : Lam.lambda_switch)
671671
let get_block_name i = match get_block i with
672672
| None -> None
673673
| Some ({block_type = Some block_type} as block) ->
674-
Some {block.cstr_name with literal = Some (Block block_type)}
675-
| Some ({block_type = None; cstr_name}) ->
676-
Some cstr_name in
674+
Some {block.literal with literal_type = Some (Block block_type)}
675+
| Some ({block_type = None; literal}) ->
676+
Some literal in
677677
let tag_name = get_tag_name sw_names in
678678
let untagged = block_cases <> [] in
679679
let compile_whole (cxt : Lam_compile_context.t) =
@@ -744,7 +744,7 @@ and compile_untagged_cases cxt switch_exp table default =
744744
let literal = function
745745
| literal -> E.literal literal
746746
in
747-
let add_runtime_type_check (literal: Lambda.literal) x y = match literal with
747+
let add_runtime_type_check (literal: Ast_untagged_variants.literal_type) x y = match literal with
748748
| Block IntType
749749
| Block StringType
750750
| Block FloatType
@@ -754,14 +754,14 @@ and compile_untagged_cases cxt switch_exp table default =
754754
(* This should not happen because unknown must be the only non-literal case *)
755755
assert false
756756
| Bool _ | Float _ | Int _ | String _ | Null | Undefined -> x in
757-
let mk_eq (i : Lambda.literal option) x j y = match i, j with
757+
let mk_eq (i : Ast_untagged_variants.literal_type option) x j y = match i, j with
758758
| Some literal, _ -> (* XX *)
759759
add_runtime_type_check literal x y
760760
| _, Some literal ->
761761
add_runtime_type_check literal y x
762762
| _ -> E.string_equal x y
763763
in
764-
let is_array (l, _) = l = Lambda.Block Array in
764+
let is_array (l, _) = l = Ast_untagged_variants.Block Array in
765765
let body ?default ?declaration e clauses =
766766
let array_clauses = Ext_list.filter clauses is_array in
767767
match array_clauses with
@@ -786,7 +786,7 @@ and compile_stringswitch l cases default (lambda_cxt : Lam_compile_context.t) =
786786
Be careful: we should avoid multiple evaluation of l,
787787
The [gen] can be elimiated when number of [cases] is less than 3
788788
*)
789-
let cases = cases |> List.map (fun (s,l) -> Lambda.String s, l) in
789+
let cases = cases |> List.map (fun (s,l) -> Ast_untagged_variants.String s, l) in
790790
match
791791
compile_lambda { lambda_cxt with continuation = NeedValue Not_tail } l
792792
with

0 commit comments

Comments
 (0)