Design decisions

52. Static closure & operation-value lowering (2026-11-21)

  1. Three-tier static closure lowering in cpl-codegen (devirt.rs, emit.rs, expr.rs, stmt.rs), with std::function only for genuinely dynamic values.
    • Direct elision (StaticDirect): when a struct fn-field or fn parameter holds a single statically known zero-capture closure and never escapes to a dynamic std::function home or assignment, calls emit directly as cpl_fnN(...) and the std::function field/parameter is replaced with a 1-byte bool placeholder (false at construction/call sites). Eliminates std::function completely from Monoid, Action, SegTree, LazySegTree, DualSegTree, SparseTable, DisjointSparseTable.
    • Template functor parameters (StaticTemplate): any non-recursive, non-entry/judge/hook function with a non-mut, non-reassigned Ty::Fn parameter that is not directly elided lowers to a C++17 function template (template <class F0, ...>, F0&& p), passing lambdas (including capturing/mutable ones) by type with zero std::function and full inlining.
    • Local auto bindings (StaticLocalAuto): any non-hoisted, non-reassigned local closure binding (let f = ... or nested fn via(...) = ...) emits auto f = [...](...) { ... }; instead of std::function<...>.
    • Dynamic fallback (DynamicStdFunction): genuinely dynamic fn values (struct fields holding multiple or capturing closures, recursive/entry/judge fn parameters, mutable/reassigned fn slots) retain std::function. #include <functional> is emitted only when std::function appears in the output.
    • Visibility (cpl explain <file.cp>): cpl explain <file.cp> (and --json) runs cpl_codegen::explain_closures and reports every fn-typed site's lowering mode (static: direct, static: template, static: auto, or dynamic: std::function with reason), fulfilling the "nothing secret under the hood" rule.
    • Return-tuple move: returning a struct/tuple literal moves owned non-scalar local fields (std::move(table)) when unreferenced by sibling fields, preventing O(n log n) vector copies on range-structure constructors (DisjointSparseTable 35.4 ms / 1.98x → 18.9 ms / 1.09x vs matched C++17). Reverse: revert elidable, template_params, local_auto, and explain_closures in crates/cpl-codegen/src/devirt.rs, the emit.rs/expr.rs/stmt.rs hooks, and explain_file in crates/cpl-cli/src/lib.rs.