Skip to content

Tests: tailrec coverage (§9) #530

Closed

Description

@ehartford

Summary

Add missing @[tailrec] coverage for recursive calls in loop bodies. The implementation already treats loop bodies as non-tail, but the suite lacks a targeted regression test.

Spec

§9.2 lists loop bodies among constructs that are not tail position (docs/with-specification.md:3256-3263). @[tailrec] must reject recursive calls that cannot be lowered without stack growth (docs/with-specification.md:3233-3238).

Current state

The MIR-level tailrec verifier marks NK_FOR, NK_WHILE, and NK_LOOP body traversal with in_tail = 0 (src/MirLower.w:9519-9520). Existing compile-error tests cover not-tail arithmetic and active defer cleanup (test/compile_errors/err_tailrec_not_tail.w, test/compile_errors/err_tailrec_defer_cleanup.w), and behavior tests cover successful tailrec forms. The requirements campaign notes that the loop-body case is untested (out/req-campaign/g04.tsv:71).

Implementation plan

  1. Add a compile-error test where the only recursive call is syntactically inside a loop body and would otherwise look final within that body.
  2. Assert the existing verifier diagnostic recursive call is not in tail position (function is @[tailrec]).
  3. Keep this as test-only unless the test unexpectedly passes; if it passes, fix tailrec_verify_recursive_edges rather than weakening the test.

Test plan

Add test/compile_errors/err_tailrec_loop_body.w:

@[tailrec]
fn f(n: i32) -> i32:
    while n > 0:
        return f(n - 1)
    0

or a loop: variant, expecting the not-tail diagnostic.

Add a second case for for only if the current iteration surface makes a compact fixture straightforward.

Acceptance criteria

  • A recursive call inside a loop body is covered by a compile-error test.
  • Existing positive tailrec behavior remains green.
  • No implementation change is made unless the new test exposes a regression.
  • with build && with build :fixpoint && with build :test pass.

Dependencies

#478 for the separate live-Drop-local tailrec rule.

Traceability

From the requirements campaign (docs/requirements.md, spec v7.1). Affected requirement IDs: 9.2.1.11.

Activity

  1. ehartford commented on Jul 3, 2026

    @ehartford
    ContributorAuthor

    Fixed + covered (bfe7865). The tailrec verifier had a real bug: NK_FOR/NK_WHILE/NK_LOOP were all read via get_data2(node), but the body field is d2 only for NK_FOR — NK_WHILE body is d1 (d2=label), NK_LOOP body is d0. So while/loop bodies were never traversed and a BARE recursive call inside a while/loop silently passed @[tailrec] verification (for-loops were correct). Split per node kind (NK_WHILE also checks its cond). Pinned: err_tailrec_bare_call_in_while / err_tailrec_bare_call_in_loop (now rejected), behav_tailrec_return_in_while (return-tail in a while stays valid). Note: the issue's proposed fixture used return f(...) which is ALWAYS tail regardless of the loop, so it never would have caught this — the bug is the bare-call form. §9.

  2. added a commit that references this issue on Jul 4, 2026
    bfe7865
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