Skip to content

Commit 421c6d7

Browse files
authored
Merge pull request #3275 from ntrel/align
[spec/attribute] Improve AlignAttribute docs
2 parents 17bb4e6 + 9ce6677 commit 421c6d7

File tree

1 file changed

+39
-11
lines changed

1 file changed

+39
-11
lines changed

spec/attribute.dd

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,7 @@ $(GNAME AlignAttribute):
227227
sets it to the default, which matches the default member alignment
228228
of the companion C compiler.)
229229

230+
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
230231
--------
231232
struct S
232233
{
@@ -236,10 +237,11 @@ struct S
236237
long c; // placed at offset 8
237238
}
238239
static assert(S.alignof == 8);
240+
static assert(S.c.offsetof == 8);
239241
static assert(S.sizeof == 16);
240242
--------
241-
242-
$(P $(I AssignExpression) specifies the alignment
243+
)
244+
$(P The $(I AssignExpression) specifies the alignment
243245
which matches the behavior of the companion C compiler when non-default
244246
alignments are used. It must be a positive power of 2.
245247
)
@@ -248,6 +250,7 @@ static assert(S.sizeof == 16);
248250
fields are packed together.
249251
)
250252

253+
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
251254
--------
252255
struct S
253256
{
@@ -257,13 +260,15 @@ struct S
257260
long c; // placed at offset 5
258261
}
259262
static assert(S.alignof == 1);
263+
static assert(S.c.offsetof == 5);
260264
static assert(S.sizeof == 13);
261265
--------
262-
266+
)
263267
$(P The natural alignment of an aggregate is the maximum alignment of its
264268
fields. It can be overridden by setting the alignment outside of the
265269
aggregate.)
266270

271+
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
267272
--------
268273
align (2) struct S
269274
{
@@ -273,12 +278,14 @@ align (2) struct S
273278
long c; // placed at offset 5
274279
}
275280
static assert(S.alignof == 2);
281+
static assert(S.c.offsetof == 5);
276282
static assert(S.sizeof == 14);
277283
--------
278-
284+
)
279285
$(P Setting the alignment of a field aligns it to that power of 2, regardless
280286
of the size of the field.)
281287

288+
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
282289
--------
283290
struct S
284291
{
@@ -287,9 +294,19 @@ struct S
287294
align (16) short c; // placed at offset 16
288295
}
289296
static assert(S.alignof == 16);
297+
static assert(S.c.offsetof == 16);
290298
static assert(S.sizeof == 32);
291299
--------
300+
)
301+
$(P The $(I AlignAttribute) is reset to the default when
302+
entering a function scope or a non-anonymous struct, union, class, and restored
303+
when exiting that scope.
304+
It is not inherited from a base class.
305+
)
292306

307+
$(P See also: $(DDSUBLINK spec/struct, struct_layout, Struct Layout).)
308+
309+
$(H3 $(LNAME2 align_gc, GC Compatibility))
293310

294311
$(P Do not align references or pointers that were allocated
295312
using $(GLINK2 expression, NewExpression) on boundaries that are not
@@ -298,15 +315,26 @@ static assert(S.sizeof == 32);
298315
byte boundaries.
299316
)
300317

301-
$(UNDEFINED_BEHAVIOR If any pointers and references to GC
302-
allocated objects are not aligned on `size_t` byte boundaries.)
318+
$(SPEC_RUNNABLE_EXAMPLE_FAIL
319+
---
320+
struct S
321+
{
322+
align(1):
323+
byte b;
324+
int* p;
325+
}
303326

304-
$(P The $(I AlignAttribute) is reset to the default when
305-
entering a function scope or a non-anonymous struct, union, class, and restored
306-
when exiting that scope.
307-
It is not inherited from a base class.
308-
)
327+
static assert(S.p.offsetof == 1);
309328

329+
@safe void main()
330+
{
331+
S s;
332+
s.p = new int; // error: can't modify misaligned pointer in @safe code
333+
}
334+
---
335+
)
336+
$(UNDEFINED_BEHAVIOR If any pointers and references to GC
337+
allocated objects are not aligned on `size_t` byte boundaries.)
310338

311339

312340
$(H2 $(LNAME2 deprecated, $(D deprecated) Attribute))

0 commit comments

Comments
 (0)