authorgravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2025-05-03 20:10:42+01:00
committergravatar for alex@alexrp.comAlex Rønne Petersen <alex@alexrp.com> 2025-05-04 02:51:47+02:00
loge07d8fccd1389f4d9da4f5d906c8349d3014add8
treecfb9ea9396f9556519947de0656bc4abe1327e99
parentdb936b909471f5c7e0344489ab1e7d59ab387658
signaturebadge-check Signed by SSH key SHA256:7B/LJ7bpR1eX8aCXSr4mtd5M45VMPKcx9zY8e95b5QM

Merge pull request #23263 from mlugg/comptime-field-ptr

Sema: fix pointers to comptime fields of comptime-known aggregate pointers

4 files changed, 79 insertions(+), 33 deletions(-)

src/Sema.zig+24-9
......@@ -28582,12 +28582,17 @@ fn structFieldPtrByIndex(
2858228582 const zcu = pt.zcu;
2858328583 const ip = &zcu.intern_pool;
2858428584
28585 if (try sema.resolveDefinedValue(block, src, struct_ptr)) |struct_ptr_val| {
28586 const val = try struct_ptr_val.ptrField(field_index, pt);
28587 return Air.internedToRef(val.toIntern());
28585 const struct_type = zcu.typeToStruct(struct_ty).?;
28586 const field_is_comptime = struct_type.fieldIsComptime(ip, field_index);
28587
28588 // Comptime fields are handled later
28589 if (!field_is_comptime) {
28590 if (try sema.resolveDefinedValue(block, src, struct_ptr)) |struct_ptr_val| {
28591 const val = try struct_ptr_val.ptrField(field_index, pt);
28592 return Air.internedToRef(val.toIntern());
28593 }
2858828594 }
2858928595
28590 const struct_type = zcu.typeToStruct(struct_ty).?;
2859128596 const field_ty = struct_type.field_types.get(ip)[field_index];
2859228597 const struct_ptr_ty = sema.typeOf(struct_ptr);
2859328598 const struct_ptr_ty_info = struct_ptr_ty.ptrInfo(zcu);
......@@ -28607,6 +28612,7 @@ fn structFieldPtrByIndex(
2860728612 try Type.fromInterned(struct_ptr_ty_info.child).abiAlignmentSema(pt);
2860828613
2860928614 if (struct_type.layout == .@"packed") {
28615 assert(!field_is_comptime);
2861028616 switch (struct_ty.packedStructFieldPtrInfo(struct_ptr_ty, field_index, pt)) {
2861128617 .bit_ptr => |packed_offset| {
2861228618 ptr_ty_data.flags.alignment = parent_align;
......@@ -28617,6 +28623,7 @@ fn structFieldPtrByIndex(
2861728623 },
2861828624 }
2861928625 } else if (struct_type.layout == .@"extern") {
28626 assert(!field_is_comptime);
2862028627 // For extern structs, field alignment might be bigger than type's
2862128628 // natural alignment. Eg, in `extern struct { x: u32, y: u16 }` the
2862228629 // second field is aligned as u32.
......@@ -28640,7 +28647,7 @@ fn structFieldPtrByIndex(
2864028647
2864128648 const ptr_field_ty = try pt.ptrTypeSema(ptr_ty_data);
2864228649
28643 if (struct_type.fieldIsComptime(ip, field_index)) {
28650 if (field_is_comptime) {
2864428651 try struct_ty.resolveStructFieldInits(pt);
2864528652 const val = try pt.intern(.{ .ptr = .{
2864628653 .ty = ptr_field_ty.toIntern(),
......@@ -29173,7 +29180,8 @@ fn tupleFieldPtr(
2917329180 const pt = sema.pt;
2917429181 const zcu = pt.zcu;
2917529182 const tuple_ptr_ty = sema.typeOf(tuple_ptr);
29176 const tuple_ty = tuple_ptr_ty.childType(zcu);
29183 const tuple_ptr_info = tuple_ptr_ty.ptrInfo(zcu);
29184 const tuple_ty: Type = .fromInterned(tuple_ptr_info.child);
2917729185 try tuple_ty.resolveFields(pt);
2917829186 const field_count = tuple_ty.structFieldCount(zcu);
2917929187
......@@ -29191,9 +29199,16 @@ fn tupleFieldPtr(
2919129199 const ptr_field_ty = try pt.ptrTypeSema(.{
2919229200 .child = field_ty.toIntern(),
2919329201 .flags = .{
29194 .is_const = !tuple_ptr_ty.ptrIsMutable(zcu),
29195 .is_volatile = tuple_ptr_ty.isVolatilePtr(zcu),
29196 .address_space = tuple_ptr_ty.ptrAddressSpace(zcu),
29202 .is_const = tuple_ptr_info.flags.is_const,
29203 .is_volatile = tuple_ptr_info.flags.is_volatile,
29204 .address_space = tuple_ptr_info.flags.address_space,
29205 .alignment = a: {
29206 if (tuple_ptr_info.flags.alignment == .none) break :a .none;
29207 // The tuple pointer isn't naturally aligned, so the field pointer might be underaligned.
29208 const tuple_align = tuple_ptr_info.flags.alignment;
29209 const field_align = try field_ty.abiAlignmentSema(pt);
29210 break :a tuple_align.min(field_align);
29211 },
2919729212 },
2919829213 });
2919929214
src/codegen/c.zig+18-24
......@@ -611,7 +611,7 @@ pub const Function = struct {
611611 const a = try Assignment.start(f, writer, ctype);
612612 try f.writeCValue(writer, dst, .Other);
613613 try a.assign(f, writer);
614 try f.writeCValue(writer, src, .Initializer);
614 try f.writeCValue(writer, src, .Other);
615615 try a.end(f, writer);
616616 }
617617
......@@ -2826,7 +2826,7 @@ pub fn genLazyFn(o: *Object, lazy_ctype_pool: *const CType.Pool, lazy_fn: LazyFn
28262826 });
28272827 try o.dg.renderTypeAndName(w, name_ty, .{ .identifier = "name" }, Const, .none, .complete);
28282828 try w.writeAll(" = ");
2829 try o.dg.renderValue(w, Value.fromInterned(name_val), .Initializer);
2829 try o.dg.renderValue(w, Value.fromInterned(name_val), .StaticInitializer);
28302830 try w.writeAll(";\n return (");
28312831 try o.dg.renderType(w, name_slice_ty);
28322832 try w.print("){{{}, {}}};\n", .{
......@@ -4044,7 +4044,7 @@ fn airStore(f: *Function, inst: Air.Inst.Index, safety: bool) !CValue {
40444044 const new_local = try f.allocLocal(inst, src_ty);
40454045 try f.writeCValue(writer, new_local, .Other);
40464046 try writer.writeAll(" = ");
4047 try f.writeCValue(writer, src_val, .Initializer);
4047 try f.writeCValue(writer, src_val, .Other);
40484048 try writer.writeAll(";\n");
40494049
40504050 break :blk new_local;
......@@ -4515,7 +4515,7 @@ fn airSlice(f: *Function, inst: Air.Inst.Index) !CValue {
45154515 const a = try Assignment.start(f, writer, .usize);
45164516 try f.writeCValueMember(writer, local, .{ .identifier = "len" });
45174517 try a.assign(f, writer);
4518 try f.writeCValue(writer, len, .Initializer);
4518 try f.writeCValue(writer, len, .Other);
45194519 try a.end(f, writer);
45204520 }
45214521 return local;
......@@ -4933,7 +4933,7 @@ fn airSwitchDispatch(f: *Function, inst: Air.Inst.Index) !void {
49334933 const cond_local = f.loop_switch_conds.get(br.block_inst).?;
49344934 try f.writeCValue(writer, .{ .local = cond_local }, .Other);
49354935 try writer.writeAll(" = ");
4936 try f.writeCValue(writer, cond, .Initializer);
4936 try f.writeCValue(writer, cond, .Other);
49374937 try writer.writeAll(";\n");
49384938 try writer.print("goto zig_switch_{d}_loop;", .{@intFromEnum(br.block_inst)});
49394939}
......@@ -4978,14 +4978,8 @@ fn bitcast(f: *Function, dest_ty: Type, operand: CValue, operand_ty: Type) !CVal
49784978 const operand_lval = if (operand == .constant) blk: {
49794979 const operand_local = try f.allocLocal(null, operand_ty);
49804980 try f.writeCValue(writer, operand_local, .Other);
4981 if (operand_ty.isAbiInt(zcu)) {
4982 try writer.writeAll(" = ");
4983 } else {
4984 try writer.writeAll(" = (");
4985 try f.renderType(writer, operand_ty);
4986 try writer.writeByte(')');
4987 }
4988 try f.writeCValue(writer, operand, .Initializer);
4981 try writer.writeAll(" = ");
4982 try f.writeCValue(writer, operand, .Other);
49894983 try writer.writeAll(";\n");
49904984 break :blk operand_local;
49914985 } else operand;
......@@ -5697,7 +5691,7 @@ fn airOptionalPayloadPtrSet(f: *Function, inst: Air.Inst.Index) !CValue {
56975691 const a = try Assignment.start(f, writer, opt_ctype);
56985692 try f.writeCValueDeref(writer, operand);
56995693 try a.assign(f, writer);
5700 try f.object.dg.renderValue(writer, Value.false, .Initializer);
5694 try f.object.dg.renderValue(writer, Value.false, .Other);
57015695 try a.end(f, writer);
57025696 return .none;
57035697 },
......@@ -5717,7 +5711,7 @@ fn airOptionalPayloadPtrSet(f: *Function, inst: Air.Inst.Index) !CValue {
57175711 const a = try Assignment.start(f, writer, opt_ctype);
57185712 try f.writeCValueDerefMember(writer, operand, .{ .identifier = "is_null" });
57195713 try a.assign(f, writer);
5720 try f.object.dg.renderValue(writer, Value.false, .Initializer);
5714 try f.object.dg.renderValue(writer, Value.false, .Other);
57215715 try a.end(f, writer);
57225716 }
57235717 if (f.liveness.isUnused(inst)) return .none;
......@@ -5843,7 +5837,7 @@ fn airFieldParentPtr(f: *Function, inst: Air.Inst.Index) !CValue {
58435837 try writer.writeByte(')');
58445838
58455839 switch (fieldLocation(container_ptr_ty, field_ptr_ty, extra.field_index, pt)) {
5846 .begin => try f.writeCValue(writer, field_ptr_val, .Initializer),
5840 .begin => try f.writeCValue(writer, field_ptr_val, .Other),
58475841 .field => |field| {
58485842 const u8_ptr_ty = try pt.adjustPtrTypeChild(field_ptr_ty, .u8);
58495843
......@@ -5897,7 +5891,7 @@ fn fieldPtr(
58975891 try writer.writeByte(')');
58985892
58995893 switch (fieldLocation(container_ptr_ty, field_ptr_ty, field_index, pt)) {
5900 .begin => try f.writeCValue(writer, container_ptr_val, .Initializer),
5894 .begin => try f.writeCValue(writer, container_ptr_val, .Other),
59015895 .field => |field| {
59025896 try writer.writeByte('&');
59035897 try f.writeCValueDerefMember(writer, container_ptr_val, field);
......@@ -6020,7 +6014,7 @@ fn airStructFieldVal(f: *Function, inst: Air.Inst.Index) !CValue {
60206014 const operand_local = try f.allocLocal(inst, struct_ty);
60216015 try f.writeCValue(writer, operand_local, .Other);
60226016 try writer.writeAll(" = ");
6023 try f.writeCValue(writer, struct_byval, .Initializer);
6017 try f.writeCValue(writer, struct_byval, .Other);
60246018 try writer.writeAll(";\n");
60256019 break :blk operand_local;
60266020 } else struct_byval;
......@@ -6118,7 +6112,7 @@ fn airUnwrapErrUnionPay(f: *Function, inst: Air.Inst.Index, is_ptr: bool) !CValu
61186112 try writer.writeAll(" = (");
61196113 try f.renderType(writer, inst_ty);
61206114 try writer.writeByte(')');
6121 try f.writeCValue(writer, operand, .Initializer);
6115 try f.writeCValue(writer, operand, .Other);
61226116 try writer.writeAll(";\n");
61236117 return local;
61246118 }
......@@ -6163,7 +6157,7 @@ fn airWrapOptional(f: *Function, inst: Air.Inst.Index) !CValue {
61636157 const a = try Assignment.start(f, writer, operand_ctype);
61646158 try f.writeCValueMember(writer, local, .{ .identifier = "payload" });
61656159 try a.assign(f, writer);
6166 try f.writeCValue(writer, operand, .Initializer);
6160 try f.writeCValue(writer, operand, .Other);
61676161 try a.end(f, writer);
61686162 }
61696163 return local;
......@@ -6364,7 +6358,7 @@ fn airArrayToSlice(f: *Function, inst: Air.Inst.Index) !CValue {
63646358 try f.writeCValueMember(writer, local, .{ .identifier = "ptr" });
63656359 try a.assign(f, writer);
63666360 if (operand == .undef) {
6367 try f.writeCValue(writer, .{ .undef = inst_ty.slicePtrFieldType(zcu) }, .Initializer);
6361 try f.writeCValue(writer, .{ .undef = inst_ty.slicePtrFieldType(zcu) }, .Other);
63686362 } else {
63696363 const ptr_ctype = try f.ctypeFromType(ptr_ty, .complete);
63706364 const ptr_child_ctype = ptr_ctype.info(ctype_pool).pointer.elem_ctype;
......@@ -6381,7 +6375,7 @@ fn airArrayToSlice(f: *Function, inst: Air.Inst.Index) !CValue {
63816375 try writer.writeByte('&');
63826376 try f.writeCValueDeref(writer, operand);
63836377 try writer.print("[{}]", .{try f.fmtIntLiteral(try pt.intValue(.usize, 0))});
6384 } else try f.writeCValue(writer, operand, .Initializer);
6378 } else try f.writeCValue(writer, operand, .Other);
63856379 }
63866380 try a.end(f, writer);
63876381 }
......@@ -6911,7 +6905,7 @@ fn airMemset(f: *Function, inst: Air.Inst.Index, safety: bool) !CValue {
69116905 try writer.writeAll("for (");
69126906 try f.writeCValue(writer, index, .Other);
69136907 try writer.writeAll(" = ");
6914 try f.object.dg.renderValue(writer, try pt.intValue(.usize, 0), .Initializer);
6908 try f.object.dg.renderValue(writer, try pt.intValue(.usize, 0), .Other);
69156909 try writer.writeAll("; ");
69166910 try f.writeCValue(writer, index, .Other);
69176911 try writer.writeAll(" != ");
......@@ -7281,7 +7275,7 @@ fn airReduce(f: *Function, inst: Air.Inst.Index) !CValue {
72817275 .float => try pt.floatValue(scalar_ty, std.math.nan(f128)),
72827276 else => unreachable,
72837277 },
7284 }, .Initializer);
7278 }, .Other);
72857279 try writer.writeAll(";\n");
72867280
72877281 const v = try Vectorize.start(f, inst, writer, operand_ty);
test/behavior/tuple.zig+18
......@@ -603,3 +603,21 @@ test "empty union in tuple" {
603603 try std.testing.expectEqualStrings("0", info.@"struct".fields[0].name);
604604 try std.testing.expect(@typeInfo(info.@"struct".fields[0].type) == .@"union");
605605}
606
607test "field pointer of underaligned tuple" {
608 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
609 const S = struct {
610 fn doTheTest() !void {
611 const T = struct { u8, u32 };
612 var val: T align(2) = .{ 1, 2 };
613
614 comptime assert(@TypeOf(&val[0]) == *u8); // `u8` field pointer isn't overaligned
615 comptime assert(@TypeOf(&val[1]) == *align(2) u32); // `u32` field pointer is correctly underaligned
616
617 try expect(val[0] == 1);
618 try expect(val[1] == 2);
619 }
620 };
621 try S.doTheTest();
622 try comptime S.doTheTest();
623}
test/cases/compile_errors/runtime_store_to_comptime_field.zig created+19
......@@ -0,0 +1,19 @@
1const init: u32 = 1;
2fn rt() u32 {
3 return 3;
4}
5
6var tuple_val = .{init};
7export fn tuple_field() void {
8 tuple_val[0] = rt();
9}
10
11var struct_val = .{ .x = init };
12export fn struct_field() void {
13 struct_val.x = rt();
14}
15
16// error
17//
18// :8:14: error: cannot store runtime value in compile time variable
19// :13:15: error: cannot store runtime value in compile time variable