Skip to content

Commit bd4a722

Browse files
cknittclaude
andcommitted
Name exceptions by where they are defined in exception analysis
Why: A review of 81ce8eb found that it broke common code. A @throws naming another module's exception got the callee's module put in front (`ThrowsRepo.ThrowsErrors.NotFound`). A binding's own @throws was still compared as written, so `@throws(Bad)` after `open ThrowsErrors`, an alias, or any name in a namespaced project warned twice. And how a name resolved depended on whether some other file caught it. Every exception now has one name: where it is defined. A file's own exceptions are named with the file and module (`File.M.E`), a namespace is left out, and `exception E = Lib.F` is `Lib.F`. Every @throws name is resolved in the scope it was written in: the module and the modules around it, what they include, and the file's opens and module aliases. The first candidate the project defines, catches or throws wins. A binding's own @throws is resolved the same way. Messages leave out the current file's name, so existing warnings read as before. A namespaced callee path now drops the namespace (`Ns.Lib.f` is `Lib.f`), so calls in namespaced projects find the binding's @throws. A functor parameter's declarations are still named after the parameter: `X.get`'s `@throws(E)` is `X.E`. A fixture shows this limit. Reanalyze modified (names from another module, open and alias): Example: // ThrowsErrors.res exception NotFound exception Bad(string) // ThrowsRepo.res @throws(ThrowsErrors.NotFound) let find = x => x > 0 ? x : throw(ThrowsErrors.NotFound) open ThrowsErrors @throws(Bad) let viaOpen = x => x > 0 ? x : throw(Bad("negative")) // ThrowsRepoCalls.res let catchFind = x => try ThrowsRepo.find(x) catch { | ThrowsErrors.NotFound => 0 } Before (reanalyze): tests/analysis_tests/tests-reanalyze/deadcode/src/exception Exception Analysis ThrowsRepo.res:17:5-11 viaOpen might throw ThrowsErrors.Bad (ThrowsRepo.res:17:31) and is annotated with redundant @throws(Bad) Exception Analysis ThrowsRepoCalls.res:3:5-13 catchFind might throw ThrowsRepo.ThrowsErrors.NotFound (ThrowsRepoCalls.res:4:6) and is not annotated with @throws(ThrowsRepo.ThrowsErrors.NotFound) Exception Analysis ThrowsRepoCalls.res:24:5-13 annotated might throw ThrowsRepo.ThrowsErrors.NotFound (ThrowsRepoCalls.res:24:21) and is annotated with redundant @throws(ThrowsErrors.NotFound) … Analysis reported 63 issues (Exception Analysis:63) After (reanalyze): tests/analysis_tests/tests-reanalyze/deadcode/src/exception Exception Analysis ThrowsLibExnCalls.res:101:7-12 caught might throw X.E (ThrowsLibExnCalls.res:102:8) and is not annotated with @throws(X.E) Analysis reported 47 issues (Exception Analysis:47) Review: Why not resolve @throws in the type checker, which has the environment? That would be exact, and would also handle functor parameters, but it is a much bigger compiler change. The .cmt keeps no environments, so reanalyze rebuilds the scope it needs from the file. Review: Can a guess pick the wrong exception? Only when two candidates both exist, as a module's own E and an E it opens. The module's own name is tried first, then what it includes, then the outer modules, then the opens, which can differ from the language's shadowing order. 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>
1 parent 81ce8eb commit bd4a722

8 files changed

Lines changed: 468 additions & 176 deletions

File tree

‎analysis/reanalyze/src/exception.ml‎

Lines changed: 370 additions & 175 deletions
Large diffs are not rendered by default.

‎tests/analysis_tests/tests-reanalyze/deadcode/expected/exception.txt‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,5 +183,9 @@
183183
Exception Analysis
184184
ThrowsLibExnCalls.res:48:5-12
185185
uncaught might throw ThrowsLibExn.E (ThrowsLibExnCalls.res:48:20) and is not annotated with @throws(ThrowsLibExn.E)
186+
187+
Exception Analysis
188+
ThrowsLibExnCalls.res:101:7-12
189+
caught might throw X.E (ThrowsLibExnCalls.res:102:8) and is not annotated with @throws(X.E)
186190

