Skip to content

Commit d4c5396

Browse files
committed
Sema: fix pointers to comptime fields of comptime-known aggregate pointers
Resolves: ziglang#23190
1 parent 5ed8bd5 commit d4c5396

File tree

2 files changed

+31
-5
lines changed

2 files changed

+31
-5
lines changed

src/Sema.zig

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28013,12 +28013,17 @@ fn structFieldPtrByIndex(
2801328013
const zcu = pt.zcu;
2801428014
const ip = &zcu.intern_pool;
2801528015

28016-
if (try sema.resolveDefinedValue(block, src, struct_ptr)) |struct_ptr_val| {
28017-
const val = try struct_ptr_val.ptrField(field_index, pt);
28018-
return Air.internedToRef(val.toIntern());
28016+
const struct_type = zcu.typeToStruct(struct_ty).?;
28017+
const field_is_comptime = struct_type.fieldIsComptime(ip, field_index);
28018+
28019+
// Comptime fields are handled later
28020+
if (!field_is_comptime) {
28021+
if (try sema.resolveDefinedValue(block, src, struct_ptr)) |struct_ptr_val| {
28022+
const val = try struct_ptr_val.ptrField(field_index, pt);
28023+
return Air.internedToRef(val.toIntern());
28024+
}
2801928025
}
2802028026

28021-
const struct_type = zcu.typeToStruct(struct_ty).?;
2802228027
const field_ty = struct_type.field_types.get(ip)[field_index];
2802328028
const struct_ptr_ty = sema.typeOf(struct_ptr);
2802428029
const struct_ptr_ty_info = struct_ptr_ty.ptrInfo(zcu);
@@ -28038,6 +28043,7 @@ fn structFieldPtrByIndex(
2803828043
try Type.fromInterned(struct_ptr_ty_info.child).abiAlignmentSema(pt);
2803928044

2804028045
if (struct_type.layout == .@"packed") {
28046+
assert(!field_is_comptime);
2804128047
switch (struct_ty.packedStructFieldPtrInfo(struct_ptr_ty, field_index, pt)) {
2804228048
.bit_ptr => |packed_offset| {
2804328049
ptr_ty_data.flags.alignment = parent_align;
@@ -28048,6 +28054,7 @@ fn structFieldPtrByIndex(
2804828054
},
2804928055
}
2805028056
} else if (struct_type.layout == .@"extern") {
28057+
assert(!field_is_comptime);
2805128058
// For extern structs, field alignment might be bigger than type's
2805228059
// natural alignment. Eg, in `extern struct { x: u32, y: u16 }` the
2805328060
// second field is aligned as u32.
@@ -28071,7 +28078,7 @@ fn structFieldPtrByIndex(
2807128078

2807228079
const ptr_field_ty = try pt.ptrTypeSema(ptr_ty_data);
2807328080

28074-
if (struct_type.fieldIsComptime(ip, field_index)) {
28081+
if (field_is_comptime) {
2807528082
try struct_ty.resolveStructFieldInits(pt);
2807628083
const val = try pt.intern(.{ .ptr = .{
2807728084
.ty = ptr_field_ty.toIntern(),
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
const init: u32 = 1;
2+
fn rt() u32 {
3+
return 3;
4+
}
5+
6+
var tuple_val = .{init};
7+
export fn tuple_field() void {
8+
tuple_val[0] = rt();
9+
}
10+
11+
var struct_val = .{ .x = init };
12+
export 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

0 commit comments

Comments
 (0)