Commit b55b8e7745

kcbanner <kcbanner@gmail.com>
2023-04-23 01:02:18
add support for .field_ptr in elemValueAdvanced
This fixes a crash when @compileLog is passed a slice backed by an aggregate field at comptime.
1 parent bc4d9f3
Changed files (2)
src/value.zig
@@ -3002,6 +3002,15 @@ pub const Value = extern union {
                 const data = val.castTag(.elem_ptr).?.data;
                 return data.array_ptr.elemValueAdvanced(mod, index + data.index, arena, buffer);
             },
+            .field_ptr => {
+                const data = val.castTag(.field_ptr).?.data;
+                if (data.container_ptr.pointerDecl()) |decl_index| {
+                    const container_decl = mod.declPtr(decl_index);
+                    const field_type = data.container_ty.structFieldType(data.field_index);
+                    const field_val = container_decl.val.fieldValue(field_type, data.field_index);
+                    return field_val.elemValueAdvanced(mod, index, arena, buffer);
+                } else unreachable;
+            },
 
             // The child type of arrays which have only one possible value need
             // to have only one possible value itself.
test/cases/comptime_aggregate_print.zig
@@ -0,0 +1,32 @@
+const UnionContainer = union {
+    buf: [2]i32,
+};
+
+fn getUnionSlice() []i32 {
+    var c = UnionContainer{ .buf = .{ 1, 2 } };
+    return c.buf[0..2];
+}
+
+const StructContainer = struct {
+    buf: [2]i32,
+};
+
+fn getStructSlice() []i32 {
+    var c = StructContainer{ .buf = .{ 3, 4 } };
+    return c.buf[0..2];
+}
+
+comptime {
+    @compileLog(getUnionSlice());
+    @compileLog(getStructSlice());
+}
+
+pub fn main() !void {}
+
+// error
+//
+// :20:5: error: found compile log statement
+//
+// Compile Log Output:
+// @as([]i32, .{ 1, 2 })
+// @as([]i32, .{ 3, 4 })