Semantic Diff

#9476 Match @throws exceptions with catches across modules

1 commit

4bcdd8614619ed582a89ae4e7228dbe362391125..pr-9476

92428986b · cknitt + Claude · Nov 29, 2026, 07:33 UTC

Resolve the exceptions @throws names in the type checker

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.

modifiedReanalyze exceptions of other modules

// ThrowsLibExn.resexception E@throws(E)external parse: string => int = "parseThrowing" // ThrowsLibExnCalls.reslet catchExternal = s =>  try ThrowsLibExn.parse(s) catch {  | ThrowsLibExn.E => 0  }exception Elet catchUnrelatedLocal = s =>  try ThrowsLibExn.parse(s) catch {  | E => 0  }
reanalyze Exception Analysis
Before tests/analysis_tests/tests-reanalyze/deadcode/src/exception
After tests/analysis_tests/tests-reanalyze/deadcode/src/exception
ThrowsLibExnCalls.res:4:5-17
catchExternal might throw E (ThrowsLibExnCalls.res:5:6) and is not annotated with @throws(E)
ThrowsRepoCalls.res:29:5-20
annotatedViaOpen might throw ThrowsErrors.NotFound (ThrowsRepoCalls.res:29:28) and is annotated with redundant @throws(NotFound)
ThrowsScopes.res:2:5-18
catchIfaceOpen might throw NotFound (ThrowsScopes.res:3:6) and is not annotated with @throws(NotFound)
ThrowsLibExnCalls.res:42:5-23
catchUnrelatedLocal might throw ThrowsLibExn.E (ThrowsLibExnCalls.res:43:6) and is not annotated with @throws(ThrowsLibExn.E)
…
…

addedDiagnostic warning 113

@throws(Missing)let f = () => 1
diagnostic
After tests/error_tests/single/fixtures/throws_unbound_warning.res
Warning number 113throws_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.