At runtime, we can create two mutually referencing pointers like this:
test "runtime" {
var x: *const anyopaque = undefined;
var y: *const anyopaque = undefined;
x = @ptrCast(&y);
y = @ptrCast(&x);
try std.testing.expectEqual(@intFromPtr(x), @intFromPtr(&y));
try std.testing.expectEqual(@intFromPtr(y), @intFromPtr(&x));
}
However, when I attempt to create them at compile time, the following error occurs:
test "comptime" {
const x, const y = comptime blk: {
var x: *const anyopaque = undefined;
var y: *const anyopaque = undefined;
x = @ptrCast(&y);
y = @ptrCast(&x);
break :blk .{ x, y };
};
try std.testing.expectEqual(@intFromPtr(x), @intFromPtr(&y));
try std.testing.expectEqual(@intFromPtr(y), @intFromPtr(&x));
}
error: runtime value contains reference to comptime var
try std.testing.expectEqual(@intFromPtr(x), @intFromPtr(&y));
How can this be achieved?
TIPS: The code above is minimized for testing purposes; my goal is conceptually equivalent to creating a doubly linked list at compile time.