Skip to content

Commit c95f8b8

Browse files
committed
POC print variant runtime repr
As in `type t = @as(undefined) A` Triggered in error message: ```res Type declarations do not match: type t = @as(undefined) A is not included in type t = @as(null) A ```
1 parent e4439c9 commit c95f8b8

File tree

4 files changed

+42
-19
lines changed

4 files changed

+42
-19
lines changed

jscomp/ml/oprint.ml

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -499,13 +499,13 @@ and print_out_signature ppf =
499499
match items with
500500
Osig_typext(ext, Oext_next) :: items ->
501501
gather_extensions
502-
((ext.oext_name, ext.oext_args, ext.oext_ret_type) :: acc)
502+
((ext.oext_name, ext.oext_args, ext.oext_ret_type, ext.oext_repr) :: acc)
503503
items
504504
| _ -> (List.rev acc, items)
505505
in
506506
let exts, items =
507507
gather_extensions
508-
[(ext.oext_name, ext.oext_args, ext.oext_ret_type)]
508+
[(ext.oext_name, ext.oext_args, ext.oext_ret_type, ext.oext_repr)]
509509
items
510510
in
511511
let te =
@@ -531,7 +531,7 @@ and print_out_sig_item ppf =
531531
name !out_class_type clt
532532
| Osig_typext (ext, Oext_exception) ->
533533
fprintf ppf "@[<2>exception %a@]"
534-
print_out_constr (ext.oext_name, ext.oext_args, ext.oext_ret_type)
534+
print_out_constr (ext.oext_name, ext.oext_args, ext.oext_ret_type, ext.oext_repr)
535535
| Osig_typext (ext, _es) ->
536536
print_out_extension_constructor ppf ext
537537
| Osig_modtype (name, Omty_abstract) ->
@@ -639,7 +639,10 @@ and print_out_type_decl kwd ppf td =
639639
print_immediate
640640
print_unboxed
641641

642-
and print_out_constr ppf (name, tyl,ret_type_opt) =
642+
and print_out_constr ppf (name, tyl, ret_type_opt, repr) =
643+
let () = match repr with
644+
| None -> ()
645+
| Some s -> pp_print_string ppf s in
643646
let name =
644647
match name with
645648
| "::" -> "(::)" (* #7200 *)
@@ -686,7 +689,7 @@ and print_out_extension_constructor ppf ext =
686689
fprintf ppf "@[<hv 2>type %t +=%s@;<1 2>%a@]"
687690
print_extended_type
688691
(if ext.oext_private = Asttypes.Private then " private" else "")
689-
print_out_constr (ext.oext_name, ext.oext_args, ext.oext_ret_type)
692+
print_out_constr (ext.oext_name, ext.oext_args, ext.oext_ret_type, ext.oext_repr)
690693

691694
and print_out_type_extension ppf te =
692695
let print_extended_type ppf =
@@ -736,13 +739,13 @@ let rec print_items ppf =
736739
match items with
737740
(Osig_typext(ext, Oext_next), None) :: items ->
738741
gather_extensions
739-
((ext.oext_name, ext.oext_args, ext.oext_ret_type) :: acc)
742+
((ext.oext_name, ext.oext_args, ext.oext_ret_type, ext.oext_repr) :: acc)
740743
items
741744
| _ -> (List.rev acc, items)
742745
in
743746
let exts, items =
744747
gather_extensions
745-
[(ext.oext_name, ext.oext_args, ext.oext_ret_type)]
748+
[(ext.oext_name, ext.oext_args, ext.oext_ret_type, ext.oext_repr)]
746749
items
747750
in
748751
let te =

jscomp/ml/outcometree.ml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ type out_type =
6363
| Otyp_object of (string * out_type) list * bool option
6464
| Otyp_record of (string * bool * bool * out_type) list
6565
| Otyp_stuff of string
66-
| Otyp_sum of (string * out_type list * out_type option) list
66+
| Otyp_sum of (string * out_type list * out_type option * string option) list
6767
| Otyp_tuple of out_type list
6868
| Otyp_var of bool * string
6969
| Otyp_variant of
@@ -118,11 +118,12 @@ and out_extension_constructor =
118118
oext_type_params: string list;
119119
oext_args: out_type list;
120120
oext_ret_type: out_type option;
121+
oext_repr: string option;
121122
oext_private: Asttypes.private_flag }
122123
and out_type_extension =
123124
{ otyext_name: string;
124125
otyext_params: string list;
125-
otyext_constructors: (string * out_type list * out_type option) list;
126+
otyext_constructors: (string * out_type list * out_type option * string option) list;
126127
otyext_private: Asttypes.private_flag }
127128
and out_val_decl =
128129
{ oval_name: string;

jscomp/ml/printtyp.ml

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -917,16 +917,24 @@ and tree_of_constructor_arguments = function
917917

918918
and tree_of_constructor cd =
919919
let name = Ident.name cd.cd_id in
920+
let nullary = Ast_untagged_variants.is_nullary_variant cd.cd_args in
921+
let repr =
922+
if not nullary then None
923+
else match Ast_untagged_variants.process_tag_type cd.cd_attributes with
924+
| Some Null -> Some "@as(null)"
925+
| Some Undefined -> Some "@as(undefined)"
926+
| Some _tag_type -> Some "XXX"
927+
| None -> None in
920928
let arg () = tree_of_constructor_arguments cd.cd_args in
921929
match cd.cd_res with
922-
| None -> (name, arg (), None)
930+
| None -> (name, arg (), None, repr)
923931
| Some res ->
924932
let nm = !names in
925933
names := [];
926934
let ret = tree_of_typexp false res in
927935
let args = arg () in
928936
names := nm;
929-
(name, args, Some ret)
937+
(name, args, Some ret, repr)
930938

931939
and tree_of_label l =
932940
let opt = l.ld_attributes |> List.exists (fun ({txt}, _) -> txt = "ns.optional" || txt = "res.optional") in
@@ -982,6 +990,7 @@ let tree_of_extension_constructor id ext es =
982990
oext_type_params = ty_params;
983991
oext_args = args;
984992
oext_ret_type = ret;
993+
oext_repr = None;
985994
oext_private = ext.ext_private }
986995
in
987996
let es =

