Skip to content

Commit b47de85

Browse files
authored
GenType: removed support for @genType.as in type definitions, which has become unnecessary. (#6099)
* Emit tags as strings. * Allow more general type for tags. Compile is_tag to `!== "object"` instead of `=== "string"`. * Do not special case variants with only 1 case with payload. Also the comment is not emitted anymore, since there's always a tag. Not special casing means that the representation is uniform, and does not change when the type is extended. This is important with zero cost ffi, where the runtime representation is exposed to the user, to reduce possible surprises. * Support @as("foo") to customize the representation of tags. * Add support for @tag(...) to customize the property used for the tag. * GenType: removed support for `@genType.as` which has become unnecessary. * Restore res_syntax analysis * Remove object converter. * Remove conversion for variants. * Emit the correct type for the variants runtime representation. * Don't auto unbox variants with 1 payload. * Refactor. * Remove array converter * Remove function converter. * Allow recursive types. Fixes rescript-association/genType#585 * Remove option converter. * Remove some conversion code. * Remove conversion code. * Cleanup * Dead code.
1 parent c73714f commit b47de85

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+488
-1580
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ These are only breaking changes for unformatted code.
4747
- Remove deprecated module `Printexc`
4848
- `@deriving(jsConverter)` not supported anymore for variant types https://github.com/rescript-lang/rescript-compiler/pull/6088
4949
- New representation for variants, where the tag is a string instead of a number. https://github.com/rescript-lang/rescript-compiler/pull/6088
50+
- GenType: removed support for `@genType.as` for records and variants which has become unnecessary. Use the language's `@as` instead to channge the runtime representation without requiring any runtime conversion during FFI.
5051

5152
#### :bug: Bug Fix
5253

@@ -84,6 +85,7 @@ These are only breaking changes for unformatted code.
8485
- Change the compilation of pattern matching for variants so it does not depends on variats being integers https://github.com/rescript-lang/rescript-compiler/pull/6085
8586
- Improve code generated for string templates https://github.com/rescript-lang/rescript-compiler/pull/6090
8687
- Move Jsx and JsxDOM and JsxEvent and JsxPPXReactSupport inside Pervasives and build them separately for curried and uncurried mode https://github.com/rescript-lang/rescript-compiler/pull/6091
88+
- Gentype: allow recursive data types https://github.com/rescript-association/genType/issues/585
8789

8890
# 10.1.4
8991

Makefile

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ bench:
1313
$(DUNE_BIN_DIR)/syntax_benchmarks
1414

1515
dce:
16-
reanalyze.exe -- -dce-cmt _build
16+
reanalyze.exe -dce-cmt _build/default/jscomp
1717

1818
ninja/ninja:
1919
./scripts/buildNinjaBinary.js
@@ -43,6 +43,7 @@ test-all: test test-gentype
4343

4444
reanalyze:
4545
reanalyze.exe -set-exit-code -all-cmt _build/default/res_syntax -suppress res_syntax/testrunner
46+
reanalyze.exe -set-exit-code -all-cmt _build/default/jscomp -suppress res_syntax/testrunner -exclude-paths jscomp/super_errors,jscomp/outcome_printer,jscomp/ounit_tests,jscomp/ml,jscomp/js_parser,jscomp/frontend,jscomp/ext,jscomp/depends,jscomp/core,jscomp/common,jscomp/cmij,jscomp/bsb_helper,jscomp/bsb
4647

4748
lib: build node_modules/.bin/semver
4849
node scripts/ninja.js config

jscomp/frontend/ast_attributes.mli

+1-1
Original file line numberDiff line numberDiff line change
@@ -94,4 +94,4 @@ val process_send_pipe : t -> (Parsetree.core_type * t) option
9494

9595
val process_as_value : t -> Lambda.as_value option
9696

97-
val process_tag_name : t -> string option
97+
val process_tag_name : t -> string option

jscomp/gentype/Annotation.ml

+52-28
Original file line numberDiff line numberDiff line change
@@ -64,65 +64,89 @@ let rec getAttributePayload checkText (attributes : Typedtree.attributes) =
6464
in
6565
match attributes with
6666
| [] -> None
67-
| ({Asttypes.txt}, payload) :: _tl when checkText txt -> (
67+
| ({txt; loc}, payload) :: _tl when checkText txt -> (
68+
let payload =
69+
match payload with
70+
| PStr [] -> Some UnrecognizedPayload
71+
| PStr ({pstr_desc = Pstr_eval (expr, _)} :: _) -> expr |> fromExpr
72+
| PStr ({pstr_desc = Pstr_extension _} :: _) -> Some UnrecognizedPayload
73+
| PStr ({pstr_desc = Pstr_value _} :: _) -> Some UnrecognizedPayload
74+
| PStr ({pstr_desc = Pstr_primitive _} :: _) -> Some UnrecognizedPayload
75+
| PStr ({pstr_desc = Pstr_type _} :: _) -> Some UnrecognizedPayload
76+
| PStr ({pstr_desc = Pstr_typext _} :: _) -> Some UnrecognizedPayload
77+
| PStr ({pstr_desc = Pstr_exception _} :: _) -> Some UnrecognizedPayload
78+
| PStr ({pstr_desc = Pstr_module _} :: _) -> Some UnrecognizedPayload
79+
| PStr ({pstr_desc = Pstr_recmodule _} :: _) -> Some UnrecognizedPayload
80+
| PStr ({pstr_desc = Pstr_modtype _} :: _) -> Some UnrecognizedPayload
81+
| PStr ({pstr_desc = Pstr_open _} :: _) -> Some UnrecognizedPayload
82+
| PStr ({pstr_desc = Pstr_class _} :: _) -> Some UnrecognizedPayload
83+
| PStr ({pstr_desc = Pstr_class_type _} :: _) -> Some UnrecognizedPayload
84+
| PStr ({pstr_desc = Pstr_include _} :: _) -> Some UnrecognizedPayload
85+
| PStr ({pstr_desc = Pstr_attribute _} :: _) -> Some UnrecognizedPayload
86+
| PPat _ -> Some UnrecognizedPayload
87+
| PSig _ -> Some UnrecognizedPayload
88+
| PTyp _ -> Some UnrecognizedPayload
89+
in
6890
match payload with
69-
| PStr [] -> Some UnrecognizedPayload
70-
| PStr ({pstr_desc = Pstr_eval (expr, _)} :: _) -> expr |> fromExpr
71-
| PStr ({pstr_desc = Pstr_extension _} :: _) -> Some UnrecognizedPayload
72-
| PStr ({pstr_desc = Pstr_value _} :: _) -> Some UnrecognizedPayload
73-
| PStr ({pstr_desc = Pstr_primitive _} :: _) -> Some UnrecognizedPayload
74-
| PStr ({pstr_desc = Pstr_type _} :: _) -> Some UnrecognizedPayload
75-
| PStr ({pstr_desc = Pstr_typext _} :: _) -> Some UnrecognizedPayload
76-
| PStr ({pstr_desc = Pstr_exception _} :: _) -> Some UnrecognizedPayload
77-
| PStr ({pstr_desc = Pstr_module _} :: _) -> Some UnrecognizedPayload
78-
| PStr ({pstr_desc = Pstr_recmodule _} :: _) -> Some UnrecognizedPayload
79-
| PStr ({pstr_desc = Pstr_modtype _} :: _) -> Some UnrecognizedPayload
80-
| PStr ({pstr_desc = Pstr_open _} :: _) -> Some UnrecognizedPayload
81-
| PStr ({pstr_desc = Pstr_class _} :: _) -> Some UnrecognizedPayload
82-
| PStr ({pstr_desc = Pstr_class_type _} :: _) -> Some UnrecognizedPayload
83-
| PStr ({pstr_desc = Pstr_include _} :: _) -> Some UnrecognizedPayload
84-
| PStr ({pstr_desc = Pstr_attribute _} :: _) -> Some UnrecognizedPayload
85-
| PPat _ -> Some UnrecognizedPayload
86-
| PSig _ -> Some UnrecognizedPayload
87-
| PTyp _ -> Some UnrecognizedPayload)
91+
| None -> None
92+
| Some payload -> Some (loc, payload))
8893
| _hd :: tl -> getAttributePayload checkText tl
8994

9095
let getGenTypeAsRenaming attributes =
9196
match attributes |> getAttributePayload tagIsGenTypeAs with
92-
| Some (StringPayload s) -> Some s
97+
| Some (_, StringPayload s) -> Some s
9398
| None -> (
9499
match attributes |> getAttributePayload tagIsGenType with
95-
| Some (StringPayload s) -> Some s
100+
| Some (_, StringPayload s) -> Some s
96101
| _ -> None)
97102
| _ -> None
98103

104+
(* This is not supported anymore: only use to give a warning *)
105+
let checkUnsupportedGenTypeAsRenaming attributes =
106+
let error ~loc =
107+
Log_.Color.setup ();
108+
Log_.info ~loc ~name:"Warning genType" (fun ppf () ->
109+
Format.fprintf ppf
110+
"@\n\
111+
@genType.as is not supported anymore in type definitions. Use @as \
112+
from the language.")
113+
in
114+
match attributes |> getAttributePayload tagIsGenTypeAs with
115+
| Some (loc, _) -> error ~loc
116+
| None -> (
117+
match attributes |> getAttributePayload tagIsGenType with
118+
| Some (loc, _) -> error ~loc
119+
| None -> ())
120+
99121
let getBsAsRenaming attributes =
100122
match attributes |> getAttributePayload tagIsBsAs with
101-
| Some (StringPayload s) -> Some s
123+
| Some (_, StringPayload s) -> Some s
102124
| _ -> None
103125

104126
let getBsAsInt attributes =
105127
match attributes |> getAttributePayload tagIsBsAs with
106-
| Some (IntPayload s) -> (
128+
| Some (_, IntPayload s) -> (
107129
try Some (int_of_string s) with Failure _ -> None)
108130
| _ -> None
109131

110132
let getAttributeImportRenaming attributes =
111133
let attributeImport = attributes |> getAttributePayload tagIsGenTypeImport in
112134
let genTypeAsRenaming = attributes |> getGenTypeAsRenaming in
113135
match (attributeImport, genTypeAsRenaming) with
114-
| Some (StringPayload importString), _ ->
136+
| Some (_, StringPayload importString), _ ->
115137
(Some importString, genTypeAsRenaming)
116138
| ( Some
117-
(TuplePayload [StringPayload importString; StringPayload renameString]),
139+
( _,
140+
TuplePayload [StringPayload importString; StringPayload renameString]
141+
),
118142
_ ) ->
119143
(Some importString, Some renameString)
120144
| _ -> (None, genTypeAsRenaming)
121145

122146
let getDocString attributes =
123147
let docPayload = attributes |> getAttributePayload tagIsOcamlDoc in
124148
match docPayload with
125-
| Some (StringPayload docString) -> "/** " ^ docString ^ " */\n"
149+
| Some (_, StringPayload docString) -> "/** " ^ docString ^ " */\n"
126150
| _ -> ""
127151

128152
let hasAttribute checkText (attributes : Typedtree.attributes) =
@@ -133,7 +157,7 @@ let fromAttributes ~loc (attributes : Typedtree.attributes) =
133157
else if hasAttribute (fun s -> tagIsGenType s || tagIsGenTypeAs s) attributes
134158
then (
135159
(match attributes |> getAttributePayload tagIsGenType with
136-
| Some UnrecognizedPayload -> ()
160+
| Some (_, UnrecognizedPayload) -> ()
137161
| Some _ ->
138162
Log_.Color.setup ();
139163
Log_.info ~loc ~name:"Warning genType" (fun ppf () ->

0 commit comments

Comments
 (0)