From 0ac8b0fa7c1fb9904d05ed422ed3412708cfc26f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micka=C3=ABl=20R=C3=A9mond?= Date: Sun, 15 Nov 2026 19:16:22 +0100 Subject: [PATCH] test(ir): cover case exports inside maybe bodies --- CHANGELOG.md | 9 +++++ crates/nera-ir/src/warnings/tests.rs | 15 ++++++++ tests/README.md | 1 + tests/maybe_case_exports.erl | 54 ++++++++++++++++++++++++++++ 4 files changed, 79 insertions(+) create mode 100644 tests/maybe_case_exports.erl diff --git a/CHANGELOG.md b/CHANGELOG.md index 95ddc12..5737a66 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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` diff --git a/crates/nera-ir/src/warnings/tests.rs b/crates/nera-ir/src/warnings/tests.rs index 9ed3689..64774ad 100644 --- a/crates/nera-ir/src/warnings/tests.rs +++ b/crates/nera-ir/src/warnings/tests.rs @@ -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. diff --git a/tests/README.md b/tests/README.md index 5ed41c8..740e05e 100644 --- a/tests/README.md +++ b/tests/README.md @@ -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 | diff --git a/tests/maybe_case_exports.erl b/tests/maybe_case_exports.erl new file mode 100644 index 0000000..c983c90 --- /dev/null +++ b/tests/maybe_case_exports.erl @@ -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.