From c80609dfecb57a3830583eeeb3e0fce5860d657b Mon Sep 17 00:00:00 2001 From: Jacob Young Date: Thu, 27 Jul 2023 22:40:54 -0400 Subject: [PATCH] Sema: don't reorder tuple fields This conflicts with anon structs which can be in-memory coercible but are never reordered. Closes #16242 --- src/Sema.zig | 4 +++- test/behavior/tuple.zig | 14 ++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/Sema.zig b/src/Sema.zig index 4a4823df2d..4936d52238 100644 --- a/src/Sema.zig +++ b/src/Sema.zig @@ -33491,7 +33491,9 @@ fn resolveStructLayout(sema: *Sema, ty: Type) CompileError!void { return sema.failWithOwnedErrorMsg(msg); } - if (struct_obj.layout == .Auto and mod.backendSupportsFeature(.field_reordering)) { + if (struct_obj.layout == .Auto and !struct_obj.is_tuple and + mod.backendSupportsFeature(.field_reordering)) + { const optimized_order = try mod.tmp_hack_arena.allocator().alloc(u32, struct_obj.fields.count()); for (struct_obj.fields.values(), 0..) |field, i| { diff --git a/test/behavior/tuple.zig b/test/behavior/tuple.zig index e9d3fcd0aa..757db0aa16 100644 --- a/test/behavior/tuple.zig +++ b/test/behavior/tuple.zig @@ -453,3 +453,17 @@ test "tuple pointer is indexable" { try expectEqual(@as(u32, 100), (&y)[0]); try expectEqual(false, (&y)[1]); } + +test "coerce anon tuple to tuple" { + if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO + if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO + if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO + if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest; // TODO + + var x: u8 = 1; + var y: u16 = 2; + var t = .{ x, y }; + var s: struct { u8, u16 } = t; + try expectEqual(x, s[0]); + try expectEqual(y, s[1]); +}