Fixes #20587 - Add align(default) (#20589)

This commit is contained in:
Quirin F. Schroll 2024-12-30 06:02:14 +01:00 committed by GitHub
parent b88ffc50d7
commit 3f90de47c1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 58 additions and 4 deletions

View file

@ -0,0 +1,23 @@
The `align` attribute now allows to specify `default` explicitly
A lone `align` sets the alignment to the types default.
Alternatively, to be more explicit, `align(default)` does the same.
```
struct S
{
align(4)
{
byte x;
align(default) long y;
long z;
}
}
void main()
{
pragma(msg, S.x.alignof); // 4
pragma(msg, S.y.alignof); // 8
pragma(msg, S.z.alignof); // 4
}
```

View file

@ -939,7 +939,7 @@ class Parser(AST, Lexer = dmd.lexer.Lexer) : Lexer
if (e)
error("redundant alignment attribute `align(%s)`", e.toChars());
else
error("redundant alignment attribute `align`");
error("redundant alignment attribute `align(default)`");
}
pAttrs.setAlignment = true;
@ -4387,7 +4387,10 @@ class Parser(AST, Lexer = dmd.lexer.Lexer) : Lexer
if (token.value == TOK.leftParenthesis)
{
nextToken();
e = parseAssignExp();
if (token.value == TOK.default_)
nextToken();
else
e = parseAssignExp();
check(TOK.rightParenthesis);
}
return e;

View file

@ -0,0 +1,28 @@
struct S
{
align(1)
{
short x1;
int y1;
long z1;
align(default)
{
short x;
int y;
long z;
}
}
}
void fun()
{
static assert(S.x1.alignof == 1);
static assert(S.y1.alignof == 1);
static assert(S.z1.alignof == 1);
static assert(S.x.alignof == short.alignof);
static assert(S.y.alignof == int.alignof);
static assert(S.z.alignof == long.alignof);
}

View file

@ -53,10 +53,10 @@ public private void f10() {}
/*
TEST_OUTPUT:
---
fail_compilation/parseStc2.d(63): Error: redundant alignment attribute `align`
fail_compilation/parseStc2.d(63): Error: redundant alignment attribute `align(default)`
fail_compilation/parseStc2.d(64): Error: redundant alignment attribute `align(1)`
fail_compilation/parseStc2.d(65): Error: redundant alignment attribute `align(1)`
fail_compilation/parseStc2.d(66): Error: redundant alignment attribute `align`
fail_compilation/parseStc2.d(66): Error: redundant alignment attribute `align(default)`
fail_compilation/parseStc2.d(67): Error: redundant alignment attribute `align(2)`
---
*/