diff options
| author | Matthew Lugg <mlugg@mlugg.co.uk> | 2025-05-03 20:10:42 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-05-03 20:10:42 +0100 |
| commit | f4e9846bca69e20f907384cdad43b86a3aae1fb2 (patch) | |
| tree | 82d5ed3d6b07bc84c1b7cf1033b01bf0f14e50bd /test | |
| parent | f83fe2714bd4441610156e1a6017d07409ad6093 (diff) | |
| parent | 81277b5487e53d3e96351c2f1b14f437321210cc (diff) | |
| download | zig-f4e9846bca69e20f907384cdad43b86a3aae1fb2.tar.gz zig-f4e9846bca69e20f907384cdad43b86a3aae1fb2.zip | |
Merge pull request #23263 from mlugg/comptime-field-ptr
Sema: fix pointers to comptime fields of comptime-known aggregate pointers
Diffstat (limited to 'test')
| -rw-r--r-- | test/behavior/tuple.zig | 18 | ||||
| -rw-r--r-- | test/cases/compile_errors/runtime_store_to_comptime_field.zig | 19 |
2 files changed, 37 insertions, 0 deletions
diff --git a/test/behavior/tuple.zig b/test/behavior/tuple.zig index 492730df61..c9d3fb4b38 100644 --- a/test/behavior/tuple.zig +++ b/test/behavior/tuple.zig @@ -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(); +} diff --git a/test/cases/compile_errors/runtime_store_to_comptime_field.zig b/test/cases/compile_errors/runtime_store_to_comptime_field.zig new file mode 100644 index 0000000000..0c5d6a7ad3 --- /dev/null +++ b/test/cases/compile_errors/runtime_store_to_comptime_field.zig @@ -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 |
