diff options
| author | Jakub Konka <kubkon@jakubkonka.com> | 2022-02-02 00:20:41 +0100 |
|---|---|---|
| committer | Jakub Konka <kubkon@jakubkonka.com> | 2022-02-02 13:43:41 +0100 |
| commit | 15ff891f044102ba6515cb07f40805452420ea24 (patch) | |
| tree | 05e00c580057290dfcfccda0a8483668322aa11f /test/behavior/struct.zig | |
| parent | 521bd2e94a3b32382b2d1de1e6185149032b49db (diff) | |
| download | zig-15ff891f044102ba6515cb07f40805452420ea24.tar.gz zig-15ff891f044102ba6515cb07f40805452420ea24.zip | |
stage2: pad out (non-packed) struct fields when lowering to bytes
* pad out (non-packed) struct fields when lowering to bytes to be
saved in the binary - prior to this change, fields would be
saved at non-aligned addresses leading to wrong accesses
* add a matching test case to `behavior/struct.zig` tests
* fix offset to field calculation in `struct_field_ptr` on `x86_64`
Diffstat (limited to 'test/behavior/struct.zig')
| -rw-r--r-- | test/behavior/struct.zig | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/test/behavior/struct.zig b/test/behavior/struct.zig index e25f862e41..c470279aad 100644 --- a/test/behavior/struct.zig +++ b/test/behavior/struct.zig @@ -18,6 +18,39 @@ test "top level fields" { try expect(@as(i32, 1235) == instance.top_level_field); } +const StructWithFields = struct { + a: u8, + b: u32, + c: u64, + d: u32, + + fn first(self: *const StructWithFields) u8 { + return self.a; + } + + fn second(self: *const StructWithFields) u32 { + return self.b; + } + + fn third(self: *const StructWithFields) u64 { + return self.c; + } + + fn fourth(self: *const StructWithFields) u32 { + return self.d; + } +}; + +test "non-packed struct has fields padded out to the required alignment" { + if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; + + const foo = StructWithFields{ .a = 5, .b = 1, .c = 10, .d = 2 }; + try expect(foo.first() == 5); + try expect(foo.second() == 1); + try expect(foo.third() == 10); + try expect(foo.fourth() == 2); +} + const StructWithNoFields = struct { fn add(a: i32, b: i32) i32 { return a + b; |
