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

verifyContext seems too strict when using adapted HashTable functions #19640

Open
vkitchen opened this issue Apr 13, 2024 · 0 comments
Open

verifyContext seems too strict when using adapted HashTable functions #19640

vkitchen opened this issue Apr 13, 2024 · 0 comments
Labels
bug Observed behavior contradicts documented or intended behavior

Comments

@vkitchen
Copy link

Zig Version

0.12.0-dev.3639+9cfac4718

Steps to Reproduce and Observed Behavior

The following minimal reproduction compiles with an unexpected error. Where types are unequal due to constness however it is in the direction where a const function can't receive a non-const parameter

const std = @import("std");

pub fn main() !void {
    var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
    const allocator = arena.allocator();

    const string_context: std.hash_map.StringContext = .{};
    var hash_map = std.StringHashMap([]u8).init(allocator);

    const key = try allocator.dupe(u8, "key");
    const gop = try hash_map.getOrPutAdapted(key, string_context);
    if (!gop.found_existing) {
        gop.key_ptr.* = key;
    }
}

Compiles with the following error

> zig build-exe test.zig
/usr/local/zig-linux-x86_64-0.12.0-dev.3639+9cfac4718/lib/std/hash_map.zig:340:13: error: Problems found with hash context type hash_map.StringContext:
                                                                                                hash_map.StringContext.hash must be fn (self, []u8) u64
                                                                                                  but is actually fn (hash_map.StringContext, []const u8) u64
                                                                                                  Second parameter must be []u8, but is []const u8
                                                                                                hash_map.StringContext.eql must be fn (self, []u8, []const u8) bool
                                                                                                  but is actually fn (hash_map.StringContext, []const u8, []const u8) bool
                                                                                                  Second parameter must be []u8, but is []const u8
            @compileError("Problems found with hash context type " ++ @typeName(Context) ++ ":" ++ errors);
            ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/local/zig-linux-x86_64-0.12.0-dev.3639+9cfac4718/lib/std/hash_map.zig:1174:35: note: called from here
            comptime verifyContext(@TypeOf(ctx), @TypeOf(key), K, Hash, false);
                     ~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/local/zig-linux-x86_64-0.12.0-dev.3639+9cfac4718/lib/std/hash_map.zig:1337:48: note: called from here
                    const index = self.getIndex(key, key_ctx) orelse return err;

Expected Behavior

The program to compile as casting to const is safe

@vkitchen vkitchen added the bug Observed behavior contradicts documented or intended behavior label Apr 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Observed behavior contradicts documented or intended behavior
Projects
None yet
Development

No branches or pull requests

1 participant