]> gcc.gnu.org Git - gcc.git/commitdiff
d: Respect explicit align(N) type alignment (PR100935)
authorIain Buclaw <ibuclaw@gdcproject.org>
Wed, 9 Jun 2021 17:37:22 +0000 (19:37 +0200)
committerIain Buclaw <ibuclaw@gdcproject.org>
Wed, 9 Jun 2021 17:46:53 +0000 (19:46 +0200)
It was previously the natural type alignment, defined as the maximum of
the field alignments for an aggregate.  Make sure an explicit align(N)
overrides it.

gcc/d/ChangeLog:

PR d/100935
* dmd/mtype.c (Type::getProperty): Prefer explicit alignment over
natural alignment for alignof property.

gcc/testsuite/ChangeLog:

PR d/100935
* gdc.test/compilable/aggr_alignment.d: Add test cases.

(cherry picked from commit 04fea2d66bd680beb1a204e62f2f459307000813)

gcc/d/dmd/mtype.c
gcc/testsuite/gdc.test/compilable/aggr_alignment.d

index 9ef8ab4e5f48deedeac67b467210d965d5eced4f..6cccf40df986ba519e38529b828b7dd0723bd9e8 100644 (file)
@@ -2040,7 +2040,10 @@ Expression *Type::getProperty(Loc loc, Identifier *ident, int flag)
     }
     else if (ident == Id::__xalignof)
     {
-        e = new IntegerExp(loc, alignsize(), Type::tsize_t);
+        unsigned explicitAlignment = alignment();
+        unsigned naturalAlignment = alignsize();
+        unsigned actualAlignment = (explicitAlignment == STRUCTALIGN_DEFAULT ? naturalAlignment : explicitAlignment);
+        e = new IntegerExp(loc, actualAlignment, Type::tsize_t);
     }
     else if (ident == Id::_init)
     {
index bf602ff31a476a987b8c4d7cee6dabbe4bcebb97..0c727e2fec560a81ee1d63b857f4c86a076e765a 100644 (file)
@@ -27,6 +27,26 @@ static assert(C2.int1.offsetof == payloadOffset + 8);
 static assert(C2.alignof == size_t.sizeof);
 static assert(__traits(classInstanceSize, C2) == payloadOffset + 12);
 
+align(8) struct PaddedStruct
+{
+    bool flag;
+    align(2) S1 s1;
+}
+
+static assert(PaddedStruct.s1.offsetof == 2);
+static assert(PaddedStruct.alignof == 8);
+static assert(PaddedStruct.sizeof == 16);
+
+align(1) struct UglyStruct
+{
+    bool flag;
+    int i;
+    ubyte u;
+}
+
+static assert(UglyStruct.i.offsetof == 4);
+static assert(UglyStruct.alignof == 1);
+static assert(UglyStruct.sizeof == 9);
 
 /***************************************************/
 // https://issues.dlang.org/show_bug.cgi?id=19914
This page took 0.074132 seconds and 5 git commands to generate.