Skip to content

Commit da0f5b4

Browse files
authored
fix(ir): preserve leading comprehension filters
Leading filters run once, in order, before any generator source. Filter-only comprehensions produce one body when filters pass, or an empty result otherwise. Leading-filter bindings and binary-body exceptions reach the correct scope and handler.
1 parent 6cd3afa commit da0f5b4

6 files changed

Lines changed: 197 additions & 2 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,16 @@
22

33
All notable milestones for the nera project are documented in this file.
44

5+
## Comprehensions keep filters before the first generator
6+
7+
- List and binary comprehensions evaluate leading filters once, in order,
8+
before evaluating a generator source. A false filter skips later filters,
9+
sources and the body, as on BEAM.
10+
- Filter-only comprehensions produce the body once when the filters pass,
11+
or an empty list or bitstring when they do not.
12+
- The `comprehension_leading_filter` regression runs unchanged natively and
13+
on BEAM, checking results, evaluation order, captures and exceptions.
14+
515
## Fatal self-exit signals stop execution immediately
616

717
- `exit(self(), Reason)` now stops before the next expression when the signal

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

Lines changed: 72 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,16 @@ impl IrBuilder {
4343
qualifiers: &[ast::LcQualifier],
4444
finalize_as_binary: bool,
4545
) -> Result<VarId, LowerError> {
46+
let leading_filters = qualifiers.iter()
47+
.take_while(|q| matches!(q, ast::LcQualifier::Filter(..))).count();
48+
if leading_filters > 0 {
49+
return self.in_local_scope(|builder| {
50+
builder.lower_leading_comprehension_filters(
51+
body_expr, qualifiers, leading_filters, finalize_as_binary,
52+
)
53+
});
54+
}
55+
4656
// ── 1. Split qualifiers into generator groups ──
4757
let (groups, bin_flags) = Self::split_lc_qualifiers(qualifiers)?;
4858
let n = groups.len();
@@ -111,7 +121,67 @@ impl IrBuilder {
111121
Ok(call_result)
112122
}
113123

114-
/// Split LC qualifiers into generator groups.
124+
/// Leading filters run once, before the first source, in the comprehension's
125+
/// local scope. Keep their bindings available to later filters, sources and
126+
/// captures, but not to code after the comprehension.
127+
fn lower_leading_comprehension_filters(
128+
&mut self,
129+
body_expr: &ast::Expr,
130+
qualifiers: &[ast::LcQualifier],
131+
leading_filters: usize,
132+
finalize_as_binary: bool,
133+
) -> Result<VarId, LowerError> {
134+
let skip_block = self.fresh_block_with_id();
135+
let join_block = self.fresh_block_with_id();
136+
let result = self.fresh_var();
137+
for qualifier in &qualifiers[..leading_filters] {
138+
let ast::LcQualifier::Filter(filter, _) = qualifier else {
139+
unreachable!("leading qualifiers are filters");
140+
};
141+
let condition = self.lower_expr(filter, false)?;
142+
let next_block = self.fresh_block_with_id();
143+
self.set_terminator(Terminator::Branch(condition, next_block, skip_block));
144+
self.close_current_block();
145+
self.start_block(next_block);
146+
}
147+
148+
let value = if leading_filters < qualifiers.len() {
149+
self.lower_comprehension(body_expr, &qualifiers[leading_filters..], finalize_as_binary)?
150+
} else {
151+
let element = self.lower_comprehension_body(body_expr, finalize_as_binary)?;
152+
if finalize_as_binary {
153+
element
154+
} else {
155+
let nil = self.emit_const(Literal::Nil);
156+
let list = self.fresh_var();
157+
self.emit(Instruction::MakeCons(list, element, nil));
158+
list
159+
}
160+
};
161+
self.emit(Instruction::Move(result, value));
162+
self.set_terminator(Terminator::Jump(join_block, vec![]));
163+
self.close_current_block();
164+
165+
self.start_block(skip_block);
166+
let empty = self.emit_const(Literal::Nil);
167+
let empty = if finalize_as_binary {
168+
let bits = self.fresh_var();
169+
self.emit(Instruction::Call(
170+
bits, CallTarget::Bif("list_to_bitstring".to_string()), vec![empty],
171+
));
172+
bits
173+
} else {
174+
empty
175+
};
176+
self.emit(Instruction::Move(result, empty));
177+
self.set_terminator(Terminator::Jump(join_block, vec![]));
178+
self.close_current_block();
179+
self.start_block(join_block);
180+
Ok(result)
181+
}
182+
183+
/// Split LC qualifiers into generator groups. Leading filters have already
184+
/// been lowered, so the first qualifier here is a generator.
115185
/// Each group: (pattern, source_expr, [filter_exprs]).
116186
/// Keep binary patterns intact: their sizes and segment boundaries define
117187
/// how much source each iteration consumes.
@@ -223,7 +293,7 @@ impl IrBuilder {
223293
let badarg = self.emit_const(Literal::Atom(badarg));
224294
let raised = self.fresh_var();
225295
self.emit(Instruction::RaiseException(raised, ExceptionClass::Error, badarg));
226-
self.set_terminator(Terminator::Return(raised));
296+
self.set_raise_terminator(raised);
227297
self.close_current_block();
228298
self.start_block(append_block);
229299
Ok(elem)

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,7 @@ fn check_unbound_in_expr(
418418
ast::Expr::ListComp(body_expr, qualifiers, _)
419419
| ast::Expr::BinComp(body_expr, qualifiers, _) => {
420420
let mut lc_scope = scope.clone();
421+
let mut before_generator = true;
421422
for q in qualifiers {
422423
match q {
423424
ast::LcQualifier::Generator(pat, source_expr, _)
@@ -427,9 +428,13 @@ fn check_unbound_in_expr(
427428
// not from the bindings this generator introduces.
428429
check_unbound_in_pattern_exprs(pat, &lc_scope, atom_names, diagnostics);
429430
collect_pattern_bindings(pat, &mut lc_scope);
431+
before_generator = false;
430432
}
431433
ast::LcQualifier::Filter(filter_expr, _) => {
432434
check_unbound_in_expr(filter_expr, &lc_scope, atom_names, diagnostics);
435+
if before_generator {
436+
collect_exported_bindings(filter_expr, &mut lc_scope);
437+
}
433438
}
434439
}
435440
}

‎crates/nera-ir/tests/verify_lowered.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ fixtures! {
6767
binary_rest => "tests/multi_clause_binary_rest.erl",
6868
map_update => "tests/map_exact_update.erl",
6969
list_comprehension => "tests/lc_nested.erl",
70+
comprehension_leading_filter => "tests/comprehension_leading_filter.erl",
7071
generator_pattern => "tests/lc_generator_pattern.erl",
7172
closures => "tests/fun_nested.erl",
7273
recursive_closure => "tests/tco_closure_tail.erl",

‎tests/README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,7 @@ These are automatically skipped by the runner and show up as SKIP in output.
345345
| `list_append.erl` | ++ and -- operators | ~15 |
346346
| `lists_member_exact.erl` | lists:member/2 exact equality: integer/float and signed-zero distinctions, compound terms, separately constructed equals, empty/later matches and direct/apply/dynamic/fun calls; numeric key-search controls; also run on BEAM | Table-driven |
347347
| `list_comprehension.erl` | Basic LC, filters, pattern gen | ~8 |
348+
| `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 |
348349
| `lc_nested.erl` | Nested list comprehension generators | ~8 |
349350
| `string_as_list.erl` | String operations, comparison | ~12 |
350351
| `string_codepoints.erl` | Non-ASCII expressions, full octal escape range, adjacency, list operations, patterns and Unicode format padding; also run on BEAM | 28 |
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
%% EXPECT: comprehension_leading_filter_ok
2+
%% Leading filters must run before any generator source, and filter-only
3+
%% comprehensions produce their body once or not at all, as on BEAM.
4+
%% Run this same fixture on native Nera and BEAM.
5+
-module(comprehension_leading_filter).
6+
-export([test/0]).
7+
8+
test() ->
9+
[] = [X || false, X <- [1, 2]],
10+
[ok] = [ok || true],
11+
[] = [ok || false],
12+
<<>> = << <<X>> || false, X <- [1, 2] >>,
13+
<<7:3>> = << <<7:3>> || true >>,
14+
<<>> = << <<7:3>> || false >>,
15+
lists:foreach(fun(Flag) ->
16+
Expected = case Flag of true -> [1, 2]; false -> [] end,
17+
Expected = [X || Flag, X <- id([1, 2])],
18+
Expected = [X || Flag, <<X>> <= id(<<1, 2>>)],
19+
ExpectedBits = case Flag of true -> <<1:3, 2:3>>; false -> <<>> end,
20+
ExpectedBits = << <<X:3>> || Flag, X <- id([1, 2]) >>,
21+
ExpectedBits = << <<X:3>> || Flag, <<X>> <= id(<<1, 2>>) >>,
22+
ExpectedOne = case Flag of true -> [ok]; false -> [] end,
23+
ExpectedOne = [ok || Flag, id(true)],
24+
ExpectedByte = case Flag of true -> <<42>>; false -> <<>> end,
25+
ExpectedByte = << <<42>> || Flag, id(true) >>
26+
end, [true, false]),
27+
traces(),
28+
captures(),
29+
exceptions(),
30+
io:format("comprehension_leading_filter_ok~n"),
31+
ok.
32+
33+
traces() ->
34+
{[], [first, stop]} = traced(fun() ->
35+
[event(body, X) || event(first, true), event(stop, false),
36+
event(skipped, true), X <- event(source, [1, 2])]
37+
end),
38+
{<<>>, [stop]} = traced(fun() ->
39+
<< (event(body, <<X>>)) || event(stop, false),
40+
<<X>> <= event(source, <<1, 2>>) >>
41+
end),
42+
{[ok], [first, second, body]} = traced(fun() ->
43+
[event(body, ok) || event(first, true), event(second, true)]
44+
end),
45+
{<<1:3>>, [first, body]} = traced(fun() ->
46+
<< (event(body, <<1:3>>)) || event(first, true) >>
47+
end),
48+
{[11, 21, 12, 22], [first, source, inner, body, body, inner, body, body]} =
49+
traced(fun() ->
50+
[event(body, X + Y) || event(first, true), X <- event(source, [1, 2]),
51+
Y <- event(inner, [10, 20])]
52+
end),
53+
{<<1, 2>>, [first, source, body, body]} = traced(fun() ->
54+
<< (event(body, <<X>>)) || event(first, true),
55+
<<X>> <= event(source, <<1, 2>>) >>
56+
end),
57+
ok.
58+
59+
captures() ->
60+
Outer = id(2),
61+
[1, 2] = [Outer || Outer =:= 2, Outer <- id([1, 2])],
62+
<<1, 2>> = << <<Outer>> || Outer =:= 2, <<Outer>> <= id(<<1, 2>>) >>,
63+
<<0, Tail/bits>> = id(<<0, 5:3>>),
64+
[Tail] = [Tail || bit_size(Tail) =:= 3],
65+
Tail = << Tail || bit_size(Tail) =:= 3 >>,
66+
3 = bit_size(Tail),
67+
[3] = [BodyBound || begin BodyBound = id(3), BodyBound > 0 end],
68+
[4] = [SourceBound + X || begin SourceBound = id(3), SourceBound > 0 end,
69+
X <- id([1])],
70+
<<4>> = << <<(BitsBound + X)>> || begin BitsBound = id(3), BitsBound > 0 end,
71+
<<X>> <= id(<<1>>) >>,
72+
[[1], [2]] = [[X || X > 0] || X <- id([1, 2])],
73+
ok.
74+
75+
exceptions() ->
76+
[] = [error(body_ran) || id(false)],
77+
<<>> = << (id(not_bits)) || id(false) >>,
78+
[] = [X || id(false), X <- error(source_ran)],
79+
<<>> = << <<X>> || id(false), <<X>> <= error(source_ran) >>,
80+
{{throw, filter_failed}, [first]} = traced(fun() ->
81+
[event(body, X) || event(first, true), throw(filter_failed),
82+
X <- event(source, [1])]
83+
end),
84+
{{throw, filter_failed}, [first]} = traced(fun() ->
85+
<< (event(body, <<1>>)) || event(first, true), throw(filter_failed) >>
86+
end),
87+
{error, badarg} = result(fun() -> << (id(not_bits)) || id(true) >> end),
88+
badarg = try
89+
<< (id(not_bits)) || id(true) >>,
90+
error(after_bad_body)
91+
catch error:BodyReason -> BodyReason end,
92+
filter_failed = try
93+
[ok || throw(filter_failed)],
94+
error(after_bad_filter)
95+
catch throw:FilterReason -> FilterReason end,
96+
ok.
97+
98+
traced(Fun) ->
99+
put(trace, []),
100+
Result = result(Fun),
101+
{Result, lists:reverse(erase(trace))}.
102+
103+
result(Fun) ->
104+
try Fun() catch Class:Reason -> {Class, Reason} end.
105+
106+
event(Name, Value) -> put(trace, [Name | get(trace)]), Value.
107+
108+
id(X) -> X.

0 commit comments

Comments
 (0)