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 {...@@ -10992,23 +10992,8 @@ const SwitchProngAnalysis = struct {
1099210992
10993 // By-reference captures have some further restrictions which make them easier to emit10993 // By-reference captures have some further restrictions which make them easier to emit
10994 if (capture_byref) {10994 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;
11000 const operand_ptr_info = operand_ptr_ty.ptrInfo(mod);10995 const operand_ptr_info = operand_ptr_ty.ptrInfo(mod);
11001 const capture_ptr_ty = if (same_types and same_alignment) same: {10996 const capture_ptr_ty = resolve: {
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: {
11012 // By-ref captures of hetereogeneous types are only allowed if all field10997 // By-ref captures of hetereogeneous types are only allowed if all field
11013 // pointer types are peer resolvable to each other.10998 // pointer types are peer resolvable to each other.
11014 // We need values to run PTR on, so make a bunch of undef constants.10999 // 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" {...@@ -585,26 +585,26 @@ test "switch prong pointer capture alignment" {
585 fn doTheTest() !void {585 fn doTheTest() !void {
586 const u = U{ .a = 1 };586 const u = U{ .a = 1 };
587 switch (u) {587 switch (u) {
588 .a => |*a| try expectEqual(*align(8) const u8, @TypeOf(a)),588 .a => |*a| comptime assert(@TypeOf(a) == *align(8) const u8),
589 .b, .c => |*p| {589 .b, .c => |*p| {
590 _ = p;590 _ = p;
591 @panic("fail");591 return error.TestFailed;
592 },592 },
593 }593 }
594594
595 switch (u) {595 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),
597 .c => |*p| {597 .c => |*p| {
598 _ = p;598 _ = p;
599 @panic("fail");599 return error.TestFailed;
600 },600 },
601 }601 }
602602
603 switch (u) {603 switch (u) {
604 .a, .c => |*p| try expectEqual(*const u8, @TypeOf(p)),604 .a, .c => |*p| comptime assert(@TypeOf(p) == *const u8),
605 .b => |*p| {605 .b => |*p| {
606 _ = p;606 _ = p;
607 @panic("fail");607 return error.TestFailed;
608 },608 },
609 }609 }
610 }610 }
...@@ -612,19 +612,19 @@ test "switch prong pointer capture alignment" {...@@ -612,19 +612,19 @@ test "switch prong pointer capture alignment" {
612 fn doTheTest2() !void {612 fn doTheTest2() !void {
613 const un1 = U{ .b = 1 };613 const un1 = U{ .b = 1 };
614 switch (un1) {614 switch (un1) {
615 .b => |*a| try expectEqual(*align(4) const u8, @TypeOf(a)),615 .b => |*b| comptime assert(@TypeOf(b) == *align(4) const u8),
616 .a, .c => |*p| {616 .a, .c => |*p| {
617 _ = p;617 _ = p;
618 @panic("fail");618 return error.TestFailed;
619 },619 },
620 }620 }
621621
622 const un2 = U{ .c = 1 };622 const un2 = U{ .c = 1 };
623 switch (un2) {623 switch (un2) {
624 .c => |*a| try expectEqual(*const u8, @TypeOf(a)),624 .c => |*c| comptime assert(@TypeOf(c) == *const u8),
625 .a, .b => |*p| {625 .a, .b => |*p| {
626 _ = p;626 _ = p;
627 @panic("fail");627 return error.TestFailed;
628 },628 },
629 }629 }
630 }630 }