Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit f3a69b1

Browse files
committedMar 17, 2025··
Sema: fix alignment of runtime field pointer of underaligned tuple
1 parent 3b0734a commit f3a69b1

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed
 

‎src/Sema.zig

+12-4
Original file line numberDiff line numberDiff line change
@@ -28513,7 +28513,8 @@ fn tupleFieldPtr(
2851328513
const pt = sema.pt;
2851428514
const zcu = pt.zcu;
2851528515
const tuple_ptr_ty = sema.typeOf(tuple_ptr);
28516-
const tuple_ty = tuple_ptr_ty.childType(zcu);
28516+
const tuple_ptr_info = tuple_ptr_ty.ptrInfo(zcu);
28517+
const tuple_ty: Type = .fromInterned(tuple_ptr_info.child);
2851728518
try tuple_ty.resolveFields(pt);
2851828519
const field_count = tuple_ty.structFieldCount(zcu);
2851928520

@@ -28531,9 +28532,16 @@ fn tupleFieldPtr(
2853128532
const ptr_field_ty = try pt.ptrTypeSema(.{
2853228533
.child = field_ty.toIntern(),
2853328534
.flags = .{
28534-
.is_const = !tuple_ptr_ty.ptrIsMutable(zcu),
28535-
.is_volatile = tuple_ptr_ty.isVolatilePtr(zcu),
28536-
.address_space = tuple_ptr_ty.ptrAddressSpace(zcu),
28535+
.is_const = tuple_ptr_info.flags.is_const,
28536+
.is_volatile = tuple_ptr_info.flags.is_volatile,
28537+
.address_space = tuple_ptr_info.flags.address_space,
28538+
.alignment = a: {
28539+
if (tuple_ptr_info.flags.alignment == .none) break :a .none;
28540+
// The tuple pointer isn't naturally aligned, so the field pointer might be underaligned.
28541+
const tuple_align = tuple_ptr_info.flags.alignment;
28542+
const field_align = try field_ty.abiAlignmentSema(pt);
28543+
break :a tuple_align.min(field_align);
28544+
},
2853728545
},
2853828546
});
2853928547

‎test/behavior/tuple.zig

+17
Original file line numberDiff line numberDiff line change
@@ -603,3 +603,20 @@ 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+
607+
test "field pointer of underaligned tuple" {
608+
const S = struct {
609+
fn doTheTest() !void {
610+
const T = struct { u8, u32 };
611+
var val: T align(2) = .{ 1, 2 };
612+
613+
comptime assert(@TypeOf(&val[0]) == *u8); // `u8` field pointer isn't overaligned
614+
comptime assert(@TypeOf(&val[1]) == *align(2) u32); // `u32` field pointer is correctly underaligned
615+
616+
try expect(val[0] == 1);
617+
try expect(val[1] == 2);
618+
}
619+
};
620+
try S.doTheTest();
621+
try comptime S.doTheTest();
622+
}

0 commit comments

Comments
 (0)
Please sign in to comment.