Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Jan 19, 2024

Conversation

nektro
Copy link
Contributor

@nektro nektro commented Dec 20, 2023

Fixes #18321
Fixes #16454
Fixes #1383

@Jarred-Sumner
Copy link
Contributor

nice

@Vexu Vexu assigned andrewrk and unassigned andrewrk Dec 21, 2023
@Vexu Vexu requested a review from andrewrk December 21, 2023 14:09
@whfuyn
Copy link

whfuyn commented Dec 22, 2023

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.

@nektro
Copy link
Contributor Author

nektro commented Dec 22, 2023

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

Copy link
Member

@andrewrk andrewrk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lacks a behavior test.

@nektro
Copy link
Contributor Author

nektro commented Jan 9, 2024

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

@andrewrk
Copy link
Member

andrewrk commented Jan 9, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants