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

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

## Pattern inputs are checked before their own bindings

- Function heads, `case`, `receive`, `try ... of`, catch and `maybe ... else`
clauses now reject map keys and binary sizes supplied only by the same
pattern, including sibling arguments and nested patterns, as `erlc` does.
- Ordinary `=` and conditional `?=` matches apply the same check. Keys and
sizes bound before the match, including by its completed right-hand side,
remain available; earlier segments of the same binary still supply sizes.
- Compiler-error fixtures cover each pattern site; `pattern_clause_inputs`
checks valid prior inputs and sequential binary sizes natively and on BEAM.

## Unsupported float segment widths compile as on OTP 29

- Literal float segment widths outside the supported encodings no longer reject
Expand Down
114 changes: 114 additions & 0 deletions crates/nera-ir/src/warnings/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1140,6 +1140,120 @@ fn fun_pattern_inputs_reject_same_head_bindings() {
}
}

#[test]
fn function_pattern_inputs_reject_same_head_bindings() {
// Clause inputs are reads before any argument in this head binds a name.
for (head, name) in [
("K, #{K := V}", "K"),
("#{K := V}, K", "K"),
("#{key := K, K := V}", "K"),
("{K, #{{route, K} := V}}", "K"),
("K = #{K := V}", "K"),
("#r{key = K, value = #{K := V}}", "K"),
("N, <<V:N>>", "N"),
("<<N>>, <<V:N>>", "N"),
("<<V:N, N>>", "N"),
("<<V:V>>", "V"),
] {
let source = format!("-module(test). -record(r, {{key, value}}). f({head}) -> V.");
assert_unbound_pattern_input(&source, name);
}
}

#[test]
fn clause_and_match_pattern_inputs_reject_same_head_bindings() {
// The requested extension applies at every ordinary clause/match site,
// including nested key expressions and sizes outside a single binary.
for (pattern, name) in [
("{K, #{K := V}}", "K"),
("#{key := K, K := V}", "K"),
("[K, #{{route, K} := V}]", "K"),
("K = #{K := V}", "K"),
("#r{key = K, value = #{K := V}}", "K"),
("{N, <<V:(N + 1)>>}", "N"),
("{<<N>>, <<V:N>>}", "N"),
("<<V:N, N>>", "N"),
("<<V:V>>", "V"),
] {
for expr in [
format!("case Input of {pattern} -> V end"),
format!("receive {pattern} -> V end"),
format!("try Input of {pattern} -> V catch _:_ -> no end"),
format!("try Input() catch _:{pattern} -> V end"),
format!("maybe ok ?= Input else {pattern} -> V end"),
format!("{pattern} = Input, V"),
format!("maybe {pattern} = Input, V end"),
format!("maybe {pattern} ?= Input, V end"),
] {
let source = format!("-module(test). -record(r, {{key, value}}). f(Input) -> {expr}.");
assert_unbound_pattern_input(&source, name);
}
}
// The class and stacktrace are siblings of the reason pattern, not
// inputs to it. A try/maybe body likewise cannot supply handler inputs.
for expr in [
"try Input() catch K:#{K := V} -> V end",
"try Input() catch _:#{K := V}:K -> V end",
"try K = route, Input() catch _:#{K := V} -> V end",
"maybe K = route, ok ?= Input else #{K := V} -> V end",
] {
let source = format!("-module(test). f(Input) -> {expr}.");
assert_unbound_pattern_input(&source, "K");
}
}