187-
Analysis reported 46 issues (Exception Analysis:46)
191+
Analysis reported 47 issues (Exception Analysis:47)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
exception NotFound
2+
exception Bad(string)

‎tests/analysis_tests/tests-reanalyze/deadcode/src/exception/ThrowsLibExn.res‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,8 @@ module Outer = {
1717
@throws(E)
1818
external findOuter: int => int = "findOuterThrowing"
1919
}
20+
21+
module type S = {
22+
@throws(E)
23+
let get: int => int
24+
}

‎tests/analysis_tests/tests-reanalyze/deadcode/src/exception/ThrowsLibExnCalls.res‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,37 @@ let catchLocalQualified = x =>
6969
try Local.find(x) catch {
7070
| Local.G => 0
7171
}
72+
73+
// Outer.findOuter's @throws(E) names ThrowsLibExn.E.
74+
@throws(ThrowsLibExn.E)
75+
let annotatedOuter = x => ThrowsLibExn.Outer.findOuter(x)
76+
77+
exception Rebound = ThrowsLibExn.E
78+
79+
let catchRebound = s =>
80+
try ThrowsLibExn.parse(s) catch {
81+
| Rebound => 0
82+
}
83+
84+
let catchReexported = s =>
85+
try ThrowsReexport.parse(s) catch {
86+
| ThrowsLibExn.E => 0
87+
}
88+
89+
module LocalReexport = {
90+
include ThrowsLibExn
91+
}
92+
93+
let catchLocalReexport = s =>
94+
try LocalReexport.parse(s) catch {
95+
| ThrowsLibExn.E => 0
96+
}
97+
98+
// Known limit: a functor parameter's declarations are named after the
99+
// parameter, so X.get's E is not resolved to ThrowsLibExn.E.
100+
module UseS = (X: ThrowsLibExn.S) => {
101+
let caught = x =>
102+
try X.get(x) catch {
103+
| ThrowsLibExn.E => 0
104+
}
105+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
include ThrowsLibExn
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Functions that throw exceptions defined in another module, ThrowsErrors.
2+
3+
@throws(ThrowsErrors.NotFound)
4+
let find = x => x > 0 ? x : throw(ThrowsErrors.NotFound)
5+
6+
@throws(ThrowsErrors.NotFound)
7+
external findExternal: int => int = "findExternal"
8+
9+
module L = ThrowsErrors
10+
11+
@throws(L.NotFound)
12+
let viaAlias = x => x > 0 ? x : throw(L.NotFound)
13+
14+
open ThrowsErrors
15+
16+
@throws(Bad)
17+
let viaOpen = x => x > 0 ? x : throw(Bad("negative"))
18+
19+
let catchViaOpen = x =>
20+
try viaOpen(x) catch {
21+
| Bad(_) => 0
22+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Calls into ThrowsRepo, whose annotations name exceptions of ThrowsErrors.
2+
3+
let catchFind = x =>
4+
try ThrowsRepo.find(x) catch {
5+
| ThrowsErrors.NotFound => 0
6+
}
7+
8+
let catchFindExternal = x =>
9+
try ThrowsRepo.findExternal(x) catch {
10+
| ThrowsErrors.NotFound => 0
11+
}
12+
13+
let catchViaAlias = x =>
14+
try ThrowsRepo.viaAlias(x) catch {
15+
| ThrowsErrors.NotFound => 0
16+
}
17+
18+
let catchViaOpen = x =>
19+
try ThrowsRepo.viaOpen(x) catch {
20+
| ThrowsErrors.Bad(_) => 0
21+
}
22+
23+
@throws(ThrowsErrors.NotFound)
24+
let annotated = x => ThrowsRepo.find(x)
25+
26+
open ThrowsErrors
27+
28+
@throws(NotFound)
29+
let annotatedViaOpen = x => ThrowsRepo.find(x)

0 commit comments

Comments
 (0)