Skip to content

eval: self-recursion, forward/mutual references and let mut fail on well-typed programs (interp diverges from compile) #833

Done

Description

@hyperpolymath

Evidence (main ee211d3)

Each program below type-checks and runs correctly when compiled to Bun-ESM and to core wasm.

Program affinescript eval Bun-ESM / wasm
fn g(x) { … 1 + g(x - 1) } (self-recursion) Runtime error: Unbound variable: g 5
main calls helper, which is declared after it Unbound variable: helper → Type error 3
is_even / is_odd (mutual recursion) Unbound variable: is_odd → Type error even
let mut k = 0; while k < 3 { k = k + 1; } Runtime error: Type mismatch: Expected mutable reference 3
println(int_to_string(2)) prints "2", with quotes 2

Root causes

  • Self-recursion. lib/interp.ml:1151 (TopFn) builds the closure with cl_env = env before binding the function's own name. There is no letrec.
  • Forward and mutual references. The CLI eval path (bin/main.ml:315, :363) type-checks with its own fold of Typecheck.check_decl. That fold skips the forward pass that Typecheck.check_program (lib/typecheck.ml:2656) uses to pre-register signatures. As a result, eval accepts a smaller language than compile.
  • let mut. StmtLet (lib/interp.ml:507) ignores mutability. StmtAssign evaluates the left-hand side to a plain value, and Value.assign then rejects that value.
  • Quoted output. The interpreter's println prints a String as an escaped OCaml literal (feat(strings): backend-independent UTF-8 octet count and code-point view #829 finding 7).

Classification

These are divergences, and all of them fail loudly. The reference interpreter refuses well-typed programs, and its frontend accepts less than compile does. None of them is a silent wrong value. The quoted println output is the exception: it is wrong output, but visibly so.

Options

Ranked by elegance and correctness first, then by dependability.

  1. (Recommended: most elegant and correct) Fix each at source:
    • Make eval call Typecheck.check_program, so there is one frontend entry for every command.
    • Make eval_program two-phase: bind every top-level function into one recursive environment by tying the knot through a mutable env cell. This matches the checker's forward pass and JS hoisting.
    • Make StmtLet with sl_mut bind a VMut cell, make variable reads dereference it, and resolve StmtAssign on an ExprVar to that cell.
    • Make println print the raw string.
  2. (Most dependable) Add differential tests: run every fixture under tests/codegen* through both eval and Bun-ESM and compare stdout. This catches the whole class, not just these instances.
  3. (Outside the box) Retire the tree-walker's CLI role in favour of the solo_cesk machine. That would also unblock tracking: interpreter non-tail single-shot resume returns the resumed value (shallow continuation) — needs delimited-continuation/CPS rewrite #623 (non-tail resume).

None of these changes language semantics: they make eval agree with the type checker and with the compiled backends.


Generated by Claude Code

Activity

  1. whatproblem-ai commented on Dec 1, 2026

    @whatproblem-ai

    Problem: On main ee211d3, affinescript eval rejects or mis-runs well-typed programs that compile and run correctly to Bun-ESM and wasm. Five cases fail: self-recursion, forward references, mutual recursion, let mut reassignment, and println quoting strings.

    Expected: eval accepts the same programs as compile and produces the same stdout as the compiled backends.

    Missing: full source of the five repro programs (the table abbreviates them with …), so they can become regression fixtures.


    Clarity: 94%. Clear enough to work on.

    ⌨️ Commands · 💬 Have feedback? Tell us · 🎯 New here? Add WhatProblem to your repo · 🔒 Privacy

  2. coderabbitai commented on Dec 1, 2026

    @coderabbitai
    Contributor
    🔗 Related PRs

    #829 - feat(strings): backend-independent UTF-8 octet count and code-point view [open]


    📝 Issue Planner

    Check the box below or use the @coderabbitai plan command to generate an implementation plan and prompts that you can use with your favorite coding assistant.

    • Create Plan

    🧪 Issue enrichment is currently in open beta.

    You can configure auto-planning by selecting labels in the issue_enrichment configuration.

    To disable automatic issue enrichment, add the following to your .coderabbit.yaml:

    issue_enrichment:
      auto_enrich:
        enabled: false

    💬 Have feedback or questions? Drop into our discord!

  3. added
    bug
    meta:umbrella
    priority:p2
    scope:repo
    status:ready
    on Dec 1, 2026
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

    bugmeta:umbrellapriority:p2scope:repostatus:ready

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions