authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-02-26 15:35:46-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-02-26 16:53:23-07:00
logd62229e3ad6069597b74874ba3b84fc185b2fa4c
treef2618b8263edb55fd0fd4c6cebaa9890b4b5838d
parent822d29286bd39b7331970e1e641ad240e7b62aee

Sema: Module.Union.abiAlignment can return 0

When the union is a 0-bit type.

3 files changed, 20 insertions(+), 20 deletions(-)

src/Module.zig+1-1
...@@ -1210,6 +1210,7 @@ pub const Union = struct {...@@ -1210,6 +1210,7 @@ pub const Union = struct {
1210 return @intCast(u32, most_index);1210 return @intCast(u32, most_index);
1211 }1211 }
12121212
1213 /// Returns 0 if the union is represented with 0 bits at runtime.
1213 pub fn abiAlignment(u: Union, target: Target, have_tag: bool) u32 {1214 pub fn abiAlignment(u: Union, target: Target, have_tag: bool) u32 {
1214 var max_align: u32 = 0;1215 var max_align: u32 = 0;
1215 if (have_tag) max_align = u.tag_ty.abiAlignment(target);1216 if (have_tag) max_align = u.tag_ty.abiAlignment(target);
...@@ -1225,7 +1226,6 @@ pub const Union = struct {...@@ -1225,7 +1226,6 @@ pub const Union = struct {
1225 };1226 };
1226 max_align = @maximum(max_align, field_align);1227 max_align = @maximum(max_align, field_align);
1227 }1228 }
1228 assert(max_align != 0);
1229 return max_align;1229 return max_align;
1230 }1230 }
12311231
test/behavior/floatop.zig+16-18
...@@ -155,15 +155,14 @@ test "@sin" {...@@ -155,15 +155,14 @@ test "@sin" {
155155
156fn testSin() !void {156fn testSin() !void {
157 // stage1 emits an incorrect compile error for `@as(ty, std.math.pi / 2)`157 // stage1 emits an incorrect compile error for `@as(ty, std.math.pi / 2)`
158 // so skip the rest of the tests.158 if (builtin.zig_backend == .stage1) return error.SkipZigTest;
159 if (builtin.zig_backend != .stage1) {159
160 inline for ([_]type{ f16, f32, f64 }) |ty| {160 inline for ([_]type{ f16, f32, f64 }) |ty| {
161 const eps = epsForType(ty);161 const eps = epsForType(ty);
162 try expect(@sin(@as(ty, 0)) == 0);162 try expect(@sin(@as(ty, 0)) == 0);
163 try expect(math.approxEqAbs(ty, @sin(@as(ty, std.math.pi)), 0, eps));163 try expect(math.approxEqAbs(ty, @sin(@as(ty, std.math.pi)), 0, eps));
164 try expect(math.approxEqAbs(ty, @sin(@as(ty, std.math.pi / 2)), 1, eps));164 try expect(math.approxEqAbs(ty, @sin(@as(ty, std.math.pi / 2)), 1, eps));
165 try expect(math.approxEqAbs(ty, @sin(@as(ty, std.math.pi / 4)), 0.7071067811865475, eps));165 try expect(math.approxEqAbs(ty, @sin(@as(ty, std.math.pi / 4)), 0.7071067811865475, eps));
166 }
167 }166 }
168167
169 {168 {
...@@ -183,15 +182,14 @@ test "@cos" {...@@ -183,15 +182,14 @@ test "@cos" {
183182
184fn testCos() !void {183fn testCos() !void {
185 // stage1 emits an incorrect compile error for `@as(ty, std.math.pi / 2)`184 // stage1 emits an incorrect compile error for `@as(ty, std.math.pi / 2)`
186 // so skip the rest of the tests.185 if (builtin.zig_backend == .stage1) return error.SkipZigTest;
187 if (builtin.zig_backend != .stage1) {186
188 inline for ([_]type{ f16, f32, f64 }) |ty| {187 inline for ([_]type{ f16, f32, f64 }) |ty| {
189 const eps = epsForType(ty);188 const eps = epsForType(ty);
190 try expect(@cos(@as(ty, 0)) == 1);189 try expect(@cos(@as(ty, 0)) == 1);
191 try expect(math.approxEqAbs(ty, @cos(@as(ty, std.math.pi)), -1, eps));190 try expect(math.approxEqAbs(ty, @cos(@as(ty, std.math.pi)), -1, eps));
192 try expect(math.approxEqAbs(ty, @cos(@as(ty, std.math.pi / 2)), 0, eps));191 try expect(math.approxEqAbs(ty, @cos(@as(ty, std.math.pi / 2)), 0, eps));
193 try expect(math.approxEqAbs(ty, @cos(@as(ty, std.math.pi / 4)), 0.7071067811865475, eps));192 try expect(math.approxEqAbs(ty, @cos(@as(ty, std.math.pi / 4)), 0.7071067811865475, eps));
194 }
195 }193 }
196194
197 {195 {
test/behavior/union.zig+3-1
...@@ -648,7 +648,9 @@ fn testEnumWithSpecifiedAndUnspecifiedTagValues(x: MultipleChoice2) !void {...@@ -648,7 +648,9 @@ fn testEnumWithSpecifiedAndUnspecifiedTagValues(x: MultipleChoice2) !void {
648}648}
649649
650test "switch on union with only 1 field" {650test "switch on union with only 1 field" {
651 if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO651 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
652 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
653 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
652654
653 var r: PartialInst = undefined;655 var r: PartialInst = undefined;
654 r = PartialInst.Compiled;656 r = PartialInst.Compiled;