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).
This commit is contained in:
agra
2026-06-11 10:47:30 +03:00
parent 37bea63302
commit 0b13498e25
26 changed files with 288 additions and 32 deletions

View File

@@ -552,8 +552,14 @@ pub fn lowerAssignment(self: *Lowering, asgn: *const ast.Assignment) void {
}
}
if (!found_local) {
// Quiet author-aware lookup (type inference only; the store
// site diagnoses ambiguity / visibility).
if (self.program_index.global_names.get(asgn.target.data.identifier.name)) |gi| {
self.target_type = gi.ty;
switch (self.selectGlobalAuthor(asgn.target.data.identifier.name)) {
.resolved => |g| self.target_type = g.ty,
.untracked => self.target_type = gi.ty,
else => {},
}
}
}
} else if (asgn.target.data == .index_expr) {
@@ -617,9 +623,11 @@ pub fn lowerAssignment(self: *Lowering, asgn: *const ast.Assignment) void {
}
}
}
// Fallback: global variable assignment
// Fallback: global variable assignment — source-aware (issue
// 0115): write the AUTHOR's global, never an unrelated module's
// same-named one.
if (!handled) {
if (self.program_index.global_names.get(id.name)) |gi| {
if (self.resolveGlobalRef(id.name, asgn.target.span)) |gi| {
if (asgn.op == .assign) {
const val_ty = self.builder.getRefType(val);
const store_val = if (val_ty != gi.ty and val_ty != .void and gi.ty != .void)
@@ -890,7 +898,7 @@ pub fn lowerExprAsPtr(self: *Lowering, node: *const Node) Ref {
}
return binding.ref;
}
} else if (self.program_index.global_names.get(id.name)) |gi| {
} else if (self.resolveGlobalRef(id.name, null)) |gi| {
// Module-global lvalue: address into the global's live storage
// so a downstream GEP/store targets the global itself, not a
// loaded copy. A pointer-typed global is loaded first to get