# 0122 — whole-program passes resolve/diagnose under a stale ambient source

> **RESOLVED** (2026-06-11, same session — found and fixed during the
> std.sx-as-pure-re-exports restructure, Agra-directed). Three
> whole-program passes ran under whatever `current_source_file` the
> previous pipeline phase happened to leave behind, instead of pinning
> the context per declaration:
>
> 1. `ErrorAnalysis.convergeClosureShapeSets` (error_analysis.zig) —
>    resolves closure-literal param/return annotations; a stale context
>    made example-declared nominal types (`Point`, `Color`) fail the E4
>    visibility gate with `type 'X' is not visible` attributed to
>    nonsense std.sx spans. Fixed: pin `setCurrentSourceFile` per
>    `fn_ast_map` entry from `body.source_file` (already stamped by
>    resolveImports).
> 2. `ErrorFlow.checkErrorFlow` (error_flow.zig) — the flow walk
>    resolves types via `inferExprType` AND emits its reject
>    diagnostics; both used the ambient file. Fixed: pin per decl.
> 3. The `UnknownTypeChecker` unknown-type loop
>    (semantic_diagnostics.zig) — emitted with the ambient file
>    (`checkBindingNames` beside it already saved/restored per node).
>    Fixed: pin `diagnostics.current_source_file` per decl.
>
> Latent on master for all three — the ambient just happened to be the
> main file with the old single-file std.sx; the restructured std.sx
> (namespace part-file imports) reordered the pipeline's last-touched
> module and exposed them. Pinned coverage: examples 0129 / 1047 /
> 1049 / 1052 / 1053 / 1056 (closure shapes with nominal types,
> error-flow reject attribution) fail without the fixes once std.sx is
> the re-export facade. Gates: zig build test 426/426, suite 588/588.

## Symptom

With a std.sx whose first declarations are namespace imports, programs
using closures with user-struct parameter types failed
`type 'Point' is not visible; #import the module that declares it`
attributed to meaningless std.sx spans, and error-flow / unknown-type
diagnostics for main-file code rendered against std.sx's line table
(e.g. expected `examples/foo.sx:22:21`, got `std.sx:16:25`).

## Reproduction

Against the pre-fix compiler with the re-export std.sx:

```sx
#import "modules/std.sx";
Point :: struct { x, y: i32; }
main :: () {
    f := closure((p: Point) -> Point => Point.{ x = p.x + 1, y = p.y });
    r := f(Point.{ x = 1, y = 2 });
    out("done\n");
}
```

## Investigation prompt

(Resolved — kept for the record.) The root pattern: any pass that runs
after module scanning and either resolves source-gated names or emits
diagnostics MUST pin the visibility/rendering context per declaration
(`setCurrentSourceFile(decl.source_file)` — syncs the lowering context
and the diagnostics renderer), never inherit the ambient. Fn bodies
carry `body.source_file` (stamped by resolveImports) for fn-keyed
walks. When auditing for siblings, check every `lowerRoot` phase that
walks `fn_ast_map` or the program decl list.
