Repository navigation
Commit 18ed190
Read @throws from library declarations in exception analysis
Why:
The exception analysis knew what library functions throw only from a
table kept by hand in exn_lib.ml. The table drifted: the Temporal
`fromStringOrThrow` functions were never added (#9182), and neither
were a few Intl and Belt ones. Third-party libraries and a project's
own externals had no way to say what they throw.
A call now takes the exceptions named by the `@throws` on the declaration
of the value it calls, and the table is gone. The `.cmi` keeps the
attributes of externals and interface declarations, so their `@throws`
reaches calls in other modules and packages, through aliases, `open`,
`include`, functors and first-class modules. For a `let` in a module
without an interface, the type checker now copies the binding's `@throws`
into the inferred signature, as it already did for `@deprecated`. The
stdlib and Belt declare what their throwing functions throw, and a guard
in the reanalyze tests fails when a stdlib or Belt `*OrThrow` or `*Exn`
declaration has no `@throws(...)`.
Analysis modified (exceptions of a call to `f`, `Exception.Event` loop):
Before (rule):
throws(`f`) = values(`f`) when `f` is in the values table
throws(`f`) = table(`f`) otherwise
After (rule):
throws(`f`) = declared(`f`) ∪ values(`f`)
Signature modified (inferred signature of a module without an interface, `Typemod.type_structure`):
Example:
/** Finds the value. */
@throws(Not_found)
let find = x => x > 0 ? x : throw(Not_found)
Before (rescript): the signature in the .cmi
let find: int => int
After (rescript):
@throws(Not_found)
let find: int => int
Reanalyze modified (calls to annotated externals):
Example:
// Lib.res
@throws(JsExn)
external parse: string => int = "parseInt"
// App.res
let parse = s => Lib.parse(s)
@throws(JsExn)
let parseAnnotated = s => Lib.parse(s)
let date = () => Temporal.PlainDate.fromStringOrThrow("x")
Before (reanalyze):
Exception Analysis
App.res:4:5-18
parseAnnotated throws nothing and is annotated with redundant @throws(JsExn)
Analysis reported 1 issues (Exception Analysis:1)
After (reanalyze):
Exception Analysis
App.res:1:5-9
parse might throw JsExn (App.res:1:17) and is not annotated with @throws(JsExn)
Exception Analysis
App.res:6:5-8
date might throw JsExn (App.res:6:17) and is not annotated with @throws(JsExn)
Analysis reported 2 issues (Exception Analysis:2)
Review: Why the union of the declaration and the values table, rather than
one winning? The values table holds the `@throws` on the implementation's
binding, the declaration the one in its interface. Either can be the only
one written, as with an annotated `.resi` `let` whose `.res` isn't
annotated, so the call can throw what either names.
Review: Why does the inferred signature take only the `@throws` family and
`@deprecated`, and not every attribute? A doc comment is an attribute too:
in the `.cmi` it would make editing a comment recompile the module's
dependents. A module with a `.resi` is unchanged, since its `.cmi` holds
the interface's declarations.
Review: What does a `@throws` without a payload mean? Nothing, as on a
binding. The guard requires a payload, and the stdlib's two payload-less
`@throws` in Stdlib_JSON.res now name `JsExn`.
Fixes #9182
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Kbf3gGhHjzBTXqSo8Dckpb
Signed-off-by: Christoph Knittel <ck@cca.io>1 parent 32116b5 commit 18ed190
68 files changed
Lines changed: 484 additions & 308 deletions
File tree
- analysis/reanalyze/src
- changes
- compiler/ml
- packages/@rescript
- belt/src
- runtime
- lib
- es6
- js
- tests/analysis_tests/tests-reanalyze
- deadcode
- expected
- src/exception
Some content is hidden
Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
127 | 127 | | |
128 | 128 | | |
129 | 129 | | |
130 | | - | |
| 130 | + | |
| 131 | + | |
131 | 132 | | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
132 | 136 | | |
133 | 137 | | |
134 | 138 | | |
135 | 139 | | |
136 | | - | |
137 | | - | |
138 | | - | |
139 | | - | |
140 | | - | |
| 140 | + | |
| 141 | + | |
141 | 142 | | |
142 | 143 | | |
143 | 144 | | |
| |||
280 | 281 | | |
281 | 282 | | |
282 | 283 | | |
283 | | - | |
| 284 | + | |
284 | 285 | | |
285 | 286 | | |
286 | 287 | | |
| |||
322 | 323 | | |
323 | 324 | | |
324 | 325 | | |
| 326 | + | |
| 327 | + | |
| 328 | + | |
| 329 | + | |
| 330 | + | |
| 331 | + | |
| 332 | + | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
325 | 352 | | |
326 | 353 | | |
327 | 354 | | |
328 | 355 | | |
329 | 356 | | |
330 | 357 | | |
331 | 358 | | |
332 | | - | |
| 359 | + | |
333 | 360 | | |
334 | 361 | | |
335 | 362 | | |
| |||
343 | 370 | | |
344 | 371 | | |
345 | 372 | | |
| 373 | + | |
| 374 | + | |
| 375 | + | |
346 | 376 | | |
347 | 377 | | |
348 | | - | |
| 378 | + | |
349 | 379 | | |
350 | 380 | | |
351 | 381 | | |
| |||
423 | 453 | | |
424 | 454 | | |
425 | 455 | | |
426 | | - | |
427 | | - | |
428 | | - | |
429 | | - | |
430 | | - | |
431 | | - | |
432 | | - | |
433 | | - | |
434 | | - | |
435 | | - | |
436 | | - | |
437 | | - | |
438 | | - | |
439 | | - | |
440 | | - | |
441 | | - | |
442 | | - | |
443 | | - | |
444 | | - | |
445 | | - | |
446 | | - | |
447 | | - | |
448 | | - | |
449 | | - | |
450 | | - | |
451 | | - | |
452 | 456 | | |
453 | 457 | | |
454 | 458 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | 3 | | |
4 | | - | |
5 | | - | |
6 | | - | |
7 | | - | |
8 | | - | |
9 | 4 | | |
10 | | - | |
11 | 5 | | |
12 | 6 | | |
13 | 7 | | |
14 | | - | |
15 | | - | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | 3 | | |
4 | | - | |
5 | | - | |
6 | | - | |
7 | 4 | | |
8 | 5 | | |
9 | | - | |
10 | | - | |
11 | 6 | | |
12 | | - | |
13 | 7 | | |
14 | | - | |
15 | | - | |
This file was deleted.
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1442 | 1442 | | |
1443 | 1443 | | |
1444 | 1444 | | |
1445 | | - | |
1446 | | - | |
| 1445 | + | |
| 1446 | + | |
| 1447 | + | |
| 1448 | + | |
| 1449 | + | |
| 1450 | + | |
| 1451 | + | |
| 1452 | + | |
| 1453 | + | |
1447 | 1454 | | |
1448 | 1455 | | |
1449 | | - | |
| 1456 | + | |
1450 | 1457 | | |
1451 | 1458 | | |
1452 | 1459 | | |
| |||
1460 | 1467 | | |
1461 | 1468 | | |
1462 | 1469 | | |
1463 | | - | |
| 1470 | + | |
| 1471 | + | |
| 1472 | + | |
1464 | 1473 | | |
1465 | 1474 | | |
1466 | 1475 | | |
| |||
0 commit comments