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

Sema: fix pointers to comptime fields of comptime-known aggregate pointers
This commit is contained in:
Matthew Lugg
2025-05-03 20:10:42 +01:00
committed by GitHub
4 changed files with 79 additions and 33 deletions

View File

@@ -602,3 +602,21 @@ test "empty union in tuple" {
try std.testing.expectEqualStrings("0", info.@"struct".fields[0].name);
try std.testing.expect(@typeInfo(info.@"struct".fields[0].type) == .@"union");
}
test "field pointer of underaligned tuple" {
if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
const S = struct {
fn doTheTest() !void {
const T = struct { u8, u32 };
var val: T align(2) = .{ 1, 2 };
comptime assert(@TypeOf(&val[0]) == *u8); // `u8` field pointer isn't overaligned
comptime assert(@TypeOf(&val[1]) == *align(2) u32); // `u32` field pointer is correctly underaligned
try expect(val[0] == 1);
try expect(val[1] == 2);
}
};
try S.doTheTest();
try comptime S.doTheTest();
}

View File

@@ -0,0 +1,19 @@
const init: u32 = 1;
fn rt() u32 {
return 3;
}
var tuple_val = .{init};
export fn tuple_field() void {
tuple_val[0] = rt();
}
var struct_val = .{ .x = init };
export fn struct_field() void {
struct_val.x = rt();
}
// error
//
// :8:14: error: cannot store runtime value in compile time variable
// :13:15: error: cannot store runtime value in compile time variable