| author | |
| committer | |
| log | 89812217b4e5fee7e2851266c17c9d47204a1573 |
| tree | 0e6445544b7651d59640b1f9bdd95e09508f5f13 |
| parent | 416a547cdb8dbbf3d2e7ce32132f0a25f2a8607e |
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,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 @@ |
| 1 | const builtin = @import("builtin"); | 1 | const std = @import("std"); |
| 2 | const expect = @import("std").testing.expect; | 2 | const builtin = std.builtin; |
| 3 | const expect = std.testing.expect; | ||
| 4 | const expectEqual = std.testing.expectEqual; | ||
| 3 | 5 | ||
| 4 | test "@sizeOf and @TypeOf" { | 6 | test "@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: u2 | 137 | a: u2 |
| 136 | }) == 2); | 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 | } |