authorgravatar for kcbanner@gmail.comCasey Banner <kcbanner@gmail.com> 2023-04-22 19:02:18-04:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2023-04-26 17:31:42+03:00
logb55b8e7745316a8e3acd4be8f2e3651d6225c710
tree2ae91e49c4624754449db88ba6a3858ebf4d2fd8
parentbc4d9f3aa93ad58f9cbc75021dc297f29dcf2961

add support for .field_ptr in elemValueAdvanced

This fixes a crash when @compileLog is passed a slice backed by an aggregate field at comptime.

2 files changed, 41 insertions(+), 0 deletions(-)

src/value.zig+9
......@@ -3002,6 +3002,15 @@ pub const Value = extern union {
30023002 const data = val.castTag(.elem_ptr).?.data;
30033003 return data.array_ptr.elemValueAdvanced(mod, index + data.index, arena, buffer);
30043004 },
3005 .field_ptr => {
3006 const data = val.castTag(.field_ptr).?.data;
3007 if (data.container_ptr.pointerDecl()) |decl_index| {
3008 const container_decl = mod.declPtr(decl_index);
3009 const field_type = data.container_ty.structFieldType(data.field_index);
3010 const field_val = container_decl.val.fieldValue(field_type, data.field_index);
3011 return field_val.elemValueAdvanced(mod, index, arena, buffer);
3012 } else unreachable;
3013 },
30053014
30063015 // The child type of arrays which have only one possible value need
30073016 // to have only one possible value itself.
test/cases/comptime_aggregate_print.zig created+32
......@@ -0,0 +1,32 @@
1const UnionContainer = union {
2 buf: [2]i32,
3};
4
5fn getUnionSlice() []i32 {
6 var c = UnionContainer{ .buf = .{ 1, 2 } };
7 return c.buf[0..2];
8}
9
10const StructContainer = struct {
11 buf: [2]i32,
12};
13
14fn getStructSlice() []i32 {
15 var c = StructContainer{ .buf = .{ 3, 4 } };
16 return c.buf[0..2];
17}
18
19comptime {
20 @compileLog(getUnionSlice());
21 @compileLog(getStructSlice());
22}
23
24pub fn main() !void {}
25
26// error
27//
28// :20:5: error: found compile log statement
29//
30// Compile Log Output:
31// @as([]i32, .{ 1, 2 })
32// @as([]i32, .{ 3, 4 })