Skip to content

Commit 53cd497

Browse files
committed
GenType: removed support for @genType.as which has become unnecessary.
1 parent 273c4f4 commit 53cd497

38 files changed

+227
-265
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ These are only breaking changes for unformatted code.
4646
- Remove deprecated module `Printexc`
4747
- `@deriving(jsConverter)` not supported anymore for variant types https://github.com/rescript-lang/rescript-compiler/pull/6088
4848
- New representation for variants, where the tag is a string instead of a number. https://github.com/rescript-lang/rescript-compiler/pull/6088
49+
- 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.
4950

5051
#### :bug: Bug Fix
5152

Makefile

Lines changed: 2 additions & 2 deletions
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
@@ -42,7 +42,7 @@ test-gentype:
4242
test-all: test test-gentype
4343

4444
reanalyze:
45-
reanalyze.exe -set-exit-code -all-cmt _build/default/res_syntax -suppress res_syntax/testrunner
45+
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
4646

4747
lib: build node_modules/.bin/semver
4848
node scripts/ninja.js config

jscomp/gentype/Annotation.ml

Lines changed: 52 additions & 28 deletions
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 () ->

jscomp/gentype/Converter.ml

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -198,10 +198,10 @@ let typeGetConverterNormalized ~config ~inline ~lookupId ~typeNameIsInterface
198198
in
199199
( ObjectC
200200
(fieldsConverted
201-
|> List.map (fun ({nameJS; nameRE; optional}, (converter, _)) ->
201+
|> List.map (fun ({nameJS; optional}, (converter, _)) ->
202202
{
203203
lblJS = nameJS;
204-
lblRE = nameRE;
204+
lblRE = nameJS;
205205
c =
206206
(match optional = Mandatory with
207207
| true -> converter
@@ -357,14 +357,7 @@ let rec converterIsIdentity ~config ~toJS converter =
357357
argConverter |> converterIsIdentity ~config ~toJS:(not toJS)
358358
| GroupConverter _ -> false)
359359
| IdentC -> true
360-
| ObjectC fieldsC ->
361-
fieldsC
362-
|> List.for_all (fun {lblJS; lblRE; c} ->
363-
lblJS = lblRE
364-
&&
365-
match c with
366-
| OptionC c1 -> c1 |> converterIsIdentity ~config ~toJS
367-
| _ -> c |> converterIsIdentity ~config ~toJS)
360+
| ObjectC _ -> true
368361
| OptionC c -> c |> converterIsIdentity ~config ~toJS
369362
| PromiseC c -> c |> converterIsIdentity ~config ~toJS
370363
| TupleC innerTypesC ->
@@ -456,7 +449,7 @@ let rec apply ~config ~converter ~indent ~nameGen ~toJS ~variantTables value =
456449
groupConverters
457450
|> List.mapi (fun i (s, _optional, argConverter) ->
458451
s ^ ":"
459-
^ (varNamesArr.(i)
452+
^ ((varNamesArr.(i) [@doesNotRaise])
460453
|> apply ~config ~converter:argConverter ~indent:indent2
461454
~nameGen ~toJS:notToJS ~variantTables))
462455
|> String.concat ", "

jscomp/gentype/EmitJs.ml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -513,7 +513,9 @@ let rec readCmtFilesRecursively ~config ~env ~inputCmtTranslateTypeDeclarations
513513
let exportTypeMapFromCmt =
514514
typeDeclarations
515515
|> createExportTypeMap ~config ~fromCmtReadRecursively:true
516-
~file:(cmtFile |> Filename.basename |> Filename.chop_extension)
516+
~file:
517+
(cmtFile |> Filename.basename
518+
|> (Filename.chop_extension [@doesNotRaise]))
517519
in
518520
let cmtToExportTypeMap =
519521
env.cmtToExportTypeMap |> StringMap.add cmtFile exportTypeMapFromCmt

jscomp/gentype/EmitText.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ let newNameGen () = Hashtbl.create 1
4949
let quotes x = "\"" ^ x ^ "\""
5050

5151
let quotesIfRequired x =
52-
match String.length x > 0 && x.[0] = '"' with
52+
match String.length x > 0 && (x.[0] [@doesNotRaise]) = '"' with
5353
| true -> x
5454
| false -> quotes x
5555

jscomp/gentype/EmitType.ml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,16 +60,15 @@ let typeReactRef ~type_ =
6060
{
6161
mutable_ = Mutable;
6262
nameJS = reactRefCurrent;
63-
nameRE = reactRefCurrent;
6463
optional = Mandatory;
6564
type_ = Null type_;
6665
};
6766
] )
6867

6968
let isTypeReactRef ~fields =
7069
match fields with
71-
| [{mutable_ = Mutable; nameJS; nameRE; optional = Mandatory}] ->
72-
nameJS == reactRefCurrent && nameJS == nameRE
70+
| [{mutable_ = Mutable; nameJS; optional = Mandatory}] ->
71+
nameJS == reactRefCurrent
7372
| _ -> false
7473

7574
let isTypeFunctionComponent ~fields type_ =
@@ -182,7 +181,6 @@ let rec renderType ~(config : Config.t) ?(indent = None) ~typeNameIsInterface
182181
{
183182
mutable_ = Mutable;
184183
nameJS = name;
185-
nameRE = name;
186184
optional = Mandatory;
187185
type_ = TypeVar value;
188186
}

jscomp/gentype/ExportModule.ml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ and exportModuleItemToFields =
5656
{
5757
mutable_ = Mutable;
5858
nameJS = fieldName;
59-
nameRE = fieldName;
6059
optional = Mandatory;
6160
type_ = typeForType;
6261
}

jscomp/gentype/GenTypeCommon.ml

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ type case = {label: string; labelJS: labelJS}
1818

1919
let isJSSafePropertyName name =
2020
name = ""
21-
|| (match name.[0] with
21+
|| (match name.[0] [@doesNotRaise] with
2222
| 'A' .. 'z' -> true
2323
| _ -> false)
2424
&& name
@@ -36,12 +36,12 @@ let labelJSToString ?(alwaysQuotes = false) case =
3636
let len = String.length s in
3737
len > 0
3838
&& (match len > 1 with
39-
| true -> s.[0] > '0'
39+
| true -> (s.[0] [@doesNotRaise]) > '0'
4040
| false -> true)
4141
&&
4242
let res = ref true in
4343
for i = 0 to len - 1 do
44-
match s.[i] with
44+
match s.[i] [@doesNotRaise] with
4545
| '0' .. '9' -> ()
4646
| _ -> res := false
4747
done;
@@ -78,7 +78,6 @@ and argType = {aName: string; aType: type_}
7878
and field = {
7979
mutable_: mutable_;
8080
nameJS: string;
81-
nameRE: string;
8281
optional: optional;
8382
type_: type_;
8483
}
@@ -248,7 +247,7 @@ module NodeFilename = struct
248247

249248
let concat dirname filename =
250249
let isDirSep s i =
251-
let c = s.[i] in
250+
let c = (s.[i] [@doesNotRaise]) in
252251
c = '/' || c = '\\' || c = ':'
253252
in
254253
let l = length dirname in

jscomp/gentype/GenTypeConfig.ml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,9 @@ let getShims map =
7676
| Ext_json_types.Str {str} ->
7777
let fromTo = str |> String.split_on_char '=' |> Array.of_list in
7878
assert (Array.length fromTo == 2);
79-
shims := (fromTo.(0), fromTo.(1)) :: !shims
79+
shims :=
80+
((fromTo.(0) [@doesNotRaise]), (fromTo.(1) [@doesNotRaise]))
81+
:: !shims
8082
| _ -> ())
8183
| _ -> ());
8284
!shims

jscomp/gentype/GenTypeMain.ml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,9 @@ let processCmtFile cmt =
126126
inputCMT |> cmtCheckAnnotations ~checkAnnotation
127127
in
128128
if isInterface then
129-
let cmtFileImpl = (cmtFile |> Filename.chop_extension) ^ ".cmt" in
129+
let cmtFileImpl =
130+
(cmtFile |> (Filename.chop_extension [@doesNotRaise])) ^ ".cmt"
131+
in
130132
let inputCMTImpl = readCmt cmtFileImpl in
131133
let hasGenTypeAnnotationsImpl =
132134
inputCMTImpl
@@ -168,3 +170,4 @@ let processCmtFile cmt =
168170
else (
169171
outputFile |> GeneratedFiles.logFileAction NoMatch;
170172
if Sys.file_exists outputFile then Sys.remove outputFile)
173+
[@@live]

jscomp/gentype/GeneratedFiles.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ let readLines (file : string) : string list =
2727
done;
2828
[]
2929
with End_of_file ->
30-
close_in chan;
30+
close_in chan [@doesNotRaise];
3131
!lines |> List.rev
3232
in
3333
finished_lines
@@ -37,7 +37,7 @@ let readFile (file : string) : string = String.concat "\n" (readLines file)
3737
let writeFile (filePath : string) (contents : string) =
3838
let outFile = open_out filePath in
3939
output_string outFile contents;
40-
close_out outFile
40+
close_out outFile [@doesNotRaise]
4141

4242
let writeFileIfRequired ~outputFile ~fileContents =
4343
if Sys.file_exists outputFile then

jscomp/gentype/ModuleName.ml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ let sanitizeId s =
1313
| c -> c)
1414
else s
1515
in
16-
if s <> "" && s.[0] >= 'A' && s.[0] <= 'z' then s else "_" ^ s
16+
if s <> "" && (s.[0] [@doesNotRaise]) >= 'A' && (s.[0] [@doesNotRaise]) <= 'z'
17+
then s
18+
else "_" ^ s
1719

1820
let forBsFile s = sanitizeId s ^ "BS"
1921

jscomp/gentype/NamedArgs.ml

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,25 +14,13 @@ let rec groupReversed ~revCurGroup ~revResult labeledTypes =
1414
(* Add it to the current group, not result. *)
1515
groupReversed
1616
~revCurGroup:
17-
({
18-
mutable_ = Immutable;
19-
nameJS = name;
20-
nameRE = name;
21-
optional = Optional;
22-
type_;
23-
}
17+
({mutable_ = Immutable; nameJS = name; optional = Optional; type_}
2418
:: revCurGroup)
2519
~revResult tl
2620
| _, (Label name, type_) :: tl ->
2721
groupReversed
2822
~revCurGroup:
29-
({
30-
mutable_ = Immutable;
31-
nameJS = name;
32-
nameRE = name;
33-
optional = Mandatory;
34-
type_;
35-
}
23+
({mutable_ = Immutable; nameJS = name; optional = Mandatory; type_}
3624
:: revCurGroup)
3725
~revResult tl
3826
| [], [] -> revResult

0 commit comments

Comments
 (0)