fix(0114): gate alias-qualified calls to one-level carry, pin to target

The lowerCall namespace branch routed alias.fn() through the global
qualified registration (first-wins) at any import depth, and through the
global last-wins bare map for comptime/generic members. Plain-identifier
alias roots now resolve via the carry-aware namespaceAliasVerdict:

- visible alias (own edge or ONE flat hop): the member dispatches the
  TARGET module's own fn (namespaceFnMember + fd-keyed bareAuthorFuncId),
  so two modules' same-named aliases each call their own target.
- two direct flat imports carrying the alias to distinct targets:
  loud ambiguity diagnostic.
- alias only reachable beyond one hop: "namespace 'X' is not visible".
- foreign / builtin / #compiler members keep the literal-symbol path.

Regressions: examples 0832 (two-hop), 0833 (carried collision),
0834 (own-target pin / first-wins repair).
This commit is contained in:
agra
2026-06-11 09:16:03 +03:00
parent 22552075d5
commit fbbfcb268c
26 changed files with 196 additions and 14 deletions

View File

@@ -1131,34 +1131,80 @@ pub const Lowering = struct {
};
}
/// Carry-rule resolution outcome for a namespace alias, diagnostic-free.
pub const AliasVerdict = union(enum) {
/// No edge anywhere visible from the current file binds this alias.
none,
/// ≥2 DIRECT flat imports carry the alias to DISTINCT targets.
ambiguous,
/// The alias resolves — own edge, or carried over one flat hop.
target: imports_mod.NamespaceTarget,
};
/// Resolve a namespace alias visible from the current source file under
/// the carry rule: the file's OWN `ns :: #import` edge wins; otherwise an
/// alias declared by a DIRECT flat import is carried (one level — flat
/// edges of flat edges do not chain). Two distinct carried targets for
/// the same alias diagnose as ambiguous and resolve to null.
pub fn namespaceAliasTarget(self: *Lowering, alias: []const u8, span: ?ast.Span) ?imports_mod.NamespaceTarget {
const edges = self.program_index.namespace_edges orelse return null;
const from = self.current_source_file orelse return null;
/// the same alias are ambiguous.
pub fn namespaceAliasVerdict(self: *Lowering, alias: []const u8) AliasVerdict {
const edges = self.program_index.namespace_edges orelse return .none;
const from = self.current_source_file orelse return .none;
if (edges.getPtr(from)) |own| {
if (own.get(alias)) |t| return t;
if (own.get(alias)) |t| return .{ .target = t };
}
const flat = self.program_index.flat_import_graph orelse return null;
const direct = flat.get(from) orelse return null;
const flat = self.program_index.flat_import_graph orelse return .none;
const direct = flat.get(from) orelse return .none;
var found: ?imports_mod.NamespaceTarget = null;
var it = direct.keyIterator();
while (it.next()) |dep| {
const dep_edges = edges.getPtr(dep.*) orelse continue;
const t = dep_edges.get(alias) orelse continue;
if (found) |f| {
if (!std.mem.eql(u8, f.target_module_path, t.target_module_path)) {
if (self.diagnostics) |d| {
d.addFmt(.err, span, "namespace '{s}' is ambiguous: aliases from multiple flat-imported modules point at different targets; declare the alias locally", .{alias});
}
return null;
}
if (!std.mem.eql(u8, f.target_module_path, t.target_module_path)) return .ambiguous;
} else found = t;
}
return found;
return if (found) |f| .{ .target = f } else .none;
}
/// `namespaceAliasVerdict` with the ambiguity diagnosed in place; callers
/// that don't distinguish ambiguous-from-missing use this form.
pub fn namespaceAliasTarget(self: *Lowering, alias: []const u8, span: ?ast.Span) ?imports_mod.NamespaceTarget {
switch (self.namespaceAliasVerdict(alias)) {
.target => |t| return t,
.ambiguous => {
if (self.diagnostics) |d| {
d.addFmt(.err, span, "namespace '{s}' is ambiguous: aliases from multiple flat-imported modules point at different targets; declare the alias locally", .{alias});
}
return null;
},
.none => return null,
}
}
/// True when ANY module in the program declares `alias` as a namespace
/// edge — distinguishes a not-visible alias (gate error) from a name that
/// was never an alias at all (fall through to other resolution).
pub fn aliasDeclaredAnywhere(self: *Lowering, alias: []const u8) bool {
const edges = self.program_index.namespace_edges orelse return false;
var it = edges.valueIterator();
while (it.next()) |per_file| {
if (per_file.contains(alias)) return true;
}
return false;
}
/// The target module's own fn member named `name` — a top-level fn decl
/// or a const-wrapped fn (the same surface `registerNamespaceQualifiedFns`
/// registers). Null when the member is absent or not a function.
pub fn namespaceFnMember(target: *const imports_mod.NamespaceTarget, name: []const u8) ?*const ast.FnDecl {
for (target.own_decls) |decl| {
switch (decl.data) {
.fn_decl => |*fd| if (std.mem.eql(u8, fd.name, name)) return fd,
.const_decl => |*cd| if (std.mem.eql(u8, cd.name, name) and cd.value.data == .fn_decl) return &cd.value.data.fn_decl,
else => {},
}
}
return null;
}
/// The inner member name when `node` is a namespace-rooted prefix

View File

@@ -708,6 +708,51 @@ pub fn lowerCall(self: *Lowering, c_in: *const ast.Call) Ref {
std.fmt.allocPrint(self.alloc, "{s}.{s}", .{ n, fa.field }) catch func_name
else
func_name;
// The carry gate (issue 0114): a plain-identifier root that is
// a namespace ALIAS (not a type / fn global name — those are
// the `Type.method` paths below) must be visible under the
// carry rule, and its fn members dispatch pinned to the
// alias's TARGET module — never the global first-wins
// qualified registration, never the last-wins bare fallback.
gate: {
if (fa.object.data != .identifier) break :gate;
const oname = fa.object.data.identifier.name;
if (self.program_index.global_names.contains(oname)) break :gate;
switch (self.namespaceAliasVerdict(oname)) {
.target => |target| {
const fd = Lowering.namespaceFnMember(&target, fa.field) orelse break :gate;
// Foreign / builtin / #compiler bodies keep their
// literal global symbol — the existing bare-name
// machinery below resolves them.
switch (fd.body.data) {
.foreign_expr, .builtin_expr, .compiler_expr => break :gate,
else => {},
}
if (hasComptimeParams(fd)) return self.lowerComptimeCall(fd, c);
if (fd.type_params.len > 0) return self.lowerGenericCall(fd, fa.field, c, args.items);
var sf = SelectedFunc{ .decl = fd, .source = target.target_module_path };
const fid = self.selectedFuncId(&sf, fa.field);
const func = &self.module.functions.items[@intFromEnum(fid)];
self.packVariadicCallArgs(fd, c, &args);
const final_args = self.prependCtxIfNeeded(func, args.items);
self.coerceCallArgs(final_args, func.params);
if (func.is_variadic) self.promoteCVariadicArgs(final_args, func.params.len);
return self.builder.call(fid, final_args, func.ret);
},
.ambiguous => {
if (self.diagnostics) |d|
d.addFmt(.err, fa.object.span, "namespace '{s}' is ambiguous: aliases from multiple flat-imported modules point at different targets; declare the alias locally", .{oname});
return Ref.none;
},
.none => {
if (self.aliasDeclaredAnywhere(oname)) {
if (self.diagnostics) |d|
d.addFmt(.err, fa.object.span, "namespace '{s}' is not visible; #import the module that declares it", .{oname});
return Ref.none;
}
},
}
}
// Check for comptime-expanded or generic functions (try both names)
const effective_name = if (self.program_index.fn_ast_map.get(qualified_name) != null) qualified_name else func_name;
if (self.program_index.fn_ast_map.get(effective_name)) |fd| {