diff options
| author | xdBronch <51252236+xdBronch@users.noreply.github.com> | 2025-10-02 09:26:54 -0400 |
|---|---|---|
| committer | Matthew Lugg <mlugg@mlugg.co.uk> | 2025-10-07 22:13:10 +0100 |
| commit | 2c0aa1c6f551a38ee359065cce09eadb3b34cc2d (patch) | |
| tree | 6ede62fc5feb54fa61a136ddd681108bc1c9fa91 /src/Sema.zig | |
| parent | 27aba2d776caf59bb6569934626af587fdba9c75 (diff) | |
| download | zig-2c0aa1c6f551a38ee359065cce09eadb3b34cc2d.tar.gz zig-2c0aa1c6f551a38ee359065cce09eadb3b34cc2d.zip | |
don't make anonymous tuple fields referencing `comptime var`s `comptime`
Diffstat (limited to 'src/Sema.zig')
| -rw-r--r-- | src/Sema.zig | 29 |
1 files changed, 27 insertions, 2 deletions
diff --git a/src/Sema.zig b/src/Sema.zig index 640185b749..b08112d554 100644 --- a/src/Sema.zig +++ b/src/Sema.zig @@ -20048,10 +20048,14 @@ fn arrayInitAnon( const types = try sema.arena.alloc(InternPool.Index, operands.len); const values = try sema.arena.alloc(InternPool.Index, operands.len); + var any_comptime = false; const opt_runtime_src = rs: { var runtime_src: ?LazySrcLoc = null; for (operands, 0..) |operand, i| { - const operand_src = src; // TODO better source location + const operand_src = block.src(.{ .init_elem = .{ + .init_node_offset = src.offset.node_offset.x, + .elem_index = @intCast(i), + } }); const elem = try sema.resolveInst(operand); types[i] = sema.typeOf(elem).toIntern(); if (Type.fromInterned(types[i]).zigTypeTag(zcu) == .@"opaque") { @@ -20066,6 +20070,7 @@ fn arrayInitAnon( } if (try sema.resolveValue(elem)) |val| { values[i] = val.toIntern(); + any_comptime = true; } else { values[i] = .none; runtime_src = operand_src; @@ -20074,9 +20079,21 @@ fn arrayInitAnon( break :rs runtime_src; }; + // A field can't be `comptime` if it references a `comptime var` but the aggregate can still be comptime-known. + // Replace these fields with `.none` only for generating the type. + const values_no_comptime = if (!any_comptime) values else blk: { + const new_values = try sema.arena.alloc(InternPool.Index, operands.len); + for (values, new_values) |val, *new_val| { + if (val != .none and Value.fromInterned(val).canMutateComptimeVarState(zcu)) { + new_val.* = .none; + } else new_val.* = val; + } + break :blk new_values; + }; + const tuple_ty: Type = .fromInterned(try ip.getTupleType(gpa, pt.tid, .{ .types = types, - .values = values, + .values = values_no_comptime, })); const runtime_src = opt_runtime_src orelse { @@ -20086,6 +20103,14 @@ fn arrayInitAnon( try sema.requireRuntimeBlock(block, src, runtime_src); + for (operands, 0..) |operand, i| { + const operand_src = block.src(.{ .init_elem = .{ + .init_node_offset = src.offset.node_offset.x, + .elem_index = @intCast(i), + } }); + try sema.validateRuntimeValue(block, operand_src, try sema.resolveInst(operand)); + } + if (is_ref) { const target = sema.pt.zcu.getTarget(); const alloc_ty = try pt.ptrTypeSema(.{ |
