| author | |
| committer | |
| log | b46efcde82436e73c73dab132f73aeff98673894 |
| tree | eac09fdcc87897192b050975c2eb2a6dfccc6084 |
| parent | 26b2e5fda86fe5424ebf86924d294d8dbb972eb6 |
Closes #45362 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 | 1132 | if (type_val->special != ConstValSpecialLazy) { |
| 1133 | 1133 | assert(type_val->special == ConstValSpecialStatic); |
| 1134 | 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 | 1136 | (type_val->data.x_type->id == ZigTypeIdUnion && |
| 1137 | type_val->data.x_type->data.unionation.resolve_loop_flag_zero_bits) || | |
| 1138 | type_val->data.x_type->id == ZigTypeIdPointer) | |
| 1137 | type_val->data.x_type->data.unionation.resolve_loop_flag_zero_bits)) | |
| 1139 | 1138 | { |
| 1140 | 1139 | // Does a struct/union which contains a pointer field to itself have bits? Yes. |
| 1141 | 1140 | *is_zero_bits = false; |
test/stage1/behavior/sizeof_and_typeof.zig+32-2| ... | ... | @@ -1,5 +1,7 @@ |
| 1 | const builtin = @import("builtin"); | |
| 2 | const expect = @import("std").testing.expect; | |
| 1 | const std = @import("std"); | |
| 2 | const builtin = std.builtin; | |
| 3 | const expect = std.testing.expect; | |
| 4 | const expectEqual = std.testing.expectEqual; | |
| 3 | 5 | |
| 4 | 6 | test "@sizeOf and @TypeOf" { |
| 5 | 7 | const y: @TypeOf(x) = 120; |
| ... | ... | @@ -135,3 +137,31 @@ test "@bitSizeOf" { |
| 135 | 137 | a: u2 |
| 136 | 138 | }) == 2); |
| 137 | 139 | } |
| 140 | ||
| 141 | test "@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 | } |