From ca3aa02206edef4481c6f24506bfbd3b4f18fe58 Mon Sep 17 00:00:00 2001 From: Devin Bayer Date: Tue, 13 Apr 2021 14:53:44 +0200 Subject: Update doc for struct field alignment. --- doc/langref.html.in | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/doc/langref.html.in b/doc/langref.html.in index 3515875c87..63e4c946b6 100644 --- a/doc/langref.html.in +++ b/doc/langref.html.in @@ -2763,9 +2763,24 @@ test "overaligned pointer to packed struct" { cause the test suite to fail, notifying the bug fixer to update these docs.

- It's also - planned to be able to set alignment of struct fields. + It's also possible to set alignment of struct fields:

+ {#code_begin|test#} +const std = @import("std"); +const expectEqual = std.testing.expectEqual; + +test "aligned struct fields" { + const S = struct { + a: u32 align(2), + b: u32 align(64), + }; + var foo = S{ .a = 1, .b = 2 }; + + expectEqual(64, @alignOf(S)); + expectEqual(*align(2) u32, @TypeOf(&foo.a)); + expectEqual(*align(64) u32, @TypeOf(&foo.b)); +} + {#code_end#}

Using packed structs with {#link|volatile#} is problematic, and may be a compile error in the future. For details on this subscribe to -- cgit v1.2.3