zig icon indicating copy to clipboard operation
zig copied to clipboard

do not enforce function parameters to be marked comptime if only called at comptime

Open nektro opened this issue 2 years ago • 3 comments

Fixes #18321 Fixes #16454 Fixes #1383

nektro avatar Dec 20 '23 21:12 nektro

nice

Jarred-Sumner avatar Dec 21 '23 00:12 Jarred-Sumner

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.

whfuyn avatar Dec 22 '23 11:12 whfuyn

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

nektro avatar Dec 22 '23 19:12 nektro

outside the one lib/std/comptime_string_map.zig, I should add one in test/behavior/ as well?

nektro avatar Jan 09 '24 07:01 nektro

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.

andrewrk avatar Jan 09 '24 07:01 andrewrk