From 0d1baf0c6192c2ad917311f8275cc15b5550f0ac Mon Sep 17 00:00:00 2001 From: John Schmidt Date: Thu, 8 Feb 2024 23:37:28 +0100 Subject: [PATCH] Improvements after code review --- src/Sema.zig | 17 +---------------- test/behavior/switch.zig | 20 ++++++++++---------- 2 files changed, 11 insertions(+), 26 deletions(-) diff --git a/src/Sema.zig b/src/Sema.zig index 9bb2c87a99a910413f52a445e2c25a03323093c3..25515a6cd0d16baf5aef9ae8cc009bf333540446 100644 --- a/src/Sema.zig +++ b/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. diff --git a/test/behavior/switch.zig b/test/behavior/switch.zig index b5958fac00f595002881440b730f4fb1d01edd10..fb04bced0a2105bcfc31410e59f3a7f51322d4e1 100644 --- a/test/behavior/switch.zig +++ b/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; }, } } -- 2.54.0