Skip to content

Commit 98155a1

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 962e5fb commit 98155a1

File tree

4 files changed

+34
-18
lines changed

4 files changed

+34
-18
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: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -483,7 +483,10 @@ and printOutConstructorsDoc constructors =
483483
constructors);
484484
]))
485485

486-
and printOutConstructorDoc (name, args, gadt) =
486+
and printOutConstructorDoc (name, args, gadt, repr) =
487+
let reprDoc = match repr with
488+
| None -> Doc.nil
489+
| Some s -> Doc.text (s ^ " ") in
487490
let gadtDoc =
488491
match gadt with
489492
| Some outType -> Doc.concat [Doc.text ": "; printOutTypeDoc outType]
@@ -523,7 +526,7 @@ and printOutConstructorDoc (name, args, gadt) =
523526
Doc.rparen;
524527
])
525528
in
526-
Doc.group (Doc.concat [Doc.text name; argsDoc; gadtDoc])
529+
Doc.group (Doc.concat [reprDoc; Doc.text name; argsDoc; gadtDoc])
527530

528531
and printRecordDeclRowDoc (name, mut, opt, arg) =
529532
Doc.group
@@ -808,13 +811,13 @@ and printOutSignatureDoc (signature : Outcometree.out_sig_item list) =
808811
match items with
809812
| Outcometree.Osig_typext (ext, Oext_next) :: items ->
810813
gather_extensions
811-
((ext.oext_name, ext.oext_args, ext.oext_ret_type) :: acc)
814+
((ext.oext_name, ext.oext_args, ext.oext_ret_type, ext.oext_repr) :: acc)
812815
items
813816
| _ -> (List.rev acc, items)
814817
in
815818
let exts, items =
816819
gather_extensions
817-
[(ext.oext_name, ext.oext_args, ext.oext_ret_type)]
820+
[(ext.oext_name, ext.oext_args, ext.oext_ret_type, ext.oext_repr)]
818821
items
819822
in
820823
let te =
@@ -872,7 +875,7 @@ and printOutExtensionConstructorDoc
872875
(if outExt.oext_private = Asttypes.Private then Doc.text "private "
873876
else Doc.nil);
874877
printOutConstructorDoc
875-
(outExt.oext_name, outExt.oext_args, outExt.oext_ret_type);
878+
(outExt.oext_name, outExt.oext_args, outExt.oext_ret_type, outExt.oext_repr);
876879
])
877880

878881
and printOutTypeExtensionDoc (typeExtension : Outcometree.out_type_extension) =
@@ -1084,13 +1087,13 @@ let printOutPhraseSignature signature =
10841087
match items with
10851088
| (Outcometree.Osig_typext (ext, Oext_next), None) :: items ->
10861089
gather_extensions
1087-
((ext.oext_name, ext.oext_args, ext.oext_ret_type) :: acc)
1090+
((ext.oext_name, ext.oext_args, ext.oext_ret_type, ext.oext_repr) :: acc)
10881091
items
10891092
| _ -> (List.rev acc, items)
10901093
in
10911094
let exts, signature =
10921095
gather_extensions
1093-
[(ext.oext_name, ext.oext_args, ext.oext_ret_type)]
1096+
[(ext.oext_name, ext.oext_args, ext.oext_ret_type, ext.oext_repr)]
10941097
signature
10951098
in
10961099
let te =

0 commit comments

Comments
 (0)