diff --git a/CHANGELOG.md b/CHANGELOG.md index 98967ee..5b4b083 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -58,6 +58,22 @@ All notable milestones for the nera project are documented in this file. mixed generators, closures, deferred bitstring tails, short-circuit filters, conditional rematches and evaluation traces. +## Short-circuit bindings report their unsafe operator + +- `nera check` and `nera build` report `variable 'Y' unsafe in 'andalso'` + (or `orelse`) when later code reads a name bound only in the right operand, + matching `erlc` rather than reporting only an unbound-variable error. +- Prebound names, bindings from the left operand and names used only within + the right operand remain valid. Fun and comprehension bindings stay local. +- Enclosing match patterns and later `maybe` items or comprehension filters + cannot reuse conditional names as fresh bindings. The same checks apply in + expression callees, receive timeouts and record-default expressions. +- Formatting preserves parentheses around match, send and catch operands of + binary operators, including flattened chains, so these programs retain their + syntax and grouping after formatting. +- Analyzer, formatter and compile-error regressions cover both short-circuit + operators. + ## 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-fmt/src/format.rs b/crates/nera-fmt/src/format.rs index 052d293..005e2fc 100644 --- a/crates/nera-fmt/src/format.rs +++ b/crates/nera-fmt/src/format.rs @@ -1059,16 +1059,18 @@ impl<'src> Formatter<'src> { /// This prevents `(A =:= B) and (C =:= D)` from being emitted as /// `A =:= B and C =:= D` (which re-parses with different grouping). fn format_binop_child(&mut self, child: &Expr, parent_prec: u8) -> Doc { - if let Expr::BinOp(child_op, _, _, _) = child { - if binop_precedence(*child_op) < parent_prec { - return concat_all(vec![ - text("("), - self.format_expr(child), - text(")"), - ]); - } + let needs_grouping = match child { + Expr::BinOp(child_op, _, _, _) => binop_precedence(*child_op) < parent_prec, + // These bind more weakly than every binary operator, including + // when they occur at either end of a flattened operator chain. + Expr::Match(..) | Expr::Send(..) | Expr::Catch(..) => true, + _ => false, + }; + if needs_grouping { + concat_all(vec![text("("), self.format_expr(child), text(")")]) + } else { + self.format_expr(child) } - self.format_expr(child) } // ── Helper formatters ─────────────────────────────────────────── diff --git a/crates/nera-fmt/tests/binary_operand_grouping.rs b/crates/nera-fmt/tests/binary_operand_grouping.rs new file mode 100644 index 0000000..7da8d6f --- /dev/null +++ b/crates/nera-fmt/tests/binary_operand_grouping.rs @@ -0,0 +1,82 @@ +use nera_fmt::{config::FormatConfig, format_source}; +use nera_syntax::{ast::Expr, parser::Parser}; + +#[test] +fn short_circuit_binding_fixtures_preserve_rhs_matches() { + // The unsafe-binding compiler fixtures exposed lost parentheses in CI. + // Formatting must retain their syntax and the match inside the operand. + for source in [ + include_str!("../../../tests/andalso_unsafe_binding.erl"), + include_str!("../../../tests/orelse_unsafe_binding.erl"), + ] { + let formatted = format_source(source, &FormatConfig::default()).unwrap(); + let parsed = Parser::new(&formatted).parse_module(); + assert!(!parsed.has_errors(), "{:?}\n{formatted}", parsed.errors); + let Expr::Match(_, value, _) = &parsed.module.functions[0].clauses[0].body[0] else { + panic!("lost outer match: {formatted}"); + }; + let Expr::BinOp(_, _, rhs, _) = value.as_ref() else { + panic!("lost short-circuit expression: {formatted}"); + }; + assert!(matches!(rhs.as_ref(), Expr::Match(..)), "{formatted}"); + assert_eq!( + formatted, + format_source(&formatted, &FormatConfig::default()).unwrap() + ); + } +} + +#[test] +fn weak_operands_keep_their_binary_operator_boundary() { + // The shared binary-child formatter handles both operands and flattened + // chains. Match, send and catch all bind more weakly than binary operators. + for (expression, left) in [ + ("(Y = A) andalso B", true), + ("A andalso (Y = B)", false), + ("(Y = A) andalso B andalso true", true), + ("A andalso B andalso (Y = true)", false), + ("(Pid ! true) orelse A", true), + ("A orelse (Pid ! true)", false), + ("(catch A) orelse B", true), + ("A orelse (catch B)", false), + ] { + let source = format!("-module(test). f(A, B, Pid) -> {expression}."); + let original = Parser::new(&source).parse_module(); + assert!(!original.has_errors(), "{:?}", original.errors); + let formatted = format_source(&source, &FormatConfig::default()).unwrap(); + let parsed = Parser::new(&formatted).parse_module(); + assert!(!parsed.has_errors(), "{:?}\n{formatted}", parsed.errors); + let before = &original.module.functions[0].clauses[0].body[0]; + let after = &parsed.module.functions[0].clauses[0].body[0]; + let ( + Expr::BinOp(before_op, before_lhs, before_rhs, _), + Expr::BinOp(after_op, after_lhs, after_rhs, _), + ) = (before, after) + else { + panic!("lost binary operator boundary: {formatted}"); + }; + assert_eq!(before_op, after_op); + let (mut before_child, mut after_child) = if left { + (before_lhs.as_ref(), after_lhs.as_ref()) + } else { + (before_rhs.as_ref(), after_rhs.as_ref()) + }; + if left { + while let Expr::BinOp(_, lhs, _, _) = before_child { + before_child = lhs; + } + while let Expr::BinOp(_, lhs, _, _) = after_child { + after_child = lhs; + } + } + assert_eq!( + std::mem::discriminant(before_child), + std::mem::discriminant(after_child), + "changed operand kind: {formatted}" + ); + assert_eq!( + formatted, + format_source(&formatted, &FormatConfig::default()).unwrap() + ); + } +} diff --git a/crates/nera-ir/src/warnings/ast_walkers.rs b/crates/nera-ir/src/warnings/ast_walkers.rs index 24bd1ab..38a8544 100644 --- a/crates/nera-ir/src/warnings/ast_walkers.rs +++ b/crates/nera-ir/src/warnings/ast_walkers.rs @@ -341,28 +341,7 @@ fn collect_refs(expr: &ast::Expr, refs: &mut HashSet, scoping: RefScopin ast::Expr::ListComp(body, qualifiers, _) | ast::Expr::BinComp(body, qualifiers, _) if scoping == RefScoping::Lexical => { - // Generator patterns bind fresh names for later qualifiers and the - // body. A generator's source and pattern inputs are read before - // its own pattern binds. - let mut fresh = HashSet::new(); - let mut local = HashSet::new(); - for q in qualifiers { - match q { - ast::LcQualifier::Generator(pat, expr, _) - | ast::LcQualifier::BinGenerator(pat, expr, _) => { - collect_pattern_refs(pat, &mut local); - collect_refs(expr, &mut local, scoping); - refs.extend(local.drain().filter(|id| !fresh.contains(id))); - collect_pattern_bindings(pat, &mut fresh); - } - ast::LcQualifier::Filter(expr, _) => { - collect_refs(expr, &mut local, scoping); - refs.extend(local.drain().filter(|id| !fresh.contains(id))); - } - } - } - collect_refs(body, &mut local, scoping); - refs.extend(local.difference(&fresh).copied()); + collect_comprehension_refs(body, qualifiers, refs); } ast::Expr::ListComp(body, qualifiers, _) | ast::Expr::BinComp(body, qualifiers, _) => { collect_refs(body, refs, scoping); @@ -406,6 +385,15 @@ fn collect_refs(expr: &ast::Expr, refs: &mut HashSet, scoping: RefScopin } } for clause in catch_clauses { + if scoping == RefScoping::Lexical { + if let ast::CatchClass::Variable(name, _) = clause.class { + refs.insert(name); + } + if let Some(stacktrace) = &clause.stacktrace { + collect_pattern_bindings(stacktrace, refs); + collect_pattern_refs(stacktrace, refs); + } + } collect_pattern_bindings(&clause.pattern, refs); collect_pattern_refs(&clause.pattern, refs); if let Some(ref guard) = clause.guard { @@ -469,6 +457,9 @@ fn collect_refs(expr: &ast::Expr, refs: &mut HashSet, scoping: RefScopin match item { ast::MaybeExpr::Expr(e) => collect_refs(e, refs, scoping), ast::MaybeExpr::CondMatch(pat, e, _) => { + if scoping == RefScoping::Lexical { + collect_pattern_bindings(pat, refs); + } collect_pattern_refs(pat, refs); collect_refs(e, refs, scoping); } @@ -517,6 +508,37 @@ fn collect_refs(expr: &ast::Expr, refs: &mut HashSet, scoping: RefScopin } } +/// Reads supplied before these qualifiers, respecting later generator +/// bindings. Unsafe-name checks use the same lexical rules as captures. +pub(super) fn collect_comprehension_refs( + body: &ast::Expr, + qualifiers: &[ast::LcQualifier], + refs: &mut HashSet, +) { + // Generator patterns bind fresh names for later qualifiers and the + // body. A generator's source and pattern inputs are read before + // its own pattern binds. + let mut fresh = HashSet::new(); + let mut local = HashSet::new(); + for q in qualifiers { + match q { + ast::LcQualifier::Generator(pat, expr, _) + | ast::LcQualifier::BinGenerator(pat, expr, _) => { + collect_pattern_refs(pat, &mut local); + collect_enclosing_refs(expr, &mut local); + refs.extend(local.drain().filter(|id| !fresh.contains(id))); + collect_pattern_bindings(pat, &mut fresh); + } + ast::LcQualifier::Filter(expr, _) => { + collect_enclosing_refs(expr, &mut local); + refs.extend(local.drain().filter(|id| !fresh.contains(id))); + } + } + } + collect_enclosing_refs(body, &mut local); + refs.extend(local.difference(&fresh).copied()); +} + /// Collect variable references in a guard. pub(crate) fn collect_guard_refs( guard: &ast::Guard, diff --git a/crates/nera-ir/src/warnings/mod.rs b/crates/nera-ir/src/warnings/mod.rs index 3dfda1c..0336cb2 100644 --- a/crates/nera-ir/src/warnings/mod.rs +++ b/crates/nera-ir/src/warnings/mod.rs @@ -124,6 +124,7 @@ pub fn analyze_module_with_source( unused_funcs::check_unused_functions(module, atom_names, &mut warnings); binary_segments::check_binary_segments_records(module, &mut warnings); unbound_vars::check_unbound_variables_records(module, atom_names, &mut warnings); + unsafe_vars::check_unsafe_variables_records(module, atom_names, &mut warnings); unused_records::check_unused_records(module, atom_names, &mut warnings); unused_types::check_unused_types(module, atom_names, &mut warnings); undefined_exports::check_undefined_exports(module, atom_names, &mut warnings); diff --git a/crates/nera-ir/src/warnings/tests.rs b/crates/nera-ir/src/warnings/tests.rs index 46711af..6fbdad3 100644 --- a/crates/nera-ir/src/warnings/tests.rs +++ b/crates/nera-ir/src/warnings/tests.rs @@ -1507,14 +1507,39 @@ fn comprehension_filter_bindings_reach_scope_sensitive_diagnostics() { #[test] fn comprehension_filters_do_not_export_local_or_partial_bindings() { + // Names local to the comprehension or a generator source stay unbound + // outside that scope; partial filter bindings instead have an unsafe origin. for expr in [ "[N || X <- [1], begin N = X, true end, _ <- [ok]], N", - "[N || X <- [1], case X of 1 -> N = 1, true; _ -> true end, _ <- [ok]]", - "[N || X <- [1], X > 0 andalso begin N = 1, true end, _ <- [ok]]", "[N || _ <- begin N = 1, [ok] end]", ] { assert_unbound_pattern_input( - &format!("-module(test). -export([f/0]). f() -> {expr}."), "N", + &format!("-module(test). -export([f/0]). f() -> {expr}."), + "N", + ); + } + // erlc identifies these as unsafe, not merely unbound. Do not pin the + // total error count: unbound analysis may also identify the later read. + for (expr, construct) in [ + ( + "[N || X <- [1], case X of 1 -> N = 1, true; _ -> true end, _ <- [ok]]", + "case", + ), + ( + "[N || X <- [1], X > 0 andalso begin N = 1, true end, _ <- [ok]]", + "andalso", + ), + ] { + let source = format!("-module(test). -export([f/0]). f() -> {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 + && d.code.as_deref() == Some("unsafe_variable") + && d.message == format!("variable 'N' unsafe in '{construct}'") + }), + "{source}\n{diagnostics:?}" ); } } @@ -1837,6 +1862,357 @@ fn deprecated_repeated_attributes_preserve_arity_and_spans() { // ── Unsafe variable tests ──────────────────────────────── +#[test] +fn short_circuit_rhs_bindings_are_unsafe() { + // Compiler parity: OTP rejects later reads of names bound only in the + // operand that andalso/orelse may skip, including nested bindings. + for (operator, fixture) in [ + ( + "andalso", + include_str!("../../../../tests/andalso_unsafe_binding.erl"), + ), + ( + "orelse", + include_str!("../../../../tests/orelse_unsafe_binding.erl"), + ), + ] { + for binding in [ + "Y = true", + "begin Y = true, Y end", + "id(false) orelse (Y = true)", + "case id(true) of true -> Y = true; false -> ok end", + ] { + for wrapper in [ + "_ = EXPR", + "EXPR", + "Y = EXPR", + "id(EXPR)", + "{EXPR}", + "begin EXPR end", + ] { + let expression = + wrapper.replace("EXPR", &format!("id(true) {operator} ({binding})")); + let source = fixture.replace( + if operator == "andalso" { + "_ = id(true) andalso (Y = true)" + } else { + "_ = id(false) orelse (Y = true)" + }, + &expression, + ); + let (module, names) = parse_module(&source); + let diagnostics = analyze_module(&module, &names, None); + assert!( + diagnostics.iter().any(|d| { + d.code.as_deref() == Some("unsafe_variable") + && d.severity == nera_syntax::error::Severity::Error + && d.message == format!("variable 'Y' unsafe in '{operator}'") + }), + "{source}\n{diagnostics:?}" + ); + } + } + } +} + +#[test] +fn short_circuit_unsafe_reads_include_captures_and_named_underscores() { + // OTP applies unsafe-variable errors to captured reads and named + // underscore variables, not just a direct read in the enclosing body. + for operator in ["andalso", "orelse"] { + for name in ["Y", "_Y"] { + for later in [ + name.to_string(), + format!("F = fun() -> {name} end, F()"), + format!("[{name} || _ <- [true]]"), + ] { + let source = format!( + "-module(test). -export([f/0]). + f() -> _ = id(false) {operator} ({name} = true), {later}. + id(V) -> V." + ); + let (module, names) = parse_module(&source); + let diagnostics = analyze_module(&module, &names, None); + assert!( + diagnostics.iter().any(|d| { + d.code.as_deref() == Some("unsafe_variable") + && d.severity == nera_syntax::error::Severity::Error + && d.message == format!("variable '{name}' unsafe in '{operator}'") + }), + "{source}\n{diagnostics:?}" + ); + } + } + } +} + +#[test] +fn short_circuit_pattern_consumers_are_unsafe_without_a_later_read() { + // OTP rejects the enclosing consumer itself, even when no later body + // expression reads the name. The pattern must not rebind an unsafe name. + for operator in ["andalso", "orelse"] { + for consumer in [ + "Y = EXPR", + "{Y} = {EXPR}", + "case EXPR of Y -> ok end", + "try EXPR of Y -> ok catch _:_ -> ok end", + "maybe Y ?= EXPR end", + ] { + let body = consumer.replace("EXPR", &format!("Flag {operator} (Y = true)")); + let source = format!("-module(test). -export([f/1]). f(Flag) -> {body}."); + let (module, names) = parse_module(&source); + let diagnostics = analyze_module(&module, &names, None); + assert!( + diagnostics.iter().any(|d| { + d.code.as_deref() == Some("unsafe_variable") + && d.severity == nera_syntax::error::Severity::Error + && d.message == format!("variable 'Y' unsafe in '{operator}'") + }), + "{source}\n{diagnostics:?}" + ); + } + } +} + +#[test] +fn short_circuit_nested_scopes_keep_definite_inputs() { + // Review regression: each Y is already bound at the nested operator. + // OTP accepts these programs; matching Y cannot introduce an unsafe name. + for operator in ["andalso", "orelse"] { + for context in [ + "case (Y = true) of true -> INNER end", + "try Y = true of true -> INNER catch _:_ -> ok end", + "(Y = true) andalso begin INNER end", + "[Y || begin Y = true, Y end, begin INNER end]", + "maybe Y = true, INNER end", + "maybe begin Y = true end, INNER end", + "maybe true ?= begin Y = true end, INNER end", + "F = fun Y() -> INNER end, F", + "try error(reason) catch Y:_ -> INNER end", + ] { + let body = context.replace("INNER", &format!("_ = Flag {operator} (Y = true), Y")); + let source = format!("-module(test). -export([f/1]). f(Flag) -> {body}."); + 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 short_circuit_case_scrutinee_exports_remain_safe() { + for wrapper in ["EXPR", "_ = EXPR", "{EXPR}", "id(EXPR)", "Y = EXPR"] { + let body = wrapper.replace( + "EXPR", + "case (Y = true) of true -> Y = true; false -> ok end", + ); + let source = format!("-module(test). -export([f/0]). f() -> {body}, Y. id(V) -> V."); + 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 short_circuit_local_sequences_check_conditional_names() { + for operator in ["andalso", "orelse"] { + for body in [ + "maybe _ = EXPR, Y = false end", + "maybe _ = EXPR, Y ?= false end", + "maybe _ ?= EXPR, Y = false end", + "[ok || begin _ = EXPR, true end, begin Y = false, true end]", + "[Y || begin _ = EXPR, true end]", + "[ok || begin _ = EXPR, true end, _ <- Y]", + "<< <<0>> || begin _ = EXPR, true end, begin Y = false, true end >>", + "<< <> || begin _ = EXPR, true end >>", + ] { + let body = body.replace("EXPR", &format!("Flag {operator} (Y = true)")); + let source = format!("-module(test). -export([f/1]). f(Flag) -> {body}."); + let (module, names) = parse_module(&source); + let diagnostics = analyze_module(&module, &names, None); + assert!( + diagnostics + .iter() + .any(|d| d.code.as_deref() == Some("unsafe_variable") + && d.message == format!("variable 'Y' unsafe in '{operator}'")), + "{source}\n{diagnostics:?}" + ); + } + for body in [ + "[Y || begin _ = EXPR, true end, Y <- [true]]", + "<< <> || begin _ = EXPR, true end, <> <= <<1>> >>", + "[ok || _ <- begin _ = EXPR, [ok] end, begin Y = false, true end]", + "[Y || _ <- [ok], begin Y = true, Y end, begin _ = EXPR, Y end]", + ] { + let body = body.replace("EXPR", &format!("Flag {operator} (Y = true)")); + let source = format!("-module(test). -export([f/1]). f(Flag) -> {body}."); + 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 short_circuit_unsafe_entrypoints_are_visited() { + for operator in ["andalso", "orelse"] { + for source in [ + format!("-module(test). -export([f/1]). f(Flag) -> (fun() -> Y = Flag {operator} (Y = true) end)()."), + format!("-module(test). -export([f/1]). f(Flag) -> receive after begin Y = Flag {operator} (Y = true), 0 end -> ok end."), + format!("-module(test). -record(state, {{field = fun() -> Y = true {operator} (Y = true) end}})."), + ] { + let (module, names) = parse_module(&source); + let diagnostics = analyze_module(&module, &names, None); + assert!(diagnostics.iter().any(|d| d.code.as_deref() == Some("unsafe_variable") + && d.message == format!("variable 'Y' unsafe in '{operator}'")), "{source}\n{diagnostics:?}"); + } + } +} + +#[test] +fn short_circuit_consumers_check_conditional_left_names() { + // Unlike strict operators, a short-circuit right operand consumes names + // from its left operand. OTP rejects these reads and match assertions. + for first in ["andalso", "orelse"] { + for consumer in ["andalso", "orelse"] { + for rhs in ["Y = false", "(fun() -> Y end)()", "begin Y = false, Y end"] { + let source = format!( + "-module(test). -export([f/1]). + f(Flag) -> (Flag {first} (Y = true)) {consumer} ({rhs})." + ); + let (module, names) = parse_module(&source); + let diagnostics = analyze_module(&module, &names, None); + assert!( + diagnostics + .iter() + .any(|d| d.code.as_deref() == Some("unsafe_variable") + && d.message == format!("variable 'Y' unsafe in '{first}'")), + "{source}\n{diagnostics:?}" + ); + } + for rhs in [ + "begin F = fun(Y) -> Y end, F(false) end", + "[Y || Y <- [false]]", + ] { + let source = format!( + "-module(test). -export([f/1]). + f(Flag) -> (Flag {first} (Y = true)) {consumer} ({rhs})." + ); + 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 consumer in ["and", "or", "=="] { + let source = format!( + "-module(test). -export([f/1]). + f(Flag) -> (Flag {first} (Y = true)) {consumer} (Y = false)." + ); + 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 short_circuit_pattern_consumers_include_maybe_and_catch_headers() { + // OTP treats these headers as consumers of an existing conditional name, + // not as fresh bindings. The same rule holds in a chained operand. + for operator in ["andalso", "orelse"] { + for consumer in [ + "maybe Y ?= false end", + "try error(reason) catch Y:_ -> ok end", + "try error(reason) catch _:Y -> ok end", + "try error(reason) catch _:_:Y -> ok end", + ] { + for body in [ + format!("_ = Flag {operator} (Y = true), {consumer}"), + format!("(Flag {operator} (Y = true)) orelse ({consumer})"), + ] { + let source = format!("-module(test). -export([f/1]). f(Flag) -> {body}."); + let (module, names) = parse_module(&source); + let diagnostics = analyze_module(&module, &names, None); + assert!( + diagnostics + .iter() + .any(|d| d.code.as_deref() == Some("unsafe_variable") + && d.message == format!("variable 'Y' unsafe in '{operator}'")), + "{source}\n{diagnostics:?}" + ); + } + } + for consumer in [ + "F = fun(Y) -> maybe Y ?= false end end, F(false)", + "[maybe Y ?= false end || Y <- [false]]", + ] { + let source = format!( + "-module(test). -export([f/1]). + f(Flag) -> _ = Flag {operator} (Y = true), {consumer}." + ); + 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 short_circuit_safe_bindings_remain_usable() { + // OTP accepts prebound names, definite left-operand bindings, local RHS + // reads, and later fun/generator bindings that shadow the unsafe name. + for operator in ["andalso", "orelse"] { + for body in [ + format!("Y = id(true), _ = id(true) {operator} (Y = true), Y"), + format!("_ = (Y = id(true)) {operator} true, Y"), + format!("_ = id(true) {operator} begin Y = true, Y end, ok"), + format!("_ = id(true) {operator} (Y = true), F = fun(Y) -> Y end, F(true)"), + format!("_ = id(true) {operator} (Y = true), [Y || Y <- [true]]"), + format!("_ = id(true) {operator} (F = fun() -> Y = true end), Y = true, Y"), + ] { + let source = format!("-module(test). -export([f/0]). f() -> {body}. id(V) -> V."); + 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 unsafe_var_in_case_some_branches() { // Y is bound in the first branch but not the second — using Y after diff --git a/crates/nera-ir/src/warnings/unbound_vars.rs b/crates/nera-ir/src/warnings/unbound_vars.rs index cda8fd3..39b13ce 100644 --- a/crates/nera-ir/src/warnings/unbound_vars.rs +++ b/crates/nera-ir/src/warnings/unbound_vars.rs @@ -109,6 +109,13 @@ pub(crate) fn collect_exported_bindings(expr: &ast::Expr, scope: &mut HashSet) { + collect_bindings(expr, bindings, Paths::Any); +} + /// Collect the names `expr` binds on the given paths. Funs and comprehensions /// keep their bindings local on every path. fn collect_bindings(expr: &ast::Expr, scope: &mut HashSet, paths: Paths) { @@ -534,6 +541,12 @@ fn check_unbound_in_expr( check_unbound_in_expr(e, scope, context, atom_names, diagnostics); } } + ast::Expr::BinOp(ast::BinOp::Andalso | ast::BinOp::Orelse, lhs, rhs, _) => { + check_unbound_in_expr(lhs, scope, context, atom_names, diagnostics); + let mut input_scope = scope.clone(); + collect_exported_bindings(lhs, &mut input_scope); + check_unbound_in_expr(rhs, &input_scope, context, atom_names, diagnostics); + } ast::Expr::Cons(h, t, _) | ast::Expr::Send(h, t, _) | ast::Expr::BinOp(_, h, t, _) => { check_unbound_in_expr(h, scope, context, atom_names, diagnostics); check_unbound_in_expr(t, scope, context, atom_names, diagnostics); diff --git a/crates/nera-ir/src/warnings/unsafe_vars.rs b/crates/nera-ir/src/warnings/unsafe_vars.rs index d627d5c..ebdd75c 100644 --- a/crates/nera-ir/src/warnings/unsafe_vars.rs +++ b/crates/nera-ir/src/warnings/unsafe_vars.rs @@ -1,7 +1,8 @@ //! Unsafe variable detection. //! //! In Erlang, a variable is "unsafe" when it is bound inside some but not all -//! branches of a branching expression (`case`, `if`, `receive`, `try`) and is +//! branches of a branching expression (`case`, `if`, `receive`, `try`, +//! `andalso`, `orelse`) and is //! then referenced after the expression. OTP's `erl_lint` emits: //! //! ```text @@ -22,8 +23,11 @@ use nera_syntax::ast::{self, AtomId}; use nera_syntax::error::Diagnostic; use nera_syntax::span::Span; -use super::ast_walkers::{collect_enclosing_refs, collect_pattern_bindings}; -use super::unbound_vars::collect_exported_bindings; +use super::ast_walkers::{ + collect_comprehension_refs, collect_enclosing_refs, collect_guard_refs, collect_pattern_bindings, + collect_pattern_refs, +}; +use super::unbound_vars::{collect_exported_bindings, collect_possible_bindings}; /// Check a function for unsafe variable references. pub(super) fn check_unsafe_variables_fn( @@ -40,6 +44,21 @@ pub(super) fn check_unsafe_variables_fn( } } +/// Record defaults have their own scope, including defaults never constructed. +pub(super) fn check_unsafe_variables_records( + module: &ast::Module, + atom_names: &HashMap, + diagnostics: &mut Vec, +) { + for record in &module.records { + for field in &record.fields { + if let Some(default) = &field.default { + check_unsafe_in_subexprs(default, &HashSet::new(), atom_names, diagnostics); + } + } + } +} + /// Walk a body (sequence of expressions) looking for branching constructs /// that introduce unsafe variables used in subsequent expressions. fn check_unsafe_in_body( @@ -51,11 +70,8 @@ fn check_unsafe_in_body( let mut scope = outer_scope.clone(); for (i, expr) in body.iter().enumerate() { - // For match expressions, add the bound variables to the scope. - if let ast::Expr::Match(pat, _rhs, _) = expr { - collect_pattern_bindings(pat, &mut scope); - } - + // Inspect the expression before adding its exports: a match pattern + // does not make bindings in its own RHS safe. // Collect unsafe vars introduced by this expression. let unsafe_vars = collect_unsafe_from_expr(expr, &scope); @@ -68,31 +84,7 @@ fn check_unsafe_in_body( collect_enclosing_refs(later_expr, &mut later_refs); } - for (var_id, construct, construct_span) in &unsafe_vars { - if later_refs.contains(var_id) { - let name = match atom_names.get(var_id) { - Some(s) => s.as_str(), - None => continue, - }; - // Skip underscore-prefixed variables. - if name == "_" || name.starts_with('_') { - continue; - } - diagnostics.push( - Diagnostic::error_at( - format!("variable '{}' unsafe in '{}'", name, construct), - *construct_span, - ) - .with_code("unsafe_variable".to_string()) - .with_label(format!("'{}' is bound in some branches but not all", name)) - .with_hint(format!( - "variable '{}' is bound inside a '{}' expression but not in every branch, \ - so it may not have a value when used after the '{}'", - name, construct, construct - )), - ); - } - } + report_unsafe_refs(&unsafe_vars, &later_refs, atom_names, diagnostics); } // Recurse into sub-expressions to find nested unsafe patterns. @@ -103,6 +95,56 @@ fn check_unsafe_in_body( } } +/// Check names consumed after an expression, whether by a later body +/// expression or by an enclosing pattern/branch in the same expression. +fn report_unsafe_refs( + unsafe_vars: &[(AtomId, &'static str, Span)], + refs: &HashSet, + atom_names: &HashMap, + diagnostics: &mut Vec, +) { + for (var_id, construct, construct_span) in unsafe_vars { + if refs.contains(var_id) { + let name = match atom_names.get(var_id) { + Some(s) => s.as_str(), + None => continue, + }; + // A named underscore variable is still unsafe after a + // skipped operand; only bare `_` discards the binding. + if name == "_" || (name.starts_with('_') && !matches!(*construct, "andalso" | "orelse")) + { + continue; + } + diagnostics.push( + Diagnostic::error_at( + format!("variable '{}' unsafe in '{}'", name, construct), + *construct_span, + ) + .with_code("unsafe_variable".to_string()) + .with_label(format!("'{}' is bound in some branches but not all", name)) + .with_hint(format!( + "variable '{}' is bound inside a '{}' expression but not in every branch, \ + so it may not have a value when used after the '{}'", + name, construct, construct + )), + ); + } + } +} + +fn collect_clause_refs(clauses: &[ast::CaseClause], refs: &mut HashSet) { + for clause in clauses { + collect_pattern_bindings(&clause.pattern, refs); + collect_pattern_refs(&clause.pattern, refs); + if let Some(guard) = &clause.guard { + collect_guard_refs(guard, refs); + } + for expr in &clause.body { + collect_enclosing_refs(expr, refs); + } + } +} + /// Collect variables that are unsafe coming out of a branching expression. /// /// Returns (AtomId, construct_name, construct_span) for each variable that @@ -112,18 +154,128 @@ fn collect_unsafe_from_expr( scope: &HashSet, ) -> Vec<(AtomId, &'static str, Span)> { match expr { - ast::Expr::Case(_scrutinee, clauses, span) => { + ast::Expr::BinOp(op @ (ast::BinOp::Andalso | ast::BinOp::Orelse), lhs, rhs, span) => { + let construct = if *op == ast::BinOp::Andalso { + "andalso" + } else { + "orelse" + }; + let mut input_scope = scope.clone(); + collect_exported_bindings(lhs, &mut input_scope); + let mut bindings = HashSet::new(); + collect_possible_bindings(rhs, &mut bindings); + let mut unsafe_vars = collect_unsafe_from_expr(lhs, scope); + unsafe_vars.extend( + bindings + .difference(&input_scope) + .map(|id| (*id, construct, *span)), + ); + unsafe_vars + } + + // These wrappers do not hide a conditional binding from the following + // body expressions, e.g. `_ = Flag andalso (Y = true), Y`. + ast::Expr::Match(_, rhs, _) + | ast::Expr::UnaryOp(_, rhs, _) + | ast::Expr::Throw(rhs, _) + | ast::Expr::RecordAccess { base: rhs, .. } => collect_unsafe_from_expr(rhs, scope), + ast::Expr::Tuple(exprs, _) => exprs + .iter() + .flat_map(|expr| collect_unsafe_from_expr(expr, scope)) + .collect(), + ast::Expr::Block(exprs, _) => { + let mut block_scope = scope.clone(); + let mut unsafe_vars = Vec::new(); + for expr in exprs { + unsafe_vars.extend(collect_unsafe_from_expr(expr, &block_scope)); + collect_exported_bindings(expr, &mut block_scope); + } + unsafe_vars + } + ast::Expr::Cons(lhs, rhs, _) + | ast::Expr::Send(lhs, rhs, _) + | ast::Expr::BinOp(_, lhs, rhs, _) => { + let mut unsafe_vars = collect_unsafe_from_expr(lhs, scope); + unsafe_vars.extend(collect_unsafe_from_expr(rhs, scope)); + unsafe_vars + } + ast::Expr::Binary(segments, _) => segments + .iter() + .flat_map(|segment| { + let mut unsafe_vars = collect_unsafe_from_expr(&segment.value, scope); + if let Some(size) = &segment.size { + unsafe_vars.extend(collect_unsafe_from_expr(size, scope)); + } + unsafe_vars + }) + .collect(), + ast::Expr::Map(fields, _) => fields + .iter() + .flat_map(|field| { + let mut unsafe_vars = collect_unsafe_from_expr(&field.key, scope); + unsafe_vars.extend(collect_unsafe_from_expr(&field.value, scope)); + unsafe_vars + }) + .collect(), + ast::Expr::MapUpdate(base, fields, _) => { + let mut unsafe_vars = collect_unsafe_from_expr(base, scope); + for field in fields { + unsafe_vars.extend(collect_unsafe_from_expr(&field.key, scope)); + unsafe_vars.extend(collect_unsafe_from_expr(&field.value, scope)); + } + unsafe_vars + } + ast::Expr::RecordCreate { fields, .. } => fields + .iter() + .flat_map(|field| collect_unsafe_from_expr(&field.value, scope)) + .collect(), + ast::Expr::RecordUpdate { base, fields, .. } => { + let mut unsafe_vars = collect_unsafe_from_expr(base, scope); + for field in fields { + unsafe_vars.extend(collect_unsafe_from_expr(&field.value, scope)); + } + unsafe_vars + } + ast::Expr::FunRef(ast::FunRefTarget::DynamicRemote(module, function, arity), _) => { + let mut unsafe_vars = collect_unsafe_from_expr(module, scope); + unsafe_vars.extend(collect_unsafe_from_expr(function, scope)); + unsafe_vars.extend(collect_unsafe_from_expr(arity, scope)); + unsafe_vars + } + ast::Expr::Call(target, args, _) => { + let mut unsafe_vars = Vec::new(); + match target { + ast::CallTarget::Remote(module, function) => { + unsafe_vars.extend(collect_unsafe_from_expr(module, scope)); + unsafe_vars.extend(collect_unsafe_from_expr(function, scope)); + } + ast::CallTarget::Expr(callee) => { + unsafe_vars.extend(collect_unsafe_from_expr(callee, scope)); + } + _ => {} + } + for arg in args { + unsafe_vars.extend(collect_unsafe_from_expr(arg, scope)); + } + unsafe_vars + } + + ast::Expr::Case(scrutinee, clauses, span) => { + let mut input_scope = scope.clone(); + collect_exported_bindings(scrutinee, &mut input_scope); let branch_bindings: Vec> = clauses .iter() .map(|clause| { let mut binds = HashSet::new(); collect_pattern_bindings(&clause.pattern, &mut binds); collect_body_bindings(&clause.body, &mut binds); - // Only include variables not already in scope. - binds.difference(scope).copied().collect() + // The scrutinee completes before any branch runs. + binds.difference(&input_scope).copied().collect() }) .collect(); - find_unsafe_vars(&branch_bindings, "case", *span) + let mut unsafe_vars = collect_unsafe_from_expr(scrutinee, scope); + unsafe_vars.extend(find_unsafe_vars(&branch_bindings, "case", *span)); + unsafe_vars } ast::Expr::If(clauses, span) => { @@ -250,9 +402,10 @@ fn check_unsafe_in_subexprs( diagnostics: &mut Vec, ) { match expr { - ast::Expr::Fun(_name, clauses, _) => { + ast::Expr::Fun(name, clauses, _) => { for clause in clauses { let mut fun_scope = scope.clone(); + fun_scope.extend(name); for pat in &clause.patterns { collect_pattern_bindings(pat, &mut fun_scope); } @@ -262,8 +415,18 @@ fn check_unsafe_in_subexprs( ast::Expr::Case(scrutinee, clauses, _) => { check_unsafe_in_subexprs(scrutinee, scope, atom_names, diagnostics); + let mut refs = HashSet::new(); + collect_clause_refs(clauses, &mut refs); + report_unsafe_refs( + &collect_unsafe_from_expr(scrutinee, scope), + &refs, + atom_names, + diagnostics, + ); + let mut input_scope = scope.clone(); + collect_exported_bindings(scrutinee, &mut input_scope); for clause in clauses { - let mut clause_scope = scope.clone(); + let mut clause_scope = input_scope.clone(); collect_pattern_bindings(&clause.pattern, &mut clause_scope); check_unsafe_in_body(&clause.body, &clause_scope, atom_names, diagnostics); } @@ -282,6 +445,7 @@ fn check_unsafe_in_subexprs( check_unsafe_in_body(&clause.body, &clause_scope, atom_names, diagnostics); } if let Some(after) = after { + check_unsafe_in_subexprs(&after.timeout, scope, atom_names, diagnostics); check_unsafe_in_body(&after.body, scope, atom_names, diagnostics); } } @@ -294,13 +458,28 @@ fn check_unsafe_in_subexprs( .. } => { check_unsafe_in_body(body, scope, atom_names, diagnostics); + let mut refs = HashSet::new(); + collect_clause_refs(of_clauses, &mut refs); + let mut input_scope = scope.clone(); + for expr in body { + report_unsafe_refs( + &collect_unsafe_from_expr(expr, &input_scope), + &refs, + atom_names, + diagnostics, + ); + collect_exported_bindings(expr, &mut input_scope); + } for clause in of_clauses { - let mut clause_scope = scope.clone(); + let mut clause_scope = input_scope.clone(); collect_pattern_bindings(&clause.pattern, &mut clause_scope); check_unsafe_in_body(&clause.body, &clause_scope, atom_names, diagnostics); } for clause in catch_clauses { let mut clause_scope = scope.clone(); + if let ast::CatchClass::Variable(name, _) = clause.class { + clause_scope.insert(name); + } collect_pattern_bindings(&clause.pattern, &mut clause_scope); if let Some(ref stk) = clause.stacktrace { collect_pattern_bindings(stk, &mut clause_scope); @@ -315,7 +494,7 @@ fn check_unsafe_in_subexprs( ast::Expr::ListComp(body_expr, qualifiers, _) | ast::Expr::BinComp(body_expr, qualifiers, _) => { let mut lc_scope = scope.clone(); - for q in qualifiers { + for (i, q) in qualifiers.iter().enumerate() { match q { ast::LcQualifier::Generator(pat, source_expr, _) | ast::LcQualifier::BinGenerator(pat, source_expr, _) => { @@ -324,6 +503,14 @@ fn check_unsafe_in_subexprs( } ast::LcQualifier::Filter(filter_expr, _) => { check_unsafe_in_subexprs(filter_expr, &lc_scope, atom_names, diagnostics); + let mut refs = HashSet::new(); + collect_comprehension_refs(body_expr, &qualifiers[i + 1..], &mut refs); + report_unsafe_refs( + &collect_unsafe_from_expr(filter_expr, &lc_scope), + &refs, + atom_names, + diagnostics, + ); collect_exported_bindings(filter_expr, &mut lc_scope); } } @@ -333,16 +520,44 @@ fn check_unsafe_in_subexprs( ast::Expr::Maybe { body, else_clauses, .. } => { let mut maybe_scope = scope.clone(); - for item in body { + for (i, item) in body.iter().enumerate() { + let expr = match item { + ast::MaybeExpr::Expr(expr) | ast::MaybeExpr::CondMatch(_, expr, _) => expr, + }; + let mut refs = HashSet::new(); + for later in &body[i + 1..] { + match later { + ast::MaybeExpr::Expr(expr) => collect_enclosing_refs(expr, &mut refs), + ast::MaybeExpr::CondMatch(pat, expr, _) => { + collect_pattern_bindings(pat, &mut refs); + collect_pattern_refs(pat, &mut refs); + collect_enclosing_refs(expr, &mut refs); + } + } + } + report_unsafe_refs( + &collect_unsafe_from_expr(expr, &maybe_scope), + &refs, + atom_names, + diagnostics, + ); match item { ast::MaybeExpr::Expr(e) => { - if let ast::Expr::Match(pat, _, _) = e { - collect_pattern_bindings(pat, &mut maybe_scope); - } check_unsafe_in_subexprs(e, &maybe_scope, atom_names, diagnostics); + collect_exported_bindings(e, &mut maybe_scope); } ast::MaybeExpr::CondMatch(pat, e, _) => { check_unsafe_in_subexprs(e, &maybe_scope, atom_names, diagnostics); + let mut refs = HashSet::new(); + collect_pattern_bindings(pat, &mut refs); + collect_pattern_refs(pat, &mut refs); + report_unsafe_refs( + &collect_unsafe_from_expr(e, &maybe_scope), + &refs, + atom_names, + diagnostics, + ); + collect_exported_bindings(e, &mut maybe_scope); collect_pattern_bindings(pat, &mut maybe_scope); } } @@ -359,14 +574,37 @@ fn check_unsafe_in_subexprs( } // Binary sub-expressions. - ast::Expr::Match(_, rhs, _) => { + ast::Expr::Match(pat, rhs, _) => { check_unsafe_in_subexprs(rhs, scope, atom_names, diagnostics); + let mut refs = HashSet::new(); + collect_pattern_bindings(pat, &mut refs); + collect_pattern_refs(pat, &mut refs); + report_unsafe_refs( + &collect_unsafe_from_expr(rhs, scope), + &refs, + atom_names, + diagnostics, + ); } ast::Expr::Tuple(exprs, _) => { for e in exprs { check_unsafe_in_subexprs(e, scope, atom_names, diagnostics); } } + ast::Expr::BinOp(ast::BinOp::Andalso | ast::BinOp::Orelse, lhs, rhs, _) => { + check_unsafe_in_subexprs(lhs, scope, atom_names, diagnostics); + let mut refs = HashSet::new(); + collect_enclosing_refs(rhs, &mut refs); + report_unsafe_refs( + &collect_unsafe_from_expr(lhs, scope), + &refs, + atom_names, + diagnostics, + ); + let mut input_scope = scope.clone(); + collect_exported_bindings(lhs, &mut input_scope); + check_unsafe_in_subexprs(rhs, &input_scope, atom_names, diagnostics); + } ast::Expr::Cons(h, t, _) | ast::Expr::Send(h, t, _) | ast::Expr::BinOp(_, h, t, _) => { check_unsafe_in_subexprs(h, scope, atom_names, diagnostics); check_unsafe_in_subexprs(t, scope, atom_names, diagnostics); @@ -375,9 +613,15 @@ fn check_unsafe_in_subexprs( check_unsafe_in_subexprs(e, scope, atom_names, diagnostics); } ast::Expr::Call(target, args, _) => { - if let ast::CallTarget::Remote(module_expr, fun_expr) = target { - check_unsafe_in_subexprs(module_expr, scope, atom_names, diagnostics); - check_unsafe_in_subexprs(fun_expr, scope, atom_names, diagnostics); + match target { + ast::CallTarget::Remote(module_expr, fun_expr) => { + check_unsafe_in_subexprs(module_expr, scope, atom_names, diagnostics); + check_unsafe_in_subexprs(fun_expr, scope, atom_names, diagnostics); + } + ast::CallTarget::Expr(callee) => { + check_unsafe_in_subexprs(callee, scope, atom_names, diagnostics); + } + _ => {} } for arg in args { check_unsafe_in_subexprs(arg, scope, atom_names, diagnostics); diff --git a/tests/README.md b/tests/README.md index 4121aa2..75eec10 100644 --- a/tests/README.md +++ b/tests/README.md @@ -516,6 +516,8 @@ committed catalogue with the pinned OTP abstract code. | Test File | Feature Category | Assertions | |--------------------------------|--------------------------------------------------------|-----------------| | `pattern_expr_warnings.erl` | Map-key and binary-size reads, including earlier segments in clause/body-match patterns; real unused-value controls; shared with warning analysis and run on BEAM | Exact diagnostics and completion | +| `andalso_unsafe_binding.erl` | Reject a later read of a name bound only in the right operand of `andalso`, with OTP's unsafe-variable diagnostic | (compile error) | +| `orelse_unsafe_binding.erl` | Reject a later read of a name bound only in the right operand of `orelse`, with OTP's unsafe-variable diagnostic | (compile error) | | `unresolved_call.erl` | Expected compile error for missing module | (compile error) | | `record_undefined_pattern.erl` | Expected compile error for an undefined record pattern | (compile error) | | `pattern_input_fun_key_unbound.erl` | Reject a map key supplied only by a sibling fun argument | (compile error) | diff --git a/tests/andalso_unsafe_binding.erl b/tests/andalso_unsafe_binding.erl new file mode 100644 index 0000000..6e8d1b1 --- /dev/null +++ b/tests/andalso_unsafe_binding.erl @@ -0,0 +1,9 @@ +%% EXPECT_COMPILE_ERROR: variable 'Y' unsafe in 'andalso' +-module(andalso_unsafe_binding). +-export([test/0]). + +test() -> + _ = id(true) andalso (Y = true), + Y. + +id(Value) -> Value. diff --git a/tests/orelse_unsafe_binding.erl b/tests/orelse_unsafe_binding.erl new file mode 100644 index 0000000..c574b91 --- /dev/null +++ b/tests/orelse_unsafe_binding.erl @@ -0,0 +1,9 @@ +%% EXPECT_COMPILE_ERROR: variable 'Y' unsafe in 'orelse' +-module(orelse_unsafe_binding). +-export([test/0]). + +test() -> + _ = id(false) orelse (Y = true), + Y. + +id(Value) -> Value.