Skip to content
Open
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
14 changes: 14 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,20 @@ 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.

## Branch exports count later reads as uses

- Names bound in `case`, `if` and `receive` branches no longer get false
unused-variable warnings when code reads them after the construct, including
nested branches, clause patterns, receive timeouts and later fun captures.
- The unused-variable pass checks direct matches consistently in branch bodies
and keeps warnings for genuinely unread or fun-local bindings. A later fun
that shadows a name does not count as reading the exported value.
- Later reads inside comprehensions and `maybe`, including conditional-pattern
comparisons, and key/size reads in match patterns reach the bindings they
consume without leaking across local scopes.
- Warning regressions cover the existing `nested_case_exports` fixture and
`branch_export_warnings`, shared by native and BEAM execution.

## Regression coverage for case exports inside maybe bodies

- A diagnostic regression protects names bound in every `case` branch and read
Expand Down
6 changes: 3 additions & 3 deletions crates/nera-ir/src/warnings/ast_walkers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -457,9 +457,9 @@ fn collect_refs(expr: &ast::Expr, refs: &mut HashSet<AtomId>, scoping: RefScopin
match item {
ast::MaybeExpr::Expr(e) => collect_refs(e, refs, scoping),
ast::MaybeExpr::CondMatch(pat, e, _) => {
if scoping == RefScoping::Lexical {
collect_pattern_bindings(pat, refs);
}
// As with an ordinary match, an already-bound value
// name is read for equality, not rebound.
collect_pattern_bindings(pat, refs);
collect_pattern_refs(pat, refs);
collect_refs(e, refs, scoping);
}
Expand Down
87 changes: 63 additions & 24 deletions crates/nera-ir/src/warnings/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1470,37 +1470,76 @@ fn comprehension_filter_bindings_are_available_to_later_qualifiers() {

#[test]
fn comprehension_filter_bindings_reach_scope_sensitive_diagnostics() {
// A later fun head or generator shadows N, so the earlier filter's N
// is genuinely unused as well as shadowed (erlc -Wall).
let cases: &[(&str, &str, &[&str])] = &[
(
"case V of N -> <<1>>; _ -> <<0>> end",
"X <- [1], begin N = X, true end",
&[],
),
(
"begin case V of true -> N = 1; false -> ok end, <<N>> end",
"X <- [1], begin N = X, true end",
&[],
),
(
"begin case V of true -> N = 1; false -> N = 1 end, <<N>> end",
"X <- [1], begin N = X, true end",
&[],
),
(
"begin F = fun(N) -> <<N>> end, F(V) end",
"X <- [1], begin N = X, true end",
&["unused_variable", "shadowed_variable"],
),
(
"<<N>>",
"X <- [1], begin N = X, true end, N <- [V]",
&["unused_variable", "shadowed_variable"],
),
(
"<<_N>>",
"X <- [V], begin _N = X, true end, begin _N = 1, true end",
&["match_underscore_var"],
),
(
"<<_N>>",
"X <- [true], X orelse begin _N = 1, true end, _N <- [V]",
&[],
),
("<<N>>", "_ <- [V], {N, N} <- [{1, 1}]", &[]),
];
for (open, close) in [("[", "]"), ("<<", ">>")] {
for (template, qualifiers, expected) in [
("case V of N -> <<1>>; _ -> <<0>> end",
"X <- [1], begin N = X, true end", None),
("begin case V of true -> N = 1; false -> ok end, <<N>> end",
"X <- [1], begin N = X, true end", None),
("begin case V of true -> N = 1; false -> N = 1 end, <<N>> end",
"X <- [1], begin N = X, true end", None),
("begin F = fun(N) -> <<N>> end, F(V) end",
"X <- [1], begin N = X, true end", Some("shadowed_variable")),
("<<N>>", "X <- [1], begin N = X, true end, N <- [V]",
Some("shadowed_variable")),
("<<_N>>", "X <- [V], begin _N = X, true end, begin _N = 1, true end",
Some("match_underscore_var")),
("<<_N>>", "X <- [true], X orelse begin _N = 1, true end, _N <- [V]", None),
("<<N>>", "_ <- [V], {N, N} <- [{1, 1}]", None),
] {
let source = format!("-module(test). -export([f/1]). f(V) ->
{open} ({template}) || {qualifiers} {close}.");
for (template, qualifiers, expected) in cases {
let source = format!(
"-module(test). -export([f/1]). f(V) ->
{open} ({template}) || {qualifiers} {close}."
);
let (module, names) = parse_module(&source);
let diagnostics = analyze_module(&module, &names, None);
assert!(
!diagnostics.iter().any(|d| d.severity == nera_syntax::error::Severity::Error),
!diagnostics
.iter()
.any(|d| d.severity == nera_syntax::error::Severity::Error),
"{source}\n{diagnostics:?}"
);
let relevant: Vec<_> = diagnostics.iter().filter_map(|d| d.code.as_deref())
.filter(|code| ["unsafe_variable", "exported_var", "unreachable_clause",
"unused_variable", "shadowed_variable", "match_underscore_var"].contains(code))
let relevant: Vec<_> = diagnostics
.iter()
.filter_map(|d| d.code.as_deref())
.filter(|code| {
[
"unsafe_variable",
"exported_var",
"unreachable_clause",
"unused_variable",
"shadowed_variable",
"match_underscore_var",
]
.contains(code)
})
.collect();
assert_eq!(relevant, expected.into_iter().collect::<Vec<_>>(),
"{source}\n{diagnostics:?}");
assert_eq!(relevant, *expected, "{source}\n{diagnostics:?}");
}
}
}
Expand Down
Loading