Skip to content

Commit 520c4da

Browse files
committed
fix(ir): transport only definite filter exports through scopes
1 parent e6140d4 commit 520c4da

12 files changed

Lines changed: 114 additions & 9 deletions

‎CHANGELOG.md‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,12 @@ All notable milestones for the nera project are documented in this file.
5151
- Generated helpers pass the bound values forward without repeating filters;
5252
later generator shadowing still preserves the values needed to resume outer
5353
iterations.
54+
- Only definitely bound filter values cross helper boundaries; skipped
55+
short-circuit operands do not supply values. Diagnostics recognize earlier
56+
filter bindings in conditional rematches and shadowing.
5457
- `comprehension_filter_bindings` runs unchanged natively and on BEAM, including
55-
mixed generators, closures, deferred bitstring tails and evaluation traces.
58+
mixed generators, closures, deferred bitstring tails, short-circuit filters,
59+
conditional rematches and evaluation traces.
5660

5761
## Unsupported float segment widths compile as on OTP 29
5862

‎crates/nera-ir/src/lower/list_comp.rs‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -550,11 +550,17 @@ impl IrBuilder {
550550
self_args,
551551
));
552552
} else {
553-
// Record only bindings the lowered filters actually export.
554-
// Materialize deferred tails before passing them to a helper.
555-
new_filter_names = self.collect_all_bindings().keys()
556-
.filter(|name| !before_filters.contains_key(name)).copied().collect();
553+
// Lowering may retain names from skipped short-circuit
554+
// operands. Only definite exports can cross helper boundaries.
555+
let mut definite = HashSet::new();
556+
for filter in filters {
557+
crate::warnings::collect_exported_bindings(filter, &mut definite);
558+
}
559+
new_filter_names = definite.into_iter()
560+
.filter(|name| !before_filters.contains_key(name)
561+
&& self.lookup_var(*name).is_some()).collect();
557562
new_filter_names.sort_by_key(|name| name.0);
563+
// Materialize deferred tails before passing them to a helper.
558564
let new_filter_values: Vec<VarId> = new_filter_names.iter().map(|name| {
559565
let var = self.lookup_var(*name).expect("filter binding exists");
560566
self.materialize_bound_variable(*name, var)

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,7 @@ fn check_exported_in_subexprs(
305305
}
306306
ast::LcQualifier::Filter(filter_expr, _) => {
307307
check_exported_in_subexprs(filter_expr, &lc_scope, atom_names, diagnostics);
308+
collect_exported_bindings(filter_expr, &mut lc_scope);
308309
}
309310
}
310311
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use nera_syntax::error::Diagnostic;
1111
use nera_syntax::span::Span;
1212

1313
use super::ast_walkers::{collect_pattern_bindings, collect_pattern_bindings_with_spans};
14+
use super::unbound_vars::collect_exported_bindings;
1415

1516
/// Check a function for `_`-prefixed variables reused in pattern position.
1617
pub(super) fn check_match_underscore_vars_fn(
@@ -231,6 +232,7 @@ fn check_expr(
231232
}
232233
ast::LcQualifier::Filter(filter_expr, _) => {
233234
check_expr(filter_expr, &lc_scope, atom_names, warnings);
235+
collect_exported_bindings(filter_expr, &mut lc_scope);
234236
}
235237
}
236238
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ use nera_syntax::error::Diagnostic;
5656
pub(crate) use ast_walkers::{collect_pattern_bindings, collect_pattern_refs, collect_expr_refs, collect_guard_refs};
5757
pub(crate) use binary_segments::first_refused_segment_error;
5858
pub(crate) use illegal_guards::is_guard_expr;
59+
pub(crate) use unbound_vars::collect_exported_bindings;
5960

6061
// Re-export for cross-module unused record detection.
6162
pub use unused_records::collect_used_record_names;

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use nera_syntax::ast::{self, AtomId};
1212
use nera_syntax::error::Diagnostic;
1313

1414
use super::ast_walkers::{collect_pattern_bindings, collect_pattern_bindings_with_spans};
15+
use super::unbound_vars::collect_exported_bindings;
1516

1617
/// Check a function for shadowed variables in nested scopes.
1718
pub(super) fn check_shadowed_variables_fn(
@@ -107,11 +108,12 @@ fn check_shadowed_in_expr(
107108
| ast::LcQualifier::BinGenerator(pat, source_expr, _) => {
108109
// The source expression is evaluated in the current scope.
109110
check_shadowed_in_expr(source_expr, &lc_scope, atom_names, warnings);
110-
// Generator pattern vars can shadow outer scope.
111+
// Generator patterns can shadow outer bindings,
112+
// including earlier generators and filters.
111113
let mut bound = Vec::new();
112114
collect_pattern_bindings_with_spans(pat, &mut bound);
113115
for (atom_id, span) in &bound {
114-
if outer_scope.contains(atom_id) {
116+
if lc_scope.contains(atom_id) {
115117
let name = atom_names
116118
.get(atom_id)
117119
.map(|s| s.as_str())
@@ -126,11 +128,14 @@ fn check_shadowed_in_expr(
126128
).with_code("shadowed_variable"));
127129
}
128130
}
129-
lc_scope.insert(*atom_id);
130131
}
132+
// Repeated names within this pattern are comparisons,
133+
// not shadowing of a name this same pattern introduces.
134+
lc_scope.extend(bound.iter().map(|(name, _)| *name));
131135
}
132136
ast::LcQualifier::Filter(filter_expr, _) => {
133137
check_shadowed_in_expr(filter_expr, &lc_scope, atom_names, warnings);
138+
collect_exported_bindings(filter_expr, &mut lc_scope);
134139
}
135140
}
136141
}

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

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1468,6 +1468,43 @@ fn comprehension_filter_bindings_are_available_to_later_qualifiers() {
14681468
);
14691469
}
14701470

