Skip to content

Commit af7e945

Browse files
committed
stage2: fix @sizeOf for structs with comptime fields
1 parent 2f6a01d commit af7e945

File tree

4 files changed

+15
-1
lines changed

4 files changed

+15
-1
lines changed

src/type.zig

+1-1
Original file line numberDiff line numberDiff line change
@@ -5177,7 +5177,7 @@ pub const Type = extern union {
51775177

51785178
const field = it.struct_obj.fields.values()[it.field];
51795179
defer it.field += 1;
5180-
if (!field.ty.hasRuntimeBits())
5180+
if (!field.ty.hasRuntimeBits() or field.is_comptime)
51815181
return FieldOffset{ .field = it.field, .offset = it.offset };
51825182

51835183
const field_align = field.normalAlignment(it.target);

test/behavior/align.zig

+4
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ test "alignment of struct with pointer has same alignment as usize" {
5555
}
5656

5757
test "alignment and size of structs with 128-bit fields" {
58+
if (builtin.zig_backend == .stage1) {
59+
// stage1 gets the wrong answer for a lot of targets
60+
return error.SkipZigTest;
61+
}
5862
if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
5963
if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
6064

test/behavior/bitcast.zig

+4
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,10 @@ test "bitcast generates a temporary value" {
120120
}
121121

122122
test "@bitCast packed structs at runtime and comptime" {
123+
if (builtin.zig_backend == .stage1) {
124+
// stage1 gets the wrong answer for a lot of targets
125+
return error.SkipZigTest;
126+
}
123127
if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest;
124128
if (builtin.zig_backend == .stage2_c) return error.SkipZigTest;
125129
if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest;

test/behavior/struct.zig

+6
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,10 @@ const Bitfields = packed struct {
500500
};
501501

502502
test "packed struct fields are ordered from LSB to MSB" {
503+
if (builtin.zig_backend == .stage1) {
504+
// stage1 gets the wrong answer for a lot of targets
505+
return error.SkipZigTest;
506+
}
503507
if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
504508
if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO
505509
if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
@@ -971,6 +975,8 @@ test "comptime struct field" {
971975
comptime b: i32 = 1234,
972976
};
973977

978+
comptime std.debug.assert(@sizeOf(T) == 4);
979+
974980
var foo: T = undefined;
975981
comptime try expect(foo.b == 1234);
976982
}

0 commit comments

Comments
 (0)