Skip to content

fix(test): imported modules' top-level lets evaluate in their own scope under ailang test - #2629

Merged
sunholo-voight-kampff merged 1 commit into
devfrom
fix/test-harness-imported-toplevel-let
Dec 2, 2026
Merged

sunholo-voight-kampff merged 1 commit into
devfrom
fix/test-harness-imported-toplevel-let

Conversation

@sunholo-voight-kampff

@sunholo-voight-kampff sunholo-voight-kampff commented Dec 2, 2026 •

Copy link
Copy Markdown
Collaborator

Bug

An imported module's top-level value binding that refers to another one by bare name is left unbound under ailang test, while ailang run evaluates it correctly:

module zdep
import std/list (map, any)
import std/array (make, get, updateMany)

let base = updateMany(make(2, 0), [(0, 5), (1, 7)])
let table = map(\i. get(base, i), [0, 1])

export pure func hasSeven() -> bool = any(\x. not (x == 5), table)
$ ailang test a_test.ail        # test "…" { hasSeven() }
  evaluation error: undefined variable: table
$ ailang run --entry hasSeven zdep.ail
true

let alias = plain is enough to trigger it. The same pattern in the module under test passes. --bytecode passes. Inline tests fail the same way (harness evaluation failed: undefined variable: table).

Root cause

newHarnessEvaluator (internal/testing/module_scope.go) builds module environments by hand. It evaluated every module's value lets with evaluator.Eval in the shared environment. Only the module under test's bare names exist there, so base in an imported module did not resolve. The error was then dropped (if val, err := …; err == nil && val != nil), so table was never bound and the failure showed up later as a different error.

A second, related problem: the harness compiled in pipeline.ModeEval (the REPL mode). ailang run uses ModeCheck for a module file. ModeEval also evaluated the root module's first declaration on a throwaway evaluator whose builtins were bound to an evaluator with no resolver, and nothing used the value. With a root value let built from an imported one (let doubled = map(\x. x * 2, tableOf())), the compile never finished. A goroutine dump shows unbounded evaluation inside pipeline.Run, and the run was killed after 15 s on origin/dev. ailang run on the same program prints [10, 14].

Fix

  • Value lets are evaluated as runtime.extractBindings does for run:
    • in declaration order;
    • inside the module's own scope;
    • each imported module before its importers (DFS over LoadedModule.Imports);
    • the first error is returned with run's wording: failed to evaluate let bad in module broken: RT001: integer division by zero at broken.ail:8:14.
  • newHarnessEvaluator now returns an error. All 8 call sites propagate it: named batched, named per-body, properties, inline harness, cluster harness, expected-value rows, ensures harness, and the VM bridge. Nothing is swallowed.
  • Pipeline mode: the three harness compiles use ModeCheck. The Executor's builtin-only globalResolver only fed ModeEval's discarded evaluation, so it is removed.

Not the one-path refactor. Routing the harness through runtime.ModuleRuntime would mean:

  • evaluating every module eagerly;
  • attaching contracts to functions;
  • swapping CombinedResolver for the module resolver;
  • moving root bare-name exposure and ADT-constructor injection;
  • changing how 8 harness paths find their entry: inst.Bindings instead of the shared env.

That is too large for a release-candidate fix. This PR copies run's evaluation rules into the one place the harness evaluates values and aligns the compile mode.

Audit of the rest of module_scope.go:

  • The value-let loop was the only swallowed error.
  • A re-export that never resolves stays unbound by design; using it fails loudly in CombinedResolver.
  • A non-lambda LetRec binding is skipped, and using it fails loudly with RT_REC_002.
  • bindModuleCtor / injectADTConstructors silently skip missing constructors. That is optional scope setup, not a value.

Tests

Fixtures are in internal/testing/testdata/toplevel_let.

  • TestImportedTopLevelLetChain/{evaluator,bytecode}: the imported std/array + std/list chain and the alias case. The root module a_test sorts before zdep and builds its own value let from zdep's, which covers dependency order and the ModeEval hang. 3 named tests + 1 inline row.
  • TestRootTopLevelLetChain: the same chain in the module under test.
  • TestTopLevelLetErrorSurfaces: let bad = 10 / zero in an imported module fails the test with the binding's own error, the message ailang run prints, and not "undefined variable".

Mutation testing (scratch backup, mutate, run, restore; diff verified unchanged after each):

Mutation Caught by
evaluate in the shared env again TestImportedTopLevelLetChain (both engines), TestTopLevelLetErrorSurfaces
swallow the error again TestTopLevelLetErrorSurfaces
skip import-first ordering TestImportedTopLevelLetChain (both engines)
ModeEval again in the harness compiles test binary timed out (the hang)

Corpus check: I ran every .ail file with tests under examples/, tests/ and std/ (88 files) through ailang test --json with the origin/dev binary and with this branch, and compared pass/fail/skip counts. 0 differences, 1,365 passing tests.

Gates: all clean.

  • make test-changed, make test-core
  • make lint (0 issues)
  • make check-file-sizes, make verify-examples, make check-changelog
  • go test ./cmd/ailang -run 'Test.*(Test|Named|Bytecode|Inline|Propert)'

Already fixed at HEAD: the stapledons_godot callback-resolver reports (no change needed)

These reports, pinned to v0.52.0, were the reason for the investigation. Each symptom reproduces on a v0.52.0 build and passes on origin/dev, with the stdlib path pinned for both binaries:

Symptom Fixture v0.52.0 HEAD Fixed by
$builtin.not_Bool unresolved in a std/list.any callback of an imported helper r1 fail pass 3557cd2 (named tests compile once; bodies run as entry functions on the harness evaluator)
std/array.get unresolved in a map callback r2 fail pass 3557cd2
a package function unresolved in a foldl callback (path dependency + lock) r4 fail pass 3557cd2
"intrinsic operations require OpLowering pass" for a record update in a callback r3 fail pass 2e20aa6 (the OpLowerer handles RecordUpdate)
nil-pointer panic game-module repro not reproducible standalone — —

The per-body fallback path was also checked by forcing a batch failure with a reserved __namedtest_* name. It passes for r1–r3. All four fixtures still pass on this branch under both engines.

🤖 Generated with Claude Code

Fixes #2619

…pe under ailang test

An imported module's top-level value binding that referred to another one
by bare name (`let alias = plain`, or a std/array + std/list chain) was left
unbound under `ailang test`: the harness evaluated value lets in the shared
environment, where only the module under test's bare names exist, and
dropped the error. Tests failed with "undefined variable: table" while
`ailang run` printed the right answer.

newHarnessEvaluator now evaluates them as runtime.extractBindings does for
`ailang run`: declaration order, inside the module's own scope, imported
modules first, and the first error is returned (same message as run) to
every harness path instead of being swallowed.

The harness's three pipeline compiles now use ModeCheck, as `run` does for a
module file. ModeEval also evaluated the root's first declaration on a
throwaway evaluator whose builtins had no resolver; a root value let built
from an imported one never finished. The Executor's builtin-only resolver
existed only for that evaluation and is removed.

Tests: TestImportedTopLevelLetChain (evaluator + bytecode),
TestRootTopLevelLetChain, TestTopLevelLetErrorSurfaces, fixtures in
internal/testing/testdata/toplevel_let.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@sunholo-voight-kampff
sunholo-voight-kampff merged commit c85ee48 into dev Dec 2, 2026
23 checks passed
@sunholo-voight-kampff
sunholo-voight-kampff deleted the fix/test-harness-imported-toplevel-let branch December 2, 2026 16:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ailang test: a top-level value let in an imported module that references another top-level let is undefined (run passes)

1 participant