jscomp/syntax/src/res_outcome_printer.ml

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -429,8 +429,13 @@ and print_out_constructors_doc constructors =
429429
constructors);
430430
]))
431431

432-
and print_out_constructor_doc (name, args, gadt) =
433-
let gadt_doc =
432+
and print_out_constructor_doc (name, args, gadt, repr) =
433+
let reprDoc =
434+
match repr with
435+
| None -> Doc.nil
436+
| Some s -> Doc.text (s ^ " ")
437+
in
438+
let gadtDoc =
434439
match gadt with
435440
| Some out_type -> Doc.concat [Doc.text ": "; print_out_type_doc out_type]
436441
| None -> Doc.nil
@@ -469,7 +474,7 @@ and print_out_constructor_doc (name, args, gadt) =
469474
Doc.rparen;
470475
])
471476
in
472-
Doc.group (Doc.concat [Doc.text name; args_doc; gadt_doc])
477+
Doc.group (Doc.concat [reprDoc; Doc.text name; args_doc; gadtDoc])
473478

474479
and print_record_decl_row_doc (name, mut, opt, arg) =
475480
Doc.group
@@ -758,13 +763,14 @@ and print_out_signature_doc (signature : Outcometree.out_sig_item list) =
758763
match items with
759764
| Outcometree.Osig_typext (ext, Oext_next) :: items ->
760765
gather_extensions
761-
((ext.oext_name, ext.oext_args, ext.oext_ret_type) :: acc)
766+
((ext.oext_name, ext.oext_args, ext.oext_ret_type, ext.oext_repr)
767+
:: acc)
762768
items
763769
| _ -> (List.rev acc, items)
764770
in
765771
let exts, items =
766772
gather_extensions
767-
[(ext.oext_name, ext.oext_args, ext.oext_ret_type)]
773+
[(ext.oext_name, ext.oext_args, ext.oext_ret_type, ext.oext_repr)]
768774
items
769775
in
770776
let te =
@@ -822,7 +828,10 @@ and print_out_extension_constructor_doc
822828
(if out_ext.oext_private = Asttypes.Private then Doc.text "private "
823829
else Doc.nil);
824830
print_out_constructor_doc
825-
(out_ext.oext_name, out_ext.oext_args, out_ext.oext_ret_type);
831+
( out_ext.oext_name,
832+
out_ext.oext_args,
833+
out_ext.oext_ret_type,
834+
out_ext.oext_repr );
826835
])
827836

828837
and print_out_type_extension_doc
@@ -1035,13 +1044,14 @@ let print_out_phrase_signature signature =
10351044
match items with
10361045
| (Outcometree.Osig_typext (ext, Oext_next), None) :: items ->
10371046
gather_extensions
1038-
((ext.oext_name, ext.oext_args, ext.oext_ret_type) :: acc)
1047+
((ext.oext_name, ext.oext_args, ext.oext_ret_type, ext.oext_repr)
1048+
:: acc)
10391049
items
10401050
| _ -> (List.rev acc, items)
10411051
in
10421052
let exts, signature =
10431053
gather_extensions
1044-
[(ext.oext_name, ext.oext_args, ext.oext_ret_type)]
1054+
[(ext.oext_name, ext.oext_args, ext.oext_ret_type, ext.oext_repr)]
10451055
signature
10461056
in
10471057
let te =

0 commit comments

Comments
 (0)