#[solved] question about "runtime value contains reference to comptime var"

1 messages · Page 1 of 1 (latest)

west linden
#

this code compiled in earlier versions of zig but no longer does

pub fn shaderCodeComptime(comptime bytes: []const u8) []align(@alignOf(u32)) const u8 {
    comptime var array: [@divExact(bytes.len, @sizeOf(u32))]u32 = undefined;
    const byte_ptr: [*]align(@alignOf(u32)) u8 = @ptrCast(&array);
    @memcpy(byte_ptr[0..bytes.len], bytes);
    return byte_ptr[0..bytes.len];
}
src/util.zig:7:20: error: runtime value contains reference to comptime var
    return byte_ptr[0..bytes.len];
           ~~~~~~~~^~~~~~~~~~~~~~

is this a bug i should report or is there a migration path for this.

i guess there is probably a better approach to do this type of thing and open to those ideas but i do want to know how to do this form of buffer fill at comptime -> into runtime constant

prisma sky
#

you can create a const copy and return a slice of that or if possible just return byte_ptr by value

#

as in dereference it to return an array instead of slice

west linden
#

ah i see

#

i found this ends up working which is much shorter and goes through less hoops.

pub fn shaderCodeComptime(comptime bytes: []const u8) s.GPU.ShaderSource {
    const code: [bytes.len]u8 align(@alignOf(u32)) = bytes[0..bytes.len].*;
    return code[0..bytes.len];
}
#

ithink i remember you or something else mentioning that const can have alignment but idk if it works

prisma sky
#

i dont see why it wouldnt

#

well wait

west linden
#

someone mentioned before that itmmight not work

#

however, no assertions trip and program does work

prisma sky
#

whats s.GPU.ShaderSource? youre returning the array by value which doesnt carry the alignment with it

#

wait nvm

west linden
#

sorry its annotated in the first codeblock

prisma sky
#

thats a slice

west linden
#

a slice with alignment so i think it is ok?

prisma sky
#

yeah looks fine to me

west linden
#

okay

west linden
#

[solved] question about "runtime value contains reference to comptime var"