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:
@@ -1622,9 +1622,26 @@ pub fn lowerExpr(self: *Lowering, node: *const Node) Ref {
|
||||
};
|
||||
break :blk self.builder.load(self.current_ctx_ref, ctx_ty);
|
||||
}
|
||||
// Check globals (#run constants)
|
||||
// Check globals (#run constants) — source-aware (issue 0115):
|
||||
// the global registry is last-wins across modules, so select the
|
||||
// AUTHOR first and emit ITS global, never an unrelated module's
|
||||
// same-named one.
|
||||
if (self.program_index.global_names.get(id.name)) |gi| {
|
||||
break :blk self.builder.emit(.{ .global_get = gi.id }, gi.ty);
|
||||
switch (self.selectGlobalAuthor(id.name)) {
|
||||
.resolved => |g| break :blk self.builder.emit(.{ .global_get = g.id }, g.ty),
|
||||
.not_a_global => {},
|
||||
.ambiguous => {
|
||||
if (self.diagnostics) |d|
|
||||
d.addFmt(.err, node.span, "'{s}' is ambiguous: it is declared in multiple flat-imported modules; qualify the reference or remove the duplicate import", .{id.name});
|
||||
break :blk self.emitPlaceholder(id.name);
|
||||
},
|
||||
.not_visible => {
|
||||
if (self.diagnostics) |d|
|
||||
d.addFmt(.err, node.span, "'{s}' is not visible; #import the module that declares it", .{id.name});
|
||||
break :blk self.emitError(id.name, node.span);
|
||||
},
|
||||
.untracked => break :blk self.builder.emit(.{ .global_get = gi.id }, gi.ty),
|
||||
}
|
||||
}
|
||||
// Check module-level value constants (e.g. AF_INET :s32: 2)
|
||||
if (self.program_index.module_const_map.get(id.name)) |ci_global| {
|
||||
@@ -1640,6 +1657,10 @@ pub fn lowerExpr(self: *Lowering, node: *const Node) Ref {
|
||||
// author (no per-source partition) — emit its global value.
|
||||
switch (self.selectModuleConst(id.name)) {
|
||||
.resolved => |sel| break :blk self.emitModuleConst(sel.info, sel.source),
|
||||
// Own const author with no materialized value (unsupported
|
||||
// shape, e.g. an array const) — fall through; the tail of
|
||||
// identifier lowering diagnoses it as unresolved.
|
||||
.own_opaque => {},
|
||||
.ambiguous => {
|
||||
if (self.diagnostics) |d|
|
||||
d.addFmt(.err, node.span, "'{s}' is ambiguous: it is declared in multiple flat-imported modules; qualify the reference or remove the duplicate import", .{id.name});
|
||||
@@ -1651,7 +1672,16 @@ pub fn lowerExpr(self: *Lowering, node: *const Node) Ref {
|
||||
// Check if it's a function name — produce function pointer reference
|
||||
// Resolve mangled name for block-local functions
|
||||
const eff_fn_name = if (self.scope) |scope| scope.lookupFn(id.name) orelse id.name else id.name;
|
||||
if (self.program_index.fn_ast_map.contains(eff_fn_name)) {
|
||||
// An own fn whose name a flat-merge collision dropped from the
|
||||
// global decl list (first-wins) has no `fn_ast_map` entry but IS
|
||||
// a raw-facts author — the author selection inside this arm
|
||||
// serves it, so admit it through the gate.
|
||||
const fn_author_only = !self.program_index.fn_ast_map.contains(eff_fn_name) and
|
||||
std.mem.eql(u8, eff_fn_name, id.name) and
|
||||
(if (self.scope) |scope| scope.lookup(id.name) == null else true) and
|
||||
self.current_source_file != null and
|
||||
self.selectPlainCallableAuthor(id.name, self.current_source_file.?) == .func;
|
||||
if (self.program_index.fn_ast_map.contains(eff_fn_name) or fn_author_only) {
|
||||
// Visibility check only for user-typed bare names (id.name
|
||||
// == eff_fn_name) without a UFCS alias. Mangled local-
|
||||
// scope names and UFCS rewrites are compiler indirections
|
||||
@@ -1666,11 +1696,18 @@ pub fn lowerExpr(self: *Lowering, node: *const Node) Ref {
|
||||
}
|
||||
// Type-as-value: if target is Any (Type variable), produce a type name string
|
||||
if (self.target_type == .any) {
|
||||
const fd = self.program_index.fn_ast_map.get(eff_fn_name).?;
|
||||
const fn_type_str = self.formatFnTypeString(fd);
|
||||
const sid = self.module.types.internString(fn_type_str);
|
||||
const str = self.builder.constString(sid);
|
||||
break :blk self.builder.boxAny(str, .string);
|
||||
const fd_any: ?*const ast.FnDecl = self.program_index.fn_ast_map.get(eff_fn_name) orelse fd_blk: {
|
||||
switch (self.selectPlainCallableAuthor(id.name, self.current_source_file.?)) {
|
||||
.func => |sf| break :fd_blk sf.decl,
|
||||
else => break :fd_blk null,
|
||||
}
|
||||
};
|
||||
if (fd_any) |fd| {
|
||||
const fn_type_str = self.formatFnTypeString(fd);
|
||||
const sid = self.module.types.internString(fn_type_str);
|
||||
const str = self.builder.constString(sid);
|
||||
break :blk self.builder.boxAny(str, .string);
|
||||
}
|
||||
}
|
||||
// taking a bare same-name fn as a VALUE
|
||||
// (func_ref, fn-ptr / closure coercion) must capture the
|
||||
@@ -1835,7 +1872,7 @@ pub fn lowerExpr(self: *Lowering, node: *const Node) Ref {
|
||||
}
|
||||
}
|
||||
// address_of(global) → emit global_addr (pointer to global, not load)
|
||||
if (self.program_index.global_names.get(id_name)) |gi| {
|
||||
if (self.resolveGlobalRef(id_name, node.span)) |gi| {
|
||||
const ptr_ty = self.module.types.ptrTo(gi.ty);
|
||||
break :blk self.builder.emit(.{ .global_addr = gi.id }, ptr_ty);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user