1471+
#[test]
1472+
fn comprehension_filter_bindings_reach_scope_sensitive_diagnostics() {
1473+
for (open, close) in [("[", "]"), ("<<", ">>")] {
1474+
for (template, qualifiers, expected) in [
1475+
("case V of N -> <<1>>; _ -> <<0>> end",
1476+
"X <- [1], begin N = X, true end", None),
1477+
("begin case V of true -> N = 1; false -> ok end, <<N>> end",
1478+
"X <- [1], begin N = X, true end", None),
1479+
("begin case V of true -> N = 1; false -> N = 1 end, <<N>> end",
1480+
"X <- [1], begin N = X, true end", None),
1481+
("begin F = fun(N) -> <<N>> end, F(V) end",
1482+
"X <- [1], begin N = X, true end", Some("shadowed_variable")),
1483+
("<<N>>", "X <- [1], begin N = X, true end, N <- [V]",
1484+
Some("shadowed_variable")),
1485+
("<<_N>>", "X <- [V], begin _N = X, true end, begin _N = 1, true end",
1486+
Some("match_underscore_var")),
1487+
("<<_N>>", "X <- [true], X orelse begin _N = 1, true end, _N <- [V]", None),
1488+
("<<N>>", "_ <- [V], {N, N} <- [{1, 1}]", None),
1489+
] {
1490+
let source = format!("-module(test). -export([f/1]). f(V) ->
1491+
{open} ({template}) || {qualifiers} {close}.");
1492+
let (module, names) = parse_module(&source);
1493+
let diagnostics = analyze_module(&module, &names, None);
1494+
assert!(
1495+
!diagnostics.iter().any(|d| d.severity == nera_syntax::error::Severity::Error),
1496+
"{source}\n{diagnostics:?}"
1497+
);
1498+
let relevant: Vec<_> = diagnostics.iter().filter_map(|d| d.code.as_deref())
1499+
.filter(|code| ["unsafe_variable", "exported_var", "unreachable_clause",
1500+
"unused_variable", "shadowed_variable", "match_underscore_var"].contains(code))
1501+
.collect();
1502+
assert_eq!(relevant, expected.into_iter().collect::<Vec<_>>(),
1503+
"{source}\n{diagnostics:?}");
1504+
}
1505+
}
1506+
}
1507+
14711508
#[test]
14721509
fn comprehension_filters_do_not_export_local_or_partial_bindings() {
14731510
for expr in [

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ enum Paths {
105105
/// bindings; funs, comprehensions, try, maybe, catch operands and skipped
106106
/// short-circuit operands do not supply definite bindings. The variable
107107
/// diagnostics and unreachable check share this view of later pattern inputs.
108-
pub(super) fn collect_exported_bindings(expr: &ast::Expr, scope: &mut HashSet<AtomId>) {
108+
pub(crate) fn collect_exported_bindings(expr: &ast::Expr, scope: &mut HashSet<AtomId>) {
109109
collect_bindings(expr, scope, Paths::Every);
110110
}
111111

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,7 @@ fn check_unreachable_in_expr(
273273
}
274274
ast::LcQualifier::Filter(e, _) => {
275275
check_unreachable_in_expr(e, &lc_scope, atom_names, warnings);
276+
collect_exported_bindings(e, &mut lc_scope);
276277
}
277278
}
278279
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,7 @@ fn check_unsafe_in_subexprs(
324324
}
325325
ast::LcQualifier::Filter(filter_expr, _) => {
326326
check_unsafe_in_subexprs(filter_expr, &lc_scope, atom_names, diagnostics);
327+
collect_exported_bindings(filter_expr, &mut lc_scope);
327328
}
328329
}
329330
}

0 commit comments

Comments
 (0)