diff --git a/CHANGELOG.md b/CHANGELOG.md index 9961721..3829060 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/crates/nera-ir/src/warnings/tests.rs b/crates/nera-ir/src/warnings/tests.rs index 7b9b8ce..9ed3689 100644 --- a/crates/nera-ir/src/warnings/tests.rs +++ b/crates/nera-ir/src/warnings/tests.rs @@ -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, <>", "N"), + ("<>, <>", "N"), + ("<>", "N"), + ("<>", "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, <>}", "N"), + ("{<>, <>}", "N"), + ("<>", "N"), + ("<>", "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 <> -> V end", + "receive <> -> V end", + "try Input of <> -> V catch _:_ -> no end", + "try Input() catch _:<> -> V end", + "maybe ok ?= Input else <> -> V end", + "<> = Input, V", + "maybe <> ?= 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(<>) -> 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 diff --git a/crates/nera-ir/src/warnings/unbound_vars.rs b/crates/nera-ir/src/warnings/unbound_vars.rs index 2b7d6d4..05b13ae 100644 --- a/crates/nera-ir/src/warnings/unbound_vars.rs +++ b/crates/nera-ir/src/warnings/unbound_vars.rs @@ -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); } @@ -55,21 +59,10 @@ fn check_unbound_in_body( diagnostics: &mut Vec, ) { 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); } } @@ -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); @@ -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); @@ -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); @@ -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); @@ -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); @@ -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 ── diff --git a/tests/README.md b/tests/README.md index adb5107..3f3c720 100644 --- a/tests/README.md +++ b/tests/README.md @@ -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 | @@ -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 diff --git a/tests/pattern_clause_case_unbound.erl b/tests/pattern_clause_case_unbound.erl new file mode 100644 index 0000000..810f5bd --- /dev/null +++ b/tests/pattern_clause_case_unbound.erl @@ -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. diff --git a/tests/pattern_clause_catch_unbound.erl b/tests/pattern_clause_catch_unbound.erl new file mode 100644 index 0000000..9840d9c --- /dev/null +++ b/tests/pattern_clause_catch_unbound.erl @@ -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. diff --git a/tests/pattern_clause_condmatch_unbound.erl b/tests/pattern_clause_condmatch_unbound.erl new file mode 100644 index 0000000..be1b225 --- /dev/null +++ b/tests/pattern_clause_condmatch_unbound.erl @@ -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. diff --git a/tests/pattern_clause_else_unbound.erl b/tests/pattern_clause_else_unbound.erl new file mode 100644 index 0000000..3059555 --- /dev/null +++ b/tests/pattern_clause_else_unbound.erl @@ -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. diff --git a/tests/pattern_clause_head_key_unbound.erl b/tests/pattern_clause_head_key_unbound.erl new file mode 100644 index 0000000..d2e65b7 --- /dev/null +++ b/tests/pattern_clause_head_key_unbound.erl @@ -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. diff --git a/tests/pattern_clause_head_size_unbound.erl b/tests/pattern_clause_head_size_unbound.erl new file mode 100644 index 0000000..dc77180 --- /dev/null +++ b/tests/pattern_clause_head_size_unbound.erl @@ -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. diff --git a/tests/pattern_clause_inputs.erl b/tests/pattern_clause_inputs.erl new file mode 100644 index 0000000..9ee2990 --- /dev/null +++ b/tests/pattern_clause_inputs.erl @@ -0,0 +1,94 @@ +%% EXPECT: pattern_clause_inputs_ok +-module(pattern_clause_inputs). +-export([test/0]). + +%% Same-head rejection must preserve keys/sizes bound before the pattern and +%% sizes bound by an earlier segment of that same binary. Run also on BEAM. +test() -> + Packet = id(<<8, 5>>), + Message = id({route, #{route => ok}}), + ok = head_key(id(#{route => ok})), + 5 = head_size(Packet), + ok = case_key(route, Message), + 5 = case_size(Packet), + ok = receive_key(route, Message), + 5 = receive_size(Packet), + ok = of_key(route, Message), + 5 = of_size(Packet), + ok = catch_key(route, Message), + 5 = catch_size(Packet), + ok = else_key(route, Message), + 5 = else_size(Packet), + ok = match_key(route, Message), + 5 = match_size(Packet), + ok = maybe_match_key(route, Message), + 5 = maybe_match_size(Packet), + ok = condmatch_key(route, Message), + 5 = condmatch_size(Packet), + ok = scrutinee_key(id(#{route => ok})), + ok = try_body_key(id(#{route => ok})), + ok = rhs_key(id(#{route => ok})), + ok = rhs_condmatch_key(id(#{route => ok})), + ok = earlier_maybe_key(id(#{route => ok})), + io:format("pattern_clause_inputs_ok~n"), + ok. + +head_key(#{route := V}) -> V. +head_size(<>) -> V. + +case_key(K, Message) -> + case Message of {K, #{K := V}} -> V end. +case_size(Packet) -> + case Packet of <> -> V end. + +receive_key(K, Message) -> + self() ! Message, + receive {K, #{K := V}} -> V after 1000 -> timeout end. +receive_size(Packet) -> + self() ! Packet, + receive <> -> V after 1000 -> timeout end. + +of_key(K, Message) -> + try Message of {K, #{K := V}} -> V catch _:_ -> no end. +of_size(Packet) -> + try Packet of <> -> V catch _:_ -> no end. + +catch_key(K, Message) -> + try throw(Message) catch throw:{K, #{K := V}} -> V end. +catch_size(Packet) -> + try throw(Packet) catch throw:<> -> V end. + +else_key(K, Message) -> + maybe ok ?= Message else {K, #{K := V}} -> V end. +else_size(Packet) -> + maybe ok ?= Packet else <> -> V end. + +match_key(K, Message) -> + {K, #{K := V}} = Message, + V. +match_size(Packet) -> + <> = Packet, + V. + +maybe_match_key(K, Message) -> + maybe {K, #{K := V}} = Message, V end. +maybe_match_size(Packet) -> + maybe <> = Packet, V end. +condmatch_key(K, Message) -> + maybe {K, #{K := V}} ?= Message, V end. +condmatch_size(Packet) -> + maybe <> ?= Packet, V end. + +scrutinee_key(Map) -> + case begin K = route, Map end of #{K := V} -> V end. +try_body_key(Map) -> + try K = route, Map of #{K := V} -> V catch _:_ -> no end. +rhs_key(Map) -> + #{K := V} = begin K = route, Map end, + V. +rhs_condmatch_key(Map) -> + maybe #{K := V} ?= begin K = route, Map end, V end. +earlier_maybe_key(Map) -> + maybe begin K = route end, #{K := V} ?= Map, V end. + +id(X) -> X. diff --git a/tests/pattern_clause_match_unbound.erl b/tests/pattern_clause_match_unbound.erl new file mode 100644 index 0000000..ddd42b5 --- /dev/null +++ b/tests/pattern_clause_match_unbound.erl @@ -0,0 +1,7 @@ +%% EXPECT_COMPILE_ERROR: variable 'K' is unbound +-module(pattern_clause_match_unbound). +-export([test/0]). + +test() -> {K, #{K := V}} = id({route, #{route => ok}}), V. + +id(X) -> X. diff --git a/tests/pattern_clause_maybe_match_unbound.erl b/tests/pattern_clause_maybe_match_unbound.erl new file mode 100644 index 0000000..f994aca --- /dev/null +++ b/tests/pattern_clause_maybe_match_unbound.erl @@ -0,0 +1,7 @@ +%% EXPECT_COMPILE_ERROR: variable 'K' is unbound +-module(pattern_clause_maybe_match_unbound). +-export([test/0]). + +test() -> maybe {K, #{K := V}} = id({route, #{route => ok}}), V end. + +id(X) -> X. diff --git a/tests/pattern_clause_of_unbound.erl b/tests/pattern_clause_of_unbound.erl new file mode 100644 index 0000000..1f08d62 --- /dev/null +++ b/tests/pattern_clause_of_unbound.erl @@ -0,0 +1,7 @@ +%% EXPECT_COMPILE_ERROR: variable 'K' is unbound +-module(pattern_clause_of_unbound). +-export([test/0]). + +test() -> try id({route, #{route => ok}}) of {K, #{K := V}} -> V catch _:_ -> no end. + +id(X) -> X. diff --git a/tests/pattern_clause_receive_unbound.erl b/tests/pattern_clause_receive_unbound.erl new file mode 100644 index 0000000..7e08285 --- /dev/null +++ b/tests/pattern_clause_receive_unbound.erl @@ -0,0 +1,5 @@ +%% EXPECT_COMPILE_ERROR: variable 'K' is unbound +-module(pattern_clause_receive_unbound). +-export([test/0]). + +test() -> receive {K, #{K := V}} -> V after 0 -> ok end.