Skip to content

Commit 11cde90

Browse files
Copilottsnobip
andauthored
Deprecate @raises in favor of @throws in reanalyze (#7932)
* Initial plan * Deprecate @raises in favor of @throws in reanalyze - Update reanalyze Exception.ml to accept both @throws and @raises decorators - Update warning messages to use @throws terminology - Add @throws decorator to completion with documentation - Mark @raises as deprecated in completion - Update all test files to use @throws instead of @raises - Update expected test outputs to reflect @throws terminology - Update Stdlib_JSON to use @throws instead of @raises Co-authored-by: tsnobip <2479216+tsnobip@users.noreply.github.com> * replace other references of `raise` with `throw` * update "Contribute to the API Reference" * update changelog * update docs some more * rename some more raises * rename raise to throw in API docs * remove unused @throws annotations * update completion --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: tsnobip <2479216+tsnobip@users.noreply.github.com> Co-authored-by: tsnobip <dontshootthink@gmail.com>
1 parent 112ebd5 commit 11cde90

Some content is hidden

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

67 files changed

+373
-359
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#### :nail_care: Polish
3232

3333
- Rewatch cli: do not show build command options in the root help. https://github.com/rescript-lang/rescript/pull/7715
34+
- Deprecate reanalyze `@raises` in favor of `@throws`. https://github.com/rescript-lang/rescript/pull/7932
3435

3536
#### :house: Internal
3637

CONTRIBUTING.md

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -334,30 +334,15 @@ Note that there's currently still a manual step involved on [rescript-lang.org](
334334

335335
## Contribute to the API Reference
336336

337-
The API reference is generated from doc comments in the source code. [Here](https://github.com/rescript-lang/rescript-compiler/blob/99650/jscomp/others/js_re.mli#L146-L161)'s a good example.
337+
The API reference is generated from doc comments in the source code. [Here](https://github.com/rescript-lang/rescript/blob/57c696b1a38f53badaddcc082ed29188d80df70d/packages/%40rescript/runtime/Stdlib_String.resi#L441-L458)'s a good example.
338338

339339
Some tips:
340340

341-
- The first sentence or line should be a very short summary. This is used in indexes and by tools like merlin.
342-
- Ideally, every function should have **at least one** `@example`.
343-
- Cross-reference another definition with `{! identifier}`. But use them sparingly, they’re a bit verbose (currently, at least).
344-
- Wrap non-cross-referenced identifiers and other code in `[ ... ]`.
345-
- Escape `{`, `}`, `[`, `]` and `@` using `\`.
346-
- It’s possible to use `{%html ...}` to generate custom html, but use this very, very sparingly.
347-
- A number of "documentation tags" are provided that would be nice to use, but unfortunately they’re often not supported for \`external\`s. Which is of course most of the API.
348-
- `@param` usually doesn’t work. Use `{b <param>} ...` instead
349-
- `@returns` usually doesn’t work. Use `{b returns} ...` instead.
341+
- The first sentence or line should show the function call with a very short summary.
342+
- Ideally, every function should have an `## Examples` section with **at least one** example. The examples are compiled to check that they are correct. Use `==` to generate tests from the examples.
350343
- Always use `@deprecated` when applicable.
351-
- Always use `@raise` when applicable.
352-
- Always provide a `@see` tag pointing to MDN for more information when available.
353-
354-
See [Ocamldoc documentation](http://caml.inria.fr/pub/docs/manual-ocaml/ocamldoc.html#sec333) for more details.
355-
356-
To generate the html:
357-
358-
```sh
359-
../scripts/ninja docs
360-
```
344+
- Always use `@throw` when applicable.
345+
- Always provide a `See` section pointing to MDN for more information when available.
361346

362347
## Contribute to JSX `domProps`
363348

@@ -389,6 +374,7 @@ Adding a new entry there requires re-running the analysis tests. Follow these st
389374
(If a `make` command fails, consider using the [DevContainer](#b-devcontainer).)
390375

391376
Finally, add a line to [CHANGELOG.md](CHANGELOG.md), using the `#### :nail_care: Polish` section.
377+
392378
## Code structure
393379

394380
The highlevel architecture is illustrated as below:

analysis/reanalyze/src/Common.ml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -205,12 +205,12 @@ type line = {mutable declarations: decl list; original: string}
205205

206206
module ExnSet = Set.Make (Exn)
207207

208-
type missingRaiseInfo = {
208+
type missingThrowInfo = {
209209
exnName: string;
210210
exnTable: (Exn.t, LocSet.t) Hashtbl.t;
211211
locFull: Location.t;
212212
missingAnnotations: ExnSet.t;
213-
raiseSet: ExnSet.t;
213+
throwSet: ExnSet.t;
214214
}
215215

216216
type severity = Warning | Error
@@ -234,7 +234,7 @@ type lineAnnotation = (decl * line) option
234234
type description =
235235
| Circular of {message: string}
236236
| ExceptionAnalysis of {message: string}
237-
| ExceptionAnalysisMissing of missingRaiseInfo
237+
| ExceptionAnalysisMissing of missingThrowInfo
238238
| DeadModule of {message: string}
239239
| DeadOptional of {deadOptional: deadOptional; message: string}
240240
| DeadWarning of {

analysis/reanalyze/src/Exception.ml

Lines changed: 50 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,9 @@ module Event = struct
6666
type kind =
6767
| Catches of t list (* with | E => ... *)
6868
| Call of {callee: Common.Path.t; modulePath: Common.Path.t} (* foo() *)
69-
| DoesNotRaise of
70-
t list (* DoesNotRaise(events) where events come from an expression *)
71-
| Raises (** raise E *)
69+
| DoesNotThrow of
70+
t list (* DoesNotThrow(events) where events come from an expression *)
71+
| Throws (** throw E *)
7272

7373
and t = {exceptions: Exceptions.t; kind: kind; loc: Location.t}
7474

@@ -81,14 +81,14 @@ module Event = struct
8181
(modulePath |> Common.Path.toString)
8282
(Exceptions.pp ~exnTable:None)
8383
exceptions
84-
| {kind = DoesNotRaise nestedEvents; loc} ->
85-
Format.fprintf ppf "%s DoesNotRaise(%a)@."
84+
| {kind = DoesNotThrow nestedEvents; loc} ->
85+
Format.fprintf ppf "%s DoesNotThrow(%a)@."
8686
(loc.loc_start |> posToString)
8787
(fun ppf () ->
8888
nestedEvents |> List.iter (fun e -> Format.fprintf ppf "%a " print e))
8989
()
90-
| {kind = Raises; exceptions; loc} ->
91-
Format.fprintf ppf "%s raises %a@."
90+
| {kind = Throws; exceptions; loc} ->
91+
Format.fprintf ppf "%s throws %a@."
9292
(loc.loc_start |> posToString)
9393
(Exceptions.pp ~exnTable:None)
9494
exceptions
@@ -118,7 +118,7 @@ module Event = struct
118118
in
119119
let rec loop exnSet events =
120120
match events with
121-
| ({kind = Raises; exceptions; loc} as ev) :: rest ->
121+
| ({kind = Throws; exceptions; loc} as ev) :: rest ->
122122
if !Common.Cli.debug then Log_.item "%a@." print ev;
123123
exceptions |> Exceptions.iter (fun exn -> extendExnTable exn loc);
124124
loop (Exceptions.union exnSet exceptions) rest
@@ -134,7 +134,7 @@ module Event = struct
134134
in
135135
exceptions |> Exceptions.iter (fun exn -> extendExnTable exn loc);
136136
loop (Exceptions.union exnSet exceptions) rest
137-
| ({kind = DoesNotRaise nestedEvents; loc} as ev) :: rest ->
137+
| ({kind = DoesNotThrow nestedEvents; loc} as ev) :: rest ->
138138
if !Common.Cli.debug then Log_.item "%a@." print ev;
139139
let nestedExceptions = loop Exceptions.empty nestedEvents in
140140
(if Exceptions.isEmpty nestedExceptions (* catch-all *) then
@@ -148,8 +148,8 @@ module Event = struct
148148
{
149149
message =
150150
Format.asprintf
151-
"@{<info>%s@} does not raise and is annotated with \
152-
redundant @doesNotRaise"
151+
"@{<info>%s@} does not throw and is annotated with \
152+
redundant @doesNotThrow"
153153
(name |> Name.toString);
154154
}));
155155
loop exnSet rest
@@ -158,12 +158,12 @@ module Event = struct
158158
if Exceptions.isEmpty exceptions then loop exnSet rest
159159
else
160160
let nestedExceptions = loop Exceptions.empty nestedEvents in
161-
let newRaises = Exceptions.diff nestedExceptions exceptions in
161+
let newThrows = Exceptions.diff nestedExceptions exceptions in
162162
exceptions
163163
|> Exceptions.iter (fun exn ->
164164
nestedEvents
165165
|> List.iter (fun event -> shrinkExnTable exn event.loc));
166-
loop (Exceptions.union exnSet newRaises) rest
166+
loop (Exceptions.union exnSet newThrows) rest
167167
| [] -> exnSet
168168
in
169169
let exnSet = loop Exceptions.empty events in
@@ -188,31 +188,31 @@ module Checks = struct
188188
checks := {events; exceptions; loc; locFull; moduleName; exnName} :: !checks
189189

190190
let doCheck {events; exceptions; loc; locFull; moduleName; exnName} =
191-
let raiseSet, exnTable = events |> Event.combine ~moduleName in
192-
let missingAnnotations = Exceptions.diff raiseSet exceptions in
193-
let redundantAnnotations = Exceptions.diff exceptions raiseSet in
191+
let throwSet, exnTable = events |> Event.combine ~moduleName in
192+
let missingAnnotations = Exceptions.diff throwSet exceptions in
193+
let redundantAnnotations = Exceptions.diff exceptions throwSet in
194194
(if not (Exceptions.isEmpty missingAnnotations) then
195195
let description =
196196
Common.ExceptionAnalysisMissing
197-
{exnName; exnTable; raiseSet; missingAnnotations; locFull}
197+
{exnName; exnTable; throwSet; missingAnnotations; locFull}
198198
in
199199
Log_.warning ~loc description);
200200
if not (Exceptions.isEmpty redundantAnnotations) then
201201
Log_.warning ~loc
202202
(Common.ExceptionAnalysis
203203
{
204204
message =
205-
(let raisesDescription ppf () =
206-
if raiseSet |> Exceptions.isEmpty then
207-
Format.fprintf ppf "raises nothing"
205+
(let throwsDescription ppf () =
206+
if throwSet |> Exceptions.isEmpty then
207+
Format.fprintf ppf "throws nothing"
208208
else
209-
Format.fprintf ppf "might raise %a"
209+
Format.fprintf ppf "might throw %a"
210210
(Exceptions.pp ~exnTable:(Some exnTable))
211-
raiseSet
211+
throwSet
212212
in
213213
Format.asprintf
214-
"@{<info>%s@} %a and is annotated with redundant @raises(%a)"
215-
exnName raisesDescription ()
214+
"@{<info>%s@} %a and is annotated with redundant @throws(%a)"
215+
exnName throwsDescription ()
216216
(Exceptions.pp ~exnTable:None)
217217
redundantAnnotations);
218218
})
@@ -249,35 +249,38 @@ let traverseAst () =
249249
case.c_guard |> iterExprOpt self;
250250
case.c_rhs |> iterExpr self)
251251
in
252-
let isRaise s = s = "Pervasives.raise" || s = "Pervasives.throw" in
253-
let raiseArgs args =
252+
let isThrow s = s = "Pervasives.raise" || s = "Pervasives.throw" in
253+
let throwArgs args =
254254
match args with
255255
| [(_, Some {Typedtree.exp_desc = Texp_construct ({txt}, _, _)})] ->
256256
[Exn.fromLid txt] |> Exceptions.fromList
257257
| [(_, Some {Typedtree.exp_desc = Texp_ident _})] ->
258258
[Exn.fromString "genericException"] |> Exceptions.fromList
259259
| _ -> [Exn.fromString "TODO_from_raise1"] |> Exceptions.fromList
260260
in
261-
let doesNotRaise attributes =
261+
let doesNotThrow attributes =
262262
attributes
263-
|> Annotation.getAttributePayload (fun s ->
264-
s = "doesNotRaise" || s = "doesnotraise" || s = "DoesNoRaise"
265-
|| s = "doesNotraise" || s = "doNotRaise" || s = "donotraise"
266-
|| s = "DoNoRaise" || s = "doNotraise")
263+
|> Annotation.getAttributePayload (function
264+
| "doesNotRaise" | "doesnotraise" | "DoesNoRaise" | "doesNotraise"
265+
| "doNotRaise" | "donotraise" | "DoNoRaise" | "doNotraise"
266+
| "doesNotThrow" | "doesnotthrow" | "DoesNoThrow" | "doesNotthrow"
267+
| "doNotThrow" | "donotthrow" | "DoNoThrow" | "doNotthrow" ->
268+
true
269+
| _ -> false)
267270
<> None
268271
in
269272
let expr (self : Tast_mapper.mapper) (expr : Typedtree.expression) =
270273
let loc = expr.exp_loc in
271-
let isDoesNoRaise = expr.exp_attributes |> doesNotRaise in
274+
let isDoesNoThrow = expr.exp_attributes |> doesNotThrow in
272275
let oldEvents = !currentEvents in
273-
if isDoesNoRaise then currentEvents := [];
276+
if isDoesNoThrow then currentEvents := [];
274277
(match expr.exp_desc with
275278
| Texp_ident (callee_, _, _) ->
276279
let callee =
277280
callee_ |> Common.Path.fromPathT |> ModulePath.resolveAlias
278281
in
279282
let calleeName = callee |> Common.Path.toName in
280-
if calleeName |> Name.toString |> isRaise then
283+
if calleeName |> Name.toString |> isThrow then
281284
Log_.warning ~loc
282285
(Common.ExceptionAnalysis
283286
{
@@ -299,17 +302,17 @@ let traverseAst () =
299302
args = [(_lbl1, Some {exp_desc = Texp_ident (callee, _, _)}); arg];
300303
}
301304
when (* raise @@ Exn(...) *)
302-
atat |> Path.name = "Pervasives.@@" && callee |> Path.name |> isRaise
305+
atat |> Path.name = "Pervasives.@@" && callee |> Path.name |> isThrow
303306
->
304-
let exceptions = [arg] |> raiseArgs in
305-
currentEvents := {Event.exceptions; loc; kind = Raises} :: !currentEvents;
307+
let exceptions = [arg] |> throwArgs in
308+
currentEvents := {Event.exceptions; loc; kind = Throws} :: !currentEvents;
306309
arg |> snd |> iterExprOpt self
307310
| Texp_apply {funct = {exp_desc = Texp_ident (callee, _, _)} as e; args} ->
308311
let calleeName = Path.name callee in
309-
if calleeName |> isRaise then
310-
let exceptions = args |> raiseArgs in
312+
if calleeName |> isThrow then
313+
let exceptions = args |> throwArgs in
311314
currentEvents :=
312-
{Event.exceptions; loc; kind = Raises} :: !currentEvents
315+
{Event.exceptions; loc; kind = Throws} :: !currentEvents
313316
else e |> iterExpr self;
314317
args |> List.iter (fun (_, eOpt) -> eOpt |> iterExprOpt self)
315318
| Texp_match (e, casesOk, casesExn, partial) ->
@@ -332,7 +335,7 @@ let traverseAst () =
332335
{
333336
Event.exceptions = [Exn.matchFailure] |> Exceptions.fromList;
334337
loc;
335-
kind = Raises;
338+
kind = Throws;
336339
}
337340
:: !currentEvents
338341
| Texp_try (e, cases) ->
@@ -348,21 +351,22 @@ let traverseAst () =
348351
{Event.exceptions; loc; kind = Catches !currentEvents} :: oldEvents;
349352
cases |> iterCases self
350353
| _ -> super.expr self expr |> ignore);
351-
(if isDoesNoRaise then
354+
(if isDoesNoThrow then
352355
let nestedEvents = !currentEvents in
353356
currentEvents :=
354357
{
355358
Event.exceptions = Exceptions.empty;
356359
loc;
357-
kind = DoesNotRaise nestedEvents;
360+
kind = DoesNotThrow nestedEvents;
358361
}
359362
:: oldEvents);
360363
expr
361364
in
362365
let getExceptionsFromAnnotations attributes =
363-
let raisesAnnotationPayload =
366+
let throwsAnnotationPayload =
364367
attributes
365-
|> Annotation.getAttributePayload (fun s -> s = "raises" || s = "raise")
368+
|> Annotation.getAttributePayload (fun s ->
369+
s = "throws" || s = "throw" || s = "raises" || s = "raise")
366370
in
367371
let rec getExceptions payload =
368372
match payload with
@@ -379,7 +383,7 @@ let traverseAst () =
379383
|> List.concat |> Exceptions.fromList
380384
| _ -> Exceptions.empty
381385
in
382-
match raisesAnnotationPayload with
386+
match throwsAnnotationPayload with
383387
| None -> Exceptions.empty
384388
| Some payload -> payload |> getExceptions
385389
in

analysis/reanalyze/src/Log_.ml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,9 @@ let missingRaiseInfoToText {missingAnnotations; locFull} =
102102
Format.asprintf "%a" (Exceptions.pp ~exnTable:None) missingAnnotations
103103
in
104104
if !Cli.json then
105-
EmitJson.emitAnnotate ~action:"Add @raises annotation"
105+
EmitJson.emitAnnotate ~action:"Add @throws annotation"
106106
~pos:(EmitJson.locToPos locFull)
107-
~text:(Format.asprintf "@raises(%s)\\n" missingTxt)
107+
~text:(Format.asprintf "@throws(%s)\\n" missingTxt)
108108
else ""
109109

110110
let logAdditionalInfo ~(description : description) =
@@ -117,17 +117,17 @@ let logAdditionalInfo ~(description : description) =
117117
missingRaiseInfoToText missingRaiseInfo
118118
| _ -> ""
119119

120-
let missingRaiseInfoToMessage {exnTable; exnName; missingAnnotations; raiseSet}
120+
let missingThrowInfoToMessage {exnTable; exnName; missingAnnotations; throwSet}
121121
=
122-
let raisesTxt =
123-
Format.asprintf "%a" (Exceptions.pp ~exnTable:(Some exnTable)) raiseSet
122+
let throwsTxt =
123+
Format.asprintf "%a" (Exceptions.pp ~exnTable:(Some exnTable)) throwSet
124124
in
125125
let missingTxt =
126126
Format.asprintf "%a" (Exceptions.pp ~exnTable:None) missingAnnotations
127127
in
128128
Format.asprintf
129-
"@{<info>%s@} might raise %s and is not annotated with @raises(%s)" exnName
130-
raisesTxt missingTxt
129+
"@{<info>%s@} might throw %s and is not annotated with @throws(%s)" exnName
130+
throwsTxt missingTxt
131131

132132
let descriptionToMessage (description : description) =
133133
match description with
@@ -138,7 +138,7 @@ let descriptionToMessage (description : description) =
138138
Format.asprintf "@{<info>%s@} %s" path message
139139
| ExceptionAnalysis {message} -> message
140140
| ExceptionAnalysisMissing missingRaiseInfo ->
141-
missingRaiseInfoToMessage missingRaiseInfo
141+
missingThrowInfoToMessage missingRaiseInfo
142142
| Termination {message} -> message
143143

144144
let descriptionToName (description : description) =

0 commit comments

Comments
 (0)