Skip to content

operation is runtime due to this operand for @intFromPtr #23072

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

Open
HyperCodec opened this issue Mar 4, 2025 · 2 comments
Open

operation is runtime due to this operand for @intFromPtr #23072

HyperCodec opened this issue Mar 4, 2025 · 2 comments
Labels
bug Observed behavior contradicts documented or intended behavior

Comments

@HyperCodec
Copy link

Zig Version

0.13.0 (or latest 0.14.0 dev)

Steps to Reproduce and Observed Behavior

Following an example for comptime JSON, I wrote the following:

pub const dictionary = d: {
    const dict_raw = @embedFile("dict.json");

    // TODO figure out buffer size
    var buf: [1024]u8 = undefined;
    var fba = std.heap.FixedBufferAllocator.init(&buf);
    const alloc = fba.allocator();

    const words = std.json.parseFromSliceLeaky([]WordData, alloc, dict_raw, .{}) catch unreachable;

    var dict = std.StringHashMap([]WordData).init(alloc);

   // TODO multi map entries

    break :d dict;
};

However, I get the following error when trying to build:

C:\Users\trist\zigup\zig\0.13.0\files\lib\std\mem.zig:3790:18: error: unable to evaluate comptime expression 
    const addr = @intFromPtr(ptr);
                 ^~~~~~~~~~~~~~~~
C:\Users\trist\zigup\zig\0.13.0\files\lib\std\mem.zig:3790:30: note: operation is runtime due to this operand
    const addr = @intFromPtr(ptr);
                             ^~~
C:\Users\trist\zigup\zig\0.13.0\files\lib\std\heap.zig:427:50: note: called from here
        const adjust_off = mem.alignPointerOffset(self.buffer.ptr + self.end_index, ptr_align) orelse return null;
                           ~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
C:\Users\trist\zigup\zig\0.13.0\files\lib\std\mem\Allocator.zig:86:29: note: called from here
    return self.vtable.alloc(self.ptr, len, ptr_align, ret_addr);
           ~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
C:\Users\trist\zigup\zig\0.13.0\files\lib\std\mem\Allocator.zig:225:35: note: called from here
    const byte_ptr = self.rawAlloc(byte_count, log2a(alignment), return_address) orelse return Error.OutOfMemory;
                     ~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
C:\Users\trist\zigup\zig\0.13.0\files\lib\std\mem\Allocator.zig:211:40: note: called from here
    return self.allocBytesWithAlignment(alignment, byte_count, return_address);
           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
C:\Users\trist\zigup\zig\0.13.0\files\lib\std\mem\Allocator.zig:205:75: note: called from here
    const ptr: [*]align(a) T = @ptrCast(try self.allocWithSizeAndAlignment(@sizeOf(T), a, n, return_address));
                                            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
C:\Users\trist\zigup\zig\0.13.0\files\lib\std\mem\Allocator.zig:193:41: note: called from here
    return self.allocAdvancedWithRetAddr(T, alignment, n, @returnAddress());
           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
C:\Users\trist\zigup\zig\0.13.0\files\lib\std\array_list.zig:457:67: note: called from here
                const new_memory = try self.allocator.alignedAlloc(T, alignment, new_capacity);
                                       ~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
C:\Users\trist\zigup\zig\0.13.0\files\lib\std\array_list.zig:434:51: note: called from here
            return self.ensureTotalCapacityPrecise(better_capacity);
                   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~
C:\Users\trist\zigup\zig\0.13.0\files\lib\std\array_list.zig:468:44: note: called from here
            return self.ensureTotalCapacity(try addOrOom(self.items.len, additional_count));
                   ~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
C:\Users\trist\zigup\zig\0.13.0\files\lib\std\json\static.zig:466:67: note: called from here
                                try arraylist.ensureUnusedCapacity(1);
                                    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~
C:\Users\trist\zigup\zig\0.13.0\files\lib\std\json\static.zig:140:33: note: called from here
    const value = try innerParse(T, allocator, scanner_or_reader, resolved_options);
                      ~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
C:\Users\trist\zigup\zig\0.13.0\files\lib\std\json\static.zig:88:37: note: called from here
    return parseFromTokenSourceLeaky(T, allocator, &scanner, options);
           ~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
root.zig:42:59: note: called from here
    const words = std.json.parseFromSliceLeaky([]WordData, alloc, dict_raw, .{}) catch unreachable;

Why is @intFromPtr not usable in comptime?

Expected Behavior

The code properly deserializes the JSON at compile time.

@HyperCodec HyperCodec added the bug Observed behavior contradicts documented or intended behavior label Mar 4, 2025
@HyperCodec
Copy link
Author

HyperCodec commented Mar 4, 2025

Running the exact example code works fine (aside from the first attempt where it spammed error: Unexpected with no stacktrace, then I changed nothing and reran and it worked). Adding slices to the payload/type causes a similar error to the one above.

@nektro
Copy link
Contributor

nektro commented Mar 4, 2025

this is a known limitation. the result of @intFromPtr is always runtime-known even when passed a comptime-known address because the actual number backing a constant isn't knowable until linking.

related: #15080, #22177, #14931

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

2 participants