Repository navigation
Description
Summary
Reject @[tailrec] recursive calls that leave a live Drop local across the call. The current tailrec verifier only tracks lexical defer/errdefer cleanup, so it can accept calls that still require post-call destructor work.
Spec
§9.2 says @[tailrec] turns tail-call optimization into a guarantee and the compiler must reject if it cannot eliminate stack growth (docs/with-specification.md:3233-3238). Calls with an active defer/errdefer and calls that leave a Drop-implementing local live across the call are explicitly not tail position (docs/with-specification.md:3256-3263).
Current state
Sema invokes verify_tail_position during function checking (src/SemaCheck.w:900-902), and the MIR-level tailrec verifier runs after lowering (src/compiler/Compilation.w:1200-1217). tailrec_verify_recursive_edges tracks active_cleanup for defer/errdefer only; it reports active cleanup for defer/errdefer (src/MirLower.w:9446-9458, src/MirLower.w:9473-9486) and marks loop bodies as non-tail (src/MirLower.w:9519-9520). It does not inspect live locals or scheduled Drop entries. Separately, #430 documents the broader Drop scheduling bug where moving a named Drop local can double-drop, and this issue observed the same let _ = t double-drop shape.
The root cause is that tail-position verification is syntactic/MIR-edge based and does not consult ownership liveness for Drop locals. A self-recursive call can be in syntactic tail position but still require destructor work after the call, which violates the tailrec guarantee.
Implementation plan
- Add a liveness query for Drop-implementing locals at each recursive call candidate. It can start in sema using binding scopes and last-use data, or in MIR using scheduled drops and local live ranges after Bug: Drop expression temporaries are never dropped (§2.4) #430's drop-frame fixes.
- A recursive edge is tail-call eligible only if no live
Droplocal owned by the current frame remains across the call, excluding locals moved into the call and locals already explicitlydrop(...)ped before the call. - Emit
recursive call is not in tail position (Drop local 'name' is live across the call)at the recursive call span. - Coordinate with Bug: Drop expression temporaries are never dropped (§2.4) #430 before relying on MIR scheduled drops as authoritative. Until Bug: Drop expression temporaries are never dropped (§2.4) #430 lands, a conservative sema rejection for any in-scope Drop local whose last use is after or unknown at the recursive call is preferable to accepting an unsound tailrec guarantee.
- Preserve existing positive tailrec optimization and existing negative defer/loop tests.
- Add a regression for the observed
let _ = tdouble-drop under Bug: Drop expression temporaries are never dropped (§2.4) #430, not as part of this tailrec verifier unless the fix is shared.
Test plan
Add test/compile_errors/err_tailrec_drop_local_live.w:
- define a type with
Drop.drop. - in
@[tailrec] fn f(n), bindlet t = Tracer.new(n)and tail-callf(n - 1)whiletis still live. - expect the new Drop-local tail-position diagnostic.
Add test/behavior/behav_tailrec_drop_local_explicit_drop.w only if explicit drop(t) before recursion is supported safely after #430; otherwise add a compile-error documenting the current conservative limitation.
Extend test/compile_errors/err_tailrec_defer_cleanup.w and existing tailrec behavior tests to ensure current defer and loop-body checks are not regressed.
Acceptance criteria
@[tailrec]rejects recursive calls with live Drop locals.- Explicitly ended Drop lifetimes are handled according to Bug: Drop expression temporaries are never dropped (§2.4) #430's final drop semantics.
- Existing tailrec positive tests still optimize/pass.
- Existing loop-body and defer cleanup negative tests still fail.
with build && with build :fixpoint && with build :testpass.
Dependencies
#430 for correct Drop temporary/move/drop scheduling; #530 for additional tailrec coverage.
Traceability
From the requirements campaign (docs/requirements.md, spec v7.1). Affected requirement IDs: 9.2.1.13.
Activity
ehartford commentedon Jun 12, 2026
Implemented in 47c612c. The tailrec contract verifier now tracks live Drop locals across recursive SCC edges, rejects recursive calls with a live Drop local, and accepts explicit lifetime end via drop(local)/let _ = local before the tail call. Added negative and positive tailrec coverage. Verification passed: with build, with build :fixpoint, with build :test, and with build :test-green.