Skip to content

Bug: @[tailrec] accepts Drop local live across recursive call (§9.2) #478

Closed

Description

@ehartford

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

  1. 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.
  2. A recursive edge is tail-call eligible only if no live Drop local owned by the current frame remains across the call, excluding locals moved into the call and locals already explicitly drop(...)ped before the call.
  3. Emit recursive call is not in tail position (Drop local 'name' is live across the call) at the recursive call span.
  4. 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.
  5. Preserve existing positive tailrec optimization and existing negative defer/loop tests.
  6. Add a regression for the observed let _ = t double-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), bind let t = Tracer.new(n) and tail-call f(n - 1) while t is 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 :test pass.

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

  1. ehartford commented on Jun 12, 2026

    @ehartford
    ContributorAuthor

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions