From 565b06bba013932c5d3db8626749ee5131f548b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micka=C3=ABl=20R=C3=A9mond?= Date: Sun, 15 Nov 2026 19:56:21 +0100 Subject: [PATCH 1/3] fix(ir): retain comprehension filter bindings across generators --- CHANGELOG.md | 10 ++ crates/nera-ir/src/lower/list_comp.rs | 60 +++++++--- crates/nera-ir/src/lower/tests.rs | 6 + crates/nera-ir/src/warnings/tests.rs | 26 +++++ crates/nera-ir/src/warnings/unbound_vars.rs | 8 +- tests/README.md | 1 + tests/comprehension_filter_bindings.erl | 121 ++++++++++++++++++++ 7 files changed, 213 insertions(+), 19 deletions(-) create mode 100644 tests/comprehension_filter_bindings.erl diff --git a/CHANGELOG.md b/CHANGELOG.md index e3dcc03..e9c3180 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -44,6 +44,16 @@ All notable milestones for the nera project are documented in this file. mixed maps, arithmetic, nested generators, compound guards, record defaults, evaluation order, captures and ordinary exceptions. +## Comprehension filters retain bindings across generators + +- Names bound in a list or binary comprehension filter remain available to + later generator sources, map keys, binary sizes, filters and templates. +- Generated helpers pass the bound values forward without repeating filters; + later generator shadowing still preserves the values needed to resume outer + iterations. +- `comprehension_filter_bindings` runs unchanged natively and on BEAM, including + mixed generators, closures, deferred bitstring tails and evaluation traces. + ## Unsupported float segment widths compile as on OTP 29 - Literal float segment widths outside the supported encodings no longer reject diff --git a/crates/nera-ir/src/lower/list_comp.rs b/crates/nera-ir/src/lower/list_comp.rs index 3d0e103..0982ad5 100644 --- a/crates/nera-ir/src/lower/list_comp.rs +++ b/crates/nera-ir/src/lower/list_comp.rs @@ -64,27 +64,25 @@ impl IrBuilder { // ── 3. Analyse free variables ── let captured_vars = self.collect_lc_captures(&groups, body_expr); - let num_captured = captured_vars.len() as u32; // ── 4. Pre-generate helper function names for all generators ── - let mut helpers: Vec<(AtomId, String, u32)> = Vec::new(); // (name_id, mangled, arity) + let mut helpers: Vec<(AtomId, String)> = Vec::new(); // (name_id, mangled) let prefix = if finalize_as_binary { "bc" } else { "lc" }; - for k in 0..n { + for _ in 0..n { let id = self.lc_counter; self.lc_counter += 1; let name_str = format!("__{prefix}_{id}"); let mangled = format!("{}__{prefix}_{id}", self.module_name.0); let name_id = self.fresh_atom_id(); self.atom_names.insert(name_id, name_str); - // params: iter + acc + k tails + k heads + captured - let arity = 2 + 2 * k as u32 + num_captured; - self.module_functions - .insert((name_id, arity), mangled.clone()); - helpers.push((name_id, mangled, arity)); + helpers.push((name_id, mangled)); } // ── 5. Build all helper functions ── let saved = self.save_state(); + // Each completed helper records the names its filters introduced. + // Later helpers carry their values, not expressions to evaluate again. + let mut filter_bindings = Vec::new(); for k in 0..n { if k > 0 { @@ -97,7 +95,7 @@ impl IrBuilder { self.next_block = 0; self.scopes = vec![HashMap::new()]; } - self.build_lc_helper(k, n, &groups, &helpers, &captured_vars, body_expr, finalize_as_binary, &bin_flags)?; + self.build_lc_helper(k, n, &groups, &helpers, &captured_vars, body_expr, finalize_as_binary, &bin_flags, &mut filter_bindings)?; } self.restore_state(saved); @@ -234,7 +232,8 @@ impl IrBuilder { } /// Capture expression reads before the generator that shadows their name. - /// Earlier generator values travel through head parameters, not captures. + /// Earlier generator and filter values travel through helper parameters, + /// not captures. fn collect_lc_captures( &mut self, groups: &[GeneratorGroup<'_>], @@ -313,7 +312,7 @@ impl IrBuilder { /// Build one LC helper function (helper_k of n total). /// /// Parameters for helper_k: - /// iter_k, acc, tail_0…tail_{k-1}, head_0…head_{k-1}, captured… + /// iter_k, acc, tail_0…tail_{k-1}, head_0…head_{k-1}, captured…, filter_values… /// /// The accumulator collects elements in reverse order. When the outermost /// helper (k==0) reaches the nil case, it reverses the accumulator via @@ -324,13 +323,14 @@ impl IrBuilder { k: usize, n: usize, groups: &[GeneratorGroup<'_>], - helpers: &[(AtomId, String, u32)], // (name_id, mangled, arity) + helpers: &[(AtomId, String)], // (name_id, mangled) captured_vars: &[(AtomId, VarId)], body_expr: &ast::Expr, finalize_as_binary: bool, bin_gen_flags: &[bool], + filter_bindings: &mut Vec>, ) -> Result<(), LowerError> { - let (helper_name_id, ref helper_mangled, helper_arity) = helpers[k]; + let (helper_name_id, ref helper_mangled) = helpers[k]; // Each helper is its own function. Deferred tails recorded while // building the previous helper name that helper's variables. self.match_contexts.clear(); @@ -355,6 +355,18 @@ impl IrBuilder { self.bind_var(*name, param); } + // Preserve one slot per binding layer, even if a later generator + // shadows the same name. Resuming an outer helper needs its old value. + let filter_params: Vec> = filter_bindings.iter() + .map(|names| names.iter().map(|name| (*name, self.fresh_var())).collect()) + .collect(); + let carried_filters: Vec = filter_params.iter().flatten() + .map(|(_, var)| *var).collect(); + let helper_arity = 2 + 2 * k as u32 + env_params.len() as u32 + + carried_filters.len() as u32; + self.module_functions.insert((helper_name_id, helper_arity), helper_mangled.clone()); + let mut new_filter_names = Vec::new(); + // ── Nil block ── let nil_block = self.fresh_block_with_id(); self.start_block(nil_block); @@ -390,6 +402,7 @@ impl IrBuilder { chain_args.push(*item); } chain_args.extend_from_slice(&env_params); + chain_args.extend(filter_params[..k - 1].iter().flatten().map(|(_, var)| *var)); self.set_terminator(Terminator::TailCall( CallTarget::Local(helpers[k - 1].0), chain_args, @@ -414,6 +427,9 @@ impl IrBuilder { // segment size), so they are extracted before it runs. for j in 0..k { self.bind_generator_pattern(&groups[j].0, head_params[j], false); + for (name, var) in &filter_params[j] { + self.bind_var(*name, *var); + } } let generator_inputs = self.collect_all_bindings(); @@ -442,11 +458,12 @@ impl IrBuilder { (generator_scopes, generator_match_contexts) }; + let before_filters = self.collect_all_bindings(); let filters = &groups[k].2; let is_innermost = k == n - 1; // Helper: build args for a self tail-call. - // self(tail_var, acc_var, tail_0…tail_{k-1}, head_0…head_{k-1}, captured…) + // self(tail_var, acc_var, tail_0…tail_{k-1}, head_0…head_{k-1}, captured…, filter_values…) let build_self_args = |tail_params: &[VarId], head_params: &[VarId], env_params: &[VarId], @@ -457,6 +474,7 @@ impl IrBuilder { a.extend_from_slice(tail_params); a.extend_from_slice(head_params); a.extend_from_slice(env_params); + a.extend_from_slice(&carried_filters); a }; @@ -487,6 +505,7 @@ impl IrBuilder { inner_args.extend_from_slice(&head_params); inner_args.push(head_var); inner_args.extend_from_slice(&env_params); + inner_args.extend_from_slice(&carried_filters); self.set_terminator(Terminator::TailCall( CallTarget::Local(helpers[k + 1].0), inner_args, @@ -531,6 +550,15 @@ impl IrBuilder { self_args, )); } else { + // Record only bindings the lowered filters actually export. + // Materialize deferred tails before passing them to a helper. + new_filter_names = self.collect_all_bindings().keys() + .filter(|name| !before_filters.contains_key(name)).copied().collect(); + new_filter_names.sort_by_key(|name| name.0); + let new_filter_values: Vec = new_filter_names.iter().map(|name| { + let var = self.lookup_var(*name).expect("filter binding exists"); + self.materialize_bound_variable(*name, var) + }).collect(); let next_iter = self.lower_expr(groups[k + 1].1, false)?; let mut inner_args = vec![next_iter, acc_param]; @@ -539,6 +567,8 @@ impl IrBuilder { inner_args.extend_from_slice(&head_params); inner_args.push(head_var); inner_args.extend_from_slice(&env_params); + inner_args.extend_from_slice(&carried_filters); + inner_args.extend_from_slice(&new_filter_values); self.set_terminator(Terminator::TailCall( CallTarget::Local(helpers[k + 1].0), inner_args, @@ -627,6 +657,7 @@ impl IrBuilder { all_params.extend_from_slice(&tail_params); all_params.extend_from_slice(&head_params); all_params.extend_from_slice(&env_params); + all_params.extend_from_slice(&carried_filters); let mut helper_func = IrFunction { name: helper_name_id, arity: helper_arity, @@ -643,6 +674,7 @@ impl IrBuilder { #[cfg(test)] crate::passes::assert_valid(&helper_func, "after helper yield insertion"); self.functions.push(helper_func); + filter_bindings.push(new_filter_names); Ok(()) } diff --git a/crates/nera-ir/src/lower/tests.rs b/crates/nera-ir/src/lower/tests.rs index 88b3844..62b3f02 100644 --- a/crates/nera-ir/src/lower/tests.rs +++ b/crates/nera-ir/src/lower/tests.rs @@ -1925,6 +1925,12 @@ fn test_binary_bound_segment_lowers() { .expect("bound binary segment values must lower to valid IR"); } +#[test] +fn test_comprehension_filter_bindings_lowers() { + lower_source(include_str!("../../../../tests/comprehension_filter_bindings.erl")) + .expect("filter bindings must reach later helpers as initialized values"); +} + #[test] fn test_generator_size_rebind_lowers() { // NERA-52: the final binary-consumption branch must preserve initialized diff --git a/crates/nera-ir/src/warnings/tests.rs b/crates/nera-ir/src/warnings/tests.rs index 7d442db..9ae2ba9 100644 --- a/crates/nera-ir/src/warnings/tests.rs +++ b/crates/nera-ir/src/warnings/tests.rs @@ -1456,6 +1456,32 @@ fn pattern_inputs_keep_exported_bindings() { } } +#[test] +fn comprehension_filter_bindings_are_available_to_later_qualifiers() { + let (module, names) = parse_module(include_str!( + "../../../../tests/comprehension_filter_bindings.erl" + )); + let diagnostics = analyze_module(&module, &names, None); + assert!( + !diagnostics.iter().any(|d| d.severity == nera_syntax::error::Severity::Error), + "filter bindings must be valid later inputs: {diagnostics:?}" + ); +} + +#[test] +fn comprehension_filters_do_not_export_local_or_partial_bindings() { + for expr in [ + "[N || X <- [1], begin N = X, true end, _ <- [ok]], N", + "[N || X <- [1], case X of 1 -> N = 1, true; _ -> true end, _ <- [ok]]", + "[N || X <- [1], X > 0 andalso begin N = 1, true end, _ <- [ok]]", + "[N || _ <- begin N = 1, [ok] end]", + ] { + assert_unbound_pattern_input( + &format!("-module(test). -export([f/0]). f() -> {expr}."), "N", + ); + } +} + #[test] fn unbound_in_guard() { let src = r#" diff --git a/crates/nera-ir/src/warnings/unbound_vars.rs b/crates/nera-ir/src/warnings/unbound_vars.rs index 7f4fae4..1f3be56 100644 --- a/crates/nera-ir/src/warnings/unbound_vars.rs +++ b/crates/nera-ir/src/warnings/unbound_vars.rs @@ -464,7 +464,6 @@ fn check_unbound_in_expr( ast::Expr::ListComp(body_expr, qualifiers, _) | ast::Expr::BinComp(body_expr, qualifiers, _) => { let mut lc_scope = scope.clone(); - let mut before_generator = true; for q in qualifiers { match q { ast::LcQualifier::Generator(pat, source_expr, _) @@ -476,13 +475,12 @@ fn check_unbound_in_expr( // not from the bindings this generator introduces. check_unbound_in_pattern_exprs(pat, &lc_scope, Context::Expression, atom_names, diagnostics); collect_pattern_bindings(pat, &mut lc_scope); - before_generator = false; } ast::LcQualifier::Filter(filter_expr, _) => { check_unbound_in_expr(filter_expr, &lc_scope, context, atom_names, diagnostics); - if before_generator { - collect_exported_bindings(filter_expr, &mut lc_scope); - } + // Every completed filter supplies definite bindings + // to the remaining qualifiers and template. + collect_exported_bindings(filter_expr, &mut lc_scope); } } } diff --git a/tests/README.md b/tests/README.md index 8820693..4121aa2 100644 --- a/tests/README.md +++ b/tests/README.md @@ -353,6 +353,7 @@ These are automatically skipped by the runner and show up as SKIP in output. | `list_comprehension.erl` | Basic LC, filters, pattern gen | ~8 | | `comprehension_leading_filter.erl` | Leading and filter-only list/binary comprehensions: boolean results, short-circuit order, list/binary sources, captures, bitstrings and exceptions; same fixture on native and BEAM | Table-driven | | `comprehension_guard_filter.erl` | Guard-expression list/binary filters skip raising elements: map_size, arithmetic, compound guards, leading/filter-only forms, record defaults, nested list/binary generators, call resolution, captures and ordinary error controls; same fixture on native and BEAM | Table-driven | +| `comprehension_filter_bindings.erl` | Filter-bound names read by later sources, map keys, binary sizes, filters, closures and templates across list/binary generators; shadowing, deferred tails, outer resumption and evaluation traces; same fixture on native and BEAM | Completion checked | | `lc_nested.erl` | Nested list comprehension generators | ~8 | | `string_as_list.erl` | String operations, comparison | ~12 | | `string_codepoints.erl` | Non-ASCII expressions, full octal escape range, adjacency, list operations, patterns and Unicode format padding; also run on BEAM | 28 | diff --git a/tests/comprehension_filter_bindings.erl b/tests/comprehension_filter_bindings.erl new file mode 100644 index 0000000..2cb4b8a --- /dev/null +++ b/tests/comprehension_filter_bindings.erl @@ -0,0 +1,121 @@ +%% EXPECT: comprehension_filter_bindings_ok +%% Run the same assertions natively and on BEAM. Filter bindings must cross +%% helper boundaries without repeating the filter or leaking between iterations. +-module(comprehension_filter_bindings). +-export([test/0]). + +test() -> + check(requested, [258], + [V || X <- [16], begin N = X, true end, <> <= <<1, 2>>]), + check(list_source, [11, 12, 21, 22], + [V || X <- id([10, 20]), begin Values = [X + 1, X + 2], true end, + V <- Values]), + check(binary_source, [1, 2, 3, 4], + [V || X <- id([1, 3]), begin Bits = <>, true end, + <> <= Bits]), + check(list_pattern_size, [1, 258], + [V || X <- id([8, 16]), begin N = X, true end, + <> <- id([<<1>>, <<1, 2>>, <<1, 2, 3>>])]), + check(binary_pattern_size, [1, 2, 258], + [V || X <- id([8, 16]), begin N = X, true end, + <> <= id(<<1, 2>>)]), + check(expression_size, [1, 2, 258], + [V || X <- id([8, 16]), begin N = X div 8, true end, + <> <= id(<<1, 2>>)]), + check(map_key, [1, 2], + [V || X <- id([a, b]), begin K = X, true end, + #{K := V} <- id([#{a => 1}, #{b => 2}])]), + check(nested_pattern, [1, 2], + [V || X <- id([a, b]), begin K = X, true end, + {tag, [#{K := V}]} <- id([{tag, [#{a => 1}]}, {tag, [#{b => 2}]}])]), + check(template, [{10, 1}, {10, 2}, {20, 1}, {20, 2}], + [{N, Y} || X <- id([10, 20]), begin N = X, true end, Y <- id([1, 2])]), + check(three_generators, [{8, 1, a}, {8, 1, b}, {8, 2, a}, {8, 2, b}, + {16, 258, a}, {16, 258, b}], + [{N, V, Y} || X <- id([8, 16]), begin N = X, true end, + <> <= id(<<1, 2>>), Y <- id([a, b])]), + check(two_filters, [{10, 11}, {20, 21}], + [{N, M} || X <- id([10, 20]), begin N = X, true end, + Y <- id([1]), begin M = N + Y, true end, _ <- id([ok])]), + check(later_filter, [11, 21], + [M || X <- id([10, 20]), begin N = X, true end, + Y <- id([1, 2]), begin M = N + Y, Y =:= 1 end, _ <- id([ok])]), + check(shadow_size, [{258, 7}, {258, 9}], + [{V, N} || _ <- id([ok]), begin N = id(16), true end, + <> <- id([<<1, 2, 7>>, <<1, 2, 9>>]), _ <- id([ok])]), + check(resume_shadowed, [{8, 1, 7}, {8, 2, 7}, {16, 258, 7}], + [{X, V, N} || X <- id([8, 16]), begin N = X, true end, + <> <= id(<<1, 2>>), N <- id([7]), _ <- id([ok])]), + check(branch_export, [1, 2, 258], + [V || X <- id([8, 16]), + case X of 8 -> N = id(8), true; 16 -> N = id(16), true end, + <> <= id(<<1, 2>>)]), + check(closure_template, [11, 12, 21, 22], + [(fun() -> N + Y end)() || X <- id([10, 20]), begin N = X, true end, + Y <- id([1, 2])]), + check(closure_binding, [11, 12, 21, 22], + [F(Y) || X <- id([10, 20]), begin F = fun(Y) -> X + Y end, true end, + Y <- id([1, 2])]), + check(binary_template, <<1, 2, 1, 2>>, + << <> || X <- id([8, 16]), begin N = X, true end, + <> <= id(<<1, 2>>) >>), + check(binary_map_key, <<1, 2>>, + << <> || X <- id([a, b]), begin K = X, true end, + #{K := V} <- id([#{a => 1}, #{b => 2}]) >>), + check(binary_three_generators, <<1, 2, 1, 2>>, + << <> || X <- id([8, 16]), begin N = X, true end, + <> <= id(<<1, 2>>), _ <- id([ok]) >>), + check(deferred_tail, [<<5:3>>, <<6:3>>], + [Tail || B <- id([<<0, 5:3>>, <<0, 6:3>>]), + begin <<0, Tail/bits>> = B, true end, _ <- id([ok]), _ <- id([ok])]), + check(tail_as_key, [found], + [V || B <- id([<<0, 5:3>>]), begin <<0, Key/bits>> = B, true end, + #{Key := V} <- id([#{<<5:3>> => found}])]), + Outer = id(2), + check(bound_filter_match, [2], + [Outer || X <- id([2]), begin Outer = X, true end, _ <- id([ok])]), + check(no_leak, [local], + [Local || _ <- id([ok]), begin Local = id(local), true end, _ <- id([ok])]), + Local = id(outside), + check(outside_binding, outside, Local), + traces(), + io:format("comprehension_filter_bindings_ok~n"), + ok. + +traces() -> + check(filter_once, {[11, 12, 21, 22], + [{filter, 10}, {source, 10}, {body, 11}, {body, 12}, + {filter, 20}, {source, 20}, {body, 21}, {body, 22}]}, + traced(fun() -> + [event(body, N + Y) || X <- id([10, 20]), + begin N = event(filter, X), true end, + Y <- event(source, [1, 2], N)] + end)), + check(skip_false, {[2], [{filter, 1}, {filter, 2}, {source, 2}, {body, 2}]}, + traced(fun() -> + [event(body, N) || X <- id([1, 2]), + begin N = event(filter, X), X =:= 2 end, + _ <- event(source, [ok], N)] + end)), + check(empty_inner, {[2], [{filter, 1}, {source, 1}, + {filter, 2}, {source, 2}, {body, 2}]}, + traced(fun() -> + [event(body, N) || X <- id([1, 2]), begin N = event(filter, X), true end, + _ <- event(source, case N of 1 -> []; 2 -> [ok] end, N)] + end)), + ok. + +traced(Fun) -> + put(trace, []), + Result = Fun(), + {Result, lists:reverse(erase(trace))}. + +event(Name, Value) -> event(Name, Value, Value). +event(Name, Value, Logged) -> put(trace, [{Name, Logged} | get(trace)]), Value. + +check(Name, Expected, Actual) -> + io:format("~p: ~p~n", [Name, Actual]), + Expected = Actual, + ok. + +id(X) -> X. From e6140d439811b30a543850817f137492a6ea0d78 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micka=C3=ABl=20R=C3=A9mond?= Date: Sun, 15 Nov 2026 20:06:35 +0100 Subject: [PATCH 2/3] test(ir): cover bindings after binary outer generators --- tests/comprehension_filter_bindings.erl | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tests/comprehension_filter_bindings.erl b/tests/comprehension_filter_bindings.erl index 2cb4b8a..72b7499 100644 --- a/tests/comprehension_filter_bindings.erl +++ b/tests/comprehension_filter_bindings.erl @@ -7,6 +7,12 @@ test() -> check(requested, [258], [V || X <- [16], begin N = X, true end, <> <= <<1, 2>>]), + check(binary_outer_list_body, [1, 2, 258], + [V || <> <= id(<<8, 16>>), begin N = X, true end, + <> <= id(<<1, 2>>)]), + check(binary_outer_binary_body, <<1, 2, 1, 2>>, + << <> || <> <= id(<<8, 16>>), begin N = X, true end, + <> <= id(<<1, 2>>) >>), check(list_source, [11, 12, 21, 22], [V || X <- id([10, 20]), begin Values = [X + 1, X + 2], true end, V <- Values]), From 520c4dafeea4286f6bd108a2d0d78f1bd8c782f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micka=C3=ABl=20R=C3=A9mond?= Date: Sun, 15 Nov 2026 20:33:31 +0100 Subject: [PATCH 3/3] fix(ir): transport only definite filter exports through scopes --- CHANGELOG.md | 6 ++- crates/nera-ir/src/lower/list_comp.rs | 14 ++++-- crates/nera-ir/src/warnings/exported_vars.rs | 1 + .../src/warnings/match_underscore_var.rs | 2 + crates/nera-ir/src/warnings/mod.rs | 1 + crates/nera-ir/src/warnings/shadowed.rs | 11 +++-- crates/nera-ir/src/warnings/tests.rs | 37 +++++++++++++++ crates/nera-ir/src/warnings/unbound_vars.rs | 2 +- crates/nera-ir/src/warnings/unreachable.rs | 1 + crates/nera-ir/src/warnings/unsafe_vars.rs | 1 + crates/nera-ir/src/warnings/unused_vars.rs | 1 + tests/comprehension_filter_bindings.erl | 46 +++++++++++++++++++ 12 files changed, 114 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e9c3180..98967ee 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -51,8 +51,12 @@ All notable milestones for the nera project are documented in this file. - Generated helpers pass the bound values forward without repeating filters; later generator shadowing still preserves the values needed to resume outer iterations. +- Only definitely bound filter values cross helper boundaries; skipped + short-circuit operands do not supply values. Diagnostics recognize earlier + filter bindings in conditional rematches and shadowing. - `comprehension_filter_bindings` runs unchanged natively and on BEAM, including - mixed generators, closures, deferred bitstring tails and evaluation traces. + mixed generators, closures, deferred bitstring tails, short-circuit filters, + conditional rematches and evaluation traces. ## Unsupported float segment widths compile as on OTP 29 diff --git a/crates/nera-ir/src/lower/list_comp.rs b/crates/nera-ir/src/lower/list_comp.rs index 0982ad5..039fdca 100644 --- a/crates/nera-ir/src/lower/list_comp.rs +++ b/crates/nera-ir/src/lower/list_comp.rs @@ -550,11 +550,17 @@ impl IrBuilder { self_args, )); } else { - // Record only bindings the lowered filters actually export. - // Materialize deferred tails before passing them to a helper. - new_filter_names = self.collect_all_bindings().keys() - .filter(|name| !before_filters.contains_key(name)).copied().collect(); + // Lowering may retain names from skipped short-circuit + // operands. Only definite exports can cross helper boundaries. + let mut definite = HashSet::new(); + for filter in filters { + crate::warnings::collect_exported_bindings(filter, &mut definite); + } + new_filter_names = definite.into_iter() + .filter(|name| !before_filters.contains_key(name) + && self.lookup_var(*name).is_some()).collect(); new_filter_names.sort_by_key(|name| name.0); + // Materialize deferred tails before passing them to a helper. let new_filter_values: Vec = new_filter_names.iter().map(|name| { let var = self.lookup_var(*name).expect("filter binding exists"); self.materialize_bound_variable(*name, var) diff --git a/crates/nera-ir/src/warnings/exported_vars.rs b/crates/nera-ir/src/warnings/exported_vars.rs index 8fa7341..26094ba 100644 --- a/crates/nera-ir/src/warnings/exported_vars.rs +++ b/crates/nera-ir/src/warnings/exported_vars.rs @@ -305,6 +305,7 @@ fn check_exported_in_subexprs( } ast::LcQualifier::Filter(filter_expr, _) => { check_exported_in_subexprs(filter_expr, &lc_scope, atom_names, diagnostics); + collect_exported_bindings(filter_expr, &mut lc_scope); } } } diff --git a/crates/nera-ir/src/warnings/match_underscore_var.rs b/crates/nera-ir/src/warnings/match_underscore_var.rs index fefd65a..86a632a 100644 --- a/crates/nera-ir/src/warnings/match_underscore_var.rs +++ b/crates/nera-ir/src/warnings/match_underscore_var.rs @@ -11,6 +11,7 @@ use nera_syntax::error::Diagnostic; use nera_syntax::span::Span; use super::ast_walkers::{collect_pattern_bindings, collect_pattern_bindings_with_spans}; +use super::unbound_vars::collect_exported_bindings; /// Check a function for `_`-prefixed variables reused in pattern position. pub(super) fn check_match_underscore_vars_fn( @@ -231,6 +232,7 @@ fn check_expr( } ast::LcQualifier::Filter(filter_expr, _) => { check_expr(filter_expr, &lc_scope, atom_names, warnings); + collect_exported_bindings(filter_expr, &mut lc_scope); } } } diff --git a/crates/nera-ir/src/warnings/mod.rs b/crates/nera-ir/src/warnings/mod.rs index 6d6f947..3dfda1c 100644 --- a/crates/nera-ir/src/warnings/mod.rs +++ b/crates/nera-ir/src/warnings/mod.rs @@ -56,6 +56,7 @@ use nera_syntax::error::Diagnostic; pub(crate) use ast_walkers::{collect_pattern_bindings, collect_pattern_refs, collect_expr_refs, collect_guard_refs}; pub(crate) use binary_segments::first_refused_segment_error; pub(crate) use illegal_guards::is_guard_expr; +pub(crate) use unbound_vars::collect_exported_bindings; // Re-export for cross-module unused record detection. pub use unused_records::collect_used_record_names; diff --git a/crates/nera-ir/src/warnings/shadowed.rs b/crates/nera-ir/src/warnings/shadowed.rs index e0db49e..c5aa3be 100644 --- a/crates/nera-ir/src/warnings/shadowed.rs +++ b/crates/nera-ir/src/warnings/shadowed.rs @@ -12,6 +12,7 @@ use nera_syntax::ast::{self, AtomId}; use nera_syntax::error::Diagnostic; use super::ast_walkers::{collect_pattern_bindings, collect_pattern_bindings_with_spans}; +use super::unbound_vars::collect_exported_bindings; /// Check a function for shadowed variables in nested scopes. pub(super) fn check_shadowed_variables_fn( @@ -107,11 +108,12 @@ fn check_shadowed_in_expr( | ast::LcQualifier::BinGenerator(pat, source_expr, _) => { // The source expression is evaluated in the current scope. check_shadowed_in_expr(source_expr, &lc_scope, atom_names, warnings); - // Generator pattern vars can shadow outer scope. + // Generator patterns can shadow outer bindings, + // including earlier generators and filters. let mut bound = Vec::new(); collect_pattern_bindings_with_spans(pat, &mut bound); for (atom_id, span) in &bound { - if outer_scope.contains(atom_id) { + if lc_scope.contains(atom_id) { let name = atom_names .get(atom_id) .map(|s| s.as_str()) @@ -126,11 +128,14 @@ fn check_shadowed_in_expr( ).with_code("shadowed_variable")); } } - lc_scope.insert(*atom_id); } + // Repeated names within this pattern are comparisons, + // not shadowing of a name this same pattern introduces. + lc_scope.extend(bound.iter().map(|(name, _)| *name)); } ast::LcQualifier::Filter(filter_expr, _) => { check_shadowed_in_expr(filter_expr, &lc_scope, atom_names, warnings); + collect_exported_bindings(filter_expr, &mut lc_scope); } } } diff --git a/crates/nera-ir/src/warnings/tests.rs b/crates/nera-ir/src/warnings/tests.rs index 9ae2ba9..46711af 100644 --- a/crates/nera-ir/src/warnings/tests.rs +++ b/crates/nera-ir/src/warnings/tests.rs @@ -1468,6 +1468,43 @@ fn comprehension_filter_bindings_are_available_to_later_qualifiers() { ); } +#[test] +fn comprehension_filter_bindings_reach_scope_sensitive_diagnostics() { + 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, <> end", + "X <- [1], begin N = X, true end", None), + ("begin case V of true -> N = 1; false -> N = 1 end, <> end", + "X <- [1], begin N = X, true end", None), + ("begin F = fun(N) -> <> end, F(V) end", + "X <- [1], begin N = X, true end", Some("shadowed_variable")), + ("<>", "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), + ("<>", "_ <- [V], {N, N} <- [{1, 1}]", None), + ] { + 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), + "{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)) + .collect(); + assert_eq!(relevant, expected.into_iter().collect::>(), + "{source}\n{diagnostics:?}"); + } + } +} + #[test] fn comprehension_filters_do_not_export_local_or_partial_bindings() { for expr in [ diff --git a/crates/nera-ir/src/warnings/unbound_vars.rs b/crates/nera-ir/src/warnings/unbound_vars.rs index 1f3be56..cda8fd3 100644 --- a/crates/nera-ir/src/warnings/unbound_vars.rs +++ b/crates/nera-ir/src/warnings/unbound_vars.rs @@ -105,7 +105,7 @@ enum Paths { /// bindings; funs, comprehensions, try, maybe, catch operands and skipped /// short-circuit operands do not supply definite bindings. The variable /// diagnostics and unreachable check share this view of later pattern inputs. -pub(super) fn collect_exported_bindings(expr: &ast::Expr, scope: &mut HashSet) { +pub(crate) fn collect_exported_bindings(expr: &ast::Expr, scope: &mut HashSet) { collect_bindings(expr, scope, Paths::Every); } diff --git a/crates/nera-ir/src/warnings/unreachable.rs b/crates/nera-ir/src/warnings/unreachable.rs index 11efad4..b46b47a 100644 --- a/crates/nera-ir/src/warnings/unreachable.rs +++ b/crates/nera-ir/src/warnings/unreachable.rs @@ -273,6 +273,7 @@ fn check_unreachable_in_expr( } ast::LcQualifier::Filter(e, _) => { check_unreachable_in_expr(e, &lc_scope, atom_names, warnings); + collect_exported_bindings(e, &mut lc_scope); } } } diff --git a/crates/nera-ir/src/warnings/unsafe_vars.rs b/crates/nera-ir/src/warnings/unsafe_vars.rs index 9ceb007..d627d5c 100644 --- a/crates/nera-ir/src/warnings/unsafe_vars.rs +++ b/crates/nera-ir/src/warnings/unsafe_vars.rs @@ -324,6 +324,7 @@ fn check_unsafe_in_subexprs( } ast::LcQualifier::Filter(filter_expr, _) => { check_unsafe_in_subexprs(filter_expr, &lc_scope, atom_names, diagnostics); + collect_exported_bindings(filter_expr, &mut lc_scope); } } } diff --git a/crates/nera-ir/src/warnings/unused_vars.rs b/crates/nera-ir/src/warnings/unused_vars.rs index 62c4ddd..4c9a17b 100644 --- a/crates/nera-ir/src/warnings/unused_vars.rs +++ b/crates/nera-ir/src/warnings/unused_vars.rs @@ -314,6 +314,7 @@ fn check_unused_in_expr( } ast::LcQualifier::Filter(expr, _) => { check_unused_in_expr(expr, atom_names, warnings, &lc_scope); + collect_exported_bindings(expr, &mut lc_scope); } } } diff --git a/tests/comprehension_filter_bindings.erl b/tests/comprehension_filter_bindings.erl index 72b7499..61777ac 100644 --- a/tests/comprehension_filter_bindings.erl +++ b/tests/comprehension_filter_bindings.erl @@ -84,10 +84,56 @@ test() -> [Local || _ <- id([ok]), begin Local = id(local), true end, _ <- id([ok])]), Local = id(outside), check(outside_binding, outside, Local), + skipped_bindings(), + lists:foreach(fun conditional_rematches/1, [true, false]), traces(), io:format("comprehension_filter_bindings_ok~n"), ok. +skipped_bindings() -> + check(skipped_orelse_list, [ok, ok], + [Y || X <- id([true, false]), X orelse begin _N = id(1), true end, + Y <- id([ok])]), + check(skipped_orelse_binary, <<1, 1>>, + << Y || X <- id([true, false]), X orelse begin _N = id(1), true end, + Y <- id([<<1>>]) >>), + check(nested_short_circuit_list, [ok, ok], + [Y || X <- id([true, false]), + X orelse (id(false) orelse begin _N = id(1), true end), Y <- id([ok])]), + check(nested_short_circuit_binary, <<1, 1>>, + << Y || X <- id([true, false]), + X orelse (id(false) orelse begin _N = id(1), true end), Y <- id([<<1>>]) >>), + check(skipped_andalso_list, [ok], + [Y || X <- id([true, false]), X andalso begin _N = id(1), true end, + Y <- id([ok])]), + check(skipped_andalso_binary, <<1>>, + << Y || X <- id([true, false]), X andalso begin _N = id(1), true end, + Y <- id([<<1>>]) >>), + ok. + +conditional_rematches(Flag) -> + check({conditional_source, Flag}, [1], + [Y || X <- id([1]), begin N = X, true end, + Y <- begin case Flag of true -> N = 1; false -> ok end, [N] end]), + check({conditional_filter, Flag}, [1], + [Y || X <- id([1]), begin N = X, true end, + begin case Flag of true -> N = 1; false -> ok end, N =:= 1 end, + Y <- id([N])]), + check({conditional_template, Flag}, [1], + [begin case Flag of true -> N = 1; false -> ok end, N end + || X <- id([1]), begin N = X, true end, _ <- id([ok])]), + check({conditional_binary_source, Flag}, <<1>>, + << Y || X <- id([1]), begin N = X, true end, + Y <- begin case Flag of true -> N = 1; false -> ok end, [<>] end >>), + check({conditional_binary_filter, Flag}, <<1>>, + << Y || X <- id([1]), begin N = X, true end, + begin case Flag of true -> N = 1; false -> ok end, N =:= 1 end, + Y <- id([<>]) >>), + check({conditional_binary_template, Flag}, <<1>>, + << (begin case Flag of true -> N = 1; false -> ok end, <> end) + || X <- id([1]), begin N = X, true end, _ <- id([ok]) >>), + ok. + traces() -> check(filter_once, {[11, 12, 21, 22], [{filter, 10}, {source, 10}, {body, 11}, {body, 12},