diff --git a/src/ir/lower.test.zig b/src/ir/lower.test.zig index 7a97a16..eb682e9 100644 --- a/src/ir/lower.test.zig +++ b/src/ir/lower.test.zig @@ -1400,4 +1400,30 @@ test "lower: shadowed same-name author gets its own FuncId + real body (fix-0102 } try std.testing.expect(first != null and second != null); try std.testing.expect(first.? != second.?); + + // F1 (attempt-2): the identity map must be keyed by the STABLE AST field + // pointer for BOTH same-name authors — the exact pointers `fn_ast_map` and + // `module_fns` carry — not a per-iteration switch-capture temporary. If the + // winner were keyed by `&fd` (the scanDecls bug), this lookup by the stable + // `fn_ast_map` pointer would miss (null). fix-0102c routes calls through + // exactly these pointers, so the round-trip must hold here. + const winner_fd = lowering.program_index.fn_ast_map.get("greet").?; + const winner_fid = lowering.fn_decl_fids.get(winner_fd); + try std.testing.expect(winner_fid != null); + // Round-trips to the first-wins winner FuncId (resolveFuncByName's pick). + try std.testing.expectEqual(lowering.resolveFuncByName("greet").?, winner_fid.?); + + // The shadowed author's stable pointer lives in `module_fns`; find the one + // that is NOT the winner and confirm IT round-trips to a DISTINCT FuncId. + var shadow_fd: ?*const ast.FnDecl = null; + var mf_it = module_fns.iterator(); + while (mf_it.next()) |path_entry| { + if (path_entry.value_ptr.get("greet")) |fd| { + if (fd != winner_fd) shadow_fd = fd; + } + } + try std.testing.expect(shadow_fd != null); + const shadow_fid = lowering.fn_decl_fids.get(shadow_fd.?); + try std.testing.expect(shadow_fid != null); + try std.testing.expect(shadow_fid.? != winner_fid.?); } diff --git a/src/ir/lower.zig b/src/ir/lower.zig index f65ec13..5c7f48b 100644 --- a/src/ir/lower.zig +++ b/src/ir/lower.zig @@ -791,8 +791,14 @@ pub const Lowering = struct { self.program_index.fn_ast_map.put(fd.name, &decl.data.fn_decl) catch {}; self.program_index.import_flags.put(fd.name, is_imported) catch {}; } - // Declare extern stub for all functions (bodies lowered lazily) - self.declareFunction(&fd, fd.name); + // Declare extern stub for all functions (bodies lowered + // lazily). Key the identity map (`fn_decl_fids`, inside + // `declareFunction`) by the STABLE AST field pointer — the + // same `&decl.data.fn_decl` stored in `fn_ast_map` and + // `module_fns` — not the switch-capture copy `fd`, whose + // address is a per-iteration stack temporary that no later + // decl-identity lookup can reproduce. + self.declareFunction(&decl.data.fn_decl, fd.name); }, .const_decl => |cd| { if (cd.value.data == .fn_decl) {