authorgravatar for john.schmidt.h@gmail.comJohn Schmidt <john.schmidt.h@gmail.com> 2024-02-08 23:37:28+01:00
committergravatar for john.schmidt.h@gmail.comJohn Schmidt <john.schmidt.h@gmail.com> 2024-02-08 23:49:03+01:00
log0d1baf0c6192c2ad917311f8275cc15b5550f0ac
treef0f5a339c84506b3786762d540b7416369c567db
parentdbcd53def0c82e7b1197194f48be534930f9c34b

Improvements after code review


2 files changed, 11 insertions(+), 26 deletions(-)

src/Sema.zig+1-16
......@@ -10992,23 +10992,8 @@ const SwitchProngAnalysis = struct {
1099210992
1099310993 // By-reference captures have some further restrictions which make them easier to emit
1099410994 if (capture_byref) {
10995 const first_field_alignment = union_obj.fieldAlign(ip, first_field_index);
10996 const same_alignment = for (field_indices[1..]) |field_idx| {
10997 const field_alignment = union_obj.fieldAlign(ip, field_idx);
10998 if (field_alignment != first_field_alignment) break false;
10999 } else true;
1100010995 const operand_ptr_info = operand_ptr_ty.ptrInfo(mod);
11001 const capture_ptr_ty = if (same_types and same_alignment) same: {
11002 break :same try sema.ptrType(.{
11003 .child = capture_ty.toIntern(),
11004 .flags = .{
11005 .is_const = operand_ptr_info.flags.is_const,
11006 .is_volatile = operand_ptr_info.flags.is_volatile,
11007 .address_space = operand_ptr_info.flags.address_space,
11008 .alignment = first_field_alignment,
11009 },
11010 });
11011 } else resolve: {
10996 const capture_ptr_ty = resolve: {
1101210997 // By-ref captures of hetereogeneous types are only allowed if all field
1101310998 // pointer types are peer resolvable to each other.
1101410999 // We need values to run PTR on, so make a bunch of undef constants.
test/behavior/switch.zig+10-10
......@@ -585,26 +585,26 @@ test "switch prong pointer capture alignment" {
585585 fn doTheTest() !void {
586586 const u = U{ .a = 1 };
587587 switch (u) {
588 .a => |*a| try expectEqual(*align(8) const u8, @TypeOf(a)),
588 .a => |*a| comptime assert(@TypeOf(a) == *align(8) const u8),
589589 .b, .c => |*p| {
590590 _ = p;
591 @panic("fail");
591 return error.TestFailed;
592592 },
593593 }
594594
595595 switch (u) {
596 .a, .b => |*p| try expectEqual(*align(4) const u8, @TypeOf(p)),
596 .a, .b => |*p| comptime assert(@TypeOf(p) == *align(4) const u8),
597597 .c => |*p| {
598598 _ = p;
599 @panic("fail");
599 return error.TestFailed;
600600 },
601601 }
602602
603603 switch (u) {
604 .a, .c => |*p| try expectEqual(*const u8, @TypeOf(p)),
604 .a, .c => |*p| comptime assert(@TypeOf(p) == *const u8),
605605 .b => |*p| {
606606 _ = p;
607 @panic("fail");
607 return error.TestFailed;
608608 },
609609 }
610610 }
......@@ -612,19 +612,19 @@ test "switch prong pointer capture alignment" {
612612 fn doTheTest2() !void {
613613 const un1 = U{ .b = 1 };
614614 switch (un1) {
615 .b => |*a| try expectEqual(*align(4) const u8, @TypeOf(a)),
615 .b => |*b| comptime assert(@TypeOf(b) == *align(4) const u8),
616616 .a, .c => |*p| {
617617 _ = p;
618 @panic("fail");
618 return error.TestFailed;
619619 },
620620 }
621621
622622 const un2 = U{ .c = 1 };
623623 switch (un2) {
624 .c => |*a| try expectEqual(*const u8, @TypeOf(a)),
624 .c => |*c| comptime assert(@TypeOf(c) == *const u8),
625625 .a, .b => |*p| {
626626 _ = p;
627 @panic("fail");
627 return error.TestFailed;
628628 },
629629 }
630630 }