Files
sx/issues/0115-same-name-const-scalar-array-collision.md
agra 0b13498e25 fix(0115): source-aware global selection — own-wins for module globals
The globals registry (global_names) was last-wins across modules with no
per-importer gate: any module's bare K could read/write/type against an
unrelated module's same-named global (hash.sx's K table hijacked every
user K once std's namespace tail pulled hash into the program), and an
own const of an unsupported shape borrowed another module's const and
panicked at the unresolved-type tripwire.

- var_decl joins RawDeclRef: module globals are selectable raw authors.
- selectGlobalAuthor (the globals analogue of F2's selectModuleConst):
  own author wins, one flat-visible author resolves, >=2 distinct flat
  authors diagnose loudly, authored-but-not-visible diagnoses, and a
  compiler-synthesized global (no raw author) emits untracked. A var_decl
  author whose per-source registration was deduped at flat-merge (two
  modules declaring the same extern symbol) serves the symbol's
  registration.
- All bare-identifier global sites route through it: value read, addr-of,
  assignment (store + compound), lvalue address, fn-ptr call, call param
  typing, and expression type inference.
- selectModuleConst gains .own_opaque: an own const author with no
  materialized per-source value (e.g. an array '::' const) blocks
  borrowing another module's same-named const — the read diagnoses
  cleanly instead of panicking.
- The fn-as-VALUE arm admits raw-facts-only authors: an own fn whose name
  a flat-merge collision dropped from the global decl list (first-wins)
  now resolves via author selection for func_ref/closure/Any shapes too.

Regressions: examples 0835 (own const vs flat array global), 0836 (main
const vs namespaced array global, incl. inference), 0837 (own array
const never borrows cross-module — clean unresolved).
2026-06-11 10:47:30 +03:00

123 lines
5.8 KiB
Markdown

# 0115 — same-name consts of different shapes collide across modules (panic / silent clobber)
> **RESOLVED** (2026-06-11). Root cause: the globals registry
> (`global_names`) is last-wins across modules and every read/write/addr/
> call site consulted it with no source-awareness; `var_decl` was not even
> a selectable raw author. Fix: `var_decl` joins `RawDeclRef`;
> `selectGlobalAuthor` (the globals analogue of F2's `selectModuleConst`)
> selects the author own-wins / one-flat-visible / ambiguous-loudly and
> serves the AUTHOR's per-source global; all bare-identifier global sites
> (read, addr-of, assignment, fn-ptr call, type inference) route through
> it. `selectModuleConst` gained `.own_opaque` so an own const author with
> no materialized value (unsupported shape, e.g. an array `::` const)
> blocks borrowing another module's same-named const instead of panicking.
> The fn-as-VALUE arm admits raw-facts-only authors (an own fn dropped from
> the global decl list by a flat-merge collision — the 0601 `test` case).
> Regression tests: examples/0835, 0836, 0837. The co-blockers also
> landed: dead-global elimination at emit (unreferenced plain-data globals
> are not emitted) and 1055/1056 no longer pin global error ordinals —
> the full std namespace tail is enabled on top.
**Symptom.** When two modules in one program declare a same-named module
const with DIFFERENT shapes (scalar `K : s64 : 4` vs array
`K : [4]s64 : .[...]`), resolution conflates them instead of selecting
per-author:
- **Observed (minimal repro below)**: compiler PANIC — `unresolved type
reached LLVM emission` (`src/backend/llvm/types.zig:175`, the
`.unresolved` sentinel tripwire).
- **Observed (full std-tail topology)**: SILENT WRONG VALUES — a module's
own scalar `K` reads as the other module's array global (prints the
array's address or the whole array). Seen corpus-wide when
`hash :: #import "modules/std/hash.sx"` (hash.sx declares the SHA-256
`K : [64]s64` table) is added to the std.sx namespace tail: examples
0786/0787/0788/0789/0791/0793/0794 (same-name-const family), 0162, 0168
all read hash's `K` instead of their own.
- **Expected**: own-wins / per-author const selection (the documented F2
semantics — readme "Own-wins holds at every one of those sites") applies
regardless of the consts' shapes; no cross-shape leakage, no panic.
This blocks the PLAN-STDLIB "full tail" follow-up: fs/process/socket/
json/cli/hash/test cannot join the std.sx namespace tail until same-name
consts are robust across every module pulled into every program.
## Reproduction (panic variant — minimal, standalone)
```sx
// h.sx
K : [4]s64 : .[11, 22, 33, 44];
use_k :: () -> s64 { K[2] }
```
```sx
// main.sx
#import "modules/std.sx";
h :: #import "h.sx";
K : s64 : 4;
main :: () { print("K={} h.use_k={}\n", K, h.use_k()); }
```
Run `main.sx` → panic `unresolved type reached LLVM emission`.
Expected: prints `K=4 h.use_k=33`.
## Reproduction (silent-clobber variant — full topology)
Add the full tail to `library/modules/std.sx` after the existing
`mem/xml/log` lines:
```sx
fs :: #import "modules/std/fs.sx";
process :: #import "modules/std/process.sx";
socket :: #import "modules/std/socket.sx";
json :: #import "modules/std/json.sx";
cli :: #import "modules/std/cli.sx";
hash :: #import "modules/std/hash.sx";
test :: #import "modules/std/test.sx";
```
Run `bash tests/run_examples.sh` → ~50 failures. The const-family
failures (0786 prints `a=4318334368 b=4318334368`, 0162 prints the whole
64-entry array for `K=...`) are this bug. (A flat-importing main + a
namespaced array-K module WITHOUT the tail topology resolves correctly —
the silent variant needs the tail's shape, where hash.sx itself
flat-imports std.sx. The panic variant above is the minimal entry point.)
## Co-blockers observed in the same experiment (note, possibly separate issues)
1. **Eager emission bloat**: with the tail in place, every program emits
hash's 64-entry `@K` table plus `@OS/@ARCH/@POINTER_SIZE` globals even
when unused (visible in every pinned `.ir` snapshot). Tail modules'
globals should emit lazily (only when referenced).
2. **0601-comptime-meta** prints nothing (comptime meta machinery breaks
with the tail in place — root cause unknown, possibly same-name
generic/comptime fn last-wins: `isPlainFreeFn` excludes generic /
comptime authors from own-wins rerouting).
3. **1055/1056 errors-enum-value**: user-visible error ints shift when
tail modules' error sets join the global error-tag registry (numbering
coupling, arguably inherent; snapshot fragility at minimum).
## Investigation prompt
Same-name module consts are selected own-wins via `selectModuleConst`
(F2, src/ir/lower/expr.zig ~1641) over `module_const_map` — but ARRAY
consts lower as GLOBALS (`@K = internal global [4 x s64]`), registered in
a different, still last-wins registry (find it: grep the lowering for
where a top-level array const becomes a module global — likely
`lowerGlobalDecl` / the global-var map in src/ir/lower/decl.zig). The
panic happens because the scalar `K`'s type resolution reads the OTHER
author's array shape (or vice versa) and poisons to `.unresolved`.
The fix likely needs:
1. Source-aware selection for the globals registry, mirroring
`selectModuleConst` (own-wins, ≥2 flat-visible authors → loud
ambiguity) — including the MIXED scalar/array case where the two
authors live in different registries today.
2. The 0786-family examples already pin scalar/scalar own-wins; add a
scalar-vs-array pin (the minimal repro above) once fixed.
Verification: run the panic repro (expect `K=4 h.use_k=33`), then add the
full tail to std.sx and run `bash tests/run_examples.sh` — the const
family (0786/0787/0788/0789/0791/0793/0794, 0162, 0168) must pass; the
remaining tail failures decompose into co-blockers 1-3 above (file
separately if they persist).