Skip to content

Commit 61a6211

Browse files
committed
Allow recursive types.
Fixes rescript-association/genType#585
1 parent 57e546c commit 61a6211

File tree

4 files changed

+19
-21
lines changed

4 files changed

+19
-21
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ These are only breaking changes for unformatted code.
8585
- 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
8686
- Improve code generated for string templates https://github.com/rescript-lang/rescript-compiler/pull/6090
8787
- 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
8889

8990
# 10.1.4
9091

jscomp/gentype/Converter.ml

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,13 @@
11
open GenTypeCommon
22

3-
type t =
4-
| CircularC of string * t
5-
| IdentC
6-
| OptionC of t
7-
| PromiseC of t
8-
| TupleC of t list
3+
type t = IdentC | OptionC of t | PromiseC of t | TupleC of t list
94

105
and groupedArgConverter =
116
| ArgConverter of t
127
| GroupConverter of (string * optional * t) list
138

149
let rec toString converter =
1510
match converter with
16-
| CircularC (s, c) -> "circular(" ^ s ^ " " ^ toString c ^ ")"
1711
| IdentC -> "id"
1812
| OptionC c -> "option(" ^ toString c ^ ")"
1913
| PromiseC c -> "promise(" ^ toString c ^ ")"
@@ -51,6 +45,7 @@ let typeGetConverterNormalized ~config ~inline ~lookupId ~typeNameIsInterface
5145
circular := name;
5246
(IdentC, normalized_))
5347
else
48+
let visited = visited |> StringSet.add name in
5449
match name |> lookupId with
5550
| {CodeItem.annotation = GenTypeOpaque} -> (IdentC, normalized_)
5651
| {annotation = NoGenType} -> (IdentC, normalized_)
@@ -140,16 +135,11 @@ let typeGetConverterNormalized ~config ~inline ~lookupId ~typeNameIsInterface
140135
(converter, {aName; aType = tNormalized})
141136
in
142137
let converter, normalized = type0 |> visit ~visited:StringSet.empty in
143-
let finalConverter =
144-
match !circular <> "" with
145-
| true -> CircularC (!circular, converter)
146-
| false -> converter
147-
in
148138
if !Debug.converter then
149139
Log_.item "Converter type0:%s converter:%s\n"
150140
(type0 |> EmitType.typeToString ~config ~typeNameIsInterface)
151-
(finalConverter |> toString);
152-
(finalConverter, normalized)
141+
(converter |> toString);
142+
(converter, normalized)
153143

154144
let typeGetConverter ~config ~lookupId ~typeNameIsInterface type_ =
155145
type_
@@ -164,7 +154,6 @@ let typeGetNormalized ~config ~inline ~lookupId ~typeNameIsInterface type_ =
164154

165155
let rec converterIsIdentity ~config ~toJS converter =
166156
match converter with
167-
| CircularC (_, c) -> c |> converterIsIdentity ~config ~toJS
168157
| IdentC -> true
169158
| OptionC c -> c |> converterIsIdentity ~config ~toJS
170159
| PromiseC c -> c |> converterIsIdentity ~config ~toJS
@@ -175,12 +164,6 @@ let rec apply ~(config : Config.t) ~converter ~indent ~nameGen ~toJS
175164
~variantTables value =
176165
match converter with
177166
| _ when converter |> converterIsIdentity ~config ~toJS -> value
178-
| CircularC (s, c) ->
179-
value
180-
|> EmitText.addComment
181-
~comment:
182-
("WARNING: circular type " ^ s ^ ". Only shallow converter applied.")
183-
|> apply ~config ~converter:c ~indent ~nameGen ~toJS ~variantTables
184167
| IdentC -> value
185168
| OptionC c ->
186169
EmitText.parens

jscomp/gentype_tests/typescript-react-example/src/Variants.gen.tsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import * as VariantsBS__Es6Import from './Variants.bs';
77
const VariantsBS: any = VariantsBS__Es6Import;
88

9+
import type {list} from '../src/shims/RescriptPervasives.shim';
10+
911
// tslint:disable-next-line:interface-over-type-literal
1012
export type weekday =
1113
"monday"
@@ -35,6 +37,12 @@ export type x2 = "x" | "same";
3537
export type type_ = "type";
3638
export type type = type_;
3739

40+
// tslint:disable-next-line:interface-over-type-literal
41+
export type myList = "E" | { TAG: "C"; _0: number; _1: myList };
42+
43+
// tslint:disable-next-line:interface-over-type-literal
44+
export type builtinList = list<number>;
45+
3846
// tslint:disable-next-line:interface-over-type-literal
3947
export type result1<a,b> =
4048
{ TAG: "Ok"; _0: a }

jscomp/gentype_tests/typescript-react-example/src/Variants.res

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,12 @@ type testGenTypeAs3 = [
7979
@genType @genType.as("type")
8080
type type_ = | @genType.as("type") Type
8181

82+
@genType
83+
type rec myList = E | C(int, myList)
84+
85+
@genType
86+
type builtinList = list<int>
87+
8288
@genType
8389
let polyWithOpt = foo =>
8490
foo === "bar"

0 commit comments

Comments
 (0)