authorgravatar for justusk@noreply.codeberg.orgJustus Klausecker <justusk@noreply.codeberg.org> 2026-06-16 11:51:54+02:00
committergravatar for justusk@noreply.codeberg.orgJustus Klausecker <justusk@noreply.codeberg.org> 2026-06-16 11:51:54+02:00
log0bcaad394ddbc629ab0b38aebbe90545ea3e9bab
tree41377afcda2a66cf9b7b0cdf5c0e984085c1547e
parenta3ae499dc29747630f2801aab9bbc9661a4a0169
parentdb0d5b4c126388ab9d48435d52cbed35da811c83

Merge pull request 'Sema: make struct fields referencing comptime vars not comptime' (#31462) from rmehri01/zig:struct_comptime_var_ref into master

Reviewed-on: https://codeberg.org/ziglang/zig/pulls/31462 Reviewed-by: Justus Klausecker <justusk@noreply.codeberg.org>

3 files changed, 43 insertions(+), 6 deletions(-)

src/Sema.zig+25-6
......@@ -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(.{
......@@ -19095,13 +19116,11 @@ fn arrayInitAnon(
1909519116 .values = values_no_comptime,
1909619117 }));
1909719118
19098 const runtime_src = opt_runtime_src orelse {
19119 _ = opt_runtime_src orelse {
1909919120 const tuple_val = try pt.aggregateValue(tuple_ty, values);
1910019121 return sema.addConstantMaybeRef(tuple_val, is_ref);
1910119122 };
1910219123
19103 try sema.requireRuntimeBlock(block, src, runtime_src);
19104
1910519124 for (operands, 0..) |operand, i| {
1910619125 const operand_src = block.src(.{ .init_elem = .{
1910719126 .init_node_offset = src.offset.node_offset.x,
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