#[test]
fn clause_and_match_pattern_inputs_keep_prior_bindings() {
// erlc accepts enclosing inputs, completed scrutinee/RHS/try bindings,
// and earlier segments of the same binary, but not sibling patterns.
for expr in [
"case Input of {K, #{K := V}} -> V end",
"receive {K, #{K := V}} -> V end",
"try Input of {K, #{K := V}} -> V catch _:_ -> no end",
"try Input() catch _:{K, #{K := V}} -> V end",
"maybe ok ?= Input else {K, #{K := V}} -> V end",
"{K, #{K := V}} = Input, V",
"maybe {K, #{K := V}} = Input, V end",
"maybe {K, #{K := V}} ?= Input, V end",
"case Input of <<N, V:N>> -> V end",
"receive <<N, V:N>> -> V end",
"try Input of <<N, V:N>> -> V catch _:_ -> no end",
"try Input() catch _:<<N, V:N>> -> V end",
"maybe ok ?= Input else <<N, V:N>> -> V end",
"<<N, V:N>> = Input, V",
"maybe <<N, V:N>> ?= Input, V end",
] {
let source = format!("-module(test). f(K, Input) -> {expr}.");
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:?}"
);
}
for source in [
"-module(test). f(<<N, V:N>>) -> V.",
"-module(test). f(Input) -> case begin K = route, Input end of #{K := V} -> V end.",
"-module(test). f(Input) -> try K = route, Input of #{K := V} -> V catch _:_ -> no end.",
"-module(test). f(Input) -> #{K := V} = begin K = route, Input end, V.",
"-module(test). f(Input) -> #{K := V} = (M = begin K = route, Input end), {M, V}.",
"-module(test). f(Input) -> maybe #{K := V} = begin K = route, Input end, V end.",
"-module(test). f(Input) -> maybe #{K := V} ?= begin K = route, Input end, V end.",
"-module(test). f(Input) -> maybe begin K = route end, #{K := V} ?= Input, V end.",
] {
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:?}"
);
}
}

