fix(lower): single unified writer for the three decl-fact maps; close param-alias leak [stdlib E1 attempt-5]

Route EVERY write of type_alias_map / module_const_map / global_names (and
their *_by_source analogues) through one helper per map
(putTypeAlias/putModuleConst/putGlobal/dropModuleConst). The global put and the
by-source put are now inseparable, so no write-site can mirror one side and
miss the other — the dual-write drift that leaked ns-only aliases past the
source-aware bare-TYPE gate. Grep-clean: no raw .put/.remove to the three maps
outside the helpers (mirrors the no-raw-TypeTable.update discipline).

The generic-struct instantiation alias sites (Secret :: Box(s32), both the
.call and .parameterized_type_expr branches) previously registered only a named
struct in the TypeTable and never reached type_aliases_by_source, so
moduleTypeAuthor missed them and a bare ns-only use leaked (exit 42, no
diagnostic). Routing those writes through the unified putTypeAlias lands the
alias in the per-source cache and the leak closes BY CONSTRUCTION — a flat use
still resolves to the same TypeId findByName would, a ns-only use is rejected.

Regression 0749 (ns-only Secret :: Box(s32) bare -> "type 'Secret' is not
visible"): fail-before on daf4bbc exit 42 no diagnostic, pass-after exit 1.
Single-author resolution byte-identical (486 passed / 0 failed). resolver.zig
single graph-walk untouched; generic/param-protocol/Vector/type-fn stay legacy.
This commit is contained in:
agra
2026-06-07 19:31:13 +03:00
parent daf4bbc862
commit 78ef2ea3d8
6 changed files with 84 additions and 58 deletions

View File

@@ -0,0 +1,17 @@
// Bare PARAMETERIZED-struct alias visibility under a NAMESPACED-only import —
// the generic-struct sibling of 0747 (plain alias) and 0743 (named type). A
// generic-struct instantiation alias (`Secret :: Box(s32)`) registers ONLY a
// named struct type in the TypeTable; its raw import fact stays `.const_decl`,
// so before the fix it was NOT recognised as a type author and a BARE `Secret`
// leaked to the registered struct with NO diagnostic (the value silently came
// out 42). The unified declaration-fact writer routes the instantiation alias
// through `type_aliases_by_source`, so the bare-TYPE gate treats it like any
// other alias: `dep.sx` is imported only as `dep :: #import`, so bare `Secret`
// is reachable ONLY as `dep.Secret` and must NOT resolve. Regression
// (attempt-5 R4-parameterized-alias-leak).
dep :: #import "0749-modules-namespaced-only-bare-param-alias-not-visible/dep.sx";
main :: () -> s32 {
s : Secret = .{ value = 42 };
s.value
}

View File

@@ -0,0 +1,5 @@
Box :: struct($T: Type) {
value: T;
}
Secret :: Box(s32);

View File

@@ -0,0 +1,5 @@
error: type 'Secret' is not visible; #import the module that declares it
--> examples/0749-modules-namespaced-only-bare-param-alias-not-visible.sx:15:9
|
15 | s : Secret = .{ value = 42 };
| ^^^^^^

View File

@@ -782,29 +782,34 @@ pub const Lowering = struct {
return ti.function.call_conv != .c; return ti.function.call_conv != .c;
} }
// ── Source-keyed cache writers (R5 §#4) ── // ── Unified declaration-fact writers (R5 §#4) ──
// Mirror each global `type_alias_map` / `module_const_map` / `global_names` // The SOLE writers of the three semantic maps — global
// write into its source-partitioned analogue, keyed by the registering // `type_alias_map` / `module_const_map` / `global_names` AND their
// decl's source. Behavior-preserving for now: the global maps stay the only // source-partitioned analogues (`*_by_source`). Invariant: the global and
// readers; the per-source maps exist for the later read-side cutover. A null // by-source write for a name are inseparable — a write-site that mirrors
// source (unreachable for a scanned top-level decl post-import-resolution) // one without the other lets a ns-only author miss `*_by_source` and leak
// falls back to the main file; if even that is absent the write is skipped // past the source-aware bare-TYPE gate. No raw `.put`/`.remove` to the
// rather than recorded under a fabricated key. // three maps exists outside these helpers (grep-checkable — mirrors the
fn recordTypeAliasBySource(self: *Lowering, source: ?[]const u8, name: []const u8, tid: TypeId) void { // no-raw-`TypeTable.update` discipline). The global map stays the only
const src = source orelse self.main_file orelse return; // READER for now; the per-source cache feeds the gate. A null source
self.program_index.putTypeAliasBySource(src, name, tid); // (unreachable for a scanned top-level decl post-import-resolution) falls
// back to the main file; if even that is absent only the by-source write is
// skipped — the global map is always written.
fn putTypeAlias(self: *Lowering, source: ?[]const u8, name: []const u8, tid: TypeId) void {
self.program_index.type_alias_map.put(name, tid) catch {};
if (source orelse self.main_file) |src| self.program_index.putTypeAliasBySource(src, name, tid);
} }
fn recordModuleConstBySource(self: *Lowering, source: ?[]const u8, name: []const u8, info: program_index_mod.ModuleConstInfo) void { fn putModuleConst(self: *Lowering, source: ?[]const u8, name: []const u8, info: program_index_mod.ModuleConstInfo) void {
const src = source orelse self.main_file orelse return; self.program_index.module_const_map.put(name, info) catch {};
self.program_index.putModuleConstBySource(src, name, info); if (source orelse self.main_file) |src| self.program_index.putModuleConstBySource(src, name, info);
} }
fn recordGlobalBySource(self: *Lowering, source: ?[]const u8, name: []const u8, info: program_index_mod.GlobalInfo) void { fn putGlobal(self: *Lowering, source: ?[]const u8, name: []const u8, info: program_index_mod.GlobalInfo) void {
const src = source orelse self.main_file orelse return; self.program_index.global_names.put(name, info) catch {};
self.program_index.putGlobalBySource(src, name, info); if (source orelse self.main_file) |src| self.program_index.putGlobalBySource(src, name, info);
} }
fn dropModuleConstBySource(self: *Lowering, source: ?[]const u8, name: []const u8) void { fn dropModuleConst(self: *Lowering, source: ?[]const u8, name: []const u8) void {
const src = source orelse self.main_file orelse return; _ = self.program_index.module_const_map.remove(name);
self.program_index.removeModuleConstBySource(src, name); if (source orelse self.main_file) |src| self.program_index.removeModuleConstBySource(src, name);
} }
/// Pass 1: Scan declarations — register ASTs and extern stubs, but don't lower bodies. /// Pass 1: Scan declarations — register ASTs and extern stubs, but don't lower bodies.
@@ -829,13 +834,11 @@ pub const Lowering = struct {
switch (cd.value.data) { switch (cd.value.data) {
.int_literal => { .int_literal => {
const info = program_index_mod.ModuleConstInfo{ .value = cd.value, .ty = .s64 }; const info = program_index_mod.ModuleConstInfo{ .value = cd.value, .ty = .s64 };
self.program_index.module_const_map.put(cd.name, info) catch {}; self.putModuleConst(decl.source_file, cd.name, info);
self.recordModuleConstBySource(decl.source_file, cd.name, info);
}, },
.float_literal => { .float_literal => {
const info = program_index_mod.ModuleConstInfo{ .value = cd.value, .ty = .f64 }; const info = program_index_mod.ModuleConstInfo{ .value = cd.value, .ty = .f64 };
self.program_index.module_const_map.put(cd.name, info) catch {}; self.putModuleConst(decl.source_file, cd.name, info);
self.recordModuleConstBySource(decl.source_file, cd.name, info);
}, },
// A const whose RHS is an integer EXPRESSION over other consts // A const whose RHS is an integer EXPRESSION over other consts
// (`M :: 2; N :: M + 1`) is itself a usable count: register it so // (`M :: 2; N :: M + 1`) is itself a usable count: register it so
@@ -845,8 +848,7 @@ pub const Lowering = struct {
// non-const), `moduleConstInt` yields null and the use diagnoses. // non-const), `moduleConstInt` yields null and the use diagnoses.
.binary_op, .unary_op => { .binary_op, .unary_op => {
const info = program_index_mod.ModuleConstInfo{ .value = cd.value, .ty = .s64 }; const info = program_index_mod.ModuleConstInfo{ .value = cd.value, .ty = .s64 };
self.program_index.module_const_map.put(cd.name, info) catch {}; self.putModuleConst(decl.source_file, cd.name, info);
self.recordModuleConstBySource(decl.source_file, cd.name, info);
}, },
else => {}, else => {},
} }
@@ -930,8 +932,7 @@ pub const Lowering = struct {
d.addFmt(.err, cd.value.span, "type alias '{s}' could not be resolved: an array dimension is not a compile-time integer constant", .{cd.name}); d.addFmt(.err, cd.value.span, "type alias '{s}' could not be resolved: an array dimension is not a compile-time integer constant", .{cd.name});
} }
} }
self.program_index.type_alias_map.put(cd.name, target_ty) catch {}; self.putTypeAlias(self.current_source_file, cd.name, target_ty);
self.recordTypeAliasBySource(self.current_source_file, cd.name, target_ty);
} else if (cd.value.data == .identifier) { } else if (cd.value.data == .identifier) {
// Identifier-RHS alias: MyAlias :: MyInt; WideAlias :: Wide; // Identifier-RHS alias: MyAlias :: MyInt; WideAlias :: Wide;
// Chase through type_alias_map, then look up named types // Chase through type_alias_map, then look up named types
@@ -940,13 +941,11 @@ pub const Lowering = struct {
// type_alias_map at use time. // type_alias_map at use time.
const rhs_name = cd.value.data.identifier.name; const rhs_name = cd.value.data.identifier.name;
if (self.program_index.type_alias_map.get(rhs_name)) |chained| { if (self.program_index.type_alias_map.get(rhs_name)) |chained| {
self.program_index.type_alias_map.put(cd.name, chained) catch {}; self.putTypeAlias(self.current_source_file, cd.name, chained);
self.recordTypeAliasBySource(self.current_source_file, cd.name, chained);
} else { } else {
const name_id = self.module.types.internString(rhs_name); const name_id = self.module.types.internString(rhs_name);
if (self.module.types.findByName(name_id)) |tid| { if (self.module.types.findByName(name_id)) |tid| {
self.program_index.type_alias_map.put(cd.name, tid) catch {}; self.putTypeAlias(self.current_source_file, cd.name, tid);
self.recordTypeAliasBySource(self.current_source_file, cd.name, tid);
} }
} }
} }
@@ -972,6 +971,13 @@ pub const Lowering = struct {
} }; } };
const alias_id = if (self.module.types.findByName(alias_name_id)) |existing| existing else self.module.types.intern(alias_info); const alias_id = if (self.module.types.findByName(alias_name_id)) |existing| existing else self.module.types.intern(alias_info);
self.module.types.updatePreservingKey(alias_id, alias_info); self.module.types.updatePreservingKey(alias_id, alias_info);
// A generic-struct instantiation alias IS a type
// author: route it through the unified writer so it
// lands in `type_aliases_by_source` and the bare-TYPE
// gate treats it like any other alias (a ns-only
// `Secret :: Box(s32)` is rejected, a flat one
// resolves to the same TypeId `findByName` would).
self.putTypeAlias(self.current_source_file, cd.name, alias_id);
} }
} else if (std.mem.eql(u8, callee_name, "Vector")) { } else if (std.mem.eql(u8, callee_name, "Vector")) {
// Builtin type constructor — checked BEFORE // Builtin type constructor — checked BEFORE
@@ -983,15 +989,13 @@ pub const Lowering = struct {
// hard-codes the vector layout. // hard-codes the vector layout.
const result_ty = self.resolveTypeCallWithBindings(call_data); const result_ty = self.resolveTypeCallWithBindings(call_data);
if (result_ty != .void) { if (result_ty != .void) {
self.program_index.type_alias_map.put(cd.name, result_ty) catch {}; self.putTypeAlias(self.current_source_file, cd.name, result_ty);
self.recordTypeAliasBySource(self.current_source_file, cd.name, result_ty);
} }
} else if (self.program_index.fn_ast_map.get(callee_name)) |fd| { } else if (self.program_index.fn_ast_map.get(callee_name)) |fd| {
// Type-returning function: Foo :: Complex(u32) // Type-returning function: Foo :: Complex(u32)
if (fd.type_params.len > 0) { if (fd.type_params.len > 0) {
if (self.instantiateTypeFunction(cd.name, callee_name, fd, call_data.args)) |result_ty| { if (self.instantiateTypeFunction(cd.name, callee_name, fd, call_data.args)) |result_ty| {
self.program_index.type_alias_map.put(cd.name, result_ty) catch {}; self.putTypeAlias(self.current_source_file, cd.name, result_ty);
self.recordTypeAliasBySource(self.current_source_file, cd.name, result_ty);
} }
} }
} }
@@ -1011,6 +1015,10 @@ pub const Lowering = struct {
} }; } };
const alias_id = if (self.module.types.findByName(alias_name_id)) |existing| existing else self.module.types.intern(alias_info); const alias_id = if (self.module.types.findByName(alias_name_id)) |existing| existing else self.module.types.intern(alias_info);
self.module.types.updatePreservingKey(alias_id, alias_info); self.module.types.updatePreservingKey(alias_id, alias_info);
// Same as the `.call` generic-struct branch: a
// parameterized-struct alias is a type author and
// must reach `type_aliases_by_source` so it gates.
self.putTypeAlias(self.current_source_file, cd.name, alias_id);
} }
} else { } else {
// Builtin parameterised type (Vector(N, T) etc) — // Builtin parameterised type (Vector(N, T) etc) —
@@ -1019,8 +1027,7 @@ pub const Lowering = struct {
// position can `const_type(<vector tid>)`. // position can `const_type(<vector tid>)`.
const result_ty = type_bridge.resolveAstType(cd.value, &self.module.types, &self.program_index.type_alias_map, &self.program_index.module_const_map); const result_ty = type_bridge.resolveAstType(cd.value, &self.module.types, &self.program_index.type_alias_map, &self.program_index.module_const_map);
if (result_ty != .void and result_ty != .unresolved) { if (result_ty != .void and result_ty != .unresolved) {
self.program_index.type_alias_map.put(cd.name, result_ty) catch {}; self.putTypeAlias(self.current_source_file, cd.name, result_ty);
self.recordTypeAliasBySource(self.current_source_file, cd.name, result_ty);
} }
} }
} }
@@ -1045,8 +1052,7 @@ pub const Lowering = struct {
}; };
if (lit_ty) |ty| { if (lit_ty) |ty| {
const info = program_index_mod.ModuleConstInfo{ .value = cd.value, .ty = ty }; const info = program_index_mod.ModuleConstInfo{ .value = cd.value, .ty = ty };
self.program_index.module_const_map.put(cd.name, info) catch {}; self.putModuleConst(self.current_source_file, cd.name, info);
self.recordModuleConstBySource(self.current_source_file, cd.name, info);
} }
} }
}, },
@@ -1126,8 +1132,7 @@ pub const Lowering = struct {
// don't pile a bogus type-mismatch on top, and don't leave the pass-0 // don't pile a bogus type-mismatch on top, and don't leave the pass-0
// placeholder behind as a usable const. // placeholder behind as a usable const.
if (ty == .unresolved) { if (ty == .unresolved) {
_ = self.program_index.module_const_map.remove(cd.name); self.dropModuleConst(self.current_source_file, cd.name);
self.dropModuleConstBySource(self.current_source_file, cd.name);
return; return;
} }
// Validate the initializer against the explicit annotation BY TYPE, so a // Validate the initializer against the explicit annotation BY TYPE, so a
@@ -1146,8 +1151,7 @@ pub const Lowering = struct {
if (self.isIntEx(ty) and isFloat(self.inferExprType(cd.value))) { if (self.isIntEx(ty) and isFloat(self.inferExprType(cd.value))) {
if (program_index_mod.evalConstFloatExpr(cd.value, self)) |fv| { if (program_index_mod.evalConstFloatExpr(cd.value, self)) |fv| {
self.diagNonIntegralNarrow(cd.value.span, fv, ty); self.diagNonIntegralNarrow(cd.value.span, fv, ty);
_ = self.program_index.module_const_map.remove(cd.name); self.dropModuleConst(self.current_source_file, cd.name);
self.dropModuleConstBySource(self.current_source_file, cd.name);
return; return;
} }
} }
@@ -1159,16 +1163,14 @@ pub const Lowering = struct {
// Evict the pass-0 placeholder (`N : string : 4` and // Evict the pass-0 placeholder (`N : string : 4` and
// `N : string : M + 2` are both pre-registered as `.s64` in scanDecls // `N : string : M + 2` are both pre-registered as `.s64` in scanDecls
// pass 0); leaving it would let a count use still fold `N`. // pass 0); leaving it would let a count use still fold `N`.
_ = self.program_index.module_const_map.remove(cd.name); self.dropModuleConst(self.current_source_file, cd.name);
self.dropModuleConstBySource(self.current_source_file, cd.name);
return; return;
} }
// Reconcile the registration with the resolved annotation (pass 0 stored // Reconcile the registration with the resolved annotation (pass 0 stored
// a literal/expression placeholder type), so the const folds and emits at // a literal/expression placeholder type), so the const folds and emits at
// its declared type — the same `put` the literal path always did. // its declared type — the same `put` the literal path always did.
const info = program_index_mod.ModuleConstInfo{ .value = cd.value, .ty = ty }; const info = program_index_mod.ModuleConstInfo{ .value = cd.value, .ty = ty };
self.program_index.module_const_map.put(cd.name, info) catch {}; self.putModuleConst(self.current_source_file, cd.name, info);
self.recordModuleConstBySource(self.current_source_file, cd.name, info);
} }
/// True iff a literal initializer of `value`'s kind is faithfully /// True iff a literal initializer of `value`'s kind is faithfully
@@ -1284,8 +1286,7 @@ pub const Lowering = struct {
.is_const = false, .is_const = false,
.is_extern = vd.is_foreign, .is_extern = vd.is_foreign,
}); });
self.program_index.global_names.put(vd.name, .{ .id = gid, .ty = var_ty }) catch {}; self.putGlobal(self.current_source_file, vd.name, .{ .id = gid, .ty = var_ty });
self.recordGlobalBySource(self.current_source_file, vd.name, .{ .id = gid, .ty = var_ty });
} }
/// Serialize a top-level global's initializer into a static `ConstantValue`. /// Serialize a top-level global's initializer into a static `ConstantValue`.
@@ -1380,14 +1381,12 @@ pub const Lowering = struct {
if (self.program_index.type_alias_map.contains(cd.name)) continue; if (self.program_index.type_alias_map.contains(cd.name)) continue;
const rhs_name = cd.value.data.identifier.name; const rhs_name = cd.value.data.identifier.name;
if (self.program_index.type_alias_map.get(rhs_name)) |chained| { if (self.program_index.type_alias_map.get(rhs_name)) |chained| {
self.program_index.type_alias_map.put(cd.name, chained) catch {}; self.putTypeAlias(decl.source_file, cd.name, chained);
self.recordTypeAliasBySource(decl.source_file, cd.name, chained);
progressed = true; progressed = true;
} else { } else {
const name_id = self.module.types.internString(rhs_name); const name_id = self.module.types.internString(rhs_name);
if (self.module.types.findByName(name_id)) |tid| { if (self.module.types.findByName(name_id)) |tid| {
self.program_index.type_alias_map.put(cd.name, tid) catch {}; self.putTypeAlias(decl.source_file, cd.name, tid);
self.recordTypeAliasBySource(decl.source_file, cd.name, tid);
progressed = true; progressed = true;
} }
} }
@@ -9940,8 +9939,7 @@ pub const Lowering = struct {
}); });
// Register for runtime lookup: identifier resolution emits global_get // Register for runtime lookup: identifier resolution emits global_get
self.program_index.global_names.put(name, .{ .id = gid, .ty = global_ty }) catch {}; self.putGlobal(self.current_source_file, name, .{ .id = gid, .ty = global_ty });
self.recordGlobalBySource(self.current_source_file, name, .{ .id = gid, .ty = global_ty });
} }
/// Lower a standalone `#run expr;` at the top level (side-effect only). /// Lower a standalone `#run expr;` at the top level (side-effect only).
@@ -14730,8 +14728,7 @@ pub const Lowering = struct {
.init_val = .{ .aggregate = ctx_fields }, .init_val = .{ .aggregate = ctx_fields },
.is_const = true, .is_const = true,
}); });
self.program_index.global_names.put(global_name, .{ .id = gid, .ty = ctx_ty }) catch {}; self.putGlobal(self.current_source_file, global_name, .{ .id = gid, .ty = ctx_ty });
self.recordGlobalBySource(self.current_source_file, global_name, .{ .id = gid, .ty = ctx_ty });
} }
/// Create a thunk function: __thunk_ConcreteType_Protocol_method(ctx: *void, args...) -> ret /// Create a thunk function: __thunk_ConcreteType_Protocol_method(ctx: *void, args...) -> ret