Skip to content

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

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.

Activity

  1. added
    bugObserved behavior contradicts documented or intended behavior
    on Sep 21, 2025
  2. xdBronch commented on Sep 21, 2025

    @xdBronch
    Contributor

    i do believe this is intentional, it was mentioned in the original pr #25138 (comment)
    in essence, these pieces of code are equivalent

    const element = foo[n];
    // same as
    const element_ptr = &foo[n]; // clear reference to comptime var!
    const element = element_ptr.*;

    this should definitely be mentioned in the upgrade guide

    This does not happen with one-dimensional arrays.

    this i am not sure of though, that part may be a bug?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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