authorgravatar for thatlemon@gmail.comLemonBoy <thatlemon@gmail.com> 2020-02-25 12:22:21+01:00
committergravatar for thatlemon@gmail.comLemonBoy <thatlemon@gmail.com> 2020-02-25 12:48:08+01:00
logb46efcde82436e73c73dab132f73aeff98673894
treeeac09fdcc87897192b050975c2eb2a6dfccc6084
parent26b2e5fda86fe5424ebf86924d294d8dbb972eb6

ir: Fix sizeOf comparison with ptr to zst

Closes #4536

2 files changed, 34 insertions(+), 5 deletions(-)

src/analyze.cpp+2-3
...@@ -1132,10 +1132,9 @@ Error type_val_resolve_zero_bits(CodeGen *g, ZigValue *type_val, ZigType *parent...@@ -1132,10 +1132,9 @@ Error type_val_resolve_zero_bits(CodeGen *g, ZigValue *type_val, ZigType *parent
1132 if (type_val->special != ConstValSpecialLazy) {1132 if (type_val->special != ConstValSpecialLazy) {
1133 assert(type_val->special == ConstValSpecialStatic);1133 assert(type_val->special == ConstValSpecialStatic);
1134 if ((type_val->data.x_type->id == ZigTypeIdStruct &&1134 if ((type_val->data.x_type->id == ZigTypeIdStruct &&
1135 type_val->data.x_type->data.structure.resolve_loop_flag_zero_bits) ||1135 type_val->data.x_type->data.structure.resolve_loop_flag_zero_bits) ||
1136 (type_val->data.x_type->id == ZigTypeIdUnion &&1136 (type_val->data.x_type->id == ZigTypeIdUnion &&
1137 type_val->data.x_type->data.unionation.resolve_loop_flag_zero_bits) ||1137 type_val->data.x_type->data.unionation.resolve_loop_flag_zero_bits))
1138 type_val->data.x_type->id == ZigTypeIdPointer)
1139 {1138 {
1140 // Does a struct/union which contains a pointer field to itself have bits? Yes.1139 // Does a struct/union which contains a pointer field to itself have bits? Yes.
1141 *is_zero_bits = false;1140 *is_zero_bits = false;
test/stage1/behavior/sizeof_and_typeof.zig+32-2
...@@ -1,5 +1,7 @@...@@ -1,5 +1,7 @@
1const builtin = @import("builtin");1const std = @import("std");
2const expect = @import("std").testing.expect;2const builtin = std.builtin;
3const expect = std.testing.expect;
4const expectEqual = std.testing.expectEqual;
35
4test "@sizeOf and @TypeOf" {6test "@sizeOf and @TypeOf" {
5 const y: @TypeOf(x) = 120;7 const y: @TypeOf(x) = 120;
...@@ -135,3 +137,31 @@ test "@bitSizeOf" {...@@ -135,3 +137,31 @@ test "@bitSizeOf" {
135 a: u2137 a: u2
136 }) == 2);138 }) == 2);
137}139}
140
141test "@sizeOf comparison against zero" {
142 const S0 = struct {
143 f: *@This(),
144 };
145 const U0 = union {
146 f: *@This(),
147 };
148 const S = struct {
149 fn doTheTest(comptime T: type, comptime result: bool) void {
150 expectEqual(result, @sizeOf(T) > 0);
151 }
152 };
153 // Zero-sized type
154 S.doTheTest(u0, false);
155 S.doTheTest(*u0, false);
156 // Non byte-sized type
157 S.doTheTest(u1, true);
158 S.doTheTest(*u1, true);
159 // Regular type
160 S.doTheTest(u8, true);
161 S.doTheTest(*u8, true);
162 S.doTheTest(f32, true);
163 S.doTheTest(*f32, true);
164 // Container with ptr pointing to themselves
165 S.doTheTest(S0, true);
166 S.doTheTest(U0, true);
167}