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: 10 additions & 1 deletion Docs/04-advanced-features/modules.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,16 @@ store line as banner # a bare name calls the zero-argument action
display line
```

All three forms work at the top level and inside your own action bodies. Because the analyzer does not read included files, it emits a **non-fatal** `Undefined action '<name>'` note for a name it cannot see statically — the program still runs and the action resolves at runtime.
All three forms work at the top level and inside your own action bodies.

Before a program runs (and with `wfl --analyze`), the CLI reads each literal `include from "path.wfl"` at the top level of the main file, plus the literal includes inside those files, to learn which actions they define. It does not run them. A call to one of those actions gets no warning when the include has already run at that point:

- In top-level code, the include comes before the call.
- In an action, container, or event-handler body, the include runs before any later statement that could run that body. Placing includes before the code that uses them satisfies both rules.

Otherwise the analyzer emits a **non-fatal** `Undefined action '<name>'` warning. The warning also stays when the analyzer cannot check the name: a dynamic path (`include from module_path`), an include inside a block, or a file it cannot read or parse. The warning does not stop the program. At runtime the call works only if an include has defined the action by then; a misspelled name is still an error.

This check has its own operation allowance, so it does not use up the program's `max_operations`. If it runs out, the remaining warnings stay. The program's time limit still applies to it.

### Including the same file more than once (diamond includes)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Included action and assertion warning fixes

Logbie-web's seven WFL suites produced 316 `ANALYZE-SEMANTIC` warnings for actions supplied by `include from` and 126 `ANALYZE-UNUSED` warnings. The actions existed and the tests passed. The CLI analyzed only the entry file before executing includes, while the unused-variable visitor skipped `expect` statements entirely.

The CLI now scans literal top-level includes transitively when undefined-action warnings exist. It parses each included file under the run's source and import limits, recognizes action definitions by name, and removes only matching warnings. Dynamic paths and unknown names retain their warnings. The unused-variable visitor now marks the assertion subject and expression-valued expected operands as uses. These changes do not execute includes during analysis or alter runtime include behavior.

Three focused real-binary regressions were written first. Before the fix, the literal-include and assertion-use cases failed; the dynamic-include case passed. After the fix, all three pass. Against the seven Logbie-web suites, the patched WFL binary removes all 316 false undefined-action warnings and 90 false unused-variable warnings. The other 36 unused names were ignored return values in three Logbie-web test files; those bindings were replaced with direct calls, leaving zero analyzer warnings across all seven suites. The account suite's file-backed SQLite case requires a writable fixture directory outside the sandbox.

The red test-only ancestor is commit bb103dae.

## Review follow-up

Review of the first version found four gaps, each reproduced by a failing test before the fix (red test-only commit af4fbd0):

- **Ordering.** The first version dropped a warning whenever any literal include defined the name, even for a call that runs before the include. Top-level statements run in order, and WFL defines an action only when its `define` statement runs, so the CLI now keeps a warning unless the include has run by the time of the call. For a call in sequential code, the include must be an earlier top-level statement. For a call in an action, container, or handler body, the include must come before any later statement that can run code (another include counts). The analyzer records which top-level statement holds each `Undefined action` warning, and the CLI compares statement positions. It does not compare line numbers, because the parser records some blocks at their closing line.
- **Operation budget.** The scan charged the run's shared operation budget, so a program whose `max_operations` covered its own work could fail before it started. The scan now has its own allowance with the same ceiling and the run's remaining time. Running out stops the scan and keeps the remaining warnings. A run deadline or cancellation during the scan exits with status 2 and an `Error:` line, like other front-end budget breaches. Before this change it appeared as an analyzer finding.
- **Assertion operands.** `expect items.length ...` and `expect other.size(wanted) ...` now count `items`, `other`, and `wanted` as used. The shared unused-variable walker now visits property-access receivers and method-call receivers and arguments.
- **Test strength.** The CLI tests now assert the exit status along with the diagnostic text.

Known limit: the check assumes that the included file supplying a name does not call back into a main-file action that uses the name before the file defines it. That case still loses its warning.
36 changes: 35 additions & 1 deletion src/analyzer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,17 @@ impl Scope {
}
}

/// Where an include-relaxed `Undefined action` warning was raised.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct UndefinedActionSite {
pub name: String,
pub line: usize,
pub column: usize,
/// Index into `Program::statements` of the top-level statement containing
/// the call, when the call was reached while analyzing one.
pub statement_index: Option<usize>,
}

#[derive(Debug, Clone)]
pub struct SemanticError {
pub message: String,
Expand Down Expand Up @@ -400,6 +411,11 @@ pub struct Analyzer {
/// actions/variables they expose; undefined-action errors are downgraded to
/// warnings to avoid false fatal failures for include-exposed actions.
has_includes: bool,
/// Index of the top-level statement being analyzed, if any.
current_statement_index: Option<usize>,
/// Every include-relaxed `Undefined action` warning, with its enclosing
/// top-level statement, so callers can tell when an include has run.
undefined_action_sites: Vec<UndefinedActionSite>,
/// Nesting depth of `try` bodies currently being analyzed. Undefined-name
/// references inside a `try` body raise catchable runtime errors (documented
/// behavior), so they are reported as warnings instead of fatal errors.
Expand Down Expand Up @@ -683,6 +699,8 @@ impl Analyzer {
current_container: None,
current_method_is_static: None,
has_includes: false,
current_statement_index: None,
undefined_action_sites: Vec::new(),
try_depth: 0,
active_loop_variables: Vec::new(),
budget_error: None,
Expand Down Expand Up @@ -802,6 +820,8 @@ impl Analyzer {
self.next_scope_id = 1;
self.errors.clear();
self.warnings.clear();
self.undefined_action_sites.clear();
self.current_statement_index = None;
self.action_parameters.clear();
self.constant_bindings.clear();
self.containers.clear();
Expand Down Expand Up @@ -861,9 +881,11 @@ impl Analyzer {
}

// PASS 2: Analyze all statements (including action bodies)
for statement in &program.statements {
for (index, statement) in program.statements.iter().enumerate() {
self.current_statement_index = Some(index);
self.analyze_statement(statement);
}
self.current_statement_index = None;
self.validate_container_inheritance_cycles();
self.warn_incompatible_inherited_property_overrides();

Expand All @@ -884,6 +906,12 @@ impl Analyzer {
&self.warnings
}

/// The include-relaxed `Undefined action` warnings from the last analysis,
/// with the top-level statement that contains each call.
pub fn undefined_action_sites(&self) -> &[UndefinedActionSite] {
&self.undefined_action_sites
}

/// Take the shared-budget breach recorded during analysis, if any. When
/// `analyze` returns `Err`, a caller must consult this to tell a fatal
/// deadline/cancellation/resource breach apart from ordinary semantic
Expand Down Expand Up @@ -962,6 +990,12 @@ impl Analyzer {
line,
column,
));
self.undefined_action_sites.push(UndefinedActionSite {
name: name.to_string(),
line,
column,
statement_index: self.current_statement_index,
});
true
} else {
false
Expand Down
32 changes: 30 additions & 2 deletions src/analyzer/static_analyzer.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::Analyzer;
use crate::diagnostics::{Severity, WflDiagnostic};
use crate::parser::ast::{Expression, Program, Statement, Type};
use crate::parser::ast::{Assertion, Expression, Program, Statement, Type};
use std::collections::{HashMap, HashSet};

#[derive(Debug, Clone)]
Expand Down Expand Up @@ -1304,6 +1304,26 @@ impl Analyzer {
self.mark_used_variables(stmt, usages);
}
}
Statement::ExpectStatement {
subject, assertion, ..
} => {
self.mark_used_in_expression(subject, usages);
Comment thread
logbie marked this conversation as resolved.
match assertion {
Assertion::Equal(expected)
| Assertion::Be(expected)
| Assertion::GreaterThan(expected)
| Assertion::LessThan(expected)
| Assertion::Contain(expected)
| Assertion::HaveLength(expected) => {
self.mark_used_in_expression(expected, usages);
}
Assertion::BeYes
| Assertion::BeNo
| Assertion::Exist
| Assertion::BeEmpty
| Assertion::BeOfType(_) => {}
}
}
// Compound-assignment / list statements read (and write) their
// operand variables — count them as uses.
Statement::AddToListStatement {
Expand Down Expand Up @@ -1549,8 +1569,16 @@ impl Analyzer {
}
}
}
Expression::MemberAccess { object, .. } => {
Expression::MemberAccess { object, .. } | Expression::PropertyAccess { object, .. } => {
self.mark_used_in_expression(object, usages);
}
Expression::MethodCall {
object, arguments, ..
} => {
self.mark_used_in_expression(object, usages);
for arg in arguments {
self.mark_used_in_expression(&arg.value, usages);
}
}
Expression::IndexAccess {
collection, index, ..
Expand Down
Loading
Loading