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 @@ -2,6 +2,16 @@

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

## Comprehensions keep filters before the first generator

- List and binary comprehensions evaluate leading filters once, in order,
before evaluating a generator source. A false filter skips later filters,
sources and the body, as on BEAM.
- Filter-only comprehensions produce the body once when the filters pass,
or an empty list or bitstring when they do not.
- The `comprehension_leading_filter` regression runs unchanged natively and
on BEAM, checking results, evaluation order, captures and exceptions.

## Fatal self-exit signals stop execution immediately

- `exit(self(), Reason)` now stops before the next expression when the signal
Expand Down
74 changes: 72 additions & 2 deletions crates/nera-ir/src/lower/list_comp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,16 @@ impl IrBuilder {
qualifiers: &[ast::LcQualifier],
finalize_as_binary: bool,
) -> Result<VarId, LowerError> {
let leading_filters = qualifiers.iter()
.take_while(|q| matches!(q, ast::LcQualifier::Filter(..))).count();
if leading_filters > 0 {
return self.in_local_scope(|builder| {
builder.lower_leading_comprehension_filters(
body_expr, qualifiers, leading_filters, finalize_as_binary,
)
});
}

// ── 1. Split qualifiers into generator groups ──
let (groups, bin_flags) = Self::split_lc_qualifiers(qualifiers)?;
let n = groups.len();
Expand Down Expand Up @@ -111,7 +121,67 @@ impl IrBuilder {
Ok(call_result)
}

/// Split LC qualifiers into generator groups.
/// Leading filters run once, before the first source, in the comprehension's
/// local scope. Keep their bindings available to later filters, sources and
/// captures, but not to code after the comprehension.
fn lower_leading_comprehension_filters(
&mut self,
body_expr: &ast::Expr,
qualifiers: &[ast::LcQualifier],
leading_filters: usize,
finalize_as_binary: bool,
) -> Result<VarId, LowerError> {
let skip_block = self.fresh_block_with_id();
let join_block = self.fresh_block_with_id();
let result = self.fresh_var();
for qualifier in &qualifiers[..leading_filters] {
let ast::LcQualifier::Filter(filter, _) = qualifier else {
unreachable!("leading qualifiers are filters");
};
let condition = self.lower_expr(filter, false)?;
let next_block = self.fresh_block_with_id();
self.set_terminator(Terminator::Branch(condition, next_block, skip_block));
self.close_current_block();
self.start_block(next_block);
}

let value = if leading_filters < qualifiers.len() {
self.lower_comprehension(body_expr, &qualifiers[leading_filters..], finalize_as_binary)?
} else {
let element = self.lower_comprehension_body(body_expr, finalize_as_binary)?;
if finalize_as_binary {
element
} else {
let nil = self.emit_const(Literal::Nil);
let list = self.fresh_var();
self.emit(Instruction::MakeCons(list, element, nil));
list
}
};
self.emit(Instruction::Move(result, value));
self.set_terminator(Terminator::Jump(join_block, vec![]));
self.close_current_block();

self.start_block(skip_block);
let empty = self.emit_const(Literal::Nil);
let empty = if finalize_as_binary {
let bits = self.fresh_var();
self.emit(Instruction::Call(
bits, CallTarget::Bif("list_to_bitstring".to_string()), vec![empty],
));
bits
} else {
empty
};
self.emit(Instruction::Move(result, empty));
self.set_terminator(Terminator::Jump(join_block, vec![]));
self.close_current_block();
self.start_block(join_block);
Ok(result)
}

/// Split LC qualifiers into generator groups. Leading filters have already
/// been lowered, so the first qualifier here is a generator.
/// Each group: (pattern, source_expr, [filter_exprs]).
/// Keep binary patterns intact: their sizes and segment boundaries define
/// how much source each iteration consumes.
Expand Down Expand Up @@ -223,7 +293,7 @@ impl IrBuilder {
let badarg = self.emit_const(Literal::Atom(badarg));
let raised = self.fresh_var();
self.emit(Instruction::RaiseException(raised, ExceptionClass::Error, badarg));
self.set_terminator(Terminator::Return(raised));
self.set_raise_terminator(raised);
self.close_current_block();
self.start_block(append_block);
Ok(elem)
Expand Down
5 changes: 5 additions & 0 deletions crates/nera-ir/src/warnings/unbound_vars.rs
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,7 @@ 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, _)
Expand All @@ -427,9 +428,13 @@ fn check_unbound_in_expr(
// not from the bindings this generator introduces.
check_unbound_in_pattern_exprs(pat, &lc_scope, 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, atom_names, diagnostics);
if before_generator {
collect_exported_bindings(filter_expr, &mut lc_scope);
}
}
}
}
Expand Down
1 change: 1 addition & 0 deletions crates/nera-ir/tests/verify_lowered.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ fixtures! {
binary_rest => "tests/multi_clause_binary_rest.erl",
map_update => "tests/map_exact_update.erl",
list_comprehension => "tests/lc_nested.erl",
comprehension_leading_filter => "tests/comprehension_leading_filter.erl",
generator_pattern => "tests/lc_generator_pattern.erl",
closures => "tests/fun_nested.erl",
recursive_closure => "tests/tco_closure_tail.erl",
Expand Down
1 change: 1 addition & 0 deletions tests/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,7 @@ These are automatically skipped by the runner and show up as SKIP in output.
| `list_append.erl` | ++ and -- operators | ~15 |
| `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 |
| `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 |
| `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 |
Expand Down
108 changes: 108 additions & 0 deletions tests/comprehension_leading_filter.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
%% EXPECT: comprehension_leading_filter_ok
%% Leading filters must run before any generator source, and filter-only
%% comprehensions produce their body once or not at all, as on BEAM.
%% Run this same fixture on native Nera and BEAM.
-module(comprehension_leading_filter).
-export([test/0]).

test() ->
[] = [X || false, X <- [1, 2]],
[ok] = [ok || true],
[] = [ok || false],
<<>> = << <<X>> || false, X <- [1, 2] >>,
<<7:3>> = << <<7:3>> || true >>,
<<>> = << <<7:3>> || false >>,
lists:foreach(fun(Flag) ->
Expected = case Flag of true -> [1, 2]; false -> [] end,
Expected = [X || Flag, X <- id([1, 2])],
Expected = [X || Flag, <<X>> <= id(<<1, 2>>)],
ExpectedBits = case Flag of true -> <<1:3, 2:3>>; false -> <<>> end,
ExpectedBits = << <<X:3>> || Flag, X <- id([1, 2]) >>,
ExpectedBits = << <<X:3>> || Flag, <<X>> <= id(<<1, 2>>) >>,
ExpectedOne = case Flag of true -> [ok]; false -> [] end,
ExpectedOne = [ok || Flag, id(true)],
ExpectedByte = case Flag of true -> <<42>>; false -> <<>> end,
ExpectedByte = << <<42>> || Flag, id(true) >>
end, [true, false]),
traces(),
captures(),
exceptions(),
io:format("comprehension_leading_filter_ok~n"),
ok.

traces() ->
{[], [first, stop]} = traced(fun() ->
[event(body, X) || event(first, true), event(stop, false),
event(skipped, true), X <- event(source, [1, 2])]
end),
{<<>>, [stop]} = traced(fun() ->
<< (event(body, <<X>>)) || event(stop, false),
<<X>> <= event(source, <<1, 2>>) >>
end),
{[ok], [first, second, body]} = traced(fun() ->
[event(body, ok) || event(first, true), event(second, true)]
end),
{<<1:3>>, [first, body]} = traced(fun() ->
<< (event(body, <<1:3>>)) || event(first, true) >>
end),
{[11, 21, 12, 22], [first, source, inner, body, body, inner, body, body]} =
traced(fun() ->
[event(body, X + Y) || event(first, true), X <- event(source, [1, 2]),
Y <- event(inner, [10, 20])]
end),
{<<1, 2>>, [first, source, body, body]} = traced(fun() ->
<< (event(body, <<X>>)) || event(first, true),
<<X>> <= event(source, <<1, 2>>) >>
end),
ok.

captures() ->
Outer = id(2),
[1, 2] = [Outer || Outer =:= 2, Outer <- id([1, 2])],
<<1, 2>> = << <<Outer>> || Outer =:= 2, <<Outer>> <= id(<<1, 2>>) >>,
<<0, Tail/bits>> = id(<<0, 5:3>>),
[Tail] = [Tail || bit_size(Tail) =:= 3],
Tail = << Tail || bit_size(Tail) =:= 3 >>,
3 = bit_size(Tail),
[3] = [BodyBound || begin BodyBound = id(3), BodyBound > 0 end],
[4] = [SourceBound + X || begin SourceBound = id(3), SourceBound > 0 end,
X <- id([1])],
<<4>> = << <<(BitsBound + X)>> || begin BitsBound = id(3), BitsBound > 0 end,
<<X>> <= id(<<1>>) >>,
[[1], [2]] = [[X || X > 0] || X <- id([1, 2])],
ok.

exceptions() ->
[] = [error(body_ran) || id(false)],
<<>> = << (id(not_bits)) || id(false) >>,
[] = [X || id(false), X <- error(source_ran)],
<<>> = << <<X>> || id(false), <<X>> <= error(source_ran) >>,
{{throw, filter_failed}, [first]} = traced(fun() ->
[event(body, X) || event(first, true), throw(filter_failed),
X <- event(source, [1])]
end),
{{throw, filter_failed}, [first]} = traced(fun() ->
<< (event(body, <<1>>)) || event(first, true), throw(filter_failed) >>
end),
{error, badarg} = result(fun() -> << (id(not_bits)) || id(true) >> end),
badarg = try
<< (id(not_bits)) || id(true) >>,
error(after_bad_body)
catch error:BodyReason -> BodyReason end,
filter_failed = try
[ok || throw(filter_failed)],
error(after_bad_filter)
catch throw:FilterReason -> FilterReason end,
ok.

traced(Fun) ->
put(trace, []),
Result = result(Fun),
{Result, lists:reverse(erase(trace))}.

result(Fun) ->
try Fun() catch Class:Reason -> {Class, Reason} end.

event(Name, Value) -> put(trace, [Name | get(trace)]), Value.

id(X) -> X.
Loading