authorgravatar for rmehri01@pm.meRyan Mehri <rmehri01@pm.me> 2026-03-11 09:03:55-04:00
committergravatar for rmehri01@tutamail.comRyan Mehri <rmehri01@tutamail.com> 2026-06-15 13:39:55-04:00
logec5dc55cd1d9881f51037bdda5e51487a3268407
tree389873821ae13cf694428bdf40f71aff25e16962
parenta85cb728775375825afe4ebd62c60ae0b361d1e9

fix: make struct fields referencing comptime vars not comptime

Similar to 2c0aa1c, just applies the same logic to structs.

3 files changed, 42 insertions(+), 3 deletions(-)

src/Sema.zig+24-3
......@@ -18777,6 +18777,18 @@ fn structInitAnon(
1877718777 break :rs runtime_index;
1877818778 };
1877918779
18780 // A field can't be `comptime` if it references a `comptime var` but the aggregate can still be comptime-known.
18781 // Replace these fields with `.none` only for generating the type.
18782 const values_no_comptime = if (!any_values) values else blk: {
18783 const new_values = try sema.arena.alloc(InternPool.Index, types.len);
18784 for (values, new_values) |val, *new_val| {
18785 if (val != .none and Value.fromInterned(val).canMutateComptimeVarState(zcu)) {
18786 new_val.* = .none;
18787 } else new_val.* = val;
18788 }
18789 break :blk new_values;
18790 };
18791
1878018792 // We treat anonymous struct types as reified types, because there are similarities: they have
1878118793 // no captures, and instead use a form of structural equivalence which we can easy represent by
1878218794 // hashing the field names/types/values. They also perform layout resolution immediately. These
......@@ -18785,7 +18797,7 @@ fn structInitAnon(
1878518797 const type_hash: u64 = hash: {
1878618798 var hasher = std.hash.Wyhash.init(0);
1878718799 hasher.update(std.mem.sliceAsBytes(types));
18788 hasher.update(std.mem.sliceAsBytes(values));
18800 hasher.update(std.mem.sliceAsBytes(values_no_comptime));
1878918801 hasher.update(std.mem.sliceAsBytes(names));
1879018802 break :hash hasher.final();
1879118803 };
......@@ -18809,9 +18821,9 @@ fn structInitAnon(
1880918821 @memcpy(wip.field_names.get(ip), names);
1881018822 @memcpy(wip.field_types.get(ip), types);
1881118823 if (any_values) {
18812 @memcpy(wip.field_values.get(ip), values);
18824 @memcpy(wip.field_values.get(ip), values_no_comptime);
1881318825 @memset(wip.field_is_comptime_bits.getAll(ip), 0);
18814 for (values, 0..) |val, field_index| {
18826 for (values_no_comptime, 0..) |val, field_index| {
1881518827 if (val == .none) continue;
1881618828 const bit_bag_index = field_index / 32;
1881718829 const mask = @as(u32, 1) << @intCast(field_index % 32);
......@@ -18839,6 +18851,15 @@ fn structInitAnon(
1883918851 return sema.addConstantMaybeRef(struct_val, is_ref);
1884018852 };
1884118853
18854 for (values, 0..) |field_val, i| {
18855 if (field_val == .none) continue; // runtime-known
18856 const field_src = block.src(.{ .init_elem = .{
18857 .init_node_offset = src.offset.node_offset.x,
18858 .elem_index = @intCast(i),
18859 } });
18860 try sema.validateRuntimeValue(block, field_src, .fromIntern(field_val));
18861 }
18862
1884218863 if (is_ref) {
1884318864 const target = zcu.getTarget();
1884418865 const alloc_ty = try pt.ptrType(.{
test/behavior/struct.zig+9
......@@ -2326,3 +2326,12 @@ test "pointer to runtime field of struct containing struct containing comptime-o
23262326 ptr = &foo.number;
23272327 try expect(ptr.* == 123);
23282328}
2329
2330test "struct field referencing comptime var isn't comptime" {
2331 comptime var v: u8 = 0;
2332 const s = .{ .v = &v };
2333 // field isn't comptime but struct is still comptime-known
2334 comptime assert(!@typeInfo(@TypeOf(s)).@"struct".field_attrs[0].@"comptime");
2335 v = 1;
2336 comptime assert(s.v.* == 1);
2337}
test/cases/compile_errors/comptime_var_referenced_at_runtime.zig+9
......@@ -71,6 +71,12 @@ export fn bax() void {
7171 @memmove(&rt, &x);
7272}
7373
74export fn qoo(i: u8) void {
75 comptime var x: u32 = 123;
76 const y = .{ .p = &x, .i = i };
77 _ = y;
78}
79
7480// error
7581//
7682// :5:19: error: runtime value contains reference to comptime var
......@@ -103,3 +109,6 @@ export fn bax() void {
103109// :71:19: error: runtime value contains reference to comptime var
104110// :71:19: note: comptime var pointers are not available at runtime
105111// :67:14: note: 'runtime_value' points to comptime var declared here
112// :76:19: error: runtime value contains reference to comptime var
113// :76:19: note: comptime var pointers are not available at runtime
114// :75:14: note: 'runtime_value' points to comptime var declared here