Bug 9766 – align(n) with n compile-time constant

Status
RESOLVED
Resolution
FIXED
Severity
enhancement
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2013-03-20T11:10:00Z
Last change time
2017-08-02T08:07:28Z
Keywords
industry, pull, spec
Assigned to
nobody
Creator
bearophile_hugs

Comments

Comment #0 by bearophile_hugs — 2013-03-20T11:10:12Z
enum uint myAlignment = 16; align(myAlignment) struct Foo {} void main() {} DMD 2.063alpha gives: temp.d(2): Error: positive integer expected, not myAlignment With this a single compile-time constant change is enough to modify at the same time and in the same way for different CPUs various alignments in the code. It's good to be more DRY and avoid magic constants.
Comment #1 by bearophile_hugs — 2013-05-23T09:58:16Z
*** Issue 10149 has been marked as a duplicate of this issue. ***
Comment #2 by leandro.lucarella — 2014-08-08T14:19:09Z
Removed reject-invalid keyword as the specification indicates the syntax is: align ( IntegerLiteral ) In Sociomantic, being able to specify a compile-time calculated value to align would be useful to overcome alignment issues with structs with static arrays in them. For example: --- struct E1 { byte key; ubyte[(char[]).sizeof] val; } --- E1.key offset: 0LU, E1.val offset: 1LU This is very impolite to the GC. Trying the same in C, it works the same, and since D defines his alignment specification in terms of the host C compiler, this is technically not a bug but a feature. But a better way to overcome this than manually adding padding bytes between both members would be appreciated (using align(8) is not portable, and starting a version() madness because of this doesn't seem like a good option).
Comment #3 by yebblies — 2014-08-09T18:02:14Z
This is probably not a bad idea. The limitation seems rather arbitrary. When I implemented deprecated(message) I intentionally made it only take a string literal, to the hassle of having an attribute rely on other declarations' semantic passes. Since then, UDAs have forced us to deal with that anyway.
Comment #4 by jiki — 2016-05-05T07:04:48Z
My quick workaround is: mixin template AlignedStr(int algn, string name, string memberlist) { enum fmt = "align(%d) struct %s { %s }"; mixin( format(fmt, algn, name, memberlist) ); } /* struct Foo */ mixin AlignedStr!(16, "Foo", q{ char a; char b; }); Yes, string mixin is power.
Comment #5 by jiki — 2016-05-09T03:36:15Z
Some of druntime core.sys.windows.* modules also want this, Like: version (Win64) align(8): else align(4): One example: https://issues.dlang.org/show_bug.cgi?id=15547 We could make work-around with string-mixin above. However, I think this is an essential feature for D.
Comment #6 by k.hara.pg — 2016-05-09T13:04:13Z
Comment #7 by bugzilla — 2016-06-23T07:21:40Z
Comment #8 by github-bugzilla — 2016-06-23T07:23:08Z
Commits pushed to master at https://github.com/dlang/dlang.org https://github.com/dlang/dlang.org/commit/0825696a6fa17931985992998bf088569805f765 Issue 9766 - align(n) with n compile-time constant https://github.com/dlang/dlang.org/commit/f4f75cbb2a458536095577d8d471fdb1fafc7dee Merge pull request #1293 from 9rnsr/fix9766 Issue 9766 - align(n) with n compile-time constant
Comment #9 by github-bugzilla — 2016-06-24T06:46:47Z
Commits pushed to master at https://github.com/dlang/dmd https://github.com/dlang/dmd/commit/70b425f9788e2687b2749b72651035de83d4e582 fix Issue 9766 - align(n) with n compile-time constant https://github.com/dlang/dmd/commit/352470884d30dac48615b7fa588888ffb52a3bd9 reboot 5750: fix Issue 9766 - align(n) with n compile-time constant https://github.com/dlang/dmd/commit/4b9b4e4f5e9747b86a9a7fa7ac1abaae23f837f1 Merge pull request #5880 from WalterBright/fix9766 reboot 5750: fix Issue 9766 - align(n) with n compile-time constant
Comment #10 by github-bugzilla — 2016-10-01T11:44:57Z
Comment #11 by github-bugzilla — 2016-10-01T11:47:22Z
Commits pushed to stable at https://github.com/dlang/dmd https://github.com/dlang/dmd/commit/70b425f9788e2687b2749b72651035de83d4e582 fix Issue 9766 - align(n) with n compile-time constant https://github.com/dlang/dmd/commit/352470884d30dac48615b7fa588888ffb52a3bd9 reboot 5750: fix Issue 9766 - align(n) with n compile-time constant https://github.com/dlang/dmd/commit/4b9b4e4f5e9747b86a9a7fa7ac1abaae23f837f1 Merge pull request #5880 from WalterBright/fix9766
Comment #12 by github-bugzilla — 2017-08-02T08:07:28Z
Commit pushed to dmd-cxx at https://github.com/dlang/dmd https://github.com/dlang/dmd/commit/5b42a07a73bb17f52ebea143bcacce033581c076 Issue 9766 - align(n) with n compile-time constant