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

@@ -0,0 +1,15 @@
// A module's own scalar const `K` and another module's same-named ARRAY
// GLOBAL (`K : [4]s64 = .[...]`) coexist: each module's bare `K` binds its
// OWN author. The global registry is last-wins across modules, so without
// source-aware selection a.sx's `K` read the array global's address.
//
// Regression (issue 0115): a.sx printed the array's address; h.sx's reads
// stayed correct only by registration order.
#import "modules/std.sx";
#import "0835-modules-same-name-global-vs-const-own/a.sx";
h :: #import "0835-modules-same-name-global-vs-const-own/h.sx";
main :: () {
print("a_k={} use_k={}\n", a_k(), h.use_k());
}

View File

@@ -0,0 +1,2 @@
K :: 1;
a_k :: () -> s64 { K }

View File

@@ -0,0 +1,2 @@
K : [4]s64 = .[11, 22, 33, 44];
use_k :: () -> s64 { K[2] }

View File

@@ -0,0 +1,15 @@
// The MAIN file's own typed scalar const `K : s64 : 4` vs a namespaced
// module's same-named array global: the main file's bare `K` is its own
// scalar — type inference must not borrow the array global's type either
// (the print pack used to format the whole 4-element array).
//
// Regression (issue 0115).
#import "modules/std.sx";
h :: #import "0836-modules-own-const-vs-ns-array-global/h.sx";
K : s64 : 4;
main :: () {
print("K={} use_k={}\n", K, h.use_k());
}

View File

@@ -0,0 +1,2 @@
K : [4]s64 = .[11, 22, 33, 44];
use_k :: () -> s64 { K[2] }

View File

@@ -0,0 +1,17 @@
// An ARRAY-typed `::` const (`K : [4]s64 : .[...]`) is not a supported
// module-const shape — its own module's read diagnoses cleanly as
// unresolved. The own author OWNS the name: the read must never borrow
// another module's same-named scalar const (which used to type `K[2]`
// against the scalar and panic at LLVM emission).
//
// Regression (issue 0115). When array `::` consts land, repoint this
// example at the new behavior.
#import "modules/std.sx";
h :: #import "0837-modules-array-const-no-cross-borrow/h.sx";
K : s64 : 4;
main :: () {
print("{}\n", h.use_k());
}

View File

@@ -0,0 +1,2 @@
K : [4]s64 : .[11, 22, 33, 44];
use_k :: () -> s64 { K[2] }

View File

@@ -0,0 +1 @@
a_k=1 use_k=33

View File

@@ -0,0 +1 @@
0

View File

@@ -0,0 +1 @@
K=4 use_k=33

View File

@@ -0,0 +1 @@
1

View File

@@ -0,0 +1,5 @@
error: unresolved 'K' (in examples/0837-modules-array-const-no-cross-borrow/h.sx fn use_k)
--> examples/0837-modules-array-const-no-cross-borrow/h.sx:2:22
|
2 | use_k :: () -> s64 { K[2] }
| ^