Skip to content

Commit 8e429c4

Browse files
committed
Add untagged variant support to genType.
1 parent 0743c95 commit 8e429c4

6 files changed

+30
-21
lines changed

jscomp/gentype/GenTypeCommon.ml

+4-4
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ type type_ =
6868
| Promise of type_
6969
| Tuple of type_ list
7070
| TypeVar of string
71-
| Variant of variant
71+
| Variant of variant (* ordinary and polymorphic variants *)
7272

7373
and fields = field list
7474
and argType = {aName: string; aType: type_}
@@ -95,7 +95,7 @@ and variant = {
9595
inherits: type_ list;
9696
noPayloads: case list;
9797
payloads: payload list;
98-
polymorphic: bool;
98+
polymorphic: bool; (* If true, this is a polymorphic variant *)
9999
unboxed: bool;
100100
}
101101

@@ -165,8 +165,8 @@ let rec depToResolvedName (dep : dep) =
165165
| Internal resolvedName -> resolvedName
166166
| Dot (p, s) -> ResolvedName.dot s (p |> depToResolvedName)
167167

168-
let createVariant ~bsStringOrInt ~inherits ~noPayloads ~payloads ~polymorphic =
169-
let unboxed = payloads = [] in
168+
let createVariant ~bsStringOrInt ~inherits ~noPayloads ~payloads ~polymorphic
169+
~unboxed =
170170
Variant {bsStringOrInt; inherits; noPayloads; payloads; polymorphic; unboxed}
171171

172172
let ident ?(builtin = true) ?(typeArgs = []) name =

jscomp/gentype/TranslateCoreType.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ and translateCoreType_ ~config ~typeVarsGen
225225
let inherits = inheritsTranslations |> List.map (fun {type_} -> type_) in
226226
let type_ =
227227
createVariant ~bsStringOrInt:(asString || asInt) ~noPayloads ~payloads
228-
~inherits ~polymorphic:true
228+
~inherits ~polymorphic:true ~unboxed:false
229229
in
230230
let dependencies =
231231
(inheritsTranslations

jscomp/gentype/TranslateTypeDeclarations.ml

+6-9
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ let createCase (label, attributes) =
2626
(match
2727
attributes |> Annotation.getAttributePayload Annotation.tagIsAs
2828
with
29-
| Some (_, IdentPayload (Lident "null")) -> NullLabel
30-
| Some (_, IdentPayload (Lident "undefined")) -> UndefinedLabel
31-
| Some (_, BoolPayload b) -> BoolLabel b
29+
| Some (_, IdentPayload (Lident "null")) -> NullLabel
30+
| Some (_, IdentPayload (Lident "undefined")) -> UndefinedLabel
31+
| Some (_, BoolPayload b) -> BoolLabel b
3232
| Some (_, FloatPayload s) -> FloatLabel s
3333
| Some (_, IntPayload i) -> IntLabel i
3434
| Some (_, StringPayload asLabel) -> StringLabel asLabel
@@ -197,7 +197,7 @@ let traslateDeclarationKind ~config ~loc ~outputFileRelative ~resolver
197197
else variant.payloads
198198
in
199199
createVariant ~bsStringOrInt:false ~inherits:variant.inherits
200-
~noPayloads ~payloads ~polymorphic:true
200+
~noPayloads ~payloads ~polymorphic:true ~unboxed:false
201201
| _ -> translation.type_
202202
in
203203
{translation with type_} |> handleGeneralDeclaration
@@ -311,11 +311,8 @@ let traslateDeclarationKind ~config ~loc ~outputFileRelative ~resolver
311311
})
312312
in
313313
let variantTyp =
314-
match (noPayloads, payloads) with
315-
| [], [{t = type_}] when unboxedAnnotation -> type_
316-
| _ ->
317-
createVariant ~bsStringOrInt:false ~inherits:[] ~noPayloads ~payloads
318-
~polymorphic:false
314+
createVariant ~bsStringOrInt:false ~inherits:[] ~noPayloads ~payloads
315+
~polymorphic:false ~unboxed:unboxedAnnotation
319316
in
320317
let resolvedTypeName = typeName |> TypeEnv.addModulePath ~typeEnv in
321318
let exportFromTypeDeclaration =

jscomp/gentype/TranslateTypeExprFromTypes.ml

+3-3
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ let translateConstr ~config ~paramsTranslation ~(path : Path.t) ~typeEnv =
146146
case 0 "Ok" paramTranslation1.type_;
147147
case 1 "Error" paramTranslation2.type_;
148148
]
149-
~polymorphic:false
149+
~polymorphic:false ~unboxed:false
150150
in
151151
{
152152
dependencies =
@@ -408,7 +408,7 @@ and translateTypeExprFromTypes_ ~config ~typeVarsGen ~typeEnv
408408
in
409409
let type_ =
410410
createVariant ~bsStringOrInt:false ~inherits:[] ~noPayloads ~payloads:[]
411-
~polymorphic:true
411+
~polymorphic:true ~unboxed:false
412412
in
413413
{dependencies = []; type_}
414414
| {noPayloads = []; payloads = [(_label, t)]; unknowns = []} ->
@@ -439,7 +439,7 @@ and translateTypeExprFromTypes_ ~config ~typeVarsGen ~typeEnv
439439
in
440440
let type_ =
441441
createVariant ~bsStringOrInt:false ~inherits:[] ~noPayloads ~payloads
442-
~polymorphic:true
442+
~polymorphic:true ~unboxed:false
443443
in
444444
let dependencies =
445445
payloadTranslations

jscomp/test/variantsMatching.gen.tsx

+12
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,15 @@ export type t = "thisIsA" | 42 | null | "D" | 3.14;
77

88
// tslint:disable-next-line:interface-over-type-literal
99
export type tNU = null | undefined;
10+
11+
// tslint:disable-next-line:interface-over-type-literal
12+
export type MyUndefined_t<a> = undefined | a;
13+
14+
// tslint:disable-next-line:interface-over-type-literal
15+
export type MyNull_t<a> = null | a;
16+
17+
// tslint:disable-next-line:interface-over-type-literal
18+
export type MyNullable_t<a> = null | undefined | a;
19+
20+
// tslint:disable-next-line:interface-over-type-literal
21+
export type MyNullableExtended_t<a> = null | undefined | "WhyNotAnotherOne" | a;

jscomp/test/variantsMatching.res

+4-4
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ module CustomizeTags = {
107107
}
108108

109109
module MyUndefined = {
110-
@unboxed
110+
@genType @unboxed
111111
type t<'a> = | @as(undefined) Undefined | Present('a)
112112
// Note: 'a must not have undefined as value
113113
// There can be only one with payload, with 1 argument, to use unboxed
@@ -125,7 +125,7 @@ module MyUndefined = {
125125
}
126126

127127
module MyNull = {
128-
@unboxed
128+
@genType @unboxed
129129
type t<'a> = | @as(null) Null | Present('a)
130130
// Note: 'a must not have null as value
131131
// There can be only one with payload, with 1 argument, to use unboxed
@@ -143,7 +143,7 @@ module MyNull = {
143143
}
144144

145145
module MyNullable = {
146-
@unboxed
146+
@genType @unboxed
147147
type t<'a> =
148148
| @as(null) Null
149149
| @as(undefined) Undefined
@@ -176,7 +176,7 @@ module MyNullable = {
176176
}
177177

178178
module MyNullableExtended = {
179-
@unboxed
179+
@genType @unboxed
180180
type t<'a> =
181181
| @as(null) Null
182182
| @as(undefined) Undefined

0 commit comments

Comments
 (0)