Commit 0d1baf0c61

John Schmidt <john.schmidt.h@gmail.com>
2024-02-08 23:37:28
Improvements after code review
1 parent dbcd53d
Changed files (2)
src
test
behavior
src/Sema.zig
@@ -10992,23 +10992,8 @@ const SwitchProngAnalysis = struct {
 
                 // By-reference captures have some further restrictions which make them easier to emit
                 if (capture_byref) {
-                    const first_field_alignment = union_obj.fieldAlign(ip, first_field_index);
-                    const same_alignment = for (field_indices[1..]) |field_idx| {
-                        const field_alignment = union_obj.fieldAlign(ip, field_idx);
-                        if (field_alignment != first_field_alignment) break false;
-                    } else true;
                     const operand_ptr_info = operand_ptr_ty.ptrInfo(mod);
-                    const capture_ptr_ty = if (same_types and same_alignment) same: {
-                        break :same try sema.ptrType(.{
-                            .child = capture_ty.toIntern(),
-                            .flags = .{
-                                .is_const = operand_ptr_info.flags.is_const,
-                                .is_volatile = operand_ptr_info.flags.is_volatile,
-                                .address_space = operand_ptr_info.flags.address_space,
-                                .alignment = first_field_alignment,
-                            },
-                        });
-                    } else resolve: {
+                    const capture_ptr_ty = resolve: {
                         // By-ref captures of hetereogeneous types are only allowed if all field
                         // pointer types are peer resolvable to each other.
                         // We need values to run PTR on, so make a bunch of undef constants.
test/behavior/switch.zig
@@ -585,26 +585,26 @@ test "switch prong pointer capture alignment" {
         fn doTheTest() !void {
             const u = U{ .a = 1 };
             switch (u) {
-                .a => |*a| try expectEqual(*align(8) const u8, @TypeOf(a)),
+                .a => |*a| comptime assert(@TypeOf(a) == *align(8) const u8),
                 .b, .c => |*p| {
                     _ = p;
-                    @panic("fail");
+                    return error.TestFailed;
                 },
             }
 
             switch (u) {
-                .a, .b => |*p| try expectEqual(*align(4) const u8, @TypeOf(p)),
+                .a, .b => |*p| comptime assert(@TypeOf(p) == *align(4) const u8),
                 .c => |*p| {
                     _ = p;
-                    @panic("fail");
+                    return error.TestFailed;
                 },
             }
 
             switch (u) {
-                .a, .c => |*p| try expectEqual(*const u8, @TypeOf(p)),
+                .a, .c => |*p| comptime assert(@TypeOf(p) == *const u8),
                 .b => |*p| {
                     _ = p;
-                    @panic("fail");
+                    return error.TestFailed;
                 },
             }
         }
@@ -612,19 +612,19 @@ test "switch prong pointer capture alignment" {
         fn doTheTest2() !void {
             const un1 = U{ .b = 1 };
             switch (un1) {
-                .b => |*a| try expectEqual(*align(4) const u8, @TypeOf(a)),
+                .b => |*b| comptime assert(@TypeOf(b) == *align(4) const u8),
                 .a, .c => |*p| {
                     _ = p;
-                    @panic("fail");
+                    return error.TestFailed;
                 },
             }
 
             const un2 = U{ .c = 1 };
             switch (un2) {
-                .c => |*a| try expectEqual(*const u8, @TypeOf(a)),
+                .c => |*c| comptime assert(@TypeOf(c) == *const u8),
                 .a, .b => |*p| {
                     _ = p;
-                    @panic("fail");
+                    return error.TestFailed;
                 },
             }
         }