Skip to content

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

@HyperCodec

Description

@HyperCodec

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugObserved behavior contradicts documented or intended behavior

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions