Skip to content

Commit 2da9a3b

Browse files
committed
[spec/attribute] Improve AlignAttribute docs
Make examples runnable & use .offsetof. Add link to Struct Layout. Add GC Compatibility subheading, move scope paragraph above. Add example showing error assigning to unaligned pointer in @safe code.
1 parent d3e26a2 commit 2da9a3b

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
@@ -223,6 +223,7 @@ $(GNAME AlignAttribute):
223223
sets it to the default, which matches the default member alignment
224224
of the companion C compiler.)
225225

226+
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
226227
--------
227228
struct S
228229
{
@@ -232,10 +233,11 @@ struct S
232233
long c; // placed at offset 8
233234
}
234235
static assert(S.alignof == 8);
236+
static assert(S.c.offsetof == 8);
235237
static assert(S.sizeof == 16);
236238
--------
237-
238-
$(P $(I AssignExpression) specifies the alignment
239+
)
240+
$(P The $(I AssignExpression) form specifies the alignment
239241
which matches the behavior of the companion C compiler when non-default
240242
alignments are used. It must be a positive power of 2.
241243
)
@@ -244,6 +246,7 @@ static assert(S.sizeof == 16);
244246
fields are packed together.
245247
)
246248

249+
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
247250
--------
248251
struct S
249252
{
@@ -253,13 +256,15 @@ struct S
253256
long c; // placed at offset 5
254257
}
255258
static assert(S.alignof == 1);
259+
static assert(S.c.offsetof == 5);
256260
static assert(S.sizeof == 13);
257261
--------
258-
262+
)
259263
$(P The natural alignment of an aggregate is the maximum alignment of its
260264
fields. It can be overridden by setting the alignment outside of the
261265
aggregate.)
262266

267+
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
263268
--------
264269
align (2) struct S
265270
{
@@ -269,12 +274,14 @@ align (2) struct S
269274
long c; // placed at offset 5
270275
}
271276
static assert(S.alignof == 2);
277+
static assert(S.c.offsetof == 5);
272278
static assert(S.sizeof == 14);
273279
--------
274-
280+
)
275281
$(P Setting the alignment of a field aligns it to that power of 2, regardless
276282
of the size of the field.)
277283

284+
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
278285
--------
279286
struct S
280287
{
@@ -283,9 +290,19 @@ struct S
283290
align (16) short c; // placed at offset 16
284291
}
285292
static assert(S.alignof == 16);
293+
static assert(S.c.offsetof == 16);
286294
static assert(S.sizeof == 32);
287295
--------
296+
)
297+
$(P The $(I AlignAttribute) is reset to the default when
298+
entering a function scope or a non-anonymous struct, union, class, and restored
299+
when exiting that scope.
300+
It is not inherited from a base class.
301+
)
288302

303+
$(P See also: $(DDSUBLINK spec/struct, struct_layout, Struct Layout).)
304+
305+
$(H3 $(LNAME2 align_gc, GC Compatibility))
289306

290307
$(P Do not align references or pointers that were allocated
291308
using $(GLINK2 expression, NewExpression) on boundaries that are not
@@ -294,15 +311,26 @@ static assert(S.sizeof == 32);
294311
byte boundaries.
295312
)
296313

297-
$(UNDEFINED_BEHAVIOR If any pointers and references to GC
298-
allocated objects are not aligned on `size_t` byte boundaries.)
314+
$(SPEC_RUNNABLE_EXAMPLE_FAIL
315+
---
316+
struct S
317+
{
318+
align(1):
319+
byte b;
320+
int* p;
321+
}
299322

300-
$(P The $(I AlignAttribute) is reset to the default when
301-
entering a function scope or a non-anonymous struct, union, class, and restored
302-
when exiting that scope.
303-
It is not inherited from a base class.
304-
)
323+
static assert(S.p.offsetof == 1);
305324

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

307335

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

0 commit comments

Comments
 (0)