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
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@

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

## Regression coverage for case exports inside maybe bodies

- A diagnostic regression protects names bound in every `case` branch and read
later in the same `maybe` body from false unbound-variable errors.
- `maybe_case_exports` checks both branch results in plain bodies and before or
after conditional matches, including match failures and explicit completion,
using the same fixture natively and on BEAM. The existing compiler behavior
is unchanged.

## Pattern inputs are checked before their own bindings

- Function heads, `case`, `receive`, `try ... of`, catch and `maybe ... else`
Expand Down
15 changes: 15 additions & 0 deletions crates/nera-ir/src/warnings/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1760,6 +1760,21 @@ fn safe_var_in_all_case_branches() {
);
}

#[test]
fn maybe_case_exports_are_bound_in_later_body_expressions() {
// The native/BEAM fixture reads each case's shared binding later in the
// same maybe body. Collecting only direct matches falsely left R unbound.
let source = include_str!("../../../../tests/maybe_case_exports.erl");
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:?}"
);
}

#[test]
fn nested_case_exports_are_safe_outer_inputs() {
// Every path in the native/BEAM fixture binds the later key or size.
Expand Down
1 change: 1 addition & 0 deletions tests/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@ These are automatically skipped by the runner and show up as SKIP in output.
| Test File | Feature Category | Assertions |
|-----------------------|-------------------------------|------------|
| `control_flow.erl` | Case, begin..end, nested flow | ~15 |
| `maybe_case_exports.erl` | Names bound in every case branch and read later in the same maybe body: plain bodies, cases before/after conditional matches, both branch results, match failures and completion; same fixture on native/BEAM | 9 |
| `if_expr.erl` | If expressions | ~15 |
| `branch_export_reads.erl` | Names bound in `begin...end` or every `case`/`if`/`receive` branch, read afterwards: bodies, nested and scrutinee exports, call arguments, `try ... of`, fun-head and generator keys and sizes, construction sizes, guards, captures, later patterns and binary tails; funs in record field defaults; fun heads and generators still shadow, and a comprehension's first generator source keeps its bindings local; a binary tail read in a comprehension source or `maybe` block stays readable after it; a name local to an earlier fun does not stop a later read; a generator source or `catch` operand reads its own exports; a sibling clause, fun clause or receive timeout may reuse the name; also run on BEAM | 71 |
| `nested_case_exports.erl` | All-path nested case bindings used afterward as direct reads, map keys, binary sizes, captures and later partial matches; mixed direct/nested branches, both branches nested, three levels and a matched case result; same fixture on BEAM | 4 input combinations |
Expand Down
54 changes: 54 additions & 0 deletions tests/maybe_case_exports.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
%% EXPECT: maybe case exports ok
-module(maybe_case_exports).
-export([test/0]).

test() ->
x = plain(id(1)),
y = plain(id(2)),
x = before_match(id(1), id({ok, x})),
y = before_match(id(2), id({ok, y})),
{failed, {ok, y}} = before_match(id(1), id({ok, y})),
{failed, mismatch} = before_match(id(2), id(mismatch)),
x = after_match(id({ok, 1})),
y = after_match(id({ok, 2})),
{failed, mismatch} = after_match(id(mismatch)),
io:format("maybe case exports ok~n"),
ok.

%% No conditional match: a later expression reads the case's shared binding.
plain(B) ->
maybe
case B of
1 -> R = x;
_ -> R = y
end,
R
end.

%% The shared binding is also an input to a later conditional match.
before_match(B, Input) ->
maybe
case B of
1 -> R = x;
_ -> R = y
end,
{ok, R} ?= Input,
R
else
Failed -> {failed, Failed}
end.

%% A successful conditional match can precede the exporting case.
after_match(Input) ->
maybe
{ok, B} ?= Input,
case B of
1 -> R = x;
_ -> R = y
end,
R
else
Failed -> {failed, Failed}
end.

id(Value) -> Value.
Loading