Skip to content

Commit

Permalink
POC print variant runtime repr
Browse files Browse the repository at this point in the history
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
```
  • Loading branch information
cristianoc committed Jun 13, 2024
1 parent e4439c9 commit 08afc2b
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 19 deletions.
17 changes: 10 additions & 7 deletions jscomp/ml/oprint.ml
Original file line number Diff line number Diff line change
Expand Up @@ -499,13 +499,13 @@ and print_out_signature ppf =
match items with
Osig_typext(ext, Oext_next) :: items ->
gather_extensions
((ext.oext_name, ext.oext_args, ext.oext_ret_type) :: acc)
((ext.oext_name, ext.oext_args, ext.oext_ret_type, ext.oext_repr) :: acc)
items
| _ -> (List.rev acc, items)
in
let exts, items =
gather_extensions
[(ext.oext_name, ext.oext_args, ext.oext_ret_type)]
[(ext.oext_name, ext.oext_args, ext.oext_ret_type, ext.oext_repr)]
items
in
let te =
Expand All @@ -531,7 +531,7 @@ and print_out_sig_item ppf =
name !out_class_type clt
| Osig_typext (ext, Oext_exception) ->
fprintf ppf "@[<2>exception %a@]"
print_out_constr (ext.oext_name, ext.oext_args, ext.oext_ret_type)
print_out_constr (ext.oext_name, ext.oext_args, ext.oext_ret_type, ext.oext_repr)
| Osig_typext (ext, _es) ->
print_out_extension_constructor ppf ext
| Osig_modtype (name, Omty_abstract) ->
Expand Down Expand Up @@ -639,7 +639,10 @@ and print_out_type_decl kwd ppf td =
print_immediate
print_unboxed

and print_out_constr ppf (name, tyl,ret_type_opt) =
and print_out_constr ppf (name, tyl, ret_type_opt, repr) =
let () = match repr with
| None -> ()
| Some s -> pp_print_string ppf s in
let name =
match name with
| "::" -> "(::)" (* #7200 *)
Expand Down Expand Up @@ -686,7 +689,7 @@ and print_out_extension_constructor ppf ext =
fprintf ppf "@[<hv 2>type %t +=%s@;<1 2>%a@]"
print_extended_type
(if ext.oext_private = Asttypes.Private then " private" else "")
print_out_constr (ext.oext_name, ext.oext_args, ext.oext_ret_type)
print_out_constr (ext.oext_name, ext.oext_args, ext.oext_ret_type, ext.oext_repr)

and print_out_type_extension ppf te =
let print_extended_type ppf =
Expand Down Expand Up @@ -736,13 +739,13 @@ let rec print_items ppf =
match items with
(Osig_typext(ext, Oext_next), None) :: items ->
gather_extensions
((ext.oext_name, ext.oext_args, ext.oext_ret_type) :: acc)
((ext.oext_name, ext.oext_args, ext.oext_ret_type, ext.oext_repr) :: acc)
items
| _ -> (List.rev acc, items)
in
let exts, items =
gather_extensions
[(ext.oext_name, ext.oext_args, ext.oext_ret_type)]
[(ext.oext_name, ext.oext_args, ext.oext_ret_type, ext.oext_repr)]
items
in
let te =
Expand Down
5 changes: 3 additions & 2 deletions jscomp/ml/outcometree.ml
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ type out_type =
| Otyp_object of (string * out_type) list * bool option
| Otyp_record of (string * bool * bool * out_type) list
| Otyp_stuff of string
| Otyp_sum of (string * out_type list * out_type option) list
| Otyp_sum of (string * out_type list * out_type option * string option) list
| Otyp_tuple of out_type list
| Otyp_var of bool * string
| Otyp_variant of
Expand Down Expand Up @@ -118,11 +118,12 @@ and out_extension_constructor =
oext_type_params: string list;
oext_args: out_type list;
oext_ret_type: out_type option;
oext_repr: string option;
oext_private: Asttypes.private_flag }
and out_type_extension =
{ otyext_name: string;
otyext_params: string list;
otyext_constructors: (string * out_type list * out_type option) list;
otyext_constructors: (string * out_type list * out_type option * string option) list;
otyext_private: Asttypes.private_flag }
and out_val_decl =
{ oval_name: string;
Expand Down
18 changes: 16 additions & 2 deletions jscomp/ml/printtyp.ml
Original file line number Diff line number Diff line change
Expand Up @@ -917,16 +917,29 @@ and tree_of_constructor_arguments = function

and tree_of_constructor cd =
let name = Ident.name cd.cd_id in
let nullary = Ast_untagged_variants.is_nullary_variant cd.cd_args in
let repr =
if not nullary then None
else match Ast_untagged_variants.process_tag_type cd.cd_attributes with
| Some Null -> Some "@as(null)"
| Some Undefined -> Some "@as(undefined)"
| Some (String s) -> Some (Printf.sprintf "@as(%S)" s)
| Some (Int i) -> Some (Printf.sprintf "@as(%d)" i)
| Some (Float f) -> Some (Printf.sprintf "@as(%s)" f)
| Some (Bool b) -> Some (Printf.sprintf "@as(%b)" b)
| Some (BigInt s) -> Some (Printf.sprintf "@as(%sn)" s)
| Some (Untagged _) (* should never happen *)
| None -> None in
let arg () = tree_of_constructor_arguments cd.cd_args in
match cd.cd_res with
| None -> (name, arg (), None)
| None -> (name, arg (), None, repr)
| Some res ->
let nm = !names in
names := [];
let ret = tree_of_typexp false res in
let args = arg () in
names := nm;
(name, args, Some ret)
(name, args, Some ret, repr)

and tree_of_label l =
let opt = l.ld_attributes |> List.exists (fun ({txt}, _) -> txt = "ns.optional" || txt = "res.optional") in
Expand Down Expand Up @@ -982,6 +995,7 @@ let tree_of_extension_constructor id ext es =
oext_type_params = ty_params;
oext_args = args;
oext_ret_type = ret;
oext_repr = None;
oext_private = ext.ext_private }
in
let es =
Expand Down
26 changes: 18 additions & 8 deletions jscomp/syntax/src/res_outcome_printer.ml
Original file line number Diff line number Diff line change
Expand Up @@ -429,8 +429,13 @@ and print_out_constructors_doc constructors =
constructors);
]))

and print_out_constructor_doc (name, args, gadt) =
let gadt_doc =
and print_out_constructor_doc (name, args, gadt, repr) =
let reprDoc =
match repr with
| None -> Doc.nil
| Some s -> Doc.text (s ^ " ")
in
let gadtDoc =
match gadt with
| Some out_type -> Doc.concat [Doc.text ": "; print_out_type_doc out_type]
| None -> Doc.nil
Expand Down Expand Up @@ -469,7 +474,7 @@ and print_out_constructor_doc (name, args, gadt) =
Doc.rparen;
])
in
Doc.group (Doc.concat [Doc.text name; args_doc; gadt_doc])
Doc.group (Doc.concat [reprDoc; Doc.text name; args_doc; gadtDoc])

and print_record_decl_row_doc (name, mut, opt, arg) =
Doc.group
Expand Down Expand Up @@ -758,13 +763,14 @@ and print_out_signature_doc (signature : Outcometree.out_sig_item list) =
match items with
| Outcometree.Osig_typext (ext, Oext_next) :: items ->
gather_extensions
((ext.oext_name, ext.oext_args, ext.oext_ret_type) :: acc)
((ext.oext_name, ext.oext_args, ext.oext_ret_type, ext.oext_repr)
:: acc)
items
| _ -> (List.rev acc, items)
in
let exts, items =
gather_extensions
[(ext.oext_name, ext.oext_args, ext.oext_ret_type)]
[(ext.oext_name, ext.oext_args, ext.oext_ret_type, ext.oext_repr)]
items
in
let te =
Expand Down Expand Up @@ -822,7 +828,10 @@ and print_out_extension_constructor_doc
(if out_ext.oext_private = Asttypes.Private then Doc.text "private "
else Doc.nil);
print_out_constructor_doc
(out_ext.oext_name, out_ext.oext_args, out_ext.oext_ret_type);
( out_ext.oext_name,
out_ext.oext_args,
out_ext.oext_ret_type,
out_ext.oext_repr );
])

and print_out_type_extension_doc
Expand Down Expand Up @@ -1035,13 +1044,14 @@ let print_out_phrase_signature signature =
match items with
| (Outcometree.Osig_typext (ext, Oext_next), None) :: items ->
gather_extensions
((ext.oext_name, ext.oext_args, ext.oext_ret_type) :: acc)
((ext.oext_name, ext.oext_args, ext.oext_ret_type, ext.oext_repr)
:: acc)
items
| _ -> (List.rev acc, items)
in
let exts, signature =
gather_extensions
[(ext.oext_name, ext.oext_args, ext.oext_ret_type)]
[(ext.oext_name, ext.oext_args, ext.oext_ret_type, ext.oext_repr)]
signature
in
let te =
Expand Down

0 comments on commit 08afc2b

Please sign in to comment.