I have a hashmap from u32 to some union. I have a "minimal" example bellow (the binary is not exactly master but close enough, and I haven't seen anything about this in the issues.)
const std = @import("std");
const mem = @import("std").mem;
const ValueRef = u32;
const BlockRef = u32;
pub const ValuePool = struct {
values: ValueMap = .{},
value_counter: ValueRef = 0,
const ValueMap = std.AutoHashMapUnmanaged(ValueRef, ValueData);
pub fn deinit(self: *ValuePool, allocator: mem.Allocator) void {
self.values.deinit(allocator);
}
pub fn put(self: *ValuePool, allocator: mem.Allocator, value: ValueData) mem.Allocator.Error!void {
try self.values.put(allocator, self.value_counter, value);
}
pub fn iterator(self: ValuePool) ValueMap.Iterator {
return self.values.iterator();
}
};
pub const Block = struct {
values: ValuePool = .{},
pub fn deinit(self: *Block, allocator: mem.Allocator) void {
self.values.deinit(allocator);
}
pub fn formata(self: Block) !void {
var iter = self.values.iterator();
while (iter.next()) |kv| {
std.debug.assert(@tagName(kv.value_ptr.*).len > 0); // if I remove this line, it still segfaults
}
}
};
pub const formatter = struct {
block: Block = .{},
pub fn format(
self: formatter,
comptime _: []const u8,
_: std.fmt.FormatOptions,
_: anytype,
) !void {
try self.block.formata();
}
};
pub const ValueData = union(enum) {
param: struct { idx: usize },
inst: i32,
};
pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
var allocator = gpa.allocator();
var f = formatter{};
defer f.block.deinit(allocator);
_ = try f.block.values.put(
allocator,
ValueData{ .param = .{ .idx = 0 } },
);
std.log.info("{}", .{f});
}```