#[test]
fn generator_pattern_inputs_reject_same_head_bindings() {
// NERA-80: both comprehension forms and both generator variants share
Expand Down
53 changes: 28 additions & 25 deletions crates/nera-ir/src/warnings/unbound_vars.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ pub(super) fn check_unbound_variables_fn(
) {
for clause in &func.clauses {
let mut scope = HashSet::new();
// No argument in this head supplies another argument's key/size.
for pat in &clause.patterns {
check_unbound_in_pattern_exprs(pat, &scope, atom_names, diagnostics);
}
for pat in &clause.patterns {
collect_pattern_bindings(pat, &mut scope);
}
Expand Down Expand Up @@ -55,21 +59,10 @@ fn check_unbound_in_body(
diagnostics: &mut Vec<Diagnostic>,
) {
for expr in body {
if let ast::Expr::Match(pat, rhs, _) = expr {
// Check RHS first (evaluated before the binding takes effect).
check_unbound_in_expr(rhs, scope, atom_names, diagnostics);
// Pattern variables are bound for subsequent expressions.
collect_pattern_bindings(pat, scope);
// Also pull in bindings from nested Match expressions on the RHS
// — e.g. `A = B = Expr` where `B = Expr` is the inner Match.
collect_exported_bindings(rhs, scope);
} else {
check_unbound_in_expr(expr, scope, atom_names, diagnostics);
// Nested Match expressions embedded in sub-expressions
// (call args, tuple elements, etc.) also export bindings
// to the enclosing body scope.
collect_exported_bindings(expr, scope);
}
check_unbound_in_expr(expr, scope, atom_names, diagnostics);
// Matches, including those nested in sub-expressions, export their
// bindings only after their pattern inputs have been checked.
collect_exported_bindings(expr, scope);
}
}

Expand Down Expand Up @@ -353,6 +346,7 @@ fn check_unbound_in_expr(
collect_exported_bindings(scrutinee, &mut input_scope);
for clause in clauses {
let mut clause_scope = input_scope.clone();
check_unbound_in_pattern_exprs(&clause.pattern, &input_scope, atom_names, diagnostics);
collect_pattern_bindings(&clause.pattern, &mut clause_scope);
if let Some(ref guard) = clause.guard {
check_unbound_in_guard(guard, &clause_scope, atom_names, diagnostics);
Expand All @@ -373,6 +367,7 @@ fn check_unbound_in_expr(
ast::Expr::Receive(clauses, after, _) => {
for clause in clauses {
let mut clause_scope = scope.clone();
check_unbound_in_pattern_exprs(&clause.pattern, scope, atom_names, diagnostics);
collect_pattern_bindings(&clause.pattern, &mut clause_scope);
if let Some(ref guard) = clause.guard {
check_unbound_in_guard(guard, &clause_scope, atom_names, diagnostics);
Expand All @@ -398,6 +393,7 @@ fn check_unbound_in_expr(
check_unbound_in_body(body, &mut try_scope, atom_names, diagnostics);
for clause in of_clauses {
let mut clause_scope = try_scope.clone();
check_unbound_in_pattern_exprs(&clause.pattern, &try_scope, atom_names, diagnostics);
collect_pattern_bindings(&clause.pattern, &mut clause_scope);
if let Some(ref guard) = clause.guard {
check_unbound_in_guard(guard, &clause_scope, atom_names, diagnostics);
Expand All @@ -407,6 +403,12 @@ fn check_unbound_in_expr(
// Catch clauses get outer scope (not try body bindings).
for clause in catch_clauses {
let mut clause_scope = scope.clone();
// Class, reason and stacktrace bindings are all siblings;
// none supplies an input to another pattern in this head.
check_unbound_in_pattern_exprs(&clause.pattern, scope, atom_names, diagnostics);
if let Some(stk) = &clause.stacktrace {
check_unbound_in_pattern_exprs(stk, scope, atom_names, diagnostics);
}
// Class variable binding (e.g. `Class:Reason:Stacktrace`).
if let ast::CatchClass::Variable(class_name, _) = &clause.class {
clause_scope.insert(*class_name);
Expand Down Expand Up @@ -459,21 +461,20 @@ fn check_unbound_in_expr(
for item in body {
match item {
ast::MaybeExpr::Expr(e) => {
if let ast::Expr::Match(pat, rhs, _) = e {
check_unbound_in_expr(rhs, &maybe_scope, atom_names, diagnostics);
collect_pattern_bindings(pat, &mut maybe_scope);
} else {
check_unbound_in_expr(e, &maybe_scope, atom_names, diagnostics);
}
check_unbound_in_expr(e, &maybe_scope, atom_names, diagnostics);
collect_exported_bindings(e, &mut maybe_scope);
}
ast::MaybeExpr::CondMatch(pat, e, _) => {
check_unbound_in_expr(e, &maybe_scope, atom_names, diagnostics);
collect_exported_bindings(e, &mut maybe_scope);
check_unbound_in_pattern_exprs(pat, &maybe_scope, atom_names, diagnostics);
collect_pattern_bindings(pat, &mut maybe_scope);
}
}
}
for clause in else_clauses {
let mut clause_scope = scope.clone();
check_unbound_in_pattern_exprs(&clause.pattern, scope, atom_names, diagnostics);
collect_pattern_bindings(&clause.pattern, &mut clause_scope);
if let Some(ref guard) = clause.guard {
check_unbound_in_guard(guard, &clause_scope, atom_names, diagnostics);
Expand All @@ -482,12 +483,14 @@ fn check_unbound_in_expr(
}
}

// ── Match expression (non-body level) ──
// ── Match expression ──
ast::Expr::Match(pat, rhs, _) => {
check_unbound_in_expr(rhs, scope, atom_names, diagnostics);
// Check pattern variables that are already bound (used as equality checks)
// but don't emit unbound errors for pattern bindings themselves.
check_unbound_in_pattern_exprs(pat, scope, atom_names, diagnostics);
// The RHS completes before matching, so its definite bindings
// are inputs; bindings introduced by this pattern are not.
let mut input_scope = scope.clone();
collect_exported_bindings(rhs, &mut input_scope);
check_unbound_in_pattern_exprs(pat, &input_scope, atom_names, diagnostics);
}

// ── Recurse into sub-expressions ──
Expand Down
11 changes: 11 additions & 0 deletions tests/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,7 @@ These are automatically skipped by the runner and show up as SKIP in output.
| `match_pinned_var.erl` | Bound var equality, badmatch on differ | ~8 |
| `map_bound_value_vars.erl` | Bound vars as map pattern values: repeated, heads, case, `=`, `?=`, receive; later keys/sizes in `=`/`?=`; bound binary tails as values and keys, and read in one branch; fun/generator shadowing | 75+ |
| `pattern_input_scope.erl` | Outer/earlier-generator map keys and binary sizes in fun/list/binary-comprehension patterns; shadowing, nested captures, captured guards, mismatch skipping, binary tails and earlier segments of the same binary (native/BEAM) | 45 |
| `pattern_clause_inputs.erl` | Prior map keys and sequential binary sizes in function/case/receive/try-of/catch/maybe-else heads and `=`/`?=` matches; completed scrutinee, try-body, RHS and earlier maybe bindings; same fixture on native/BEAM | Completion |
| `generator_size_rebind.erl` | Earlier-generator sizes with later rebinding, exact dynamic/UTF binary ends, filters, later generators and shared pattern callers (native/BEAM) | 34 |
| `map_binary_keys.erl` | Map ops with binary/tuple/float keys, semantic equality | ~35 |
| `map_update.erl` | Map update syntax `M#{k => v, k := v}` | ~12 |
Expand Down Expand Up @@ -517,6 +518,16 @@ committed catalogue with the pinned OTP abstract code.
| `pattern_input_list_unbound.erl` | Reject a list-generator key supplied only by its own pattern | (compile error) |
| `pattern_input_binary_unbound.erl` | Reject a binary-generator size supplied only by a later segment | (compile error) |
| `pattern_input_record_unbound.erl` | Reject a same-head map key in an unused record-default fun | (compile error) |
| `pattern_clause_head_key_unbound.erl` | Reject a map key supplied only by a sibling function argument | (compile error) |
| `pattern_clause_head_size_unbound.erl` | Reject a binary size supplied only by a sibling function argument | (compile error) |
| `pattern_clause_case_unbound.erl` | Reject a same-head map key in a case clause | (compile error) |
| `pattern_clause_receive_unbound.erl` | Reject a same-head map key in a receive clause | (compile error) |
| `pattern_clause_of_unbound.erl` | Reject a same-head map key in a try-of clause | (compile error) |
| `pattern_clause_catch_unbound.erl` | Reject a map key supplied only by the catch class | (compile error) |
| `pattern_clause_else_unbound.erl` | Reject a same-head map key in a maybe-else clause | (compile error) |
| `pattern_clause_match_unbound.erl` | Reject a same-pattern map key in a body match | (compile error) |
| `pattern_clause_maybe_match_unbound.erl` | Reject a same-pattern map key in a maybe-body match | (compile error) |
| `pattern_clause_condmatch_unbound.erl` | Reject a same-pattern map key in a conditional match | (compile error) |

### "Programming Erlang" Adapted Tests

Expand Down
7 changes: 7 additions & 0 deletions tests/pattern_clause_case_unbound.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
%% EXPECT_COMPILE_ERROR: variable 'K' is unbound
-module(pattern_clause_case_unbound).
-export([test/0]).

test() -> case id({route, #{route => ok}}) of {K, #{K := V}} -> V end.

id(X) -> X.
7 changes: 7 additions & 0 deletions tests/pattern_clause_catch_unbound.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
%% EXPECT_COMPILE_ERROR: variable 'K' is unbound
-module(pattern_clause_catch_unbound).
-export([test/0]).

test() -> try id(ok) catch K:#{K := V} -> V end.

id(X) -> X.
7 changes: 7 additions & 0 deletions tests/pattern_clause_condmatch_unbound.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
%% EXPECT_COMPILE_ERROR: variable 'K' is unbound
-module(pattern_clause_condmatch_unbound).
-export([test/0]).

test() -> maybe {K, #{K := V}} ?= id({route, #{route => ok}}), V end.

id(X) -> X.
7 changes: 7 additions & 0 deletions tests/pattern_clause_else_unbound.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
%% EXPECT_COMPILE_ERROR: variable 'K' is unbound
-module(pattern_clause_else_unbound).
-export([test/0]).

test() -> maybe ok ?= id(no) else {K, #{K := V}} -> V end.

id(X) -> X.
6 changes: 6 additions & 0 deletions tests/pattern_clause_head_key_unbound.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
%% EXPECT_COMPILE_ERROR: variable 'K' is unbound
-module(pattern_clause_head_key_unbound).
-export([test/0]).

test() -> f(route, #{route => ok}).
f(K, #{K := V}) -> V.
6 changes: 6 additions & 0 deletions tests/pattern_clause_head_size_unbound.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
%% EXPECT_COMPILE_ERROR: variable 'N' is unbound
-module(pattern_clause_head_size_unbound).
-export([test/0]).

test() -> f(8, <<5>>).
f(N, <<V:N>>) -> V.
Loading
Loading