With 0.6.0, @ptrToInt() surprisingly cannot be used as a constant expression:

The contents of the aforementioned function_table.zig minimal reproduction case:
const std = @import("std");
const testing = std.testing;
export fn my_func() void {}
const ExportedFunction = struct {
name: []const u8,
ptr: usize, // function types vary
};
const functions = [_]ExportedFunction {
.{.name = "my_func", .ptr = @ptrToInt(&my_func)},
};
test "reproduce error: unable to evaluate constant expression" {
testing.expect(functions[0].ptr != 0);
}
By the way, in case there's any better way to create a function lookup table storing function pointers (with varying function type signatures) to exported functions, I'm all ears...
The error is correct since the address of the function is not compile time known. If you want to make a function lookup table you have a few different choises:
@ptrCast(fn()void, my_func) and then @ptrCast again when you want to call it.@ptrToInt.const std = @import("std");
const testing = std.testing;
export fn my_func() void {}
export fn my_other_func() u32 {return 0;}
const functions = .{
.my_func = my_func,
.my_other_func = my_other_func,
};
test "reproduce error: unable to evaluate constant expression" {
testing.expect(@ptrToInt(functions.my_func) != 0);
testing.expect(@ptrToInt(functions.my_other_func) != 0);
}
Kind thanks, @Vexu, that is most helpful. TIL. Closing the issue since it's not a bug.
Most helpful comment
The error is correct since the address of the function is not compile time known. If you want to make a function lookup table you have a few different choises:
@ptrCast(fn()void, my_func)and then@ptrCastagain when you want to call it.@ptrToInt.