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
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@ All notable milestones for the nera project are documented in this file.
using the same fixture natively and on BEAM. The existing compiler behavior
is unchanged.

## Record field defaults reject outer variables

- `nera check` and `nera build` reject variables in record field defaults with
`variable 'X' in record definition`, including bindings such as
`begin X = 1, X end` and bare variable defaults.
- Unused records and explicitly overridden defaults are checked too. Suppressing
warnings does not suppress these errors. Fun-local variables and comprehension
generator bindings remain valid.
- Warning-analysis and CLI regressions cover the diagnostic and both commands.

## Pattern inputs are checked before their own bindings

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

#[test]
fn record_defaults_reject_outer_variables() {
// Record defaults must reject variables even if the default is unused or
// overridden. These are real Erlang forms rejected by erlc, not just reads
// that happen to become unbound during lowering.
for (expr, name) in [
("X", "X"),
("begin X = 1, X end", "X"),
("begin _X = 1, _X end", "_X"),
("case 1 of X -> X end", "X"),
("receive X -> X after 0 -> ok end", "X"),
("try 1 of X -> X after ok end", "X"),
("try 1 catch C:R:S -> {C, R, S} end", "C"),
("try 1 catch _Class:_ -> ok end", "_Class"),
("maybe X ?= 1, X end", "X"),
("[begin Y = X, Y end || X <- [1]]", "Y"),
("{#{key => X}, <<X>>}", "X"),
("X()", "X"),
] {
for body in ["ok", "#r{}", "#r{value = ok}"] {
let source = format!(
"-module(test). -export([f/0]).
-record(r, {{value = {expr}}}). f() -> {body}."
);
let (module, names) = parse_module(&source);
let diagnostics = analyze_module(&module, &names, None);
let diagnostic = diagnostics.iter().find(|d| {
d.message == format!("variable '{name}' in record definition")
}).unwrap_or_else(|| panic!("{source}\n{diagnostics:?}"));
assert_eq!(diagnostic.severity, nera_syntax::error::Severity::Error);
let span = diagnostic.span.expect("record variable location");
assert_eq!(&source[span.start..span.end], name);
}
}
}

#[test]
fn record_defaults_keep_local_variables() {
// erlc permits fun-local bindings and comprehension generator bindings;
// rejecting every variable node would break legal record defaults.
for expr in [
"fun(X) -> X end",
"fun() -> X = 1, X end",
"fun Again(0) -> ok; Again(X) -> Again(X - 1) end",
"[X || X <- [1]]",
"<< <<X>> || <<X>> <= <<1>> >>",
"[X || X <- begin Y = [1], Y end]",
"begin _ = 1, ok end",
"try 1 catch _:_ -> ok end",
"try 1 catch _:_:_ -> ok end",
"{1, [ok], #{key => <<1>>}}",
] {
let source = format!(
"-module(test). -export([f/0]).
-record(r, {{value = {expr}}}). f() -> #r{{}}."
);
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 record_default_pattern_inputs_are_checked() {
// NERA-80: record defaults are the same expressions outside a function;
Expand Down
Loading
Loading