Skip to content

Using a 2d-array lookup table produces error: runtime value contains reference to comptime var #25319

@IntegratedQuantum

Description

@IntegratedQuantum

Zig Version

0.16.0-dev.362+010d9a63f

Steps to Reproduce and Observed Behavior

Try to run the following code snippet:

pub fn main() void {
	comptime var lookupTable: [32][32]u8 = @splat(@splat(0));
	_ = &lookupTable;
	
	var runtimeValue: usize = 0;
	_ = &runtimeValue;
	
	_ = lookupTable[runtimeValue][runtimeValue];
}

Observe that it produces a compiler error:

$ zig-debug run test.zig
test.zig:10:17: error: runtime value contains reference to comptime var
 _ = lookupTable[runtimeValue][runtimeValue];
     ~~~~~~~~~~~^~~~~~~~~~~~~~
test.zig:10:17: note: comptime var pointers are not available at runtime
test.zig:4:11: note: 'runtime_value' points to comptime var declared here
 comptime var lookupTable: [32][32]u8 = @splat(@splat(0));
 ~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
referenced by:
    callMain [inlined]: /home/mint/zig/zig-out/lib/zig/std/start.zig:618:22
    callMainWithArgs [inlined]: /home/mint/zig/zig-out/lib/zig/std/start.zig:587:20
    posixCallMainAndExit: /home/mint/zig/zig-out/lib/zig/std/start.zig:542:36
    2 reference(s) hidden; use '-freference-trace=5' to see all references

This does not happen with one-dimensional arrays.

Expected Behavior

It used to work in older zig versions (0.16.0-dev.321+3cc0fc601), so I assume this regressed in #25154.
I assume it was an unintentional change, since it wasn't mentioned in the migration guide.

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