Repository navigation
Conversation
Changelog🚀 New Feature
Semantic Diff1 commit · Full page Read
|
| Before | After |
|---|---|
|
|
|
Signature modified (inferred signature of a module without an interface, Typemod.type_structure)
/** Finds the value. */
@throws(Not_found)
let find = x => x > 0 ? x : throw(Not_found)rescript · Before: the signature in the .cmi
+@throws(Not_found)
let find: int => intReanalyze modified (calls to annotated externals)
// 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")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)
+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)Fixes #9182
Review
- Why the union of the declaration and the values table, rather than one winning? The values table holds the
@throwson the implementation's binding, the declaration the one in its interface. Either can be the only one written, as with an annotated.resiletwhose.resisn't annotated, so the call can throw what either names. - Why does the inferred signature take only the
@throwsfamily and@deprecated, and not every attribute? A doc comment is an attribute too: in the.cmiit would make editing a comment recompile the module's dependents. A module with a.resiis unchanged, since its.cmiholds the interface's declarations. - What does a
@throwswithout a payload mean? Nothing, as on a binding. The guard requires a payload, and the stdlib's two payload-less@throwsin Stdlib_JSON.res now nameJsExn.
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #9198 +/- ##
==========================================
+ Coverage 82.47% 82.49% +0.02%
==========================================
Files 466 466
Lines 66070 66122 +52
==========================================
+ Hits 54493 54550 +57
+ Misses 11577 11572 -5
🚀 New features to boost your workflow:
|
rescript
@rescript/belt
@rescript/darwin-arm64
@rescript/linux-arm64
@rescript/linux-x64
@rescript/runtime
@rescript/win32-x64
commit: |
|
Developer playground preview: https://rescript-lang.github.io/rescript/dev-playground/?version=pr-9198 |
0a37396 to
9b3d665
Compare
|
@codex review |
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
Codex Review: Didn't find any major issues. Swish! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
9b3d665 to
76f8a78
Compare
|
@codex review |
|
Codex Review: Didn't find any major issues. Delightful! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
68c774c to
1d38461
Compare
1d38461 to
18ed190
Compare
Changelog🚀 New Feature
Semantic Diff1 commit · Full page ✏️ Read
|
| Before | After |
|---|---|
|
|
|
Signature modified (inferred signature of a module without an interface, Typemod.type_structure)
/** Finds the value. */
@throws(Not_found)
let find = x => x > 0 ? x : throw(Not_found)rescript · Before: the signature in the .cmi
+@throws(Not_found)
let find: int => intReanalyze modified (calls to annotated externals)
// 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")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)
+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)Fixes #9182
Review
- Why does the declaration win over the implementation? The values table holds the
@throwson the implementation'slet. When the interface has a@throws, that is what callers see, so it counts alone. When it has none, the implementation's counts, so an annotated.resbehind an unannotated.resistill works. - Why does the inferred signature copy only
@throwsand@deprecated? A doc comment is an attribute too, and keeping it in the.cmiwould make every edit of the comment recompile the module's dependents. A module with a.resiis unaffected, because its.cmicomes from the interface.
✅ Compiler benchmarksAllocated wordsIn total: 655,166,578 → 655,275,138 (+0.02%). Generated projects: 627,288,916 → 627,288,719 (±0.00%).
56 more changes
(inputs changed): this pull request changes runtime or Belt sources, so these benchmarks compile different code than on the base, and their changes don't count as regressions. InstructionsIn total: 9,128,430,307 → 9,128,887,418 (+0.01%).
(inputs changed): this pull request changes runtime or Belt sources, so these benchmarks compile different code than on the base, and their changes don't count as regressions. Output sizeIn total: 27,368,788 → 27,388,490 (+0.07%).
(inputs changed): this pull request changes runtime or Belt sources, so these benchmarks compile different code than on the base, and their changes don't count as regressions. Pull request d5177af compared with base 4717690 in this run. |
|
Developer playground preview: https://rescript-lang.github.io/rescript/dev-playground/?version=pr-9198 |
cristianoc
left a comment
There was a problem hiding this comment.
Dot, Cristiano's assistant, reviewing at his request.
One correctness issue in the new library-declaration path: custom exception names are transferred across module boundaries without resolving their defining scope. Please add a cross-module custom-exception regression and preserve exception identity consistently. Exact-head CI is green. This finding is based on static tracing; I did not run a compiler-built reproducer locally (OCaml/dune unavailable).
| current_events := | ||
| { | ||
| Event.exceptions = Exceptions.empty; | ||
| Event.exceptions = get_exceptions_from_annotations vd.val_attributes; |
There was a problem hiding this comment.
[P2] Resolve declaration exception identities before transferring them to callers
For a library interface declaring exception E and @throws(E) let f: unit => unit, a consumer's try Lib.f() catch { | Lib.E => () } contributes the raw annotation string E here, but the catch is represented as Lib.E. Exn.from_string is identity, Exn.from_lid keeps the qualifier, and the catch subtracts sets of those strings, so the correctly caught exception is still reported as uncaught. Conversely, catching an unrelated local E can suppress the library exception. The existing new fixtures use globally named builtins and miss this case. Please resolve names in the declaration's defining scope and compare canonical exception identities on both sides, with a custom-library-exception regression.
Why:
Review finding on #9198: a library's `@throws(E)` reached the caller as
the bare name `E`, while a catch was compared by its spelling, `Lib.E`.
So a correct catch still reported the exception, and a caller's own `E`
could hide the library's.
Now a catch or throw names an exception by its constructor's path: `Lib.E`
whether it is written `Lib.E`, `E` after `open Lib` or `L.E` for
`module L = Lib`. A callee's `@throws` names are resolved in the callee's
scope (`Lib.Inner.E`, then `Lib.E`), picking the first one the project
catches or throws. For a callee in another file the bare name is not a
candidate. Predefined exceptions such as `Not_found` and `JsExn` keep
their bare names. The same applies to the values table, so an annotated
`let` in another file without an interface is fixed too. That case is
older than this PR.
Reanalyze modified (cross-module exception names):
Example:
// ThrowsLibExn.res
exception E
@throws(E)
external parse: string => int = "parseThrowing"
@throws(E)
let viaLet = x => x > 0 ? x : throw(E)
module Inner = {
exception F
@throws(F)
external find: int => int = "findThrowing"
}
// ThrowsLibExnCalls.res
let catchExternal = s =>
try ThrowsLibExn.parse(s) catch {
| ThrowsLibExn.E => 0
}
let catchViaLet = x =>
try ThrowsLibExn.viaLet(x) catch {
| ThrowsLibExn.E => 0
}
let catchNested = x =>
try ThrowsLibExn.Inner.find(x) catch {
| ThrowsLibExn.Inner.F => 0
}
exception E
let catchUnrelatedLocal = s =>
try ThrowsLibExn.parse(s) catch {
| E => 0
}
Before (reanalyze):
Exception Analysis
ThrowsLibExnCalls.res:1:5-17
catchExternal might throw E (ThrowsLibExnCalls.res:2:6) and is not annotated with @throws(E)
Exception Analysis
ThrowsLibExnCalls.res:6:5-15
catchViaLet might throw E (ThrowsLibExnCalls.res:7:6) and is not annotated with @throws(E)
Exception Analysis
ThrowsLibExnCalls.res:11:5-15
catchNested might throw F (ThrowsLibExnCalls.res:12:6) and is not annotated with @throws(F)
After (reanalyze):
Exception Analysis
ThrowsLibExnCalls.res:18:5-23
catchUnrelatedLocal might throw ThrowsLibExn.E (ThrowsLibExnCalls.res:19:6) and is not annotated with @throws(ThrowsLibExn.E)
Review: Where do the candidate names come from, without the declaration's environment? The .cmt keeps no environments, so a name is resolved against the exceptions the project's files catch or throw. A catch can only match one of those, so the choice matters for matching. When none is known, the innermost candidate is used, which only affects the message.
Review: What does a caller now write in its own annotation? The qualified name, `@throws(ThrowsLibExn.E)`, which the message suggests. A bare `E` after `open ThrowsLibExn` no longer matches.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Kbf3gHA54VKxsXvCQyC2vw
Signed-off-by: Christoph Knittel <ck@cca.io>
bd4a722 to
9be7aca
Compare
Why:
Review finding on #9198: a library's `@throws(E)` reached the caller as
the bare name `E`, while a catch was compared by its spelling, `Lib.E`.
So a correct catch still reported the exception, and a caller's own `E`
could hide the library's.
Now a catch or throw names an exception by its constructor's path: `Lib.E`
whether it is written `Lib.E`, `E` after `open Lib` or `L.E` for
`module L = Lib`. A callee's `@throws` names are resolved in the callee's
scope (`Lib.Inner.E`, then `Lib.E`), picking the first one the project
catches or throws. For a callee in another file the bare name is not a
candidate. Predefined exceptions such as `Not_found` and `JsExn` keep
their bare names. The same applies to the values table, so an annotated
`let` in another file without an interface is fixed too. That case is
older than this PR.
Reanalyze modified (cross-module exception names):
Example:
// ThrowsLibExn.res
exception E
@throws(E)
external parse: string => int = "parseThrowing"
@throws(E)
let viaLet = x => x > 0 ? x : throw(E)
module Inner = {
exception F
@throws(F)
external find: int => int = "findThrowing"
}
// ThrowsLibExnCalls.res
let catchExternal = s =>
try ThrowsLibExn.parse(s) catch {
| ThrowsLibExn.E => 0
}
let catchViaLet = x =>
try ThrowsLibExn.viaLet(x) catch {
| ThrowsLibExn.E => 0
}
let catchNested = x =>
try ThrowsLibExn.Inner.find(x) catch {
| ThrowsLibExn.Inner.F => 0
}
exception E
let catchUnrelatedLocal = s =>
try ThrowsLibExn.parse(s) catch {
| E => 0
}
Before (reanalyze):
Exception Analysis
ThrowsLibExnCalls.res:1:5-17
catchExternal might throw E (ThrowsLibExnCalls.res:2:6) and is not annotated with @throws(E)
Exception Analysis
ThrowsLibExnCalls.res:6:5-15
catchViaLet might throw E (ThrowsLibExnCalls.res:7:6) and is not annotated with @throws(E)
Exception Analysis
ThrowsLibExnCalls.res:11:5-15
catchNested might throw F (ThrowsLibExnCalls.res:12:6) and is not annotated with @throws(F)
After (reanalyze):
Exception Analysis
ThrowsLibExnCalls.res:18:5-23
catchUnrelatedLocal might throw ThrowsLibExn.E (ThrowsLibExnCalls.res:19:6) and is not annotated with @throws(ThrowsLibExn.E)
Review: Where do the candidate names come from, without the declaration's environment? The .cmt keeps no environments, so a name is resolved against the exceptions the project's files catch or throw. A catch can only match one of those, so the choice matters for matching. When none is known, the innermost candidate is used, which only affects the message.
Review: What does a caller now write in its own annotation? The qualified name, `@throws(ThrowsLibExn.E)`, which the message suggests. A bare `E` after `open ThrowsLibExn` no longer matches.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Kbf3gHA54VKxsXvCQyC2vw
Signed-off-by: Christoph Knittel <ck@cca.io>
Why:
Review on #9198: a library's `@throws(E)` reached callers as the bare
name `E`, while a catch was compared as written (`Lib.E`), so a correct
catch was still reported and a caller's own `E` could hide the
library's. Reanalyze cannot resolve names itself, because a .cmt keeps
no environments; two attempts to guess the scope from the file still
got opens in interfaces, functors, includes and aliases wrong.
The type checker now looks up each name in a `@throws` where it is
written, for externals, `let`s in signatures and interfaces, and every
`let` binding. It adds a generated `@res.throws` that lists each
exception's readable name and identity, and the inferred signature keeps
it next to `@throws`. The identity is the defining file without its
extension and the exception's name (`src/Lib#E`), so a module's .res and
.resi agree and moving a definition within its file changes nothing.
Exception analysis compares these identities with the constructors that
catches and throws name.
A name that is not an exception in scope gets the new warning 113 and is
listed as written. A rebinding (`exception Mine = Lib.E`) is a new
definition, so catching `Mine` does not catch `Lib.E`, as before. Two
exceptions with the same name in one file share an identity.
Typedtree modified (@throws resolved where it is written):
Example:
module Errors = {
exception NotFound
}
open Errors
@throws(NotFound)
external find: int => int = "find"
Before (typed):
Tstr_primitive
value_description find/1003
attribute "throws"
[
structure_item
Pstr_eval
expression
Pexp_construct "NotFound"
[]
]
After (typed):
Tstr_primitive
value_description find/1003
attribute "throws"
[
structure_item
Pstr_eval
expression
Pexp_construct "NotFound"
[]
]
attribute "res.throws"
[
structure_item
Pstr_eval
expression
Pexp_array
[
expression
Pexp_tuple
[
expression
Pexp_constant PConst_string (source="Repo.Errors.NotFound", semantic="Repo.Errors.NotFound")
expression
Pexp_constant PConst_string (source="Repo#NotFound", semantic="Repo#NotFound")
]
]
]
Diagnostic added (warning 113):
Example:
@throws(Missing)
let f = () => 1
After (diagnostic): tests/build_tests/super_errors/fixtures/throws_unbound_warning.res
Warning number 113
throws_unbound_warning.res:3:9-15
@throws names Missing, which is not an exception in scope here. Exception analysis cannot match it with the exceptions callers catch.
Reanalyze modified (catches of another module's exceptions):
Example:
// ThrowsLibExn.res
exception E
@throws(E)
external parse: string => int = "parseThrowing"
// ThrowsLibExnCalls.res
let catchExternal = s =>
try ThrowsLibExn.parse(s) catch {
| ThrowsLibExn.E => 0
}
Before (reanalyze): tests/analysis_tests/tests-reanalyze/deadcode/src/exception
Exception Analysis
ThrowsLibExnCalls.res:4:5-17
catchExternal might throw E (ThrowsLibExnCalls.res:5:6) and is not annotated with @throws(E)
Exception Analysis
ThrowsRepoCalls.res:18:5-16
catchViaOpen might throw Bad (ThrowsRepoCalls.res:19:6) and is not annotated with @throws(Bad)
Exception Analysis
ThrowsScopes.res:2:5-18
catchIfaceOpen might throw NotFound (ThrowsScopes.res:3:6) and is not annotated with @throws(NotFound)
Exception Analysis
ThrowsLibExnCalls.res:103:7-12
caught might throw E (ThrowsLibExnCalls.res:104:8) and is not annotated with @throws(E)
…
Exception Analysis
ThrowsLibExnCalls.res:81:5-16
catchRebound might throw E (ThrowsLibExnCalls.res:82:6) and is not annotated with @throws(E)
After (reanalyze): tests/analysis_tests/tests-reanalyze/deadcode/src/exception
Exception Analysis
ThrowsLibExnCalls.res:81:5-16
catchRebound might throw ThrowsLibExn.E (ThrowsLibExnCalls.res:82:6) and is not annotated with @throws(ThrowsLibExn.E)
Review: Why an identity from the file and name instead of the path? The type checker has no module prefix for a file's own exceptions, and a path changes with aliases and namespaces. The file and name are the same through every alias, open, include and functor, and in the .res and .resi.
Review: Does this recompile more? Only the attribute is new, and it holds names, not positions, so it changes only when the exception is renamed or moves to another file.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Kbf3gHA54VKxsXvCQyC2vw
Signed-off-by: Christoph Knittel <ck@cca.io>
9be7aca to
0786ca1
Compare
Why:
Review on #9198: a library's `@throws(E)` reached callers as the bare
name `E`, while a catch was compared as written (`Lib.E`), so a correct
catch was still reported and a caller's own `E` could hide the
library's. Reanalyze cannot resolve names itself, because a .cmt keeps
no environments; two attempts to guess the scope from the file still
got opens in interfaces, functors, includes and aliases wrong.
The type checker now looks up each name in a `@throws` where it is
written, for externals, `let`s in signatures and interfaces, and every
`let` binding. It adds a generated `@res.throws` that lists each
exception's readable name and identity, and the inferred signature keeps
it next to `@throws`. The identity is the defining file without its
extension and the exception's name (`src/Lib#E`), so a module's .res and
.resi agree and moving a definition within its file changes nothing.
Exception analysis compares these identities with the constructors that
catches and throws name.
A name that is not an exception in scope gets the new warning 113 and is
listed as written. A rebinding (`exception Mine = Lib.E`) is a new
definition, so catching `Mine` does not catch `Lib.E`, as before. Two
exceptions with the same name in one file share an identity.
Typedtree modified (@throws resolved where it is written):
Example:
module Errors = {
exception NotFound
}
open Errors
@throws(NotFound)
external find: int => int = "find"
Before (typed):
Tstr_primitive
value_description find/1003
attribute "throws"
[
structure_item
Pstr_eval
expression
Pexp_construct "NotFound"
[]
]
After (typed):
Tstr_primitive
value_description find/1003
attribute "throws"
[
structure_item
Pstr_eval
expression
Pexp_construct "NotFound"
[]
]
attribute "res.throws"
[
structure_item
Pstr_eval
expression
Pexp_array
[
expression
Pexp_tuple
[
expression
Pexp_constant PConst_string (source="Repo.Errors.NotFound", semantic="Repo.Errors.NotFound")
expression
Pexp_constant PConst_string (source="Repo#NotFound", semantic="Repo#NotFound")
]
]
]
Diagnostic added (warning 113):
Example:
@throws(Missing)
let f = () => 1
After (diagnostic): tests/build_tests/super_errors/fixtures/throws_unbound_warning.res
Warning number 113
throws_unbound_warning.res:3:9-15
@throws names Missing, which is not an exception in scope here. Exception analysis cannot match it with the exceptions callers catch.
Reanalyze modified (catches of another module's exceptions):
Example:
// ThrowsLibExn.res
exception E
@throws(E)
external parse: string => int = "parseThrowing"
// ThrowsLibExnCalls.res
let catchExternal = s =>
try ThrowsLibExn.parse(s) catch {
| ThrowsLibExn.E => 0
}
Before (reanalyze): tests/analysis_tests/tests-reanalyze/deadcode/src/exception
Exception Analysis
ThrowsLibExnCalls.res:4:5-17
catchExternal might throw E (ThrowsLibExnCalls.res:5:6) and is not annotated with @throws(E)
Exception Analysis
ThrowsRepoCalls.res:18:5-16
catchViaOpen might throw Bad (ThrowsRepoCalls.res:19:6) and is not annotated with @throws(Bad)
Exception Analysis
ThrowsScopes.res:2:5-18
catchIfaceOpen might throw NotFound (ThrowsScopes.res:3:6) and is not annotated with @throws(NotFound)
Exception Analysis
ThrowsLibExnCalls.res:103:7-12
caught might throw E (ThrowsLibExnCalls.res:104:8) and is not annotated with @throws(E)
…
Exception Analysis
ThrowsLibExnCalls.res:81:5-16
catchRebound might throw E (ThrowsLibExnCalls.res:82:6) and is not annotated with @throws(E)
After (reanalyze): tests/analysis_tests/tests-reanalyze/deadcode/src/exception
Exception Analysis
ThrowsLibExnCalls.res:81:5-16
catchRebound might throw ThrowsLibExn.E (ThrowsLibExnCalls.res:82:6) and is not annotated with @throws(ThrowsLibExn.E)
Review: Why an identity from the file and name instead of the path? The type checker has no module prefix for a file's own exceptions, and a path changes with aliases and namespaces. The file and name are the same through every alias, open, include and functor, and in the .res and .resi.
Review: Does this recompile more? Only the attribute is new, and it holds names, not positions, so it changes only when the exception is renamed or moves to another file.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Kbf3gHA54VKxsXvCQyC2vw
Signed-off-by: Christoph Knittel <ck@cca.io>
0786ca1 to
b0666ce
Compare
|
@codex review |
…citly
Why:
Codex review on #9198: the compiler's new @throws reader dropped
`list{A, B}` payloads and constructors written with arguments, which
reanalyze's old reader accepted, so such annotations silently stopped
declaring anything. It also derived an exception's identity from a
location that a .cmi built with -no-keep-locs (the runtime's and
Belt's) does not keep.
The reader again takes `list{...}` and `M.E(...)` as before. An exception
whose .cmi has no locations is now identified as `#Name` on purpose,
rather than through the placeholder file name `_none_`. Codex's crash
report for that case does not hold, since `Filename.remove_extension`
does not raise on `_none_`, but the identity was accidental.
AST modified (res.throws for a list payload):
Example:
exception E
@throws(list{E, Not_found})
let f = () => throw(E)
Before (text): number of res.throws attributes in bsc -dtypedtree
0
After (text): number of res.throws attributes in bsc -dtypedtree
1
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Kbf3gHA54VKxsXvCQyC2vw
Signed-off-by: Christoph Knittel <ck@cca.io>
… exactly
Why:
Review of #9198: the type checker loads the module a @throws names, but
the dependency scanner skipped attributes. A module named only there was
built in any order, so the name resolved or got warning 113 depending
on the build, and a later edit could fail with "inconsistent
assumptions". Messages also named an exception defined in a nested
module without that module, so the annotation they suggested did not
resolve. And a library built with -no-keep-locs recorded its own
exceptions with its file, while its users see them without one.
`Depend` now adds the modules a @throws names, like any other use, so
they are built first. A module that is both named in @throws and depends
on the annotating module now gives a dependency cycle error.
`Typemod` records which module of the current file defines each
exception and module, so a file's own exception is named `Lib.Inner.F`.
Under -no-keep-locs a file's own exceptions are identified by name alone
also while it is compiled. Warning 113 is shown once for a `let` that
destructures a tuple.
Diagnostic modified (an exception of a nested module):
Example:
// ThrowsLibExn.res
module Inner = {
exception F
@throws(F)
external find: int => int = "findThrowing"
}
// ThrowsLibExnCalls.res
let uncaughtNested = x => ThrowsLibExn.Inner.find(x)
Before (reanalyze): tests/analysis_tests/tests-reanalyze/deadcode/src/exception/ThrowsLibExnCalls.res
Exception Analysis
ThrowsLibExnCalls.res:110:5-18
uncaughtNested might throw ThrowsLibExn.F (ThrowsLibExnCalls.res:110:26) and is not annotated with @throws(ThrowsLibExn.F)
After (reanalyze): tests/analysis_tests/tests-reanalyze/deadcode/src/exception/ThrowsLibExnCalls.res
Exception Analysis
ThrowsLibExnCalls.res:110:5-18
uncaughtNested might throw ThrowsLibExn.Inner.F (ThrowsLibExnCalls.res:110:26) and is not annotated with @throws(ThrowsLibExn.Inner.F)
Diagnostic modified (a module named only in @throws, tests/build_tests/throws_dependency):
Example:
// A.res
@throws(Z.E)
external find: int => int = "find"
// Z.res
exception E
Before (diagnostic): A.res built before Z.res
Warning number 113
@throws names Z.E, which is not an exception in scope here. Exception analysis cannot match it with the exceptions callers catch.
After (diagnostic): Z.res built first, no warning
Breaking: A module named in @throws that itself depends on the annotating module is now a dependency cycle. Move the exception to a module of its own.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Kbf3gHA54VKxsXvCQyC2vw
Signed-off-by: Christoph Knittel <ck@cca.io>
Why:
Review on #9198: a library's `@throws(E)` reached callers as the bare
name `E`, while a catch was compared as written (`Lib.E`), so a correct
catch was still reported and a caller's own `E` could hide the
library's. Reanalyze cannot resolve names itself, because a .cmt keeps
no environments; two attempts to guess the scope from the file still
got opens in interfaces, functors, includes and aliases wrong.
The type checker now looks up each name in a `@throws` where it is
written, for externals, `let`s in signatures and interfaces, and every
`let` binding. It adds a generated `@res.throws` that lists each
exception's readable name and identity, and the inferred signature keeps
it next to `@throws`. The identity is the defining file without its
extension and the exception's name (`src/Lib#E`), so a module's .res and
.resi agree and moving a definition within its file changes nothing.
Exception analysis compares these identities with the constructors that
catches and throws name.
A name that is not an exception in scope gets the new warning 113 and is
listed as written. A rebinding (`exception Mine = Lib.E`) is a new
definition, so catching `Mine` does not catch `Lib.E`, as before. Two
exceptions with the same name in one file share an identity.
Typedtree modified (@throws resolved where it is written):
Example:
module Errors = {
exception NotFound
}
open Errors
@throws(NotFound)
external find: int => int = "find"
Before (typed):
Tstr_primitive
value_description find/1003
attribute "throws"
[
structure_item
Pstr_eval
expression
Pexp_construct "NotFound"
[]
]
After (typed):
Tstr_primitive
value_description find/1003
attribute "throws"
[
structure_item
Pstr_eval
expression
Pexp_construct "NotFound"
[]
]
attribute "res.throws"
[
structure_item
Pstr_eval
expression
Pexp_array
[
expression
Pexp_tuple
[
expression
Pexp_constant PConst_string (source="Repo.Errors.NotFound", semantic="Repo.Errors.NotFound")
expression
Pexp_constant PConst_string (source="Repo#NotFound", semantic="Repo#NotFound")
]
]
]
Diagnostic added (warning 113):
Example:
@throws(Missing)
let f = () => 1
After (diagnostic): tests/build_tests/super_errors/fixtures/throws_unbound_warning.res
Warning number 113
throws_unbound_warning.res:3:9-15
@throws names Missing, which is not an exception in scope here. Exception analysis cannot match it with the exceptions callers catch.
Reanalyze modified (catches of another module's exceptions):
Example:
// ThrowsLibExn.res
exception E
@throws(E)
external parse: string => int = "parseThrowing"
// ThrowsLibExnCalls.res
let catchExternal = s =>
try ThrowsLibExn.parse(s) catch {
| ThrowsLibExn.E => 0
}
Before (reanalyze): tests/analysis_tests/tests-reanalyze/deadcode/src/exception
Exception Analysis
ThrowsLibExnCalls.res:4:5-17
catchExternal might throw E (ThrowsLibExnCalls.res:5:6) and is not annotated with @throws(E)
Exception Analysis
ThrowsRepoCalls.res:18:5-16
catchViaOpen might throw Bad (ThrowsRepoCalls.res:19:6) and is not annotated with @throws(Bad)
Exception Analysis
ThrowsScopes.res:2:5-18
catchIfaceOpen might throw NotFound (ThrowsScopes.res:3:6) and is not annotated with @throws(NotFound)
Exception Analysis
ThrowsLibExnCalls.res:103:7-12
caught might throw E (ThrowsLibExnCalls.res:104:8) and is not annotated with @throws(E)
…
Exception Analysis
ThrowsLibExnCalls.res:81:5-16
catchRebound might throw E (ThrowsLibExnCalls.res:82:6) and is not annotated with @throws(E)
After (reanalyze): tests/analysis_tests/tests-reanalyze/deadcode/src/exception
Exception Analysis
ThrowsLibExnCalls.res:81:5-16
catchRebound might throw ThrowsLibExn.E (ThrowsLibExnCalls.res:82:6) and is not annotated with @throws(ThrowsLibExn.E)
Review: Why an identity from the file and name instead of the path? The type checker has no module prefix for a file's own exceptions, and a path changes with aliases and namespaces. The file and name are the same through every alias, open, include and functor, and in the .res and .resi.
Review: Does this recompile more? Only the attribute is new, and it holds names, not positions, so it changes only when the exception is renamed or moves to another file.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Kbf3gHA54VKxsXvCQyC2vw
Signed-off-by: Christoph Knittel <ck@cca.io>
866c860 to
629ea53
Compare
…citly
Why:
Codex review on #9198: the compiler's new @throws reader dropped
`list{A, B}` payloads and constructors written with arguments, which
reanalyze's old reader accepted, so such annotations silently stopped
declaring anything. It also derived an exception's identity from a
location that a .cmi built with -no-keep-locs (the runtime's and
Belt's) does not keep.
The reader again takes `list{...}` and `M.E(...)` as before. An exception
whose .cmi has no locations is now identified as `#Name` on purpose,
rather than through the placeholder file name `_none_`. Codex's crash
report for that case does not hold, since `Filename.remove_extension`
does not raise on `_none_`, but the identity was accidental.
AST modified (res.throws for a list payload):
Example:
exception E
@throws(list{E, Not_found})
let f = () => throw(E)
Before (text): number of res.throws attributes in bsc -dtypedtree
0
After (text): number of res.throws attributes in bsc -dtypedtree
1
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Kbf3gHA54VKxsXvCQyC2vw
Signed-off-by: Christoph Knittel <ck@cca.io>
… exactly
Why:
Review of #9198: the type checker loads the module a @throws names, but
the dependency scanner skipped attributes. A module named only there was
built in any order, so the name resolved or got warning 113 depending
on the build, and a later edit could fail with "inconsistent
assumptions". Messages also named an exception defined in a nested
module without that module, so the annotation they suggested did not
resolve. And a library built with -no-keep-locs recorded its own
exceptions with its file, while its users see them without one.
`Depend` now adds the modules a @throws names, like any other use, so
they are built first. A module that is both named in @throws and depends
on the annotating module now gives a dependency cycle error.
`Typemod` records which module of the current file defines each
exception and module, so a file's own exception is named `Lib.Inner.F`.
Under -no-keep-locs a file's own exceptions are identified by name alone
also while it is compiled. Warning 113 is shown once for a `let` that
destructures a tuple.
Diagnostic modified (an exception of a nested module):
Example:
// ThrowsLibExn.res
module Inner = {
exception F
@throws(F)
external find: int => int = "findThrowing"
}
// ThrowsLibExnCalls.res
let uncaughtNested = x => ThrowsLibExn.Inner.find(x)
Before (reanalyze): tests/analysis_tests/tests-reanalyze/deadcode/src/exception/ThrowsLibExnCalls.res
Exception Analysis
ThrowsLibExnCalls.res:110:5-18
uncaughtNested might throw ThrowsLibExn.F (ThrowsLibExnCalls.res:110:26) and is not annotated with @throws(ThrowsLibExn.F)
After (reanalyze): tests/analysis_tests/tests-reanalyze/deadcode/src/exception/ThrowsLibExnCalls.res
Exception Analysis
ThrowsLibExnCalls.res:110:5-18
uncaughtNested might throw ThrowsLibExn.Inner.F (ThrowsLibExnCalls.res:110:26) and is not annotated with @throws(ThrowsLibExn.Inner.F)
Diagnostic modified (a module named only in @throws, tests/build_tests/throws_dependency):
Example:
// A.res
@throws(Z.E)
external find: int => int = "find"
// Z.res
exception E
Before (diagnostic): A.res built before Z.res
Warning number 113
@throws names Z.E, which is not an exception in scope here. Exception analysis cannot match it with the exceptions callers catch.
After (diagnostic): Z.res built first, no warning
Breaking: A module named in @throws that itself depends on the annotating module is now a dependency cycle. Move the exception to a module of its own.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Kbf3gHA54VKxsXvCQyC2vw
Signed-off-by: Christoph Knittel <ck@cca.io>
Why:
Review on #9198: a library's `@throws(E)` reached callers as the bare
name `E`, while a catch was compared as written (`Lib.E`), so a correct
catch was still reported and a caller's own `E` could hide the
library's. Reanalyze cannot resolve names itself, because a .cmt keeps
no environments; two attempts to guess the scope from the file still
got opens in interfaces, functors, includes and aliases wrong.
The type checker now looks up each name in a `@throws` where it is
written, for externals, `let`s in signatures and interfaces, and every
`let` binding. It adds a generated `@res.throws` that lists each
exception's readable name and identity, and the inferred signature keeps
it next to `@throws`. The identity is the defining file without its
extension and the exception's name (`src/Lib#E`), so a module's .res and
.resi agree and moving a definition within its file changes nothing.
Exception analysis compares these identities with the constructors that
catches and throws name.
A name that is not an exception in scope gets the new warning 113 and is
listed as written. A rebinding (`exception Mine = Lib.E`) is a new
definition, so catching `Mine` does not catch `Lib.E`, as before. Two
exceptions with the same name in one file share an identity.
Typedtree modified (@throws resolved where it is written):
Example:
module Errors = {
exception NotFound
}
open Errors
@throws(NotFound)
external find: int => int = "find"
Before (typed):
Tstr_primitive
value_description find/1003
attribute "throws"
[
structure_item
Pstr_eval
expression
Pexp_construct "NotFound"
[]
]
After (typed):
Tstr_primitive
value_description find/1003
attribute "throws"
[
structure_item
Pstr_eval
expression
Pexp_construct "NotFound"
[]
]
attribute "res.throws"
[
structure_item
Pstr_eval
expression
Pexp_array
[
expression
Pexp_tuple
[
expression
Pexp_constant PConst_string (source="Repo.Errors.NotFound", semantic="Repo.Errors.NotFound")
expression
Pexp_constant PConst_string (source="Repo#NotFound", semantic="Repo#NotFound")
]
]
]
Diagnostic added (warning 113):
Example:
@throws(Missing)
let f = () => 1
After (diagnostic): tests/build_tests/super_errors/fixtures/throws_unbound_warning.res
Warning number 113
throws_unbound_warning.res:3:9-15
@throws names Missing, which is not an exception in scope here. Exception analysis cannot match it with the exceptions callers catch.
Reanalyze modified (catches of another module's exceptions):
Example:
// ThrowsLibExn.res
exception E
@throws(E)
external parse: string => int = "parseThrowing"
// ThrowsLibExnCalls.res
let catchExternal = s =>
try ThrowsLibExn.parse(s) catch {
| ThrowsLibExn.E => 0
}
Before (reanalyze): tests/analysis_tests/tests-reanalyze/deadcode/src/exception
Exception Analysis
ThrowsLibExnCalls.res:4:5-17
catchExternal might throw E (ThrowsLibExnCalls.res:5:6) and is not annotated with @throws(E)
Exception Analysis
ThrowsRepoCalls.res:18:5-16
catchViaOpen might throw Bad (ThrowsRepoCalls.res:19:6) and is not annotated with @throws(Bad)
Exception Analysis
ThrowsScopes.res:2:5-18
catchIfaceOpen might throw NotFound (ThrowsScopes.res:3:6) and is not annotated with @throws(NotFound)
Exception Analysis
ThrowsLibExnCalls.res:103:7-12
caught might throw E (ThrowsLibExnCalls.res:104:8) and is not annotated with @throws(E)
…
Exception Analysis
ThrowsLibExnCalls.res:81:5-16
catchRebound might throw E (ThrowsLibExnCalls.res:82:6) and is not annotated with @throws(E)
After (reanalyze): tests/analysis_tests/tests-reanalyze/deadcode/src/exception
Exception Analysis
ThrowsLibExnCalls.res:81:5-16
catchRebound might throw ThrowsLibExn.E (ThrowsLibExnCalls.res:82:6) and is not annotated with @throws(ThrowsLibExn.E)
Review: Why an identity from the file and name instead of the path? The type checker has no module prefix for a file's own exceptions, and a path changes with aliases and namespaces. The file and name are the same through every alias, open, include and functor, and in the .res and .resi.
Review: Does this recompile more? Only the attribute is new, and it holds names, not positions, so it changes only when the exception is renamed or moves to another file.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Kbf3gHA54VKxsXvCQyC2vw
Signed-off-by: Christoph Knittel <ck@cca.io>
…citly
Why:
Codex review on #9198: the compiler's new @throws reader dropped
`list{A, B}` payloads and constructors written with arguments, which
reanalyze's old reader accepted, so such annotations silently stopped
declaring anything. It also derived an exception's identity from a
location that a .cmi built with -no-keep-locs (the runtime's and
Belt's) does not keep.
The reader again takes `list{...}` and `M.E(...)` as before. An exception
whose .cmi has no locations is now identified as `#Name` on purpose,
rather than through the placeholder file name `_none_`. Codex's crash
report for that case does not hold, since `Filename.remove_extension`
does not raise on `_none_`, but the identity was accidental.
AST modified (res.throws for a list payload):
Example:
exception E
@throws(list{E, Not_found})
let f = () => throw(E)
Before (text): number of res.throws attributes in bsc -dtypedtree
0
After (text): number of res.throws attributes in bsc -dtypedtree
1
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Kbf3gHA54VKxsXvCQyC2vw
Signed-off-by: Christoph Knittel <ck@cca.io>
629ea53 to
d19eb02
Compare
… exactly
Why:
Review of #9198: the type checker loads the module a @throws names, but
the dependency scanner skipped attributes. A module named only there was
built in any order, so the name resolved or got warning 113 depending
on the build, and a later edit could fail with "inconsistent
assumptions". Messages also named an exception defined in a nested
module without that module, so the annotation they suggested did not
resolve. And a library built with -no-keep-locs recorded its own
exceptions with its file, while its users see them without one.
`Depend` now adds the modules a @throws names, like any other use, so
they are built first. A module that is both named in @throws and depends
on the annotating module now gives a dependency cycle error.
`Typemod` records which module of the current file defines each
exception and module, so a file's own exception is named `Lib.Inner.F`.
Under -no-keep-locs a file's own exceptions are identified by name alone
also while it is compiled. Warning 113 is shown once for a `let` that
destructures a tuple.
Diagnostic modified (an exception of a nested module):
Example:
// ThrowsLibExn.res
module Inner = {
exception F
@throws(F)
external find: int => int = "findThrowing"
}
// ThrowsLibExnCalls.res
let uncaughtNested = x => ThrowsLibExn.Inner.find(x)
Before (reanalyze): tests/analysis_tests/tests-reanalyze/deadcode/src/exception/ThrowsLibExnCalls.res
Exception Analysis
ThrowsLibExnCalls.res:110:5-18
uncaughtNested might throw ThrowsLibExn.F (ThrowsLibExnCalls.res:110:26) and is not annotated with @throws(ThrowsLibExn.F)
After (reanalyze): tests/analysis_tests/tests-reanalyze/deadcode/src/exception/ThrowsLibExnCalls.res
Exception Analysis
ThrowsLibExnCalls.res:110:5-18
uncaughtNested might throw ThrowsLibExn.Inner.F (ThrowsLibExnCalls.res:110:26) and is not annotated with @throws(ThrowsLibExn.Inner.F)
Diagnostic modified (a module named only in @throws, tests/build_tests/projects/throws_dependency):
Example:
// A.res
@throws(Z.E)
external find: int => int = "find"
// Z.res
exception E
Before (diagnostic): A.res built before Z.res
Warning number 113
@throws names Z.E, which is not an exception in scope here. Exception analysis cannot match it with the exceptions callers catch.
After (diagnostic): Z.res built first, no warning
Breaking: A module named in @throws that itself depends on the annotating module is now a dependency cycle. Move the exception to a module of its own.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Kbf3gHA54VKxsXvCQyC2vw
Signed-off-by: Christoph Knittel <ck@cca.io>
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: d19eb02243
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
Why:
Codex review on #9198: an exception's identity came from its location,
which a .cmi built with -no-keep-locs drops but the .cmt keeps. So in
such a project the compiler recorded `#E` for `@throws(E)` while
reanalyze computed the file-based identity for `throw(E)`, and an
annotated function was reported as both missing and redundant.
The type checker now records the identity when it types an exception, in
a generated `@res.exn` attribute on the definition, and everything reads
it from there: the unit and module path, as `Lib.Inner.F`. Attributes
survive -no-keep-locs, and the identity holds no file paths. A rebound
exception (`exception Mine = Lib.E`) carries the identity of the one it
rebinds, so catching `Mine` now catches `Lib.E`. Exceptions of the same
name in different modules of one file no longer share an identity.
Reanalyze modified (rebinding):
Example:
// ThrowsLibExn.res
exception E
@throws(E)
external parse: string => int = "parseThrowing"
// ThrowsLibExnCalls.res
exception Rebound = ThrowsLibExn.E
let catchRebound = s =>
try ThrowsLibExn.parse(s) catch {
| Rebound => 0
}
Before (reanalyze): tests/analysis_tests/tests-reanalyze/deadcode/src/exception/ThrowsLibExnCalls.res
Exception Analysis
ThrowsLibExnCalls.res:81:5-16
catchRebound might throw ThrowsLibExn.E (ThrowsLibExnCalls.res:82:6) and is not annotated with @throws(ThrowsLibExn.E)
After (reanalyze): tests/analysis_tests/tests-reanalyze/deadcode/src/exception/ThrowsLibExnCalls.res
Analysis reported no issue for catchRebound
Review: How was the -no-keep-locs case checked? The whole reanalyze test project built with -no-keep-locs gives the same 55 findings as without it, and so does a namespaced build.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Kbf3gHA54VKxsXvCQyC2vw
Signed-off-by: Christoph Knittel <ck@cca.io>
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 1656650f46
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
1656650 to
1feb838
Compare
1feb838 to
4bcdd86
Compare
Why:
Review on #9198: exception analysis compares exceptions by name, and a
library's `@throws(E)` reached callers as the bare name `E`, while a
catch was spelled `Lib.E`. So a correct catch was still reported, and a
caller's own `E` could hide the library's. Reanalyze cannot resolve the
names itself, because a .cmt keeps no environments.
The type checker now gives every exception an identity and resolves the
names in a `@throws` where they are written:
- When it types an exception, it records the identity, the unit and
module path (`Lib.Inner.F`), in a generated `@res.exn` attribute on the
definition. A rebound exception (`exception Mine = Lib.E`) carries the
identity of the one it rebinds.
- For a `@throws` on an external, a `let` in a signature or interface, or
any `let` binding, it looks each name up and adds a generated
`@res.throws` listing each exception's name and identity. The inferred
signature keeps it next to `@throws`.
- A name that is not an exception in scope gets the new warning 113 and is
listed as written; lowercase names, which are never exceptions, do not
warn. The first exception of that name counts, so a variant constructor
declared later does not hide it. A payload may be one name or an array,
tuple or `list{...}` of names.
- `Depend` counts the modules a `@throws` names as dependencies, since the
type checker loads them.
Exception analysis compares those identities with the constructors that
catches and throws use. A call's exceptions are the declaration's
resolved `@throws`, or, when it has none, the implementation binding's:
the declaration is what callers see. Messages name an exception as the
code they are about would, relative to its enclosing module.
Known limits: an identity belongs to a declaration, and a signature's
`exception E` is a different declaration from the definition it hides.
So when only an implementation sealed by a module type annotates its
`@throws`, callers' catches of the module's `E` do not match it, and
modules sealed by one module type share its exceptions' identities. The
exceptions of two applications of one functor share an identity, and so
do same-named exceptions of a functor's parameter and body, or of a local
module and its enclosing module. A `@throws(X.E)` for an exception that a
functor parameter's module type declares keeps that declaration's
identity.
Reanalyze modified (exceptions of other modules):
Example:
// ThrowsLibExn.res
exception E
@throws(E)
external parse: string => int = "parseThrowing"
// ThrowsLibExnCalls.res
let catchExternal = s =>
try ThrowsLibExn.parse(s) catch {
| ThrowsLibExn.E => 0
}
exception E
let catchUnrelatedLocal = s =>
try ThrowsLibExn.parse(s) catch {
| E => 0
}
Before (reanalyze): tests/analysis_tests/tests-reanalyze/deadcode/src/exception
Exception Analysis
ThrowsLibExnCalls.res:4:5-17
catchExternal might throw E (ThrowsLibExnCalls.res:5:6) and is not annotated with @throws(E)
Exception Analysis
ThrowsRepoCalls.res:29:5-20
annotatedViaOpen might throw ThrowsErrors.NotFound (ThrowsRepoCalls.res:29:28) and is annotated with redundant @throws(NotFound)
Exception Analysis
ThrowsScopes.res:2:5-18
catchIfaceOpen might throw NotFound (ThrowsScopes.res:3:6) and is not annotated with @throws(NotFound)
…
After (reanalyze): tests/analysis_tests/tests-reanalyze/deadcode/src/exception
Exception Analysis
ThrowsLibExnCalls.res:42:5-23
catchUnrelatedLocal might throw ThrowsLibExn.E (ThrowsLibExnCalls.res:43:6) and is not annotated with @throws(ThrowsLibExn.E)
…
Diagnostic added (warning 113):
Example:
@throws(Missing)
let f = () => 1
After (diagnostic): tests/error_tests/single/fixtures/throws_unbound_warning.res
Warning number 113
throws_unbound_warning.res:3:9-15
@throws names Missing, which is not an exception in scope here. Exception analysis cannot match it with the exceptions callers catch.
Review: Why identities in the type checker, not in reanalyze? Only the type checker knows which exception a name means. Two attempts to rebuild the scope in reanalyze kept missing opens in interfaces, includes, functors and aliases.
Breaking: A module named in `@throws` that itself depends on the annotating module is now a dependency cycle. Move the exception to a module of its own.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Kbf3gHA54VKxsXvCQyC2vw
Signed-off-by: Christoph Knittel <ck@cca.io>
|
@codex review |
|
Codex Review: Didn't find any major issues. 🚀 Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
Why:
Review on #9198: exception analysis compares exceptions by name, and a
library's `@throws(E)` reached callers as the bare name `E`, while a
catch was spelled `Lib.E`. So a correct catch was still reported, and a
caller's own `E` could hide the library's. Reanalyze cannot resolve the
names itself, because a .cmt keeps no environments.
The type checker now gives every exception an identity and resolves the
names in a `@throws` where they are written:
- When it types an exception, it records the identity, the unit and
module path (`Lib.Inner.F`), in a generated `@res.exn` attribute on the
definition. A rebound exception (`exception Mine = Lib.E`) carries the
identity of the one it rebinds.
- For a `@throws` on an external, a `let` in a signature or interface, or
any `let` binding, it looks each name up and adds a generated
`@res.throws` listing each exception's name and identity. The inferred
signature keeps it next to `@throws`.
- A name that is not an exception in scope gets the new warning 113 and is
listed as written; lowercase names, which are never exceptions, do not
warn. The first exception of that name counts, so a variant constructor
declared later does not hide it. A payload may be one name, as a
constructor or a string (also a template), or an array, tuple or
`list{...}` of names, in braces or not.
- `Depend` counts the modules a `@throws` names as dependencies, since the
type checker loads them.
Exception analysis compares those identities with the constructors that
catches and throws use. A call's exceptions are the declaration's
resolved `@throws`, or, when it has none, the implementation binding's:
the declaration is what callers see. Messages name an exception as the
code they are about would, relative to its enclosing module.
Known limits: an identity belongs to a declaration, and a signature's
`exception E` is a different declaration from the definition it hides.
So when only an implementation sealed by a module type annotates its
`@throws`, callers' catches of the module's `E` do not match it, and
modules sealed by one module type share its exceptions' identities. The
exceptions of two applications of one functor share an identity, and so
do same-named exceptions of a functor's parameter and body, or of a local
module and its enclosing module. A `@throws(X.E)` for an exception that a
functor parameter's module type declares keeps that declaration's
identity.
Reanalyze modified (exceptions of other modules):
Example:
// ThrowsLibExn.res
exception E
@throws(E)
external parse: string => int = "parseThrowing"
// ThrowsLibExnCalls.res
let catchExternal = s =>
try ThrowsLibExn.parse(s) catch {
| ThrowsLibExn.E => 0
}
exception E
let catchUnrelatedLocal = s =>
try ThrowsLibExn.parse(s) catch {
| E => 0
}
Before (reanalyze): tests/analysis_tests/tests-reanalyze/deadcode/src/exception
Exception Analysis
ThrowsLibExnCalls.res:4:5-17
catchExternal might throw E (ThrowsLibExnCalls.res:5:6) and is not annotated with @throws(E)
Exception Analysis
ThrowsRepoCalls.res:29:5-20
annotatedViaOpen might throw ThrowsErrors.NotFound (ThrowsRepoCalls.res:29:28) and is annotated with redundant @throws(NotFound)
Exception Analysis
ThrowsScopes.res:2:5-18
catchIfaceOpen might throw NotFound (ThrowsScopes.res:3:6) and is not annotated with @throws(NotFound)
…
After (reanalyze): tests/analysis_tests/tests-reanalyze/deadcode/src/exception
Exception Analysis
ThrowsLibExnCalls.res:42:5-23
catchUnrelatedLocal might throw ThrowsLibExn.E (ThrowsLibExnCalls.res:43:6) and is not annotated with @throws(ThrowsLibExn.E)
…
Diagnostic added (warning 113):
Example:
@throws(Missing)
let f = () => 1
After (diagnostic): tests/error_tests/single/fixtures/throws_unbound_warning.res
Warning number 113
throws_unbound_warning.res:3:9-15
@throws names Missing, which is not an exception in scope here. Exception analysis cannot match it with the exceptions callers catch.
Review: Why identities in the type checker, not in reanalyze? Only the type checker knows which exception a name means. Two attempts to rebuild the scope in reanalyze kept missing opens in interfaces, includes, functors and aliases.
Breaking: A module named in `@throws` that itself depends on the annotating module is now a dependency cycle. Move the exception to a module of its own.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Kbf3gHA54VKxsXvCQyC2vw
Signed-off-by: Christoph Knittel <ck@cca.io>
4bcdd86 to
aa74ffb
Compare
Why:
Review on #9198: exception analysis compares exceptions by name, and a
library's `@throws(E)` reached callers as the bare name `E`, while a
catch was spelled `Lib.E`. So a correct catch was still reported, and a
caller's own `E` could hide the library's. Reanalyze cannot resolve the
names itself, because a .cmt keeps no environments.
The type checker now gives every exception an identity and resolves the
names in a `@throws` where they are written:
- When it types an exception, it records the identity, the unit and
module path (`Lib.Inner.F`), in a generated `@res.exn` attribute on the
definition. A rebound exception (`exception Mine = Lib.E`) carries the
identity of the one it rebinds.
- For a `@throws` on an external, a `let` in a signature or interface, or
any `let` binding, it looks each name up and adds a generated
`@res.throws` listing each exception's name and identity. The inferred
signature keeps it next to `@throws`.
- A name that is not an exception in scope gets the new warning 114 and is
listed as written; lowercase names, which are never exceptions, do not
warn. The first exception of that name counts, so a variant constructor
declared later does not hide it. A payload may be one name, as a
constructor or a string (also a template), or an array, tuple or
`list{...}` of names, in braces or not.
- `Depend` counts the modules a `@throws` names as dependencies, since the
type checker loads them.
Exception analysis compares those identities with the constructors that
catches and throws use. A call's exceptions are the declaration's
resolved `@throws`, or, when it has none, the implementation binding's:
the declaration is what callers see. Messages name an exception as the
code they are about would, relative to its enclosing module.
Known limits: an identity belongs to a declaration, and a signature's
`exception E` is a different declaration from the definition it hides.
So when only an implementation sealed by a module type annotates its
`@throws`, callers' catches of the module's `E` do not match it, and
modules sealed by one module type share its exceptions' identities. The
exceptions of two applications of one functor share an identity, and so
do same-named exceptions of a functor's parameter and body, or of a local
module and its enclosing module. A `@throws(X.E)` for an exception that a
functor parameter's module type declares keeps that declaration's
identity.
Reanalyze modified (exceptions of other modules):
Example:
// ThrowsLibExn.res
exception E
@throws(E)
external parse: string => int = "parseThrowing"
// ThrowsLibExnCalls.res
let catchExternal = s =>
try ThrowsLibExn.parse(s) catch {
| ThrowsLibExn.E => 0
}
exception E
let catchUnrelatedLocal = s =>
try ThrowsLibExn.parse(s) catch {
| E => 0
}
Before (reanalyze): tests/analysis_tests/tests-reanalyze/deadcode/src/exception
Exception Analysis
ThrowsLibExnCalls.res:4:5-17
catchExternal might throw E (ThrowsLibExnCalls.res:5:6) and is not annotated with @throws(E)
Exception Analysis
ThrowsRepoCalls.res:29:5-20
annotatedViaOpen might throw ThrowsErrors.NotFound (ThrowsRepoCalls.res:29:28) and is annotated with redundant @throws(NotFound)
Exception Analysis
ThrowsScopes.res:2:5-18
catchIfaceOpen might throw NotFound (ThrowsScopes.res:3:6) and is not annotated with @throws(NotFound)
…
After (reanalyze): tests/analysis_tests/tests-reanalyze/deadcode/src/exception
Exception Analysis
ThrowsLibExnCalls.res:42:5-23
catchUnrelatedLocal might throw ThrowsLibExn.E (ThrowsLibExnCalls.res:43:6) and is not annotated with @throws(ThrowsLibExn.E)
…
Diagnostic added (warning 114):
Example:
@throws(Missing)
let f = () => 1
After (diagnostic): tests/error_tests/single/fixtures/throws_unbound_warning.res
Warning number 114
throws_unbound_warning.res:3:9-15
@throws names Missing, which is not an exception in scope here. Exception analysis cannot match it with the exceptions callers catch.
Review: Why identities in the type checker, not in reanalyze? Only the type checker knows which exception a name means. Two attempts to rebuild the scope in reanalyze kept missing opens in interfaces, includes, functors and aliases.
Breaking: A module named in `@throws` that itself depends on the annotating module is now a dependency cycle. Move the exception to a module of its own.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Kbf3gHA54VKxsXvCQyC2vw
Signed-off-by: Christoph Knittel <ck@cca.io>
Why:
Review on #9198: exception analysis compares exceptions by name, and a
library's `@throws(E)` reached callers as the bare name `E`, while a
catch was spelled `Lib.E`. So a correct catch was still reported, and a
caller's own `E` could hide the library's. Reanalyze cannot resolve the
names itself, because a .cmt keeps no environments.
The type checker now gives every exception an identity and resolves the
names in a `@throws` where they are written:
- When it types an exception, it records the identity, the unit and
module path (`Lib.Inner.F`), in a generated `@res.exn` attribute on the
definition. A rebound exception (`exception Mine = Lib.E`) carries the
identity of the one it rebinds.
- For a `@throws` on an external, a `let` in a signature or interface, or
any `let` binding, it looks each name up and adds a generated
`@res.throws` listing each exception's name and identity. The inferred
signature keeps it next to `@throws`.
- A name that is not an exception in scope gets the new warning 114 and is
listed as written; lowercase names, which are never exceptions, do not
warn. The first exception of that name counts, so a variant constructor
declared later does not hide it. A payload may be one name, as a
constructor or a string (also a template), or an array, tuple or
`list{...}` of names, in braces or not.
- `Depend` counts the modules a `@throws` names as dependencies, since the
type checker loads them.
Exception analysis compares those identities with the constructors that
catches and throws use. A call's exceptions are the declaration's
resolved `@throws`, or, when it has none, the implementation binding's:
the declaration is what callers see. Messages name an exception as the
code they are about would, relative to its enclosing module.
Known limits: an identity belongs to a declaration, and a signature's
`exception E` is a different declaration from the definition it hides.
So when only an implementation sealed by a module type annotates its
`@throws`, callers' catches of the module's `E` do not match it, and
modules sealed by one module type share its exceptions' identities. The
exceptions of two applications of one functor share an identity, and so
do same-named exceptions of a functor's parameter and body, or of a local
module and its enclosing module, or of a module type and a module of the
same name. A `@throws(X.E)` for an exception that a functor parameter's
module type declares keeps that declaration's identity, and messages name
a functor body's exceptions by the functor, not by its application. An
interface whose `@throws` lists fewer exceptions than its implementation's
hides the rest from callers.
Reanalyze modified (exceptions of other modules):
Example:
// ThrowsLibExn.res
exception E
@throws(E)
external parse: string => int = "parseThrowing"
// ThrowsLibExnCalls.res
let catchExternal = s =>
try ThrowsLibExn.parse(s) catch {
| ThrowsLibExn.E => 0
}
exception E
let catchUnrelatedLocal = s =>
try ThrowsLibExn.parse(s) catch {
| E => 0
}
Before (reanalyze): tests/analysis_tests/tests-reanalyze/deadcode/src/exception
Exception Analysis
ThrowsLibExnCalls.res:4:5-17
catchExternal might throw E (ThrowsLibExnCalls.res:5:6) and is not annotated with @throws(E)
Exception Analysis
ThrowsRepoCalls.res:29:5-20
annotatedViaOpen might throw ThrowsErrors.NotFound (ThrowsRepoCalls.res:29:28) and is annotated with redundant @throws(NotFound)
Exception Analysis
ThrowsScopes.res:2:5-18
catchIfaceOpen might throw NotFound (ThrowsScopes.res:3:6) and is not annotated with @throws(NotFound)
…
After (reanalyze): tests/analysis_tests/tests-reanalyze/deadcode/src/exception
Exception Analysis
ThrowsLibExnCalls.res:42:5-23
catchUnrelatedLocal might throw ThrowsLibExn.E (ThrowsLibExnCalls.res:43:6) and is not annotated with @throws(ThrowsLibExn.E)
…
Diagnostic added (warning 114):
Example:
@throws(Missing)
let f = () => 1
After (diagnostic): tests/error_tests/single/fixtures/throws_unbound_warning.res
Warning number 114
throws_unbound_warning.res:3:9-15
@throws names Missing, which is not an exception in scope here. Exception analysis cannot match it with the exceptions callers catch.
Review: Why identities in the type checker, not in reanalyze? Only the type checker knows which exception a name means. Two attempts to rebuild the scope in reanalyze kept missing opens in interfaces, includes, functors and aliases.
Breaking: A module named in `@throws` that itself depends on the annotating module is now a dependency cycle. Move the exception to a module of its own.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Kbf3gHA54VKxsXvCQyC2vw
Signed-off-by: Christoph Knittel <ck@cca.io>
Why:
Review on #9198: exception analysis compares exceptions by name, and a
library's `@throws(E)` reached callers as the bare name `E`, while a
catch was spelled `Lib.E`. So a correct catch was still reported, and a
caller's own `E` could hide the library's. Reanalyze cannot resolve the
names itself, because a .cmt keeps no environments.
The type checker now gives every exception an identity and resolves the
names in a `@throws` where they are written:
- When it types an exception, it records the identity, the unit and
module path (`Lib.Inner.F`), in a generated `@res.exn` attribute on the
definition. A rebound exception (`exception Mine = Lib.E`) carries the
identity of the one it rebinds.
- For a `@throws` on an external, a `let` in a signature or interface, or
any `let` binding, it looks each name up and adds a generated
`@res.throws` listing each exception's name and identity. The inferred
signature keeps it next to `@throws`.
- A name that is not an exception in scope gets the new warning 114 and is
listed as written; lowercase names, which are never exceptions, do not
warn. The first exception of that name counts, so a variant constructor
declared later does not hide it. A payload may be one name, as a
constructor or a string (also a template), or an array, tuple or
`list{...}` of names, in braces or not.
- `Depend` counts the modules a `@throws` names as dependencies, since the
type checker loads them.
Exception analysis compares those identities with the constructors that
catches and throws use. A call's exceptions are the declaration's
resolved `@throws`, or, when it has none, the implementation binding's:
the declaration is what callers see. Messages name an exception as the
code they are about would, relative to its enclosing module.
Known limits: an identity belongs to a declaration, and a signature's
`exception E` is a different declaration from the definition it hides.
So when only an implementation sealed by a module type annotates its
`@throws`, callers' catches of the module's `E` do not match it, and
modules sealed by one module type share its exceptions' identities. The
exceptions of two applications of one functor share an identity, and so
do same-named exceptions of a functor's parameter and body, or of a local
module and its enclosing module, or of a module type and a module of the
same name. A `@throws(X.E)` for an exception that a functor parameter's
module type declares keeps that declaration's identity, and messages name
a functor body's exceptions by the functor, not by its application. An
interface whose `@throws` lists fewer exceptions than its implementation's
hides the rest from callers.
Reanalyze modified (exceptions of other modules):
Example:
// ThrowsLibExn.res
exception E
@throws(E)
external parse: string => int = "parseThrowing"
// ThrowsLibExnCalls.res
let catchExternal = s =>
try ThrowsLibExn.parse(s) catch {
| ThrowsLibExn.E => 0
}
exception E
let catchUnrelatedLocal = s =>
try ThrowsLibExn.parse(s) catch {
| E => 0
}
Before (reanalyze): tests/analysis_tests/tests-reanalyze/deadcode/src/exception
Exception Analysis
ThrowsLibExnCalls.res:4:5-17
catchExternal might throw E (ThrowsLibExnCalls.res:5:6) and is not annotated with @throws(E)
Exception Analysis
ThrowsRepoCalls.res:29:5-20
annotatedViaOpen might throw ThrowsErrors.NotFound (ThrowsRepoCalls.res:29:28) and is annotated with redundant @throws(NotFound)
Exception Analysis
ThrowsScopes.res:2:5-18
catchIfaceOpen might throw NotFound (ThrowsScopes.res:3:6) and is not annotated with @throws(NotFound)
…
After (reanalyze): tests/analysis_tests/tests-reanalyze/deadcode/src/exception
Exception Analysis
ThrowsLibExnCalls.res:42:5-23
catchUnrelatedLocal might throw ThrowsLibExn.E (ThrowsLibExnCalls.res:43:6) and is not annotated with @throws(ThrowsLibExn.E)
…
Diagnostic added (warning 114):
Example:
@throws(Missing)
let f = () => 1
After (diagnostic): tests/error_tests/single/fixtures/throws_unbound_warning.res
Warning number 114
throws_unbound_warning.res:3:9-15
@throws names Missing, which is not an exception in scope here. Exception analysis cannot match it with the exceptions callers catch.
Review: Why identities in the type checker, not in reanalyze? Only the type checker knows which exception a name means. Two attempts to rebuild the scope in reanalyze kept missing opens in interfaces, includes, functors and aliases.
Breaking: A module named in `@throws` that itself depends on the annotating module is now a dependency cycle. Move the exception to a module of its own.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Kbf3gHA54VKxsXvCQyC2vw
Signed-off-by: Christoph Knittel <ck@cca.io>
Why:
Exception analysis knew which library functions throw only from a
hand-written table in exn_lib.ml, and the table fell behind. The
Temporal `fromStringOrThrow` functions were never added (#9182), and
neither were some Intl and Belt functions. Third-party libraries and a
project's own externals had no way to declare their exceptions.
The table is gone. At each call, the analysis now reads the `@throws` on
the declaration of the called function. The `.cmi` keeps the attributes
of externals and of `let`s in interfaces, so their `@throws` reaches
calls in other modules and packages.
A `let` in a module without an interface used to lose its attributes in
the inferred signature. The type checker now copies its `@throws` there,
as it already did for `@deprecated`.
The stdlib and Belt annotate their throwing functions themselves. A
guard in the reanalyze tests fails when a stdlib or Belt function named
`*OrThrow` or `*Exn` 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`) when the declaration has a `@throws`
throws(`f`) = values(`f`) otherwise
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 does the declaration win over the implementation? The values table holds the `@throws` on the implementation's `let`. When the interface has a `@throws`, that is what callers see, so it counts alone. When it has none, the implementation's counts, so an annotated `.res` behind an unannotated `.resi` still works.
Review: Why does the inferred signature copy only `@throws` and `@deprecated`? A doc comment is an attribute too, and keeping it in the `.cmi` would make every edit of the comment recompile the module's dependents. A module with a `.resi` is unaffected, because its `.cmi` comes from the interface.
Fixes #9182
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Christoph Knittel <ck@cca.io>
aa74ffb to
d5177af
Compare
Why:
Exception analysis compares exceptions by name. A library's
`@throws(E)` reached callers as `E`, while their catch said `Lib.E`, so
a correct catch was reported and a caller's own `E` could hide the
library's exception. Which exception a name refers to depends on the
opens, aliases and includes in scope, and only the type checker knows
that. Reanalyze can't work it out, because a .cmt keeps no type
environments. (Review comment on #9198.)
The type checker now resolves the names, and stores the results in typed
fields that reach the .cmi:
- When it defines an exception, it records the module path where the
exception is defined, for example `Lib.Inner.F`, as the exception's
identity (`ext_identity`, copied to `cstr_identity`). A rebinding such as
`exception Mine = Lib.E` gets the identity of `Lib.E`.
- For a `@throws` on an external, a `let` in an interface or module type,
or any `let`, it looks up each name and stores the exceptions it found
with their identities (`val_throws`, `vb_throws`, and a third component
of `Tstr_eval`). The inferred signature keeps them.
- A name that isn't an exception in scope gets the new warning 114. The
first exception of a name counts, so a variant constructor declared
later doesn't hide it. Lowercase names never warn.
- The modules a `@throws` names become dependencies, because the type
checker loads them.
Reanalyze compares these identities with the identity of the constructor
at each catch and throw. Its messages name an exception the way the
surrounding code would, relative to the enclosing module.
Known limits: an identity belongs to a declaration, and a signature's
`exception E` is a separate declaration from the definition it hides. So
when only the implementation of a sealed module annotates `@throws`, a
caller's catch of the module's `E` doesn't match it, and modules sealed by
one module type share its exceptions' identities. In a functor,
`@throws(X.E)` keeps the identity from the parameter's module type.
Exceptions with the same name and module path also share an identity: two
applications of one functor, a functor's parameter and body, a local
module and the module around it, a module type and a module with the same
name.
Reanalyze modified (exceptions of other modules):
Example:
// ThrowsLibExn.res
exception E
@throws(E)
external parse: string => int = "parseThrowing"
// ThrowsLibExnCalls.res
let catchExternal = s =>
try ThrowsLibExn.parse(s) catch {
| ThrowsLibExn.E => 0
}
exception E
let catchUnrelatedLocal = s =>
try ThrowsLibExn.parse(s) catch {
| E => 0
}
Before (reanalyze): tests/analysis_tests/tests-reanalyze/deadcode/src/exception
Exception Analysis
ThrowsLibExnCalls.res:4:5-17
catchExternal might throw E (ThrowsLibExnCalls.res:5:6) and is not annotated with @throws(E)
…
After (reanalyze): tests/analysis_tests/tests-reanalyze/deadcode/src/exception
Exception Analysis
ThrowsLibExnCalls.res:42:5-23
catchUnrelatedLocal might throw ThrowsLibExn.E (ThrowsLibExnCalls.res:43:6) and is not annotated with @throws(ThrowsLibExn.E)
…
Diagnostic added (warning 114):
Example:
@throws(Missing)
let f = () => 1
After (diagnostic): tests/error_tests/single/fixtures/throws_unbound_warning.res
Warning number 114
throws_unbound_warning.res:3:9-15
@throws names Missing, which is not an exception in scope here. Exception analysis cannot match it with the exceptions callers catch.
Review: Why typed fields rather than attributes? The resolved exceptions are data the type checker computes, not something a user writes. Fields keep them out of reach of user attributes and need no encoding as strings. The .cmi format changes, and the magic numbers are bumped at release.
Review: Why not store the exceptions' paths, which the type checker would rewrite when a module is sealed or a functor applied? A catch's path is the path it was reached through, so `Lib.E`, `L.E` with `module L = Lib`, and `Ns.Lib.E` differ, and telling them apart needs the type environment. Making paths work needs changes to strengthening and substitution. Fields with paths could replace these string identities later.
Breaking: A module named in `@throws` that itself depends on the annotating module is now a dependency cycle. Move the exception to a module of its own.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Christoph Knittel <ck@cca.io>
Fixes #9182
The problem
Exception analysis (
rescript-tools reanalyze -exception) knew which library functions throw only from a hand-written table inanalysis/reanalyze/src/exn_lib.ml, and that table fell behind the standard library. #9182 is one example: the eight TemporalfromStringOrThrowfunctions were never added, soTemporal.PlainDate.fromStringOrThrow("x")counted as throwing nothing. Some Intl and Belt functions were missing too. Third-party libraries and a project's own externals had no way to declare their exceptions at all.The fix
The table is gone. Functions declare their exceptions with
@throwswhere they are declared, and the analysis reads that at each call.@throwson declarations reaches callers. The.cmikeeps the attributes of externals and oflets in interfaces, so their@throwsreaches calls from other modules and packages, including calls through aliases,open,include, functor results and first-class modules.A
letwithout an interface keeps its@throwstoo. Until now, aletin a module without a.resilost its attributes in the inferred signature, which is whyStdlib.panichad to be in the table. The type checker already copied@deprecatedinto the signature (#8924); it now copies@throwsthe same way. Nothing else is copied: a doc comment in the.cmi, for example, would make every edit of it recompile the module's dependents. The.cmiformat is unchanged.The stdlib and Belt annotate themselves. Every function the table listed now has its own
@throws, in its.resi, or in its.reswhere there is no interface:getExn/getOrThrowfamily across Belt's collections,OptionandResultBelt.Array.getExnandsetExn(Assert_failure)*OrThrowfunctions ofList,Null,Nullable,Option,Result,BoolandBigIntJSON.parseOrThrowandstringifyAnyJsErrorfunctions that throw/andmod(Division_by_zero)Stdlib.panicandStdlib.assertEqualThe functions #9182 found missing are annotated as well: the Temporal
fromStringOrThrowfunctions, the Intl*Exnexternals,Belt.MutableStack.popOrThrow, andgetOrThrowinBelt.Map.DictandBelt.Set.Dict.A guard keeps the annotations complete.
tests/analysis_tests/tests-reanalyze/checkThrowsAnnotations.mjsruns before the reanalyze tests and fails if a stdlib or Belt function named*OrThrowor*Exnhas no@throws.JsError.toJsExnis exempt, because it converts a value and throws nothing.Decisions to review
The declaration wins over the implementation. A call's exceptions come from the
@throwsof the declaration the caller sees, for example aletin a.resi. Only when that declaration has no@throwsdoes the analysis fall back to the@throwson the implementation's ownlet, so an annotated.resbehind an unannotated.resistill counts. If both are annotated, the interface wins, because its exceptions are the ones callers know about. The downside: when a.resilists fewer exceptions than its.res, the extra ones are hidden from callers.Functions that throw only for some arguments stay unannotated, when their names don't say so: the Temporal constructors and arithmetic,
Int.rangewith a step of 0, andDate.toTemporalInstanton an invalid date. Annotating them would flag every call. The%operator was never in the table either, although it is the same primitive asmod.Known limit
Exception analysis still compares exceptions by name, as on master. That is exact for the predefined exceptions all stdlib and Belt annotations name, such as
Not_foundandJsExn. It is not exact for a library's own exceptions: a library's@throws(E)reaches the caller asE, while the caller's catch saysLib.E. #9476, stacked on this PR, fixes that.Tests
ThrowsDecl.res,ThrowsIface.res/.resi,ThrowsDeclCalls.res) call annotated externals,.resilets and module type members from another module, also through a pipe, an alias,open,include, a functor and a first-class module.callViaLetIncludedis only reported because aletwithout an interface now keeps its@throwsin the signature.callImplOnlyIncludedchecks that an unannotated.resistill hides an annotated.resbinding from other modules.catchNarrowedcalls a function whose.resilists fewer exceptions than its.res, and only the interface's count.StdlibTest.resandBeltTest.rescover the new stdlib and Belt annotations, includingpanicandassertEqual.ExnPayloadGuard.resreplaces the deleted Yojson test: catching an exception with a payload pattern and a guard.🤖 Generated with Claude Code