Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@ All notable milestones for the nera project are documented in this file.
partial-byte, large and extracted bitstrings, non-bitstrings and higher-order
fun consumers with explicit completion.

## Bound catch classes are captured by funs

- A fun that uses an already-bound name only as a catch class now captures
its value. A mismatching exception falls through instead of treating the
name as a fresh class variable.
- The same read counts as a use of the outer binding, removing false
unused-variable warnings.
- `fun_catch_class` checks matching and mismatching classes, nested and named
funs, comprehension helpers and fun-head shadowing natively and on BEAM.

## Regression coverage for case exports inside maybe bodies

- A diagnostic regression protects names bound in every `case` branch and read
Expand Down
8 changes: 5 additions & 3 deletions crates/nera-ir/src/warnings/ast_walkers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -385,10 +385,12 @@ fn collect_refs(expr: &ast::Expr, refs: &mut HashSet<AtomId>, scoping: RefScopin
}
}
for clause in catch_clauses {
// Like reason patterns, a class name can compare an outer
// binding. Include it for closure capture and unused checks.
if let ast::CatchClass::Variable(name, _) = clause.class {
refs.insert(name);
}
if scoping == RefScoping::Lexical {
if let ast::CatchClass::Variable(name, _) = clause.class {
refs.insert(name);
}
if let Some(stacktrace) = &clause.stacktrace {
collect_pattern_bindings(stacktrace, refs);
collect_pattern_refs(stacktrace, refs);
Expand Down
23 changes: 22 additions & 1 deletion crates/nera-ir/src/warnings/unused_vars_tests.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,30 @@
//! Regression for false unused-variable warnings on map keys and binary sizes.
//! Regressions for false unused-variable warnings on pattern reads.

use nera_syntax::error::Severity;
use nera_syntax::parser::Parser;
use nera_syntax::span::Span;

#[test]
fn bound_catch_class_is_used_inside_fun() {
// Intent: an outer name read only as a catch class inside a fun is used.
// This fixture owns the requested runtime behavior natively and on BEAM;
// this test owns its unused-variable diagnostics.
let source = include_str!("../../../../tests/fun_catch_class.erl");
let mut parser = Parser::new(source);
let parsed = parser.parse_module();
assert!(parsed.errors.is_empty(), "{:?}", parsed.errors);
let diagnostics = crate::warnings::analyze_module(&parsed.module, &parser.atom_names(), None);
assert!(
diagnostics.iter().all(|d| d.severity != Severity::Error),
"unexpected errors: {diagnostics:#?}"
);
let unused: Vec<_> = diagnostics
.iter()
.filter(|d| d.code.as_deref() == Some("unused_variable"))
.collect();
assert!(unused.is_empty(), "unexpected unused variables: {unused:#?}");
}

#[test]
fn pattern_expression_reads_preserve_unused_value_warnings() {
// Intent: keys and sizes count as uses without treating value bindings as
Expand Down
1 change: 1 addition & 0 deletions tests/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,7 @@ These are automatically skipped by the runner and show up as SKIP in output.
|---------------------------|----------------------------------------|------------|
| `fun_simple.erl` | Basic anonymous fun usage | ~6 |
| `fun_closure.erl` | Closure capture and invocation | ~8 |
| `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 |
| `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 |
| `fun_errors.erl` | Fun error handling | ~6 |
| `fun_foldl.erl` | Higher-order foldl | ~4 |
Expand Down
64 changes: 64 additions & 0 deletions tests/fun_catch_class.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
%% EXPECT: fun_catch_class_ok
%% Bound catch classes are reads, even when no guard or body reads the name.
%% Run the same assertions natively and on BEAM.
-module(fun_catch_class).
-export([test/0]).

test() ->
right = requested(),
lists:foreach(fun({Class, Raised, Expected}) ->
F = capture(Class, Raised),
Expected = F(),
Outer = nested(Class, Raised),
Inner = Outer(),
Expected = Inner(),
Recursive = named(Class, Raised),
Expected = Recursive(2)
end, [
{error, throw, other}, {throw, throw, matched},
{error, error, matched}, {throw, error, other},
{exit, exit, matched}, {error, exit, other},
{not_a_class, throw, other}
]),
Comprehension = comprehension(id(error)),
[other, matched, other] = Comprehension(),
{error, matched, other} = shadowed_head(),
Fresh = fun() -> try throw(x) catch Class:_ -> Class end end,
throw = Fresh(),
io:format("fun_catch_class_ok~n"),
ok.

requested() ->
C = id(error),
F = fun() -> try throw(x) catch C:_ -> wrong; _:_ -> right end end,
F().

capture(C, Raised) ->
fun() -> try raise(Raised) catch C:_ -> matched; _:_ -> other end end.

nested(C, Raised) ->
fun() ->
fun() -> try raise(Raised) catch C:_ -> matched; _:_ -> other end end
end.

named(C, Raised) ->
fun Loop(0) -> try raise(Raised) catch C:_ -> matched; _:_ -> other end;
Loop(N) -> Loop(N - 1)
end.

comprehension(C) ->
fun() ->
[try raise(Raised) catch C:_ -> matched; _:_ -> other end
|| Raised <- id([throw, error, exit])]
end.

shadowed_head() ->
C = id(error),
F = fun(C) -> try throw(x) catch C:_ -> matched; _:_ -> other end end,
{C, F(throw), F(error)}.

raise(throw) -> throw(x);
raise(error) -> erlang:error(x);
raise(exit) -> exit(x).

id(X) -> X.
Loading