do not enforce function parameters to be marked comptime if only called at comptime
Fixes #18321 Fixes #16454 Fixes #1383
nice
Provide an example here in case it helps:
const std = @import("std");
const EnumMap = std.EnumMap;
const Ty = enum(u8) {
a,
b,
};
const HANDLERS_MAP = EnumMap(Ty, fn () callconv(.Inline) void).init(.{
.a = fa,
.b = fb,
});
inline fn fa() void {
std.debug.print("fa\n", .{});
}
inline fn fb() void {
std.debug.print("fb\n", .{});
}
pub fn main() !void {
const ty = Ty.b;
HANDLERS_MAP.get(ty).?();
}
After adding each comptime as told by compiler:
lib/std/enums.zig:277:29: error: parameter of type 'enums.EnumFieldStruct(test.Ty,?fn () callconv(.Inline) void,null)' must be declared comptime
pub fn init(init_values: EnumFieldStruct(E, ?V, @as(?V, null))) Self {
lib/std/enums.zig:1054:20: error: parameter of type 'enums.IndexedMap(enums.EnumIndexer(test.Ty),fn () callconv(.Inline) void,(function 'EnumMapExt'))' must be declared comptime
pub fn get(self: Self, key: Key) ?Value {
lib/std/enums.zig:1054:51: error: function with comptime-only return type '?fn () callconv(.Inline) void' requires all parameters to be comptime
pub fn get(comptime self: Self, key: Key) ?Value {
^~~~~~
Successfully compiled.
yes, without this change using many structures with comptime-only types is impossible while making it so they can continue to work with runtime types as well
outside the one lib/std/comptime_string_map.zig, I should add one in test/behavior/ as well?
Yes, the goal is for all behavior to be covered by behavior tests. Standard library tests are intended only to provide coverage for the standard library functionality; they are not double-purposed as coverage for the language itself.