From ee9fc54cd032b6ac0fcaa422aa9c2826fa370bfa Mon Sep 17 00:00:00 2001 From: Veikka Tuominen Date: Thu, 8 Dec 2022 22:21:49 +0200 Subject: [PATCH] TypedValue: fix handling of tuples represented as empty_struct_value --- src/TypedValue.zig | 16 ++++------------ src/type.zig | 1 - test/behavior.zig | 1 + test/behavior/bugs/13785.zig | 12 ++++++++++++ 4 files changed, 17 insertions(+), 13 deletions(-) create mode 100644 test/behavior/bugs/13785.zig diff --git a/src/TypedValue.zig b/src/TypedValue.zig index 10ab88b866..3c1972c78d 100644 --- a/src/TypedValue.zig +++ b/src/TypedValue.zig @@ -142,12 +142,10 @@ pub fn print( .extern_options_type => return writer.writeAll("std.builtin.ExternOptions"), .type_info_type => return writer.writeAll("std.builtin.Type"), - .empty_struct_value => return writer.writeAll(".{}"), - .aggregate => { + .empty_struct_value, .aggregate => { if (level == 0) { return writer.writeAll(".{ ... }"); } - const values = val.castTag(.aggregate).?; if (ty.zigTypeTag() == .Struct) { try writer.writeAll(".{"); const max_len = std.math.min(ty.structFieldCount(), max_aggregate_items); @@ -161,13 +159,7 @@ pub fn print( } try print(.{ .ty = ty.structFieldType(i), - .val = switch (ty.containerLayout()) { - .Packed => values.data[i], - else => ty.structFieldValueComptime(i) orelse b: { - const vals = values.data; - break :b vals[i]; - }, - }, + .val = val.fieldValue(ty, i), }, writer, level - 1, mod); } if (ty.structFieldCount() > max_aggregate_items) { @@ -184,7 +176,7 @@ pub fn print( var i: u32 = 0; while (i < max_len) : (i += 1) { - buf[i] = std.math.cast(u8, values.data[i].toUnsignedInt(target)) orelse break :str; + buf[i] = std.math.cast(u8, val.fieldValue(ty, i).toUnsignedInt(target)) orelse break :str; } const truncated = if (len > max_string_len) " (truncated)" else ""; @@ -199,7 +191,7 @@ pub fn print( if (i != 0) try writer.writeAll(", "); try print(.{ .ty = elem_ty, - .val = values.data[i], + .val = val.fieldValue(ty, i), }, writer, level - 1, mod); } if (len > max_aggregate_items) { diff --git a/src/type.zig b/src/type.zig index f587bdc490..1aefa8f7a1 100644 --- a/src/type.zig +++ b/src/type.zig @@ -5670,7 +5670,6 @@ pub const Type = extern union { switch (ty.tag()) { .@"struct" => { const struct_obj = ty.castTag(.@"struct").?.data; - assert(struct_obj.layout != .Packed); const field = struct_obj.fields.values()[index]; if (field.is_comptime) { return field.default_val; diff --git a/test/behavior.zig b/test/behavior.zig index 64ffcb7a44..e4a04bbccb 100644 --- a/test/behavior.zig +++ b/test/behavior.zig @@ -120,6 +120,7 @@ test { _ = @import("behavior/bugs/13435.zig"); _ = @import("behavior/bugs/13664.zig"); _ = @import("behavior/bugs/13714.zig"); + _ = @import("behavior/bugs/13785.zig"); _ = @import("behavior/byteswap.zig"); _ = @import("behavior/byval_arg_var.zig"); _ = @import("behavior/call.zig"); diff --git a/test/behavior/bugs/13785.zig b/test/behavior/bugs/13785.zig new file mode 100644 index 0000000000..cef117f486 --- /dev/null +++ b/test/behavior/bugs/13785.zig @@ -0,0 +1,12 @@ +const builtin = @import("builtin"); +const std = @import("std"); + +const S = packed struct { a: u0 = 0 }; +test { + if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO + if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO + if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO + + var a: u8 = 0; + try std.io.null_writer.print("\n{} {}\n", .{ a, S{} }); +}