Skip to content

Commit b6d51cc

Browse files
authored
fix(ir): capture bound catch classes in funs
Funs capture values read only as catch classes. Unused-variable diagnostics recognize those outer binding reads. Native and BEAM regressions protect matching, mismatching, nested and named funs, comprehension helpers and shadowing.
1 parent cbcc1ce commit b6d51cc

5 files changed

Lines changed: 102 additions & 4 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,16 @@ All notable milestones for the nera project are documented in this file.
1212
partial-byte, large and extracted bitstrings, non-bitstrings and higher-order
1313
fun consumers with explicit completion.
1414

15+
## Bound catch classes are captured by funs
16+
17+
- A fun that uses an already-bound name only as a catch class now captures
18+
its value. A mismatching exception falls through instead of treating the
19+
name as a fresh class variable.
20+
- The same read counts as a use of the outer binding, removing false
21+
unused-variable warnings.
22+
- `fun_catch_class` checks matching and mismatching classes, nested and named
23+
funs, comprehension helpers and fun-head shadowing natively and on BEAM.
24+
1525
## Regression coverage for case exports inside maybe bodies
1626

1727
- A diagnostic regression protects names bound in every `case` branch and read

‎crates/nera-ir/src/warnings/ast_walkers.rs‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -385,10 +385,12 @@ fn collect_refs(expr: &ast::Expr, refs: &mut HashSet<AtomId>, scoping: RefScopin
385385
}
386386
}
387387
for clause in catch_clauses {
388+
// Like reason patterns, a class name can compare an outer
389+
// binding. Include it for closure capture and unused checks.
390+
if let ast::CatchClass::Variable(name, _) = clause.class {
391+
refs.insert(name);
392+
}
388393
if scoping == RefScoping::Lexical {
389-
if let ast::CatchClass::Variable(name, _) = clause.class {
390-
refs.insert(name);
391-
}
392394
if let Some(stacktrace) = &clause.stacktrace {
393395
collect_pattern_bindings(stacktrace, refs);
394396
collect_pattern_refs(stacktrace, refs);

‎crates/nera-ir/src/warnings/unused_vars_tests.rs‎

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,30 @@
1-
//! Regression for false unused-variable warnings on map keys and binary sizes.
1+
//! Regressions for false unused-variable warnings on pattern reads.
22
33
use nera_syntax::error::Severity;
44
use nera_syntax::parser::Parser;
55
use nera_syntax::span::Span;
66

7+
#[test]
8+
fn bound_catch_class_is_used_inside_fun() {
9+
// Intent: an outer name read only as a catch class inside a fun is used.
10+
// This fixture owns the requested runtime behavior natively and on BEAM;
11+
// this test owns its unused-variable diagnostics.
12+
let source = include_str!("../../../../tests/fun_catch_class.erl");
13+
let mut parser = Parser::new(source);
14+
let parsed = parser.parse_module();
15+
assert!(parsed.errors.is_empty(), "{:?}", parsed.errors);
16+
let diagnostics = crate::warnings::analyze_module(&parsed.module, &parser.atom_names(), None);
17+
assert!(
18+
diagnostics.iter().all(|d| d.severity != Severity::Error),
19+
"unexpected errors: {diagnostics:#?}"
20+
);
21+
let unused: Vec<_> = diagnostics
22+
.iter()
23+
.filter(|d| d.code.as_deref() == Some("unused_variable"))
24+
.collect();
25+
assert!(unused.is_empty(), "unexpected unused variables: {unused:#?}");
26+
}
27+
728
#[test]
829
fn pattern_expression_reads_preserve_unused_value_warnings() {
930
// Intent: keys and sizes count as uses without treating value bindings as

‎tests/README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,7 @@ These are automatically skipped by the runner and show up as SKIP in output.
328328
|---------------------------|----------------------------------------|------------|
329329
| `fun_simple.erl` | Basic anonymous fun usage | ~6 |
330330
| `fun_closure.erl` | Closure capture and invocation | ~8 |
331+
| `fun_catch_class.erl` | Bound catch-class-only captures: class matches and mismatches, nested/named funs, comprehension helpers, fun-head shadowing and fresh-class controls; same fixture on native and BEAM | Completion checked |
331332
| `fun_equality.erl` | Same-expression closure equality, numeric/exact captures, distinct code, nested/named funs, dictionary/map/ETS keys, GC and message copies; same fixture on native and BEAM | Table-driven |
332333
| `fun_errors.erl` | Fun error handling | ~6 |
333334
| `fun_foldl.erl` | Higher-order foldl | ~4 |

‎tests/fun_catch_class.erl‎

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
%% EXPECT: fun_catch_class_ok
2+
%% Bound catch classes are reads, even when no guard or body reads the name.
3+
%% Run the same assertions natively and on BEAM.
4+
-module(fun_catch_class).
5+
-export([test/0]).
6+
7+
test() ->
8+
right = requested(),
9+
lists:foreach(fun({Class, Raised, Expected}) ->
10+
F = capture(Class, Raised),
11+
Expected = F(),
12+
Outer = nested(Class, Raised),
13+
Inner = Outer(),
14+
Expected = Inner(),
15+
Recursive = named(Class, Raised),
16+
Expected = Recursive(2)
17+
end, [
18+
{error, throw, other}, {throw, throw, matched},
19+
{error, error, matched}, {throw, error, other},
20+
{exit, exit, matched}, {error, exit, other},
21+
{not_a_class, throw, other}
22+
]),
23+
Comprehension = comprehension(id(error)),
24+
[other, matched, other] = Comprehension(),
25+
{error, matched, other} = shadowed_head(),
26+
Fresh = fun() -> try throw(x) catch Class:_ -> Class end end,
27+
throw = Fresh(),
28+
io:format("fun_catch_class_ok~n"),
29+
ok.
30+
31+
requested() ->
32+
C = id(error),
33+
F = fun() -> try throw(x) catch C:_ -> wrong; _:_ -> right end end,
34+
F().
35+
36+
capture(C, Raised) ->
37+
fun() -> try raise(Raised) catch C:_ -> matched; _:_ -> other end end.
38+
39+
nested(C, Raised) ->
40+
fun() ->
41+
fun() -> try raise(Raised) catch C:_ -> matched; _:_ -> other end end
42+
end.
43+
44+
named(C, Raised) ->
45+
fun Loop(0) -> try raise(Raised) catch C:_ -> matched; _:_ -> other end;
46+
Loop(N) -> Loop(N - 1)
47+
end.
48+
49+
comprehension(C) ->
50+
fun() ->
51+
[try raise(Raised) catch C:_ -> matched; _:_ -> other end
52+
|| Raised <- id([throw, error, exit])]
53+
end.
54+
55+
shadowed_head() ->
56+
C = id(error),
57+
F = fun(C) -> try throw(x) catch C:_ -> matched; _:_ -> other end end,
58+
{C, F(throw), F(error)}.
59+
60+
raise(throw) -> throw(x);
61+
raise(error) -> erlang:error(x);
62+
raise(exit) -> exit(x).
63+
64+
id(X) -> X.

0 commit comments

Comments
 (0)