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:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user