fix(lower): source-aware value-const resolution (own-wins / ambiguous) — close F2 [stdlib E2 attempt-3]

E2 retained per-source const declarations but left the const READ path on the
global last-wins `module_const_map`, so a module's OWN reference to a same-name
const bound the LAST global author (F2: a.sx `K::1`, b.sx `K::2`, main flat-imports
both → both read B's K). Complete the const analog of the type (`selectNominalLeaf`)
and callable (`selectPlainCallableAuthor`) source-aware models.

- `selectModuleConst`: own-wins; exactly one flat-visible author → it; ≥2 distinct
  flat-visible → `.ambiguous` (loud diagnostic, consistent with 0755/0724); none
  → `.none`. Reads the SELECTED author's per-source value (`module_consts_by_source`)
  and folds its RHS over the global leaf map, so a const-EXPRESSION chain
  (`N :: M + 1`, M flat-imported) still resolves M.
- Rewire `comptimeIntNamed` / `lookupFloatName` / `nameIsFloatTyped`, the runtime
  identifier path, and the global-init-from-const path through it; drop the now
  subsumed `moduleConstBareInvisible` gate.
- program_index: `moduleConst{Int,Float,IsFloatTyped}With` fold a selected `ci`.

Examples: 0759 (own-wins value const, a=1 b=2) + 0760 (two-flat-visible →
ambiguous). Single-author byte-identical (run_examples 498/0, 496 prior unchanged;
zig build test 423/423; corpus sweep 515 no-crash; m3te ios-sim exit 0).
This commit is contained in:
agra
2026-06-08 00:32:07 +03:00
parent 66d10c00bb
commit 72f06a109b
14 changed files with 181 additions and 38 deletions

View File

@@ -0,0 +1,16 @@
// issue 0105 / F2 — same-name VALUE const, own-wins. Two flat-imported modules
// each declare a top-level `K` with a different value and a function that reads
// `K` bare. Each function's OWN reference must bind ITS OWN module's `K`
// (own-wins), exactly as same-name structs (0754) and functions (0722) do —
// NOT the global last-wins author. Pre-fix both `a_k` and `b_k` returned B's
// `K` (the const READ path read the global last-wins `module_const_map`); now
// `a_k` returns 1 and `b_k` returns 2, resolved by the source-aware const
// author selector (`selectModuleConst`).
#import "modules/std.sx";
#import "0759-modules-same-name-const-own/a.sx";
#import "0759-modules-same-name-const-own/b.sx";
main :: () -> s32 {
print("a={} b={}\n", a_k(), b_k());
0
}

View File

@@ -0,0 +1,3 @@
// Module A authors its OWN value const `K` (1) and reads it bare.
K :: 1;
a_k :: () -> s64 { return K; }

View File

@@ -0,0 +1,5 @@
// Module B authors a DIFFERENT same-name value const `K` (2) — a shadow of A's
// `K`. Pre-fix the two collapsed last-wins in the global const map, so A's `a_k`
// read B's value; now each `K` is selected per declaring source.
K :: 2;
b_k :: () -> s64 { return K; }

View File

@@ -0,0 +1,14 @@
// issue 0105 / F2 — same-name VALUE const, two-flat-visible → AMBIGUOUS. `main`
// flat-imports two modules that each author a same-name `K` and authors none
// itself. A bare `K` reference can't be disambiguated, so the compiler emits a
// LOUD diagnostic (consistent with the type ambiguity at 0755 and the function
// ambiguity at 0724) and poisons the result — never a silent first-/last-wins
// pick.
#import "modules/std.sx";
#import "0760-modules-same-name-const-ambiguous/a.sx";
#import "0760-modules-same-name-const-ambiguous/b.sx";
main :: () -> s32 {
print("K={}\n", K);
0
}

View File

@@ -0,0 +1,3 @@
// One of two flat authors of value const `K`. A consumer that flat-imports BOTH
// and reads `K` bare cannot pick between them.
K :: 1;

View File

@@ -0,0 +1,2 @@
// The second flat author of value const `K`.
K :: 2;

View File

@@ -0,0 +1 @@
0

View File

@@ -0,0 +1 @@

View File

@@ -0,0 +1 @@
a=1 b=2

View File

@@ -0,0 +1 @@
1

View File

@@ -0,0 +1,5 @@
error: 'K' is ambiguous: it is declared in multiple flat-imported modules; qualify the reference or remove the duplicate import
--> examples/0760-modules-same-name-const-ambiguous.sx:12:21
|
12 | print("K={}\n", K);
| ^