Why is align() on structures not allowed within unit-test code blocks?
Timon Gehr
timon.gehr at gmx.ch
Sun Dec 4 07:10:42 PST 2011
On 12/04/2011 03:01 PM, GrahamC wrote:
> Is there a good reason why align() applied to structure definitions is not allowed on the struct definition line within unit-test code blocks?
>
> E.g.:
>
> unittest {
>
> align(1) struct Foo {
> char c;
> int i;
> }
>
> void Bar() {
> Foo f;
> }
>
> }
>
> int main() {
> return 0;
> }
>
>
> fails to compile. The error message is:
> found 'align' instead of statement
>
> The error message is the same for both DMD and GDC.
>
> If you take the align(1) out it compiles OK.
> If you move the structure definition out of the unit-test block but leave the align(1) in place it compiles OK.
> If you put the align(1) on each member variable definition instead of the struct line it compiles OK.
>
> If the structure type is only used within the unit-test code then I would think it ought to be possible to define it (including it's alignment attribute) within that unit-test code block.
The reason is that it is parsed as function-local and that DMD's parser
for some strange reason uses different grammar rules for declarations
when inside a function body. You could maybe file a bug report.
A possible workaround is to take the declaration out of the unittest
block and wrap it into a version(unittest) block like this:
version(unittest) {
align(1) struct Foo {
char c;
int i;
}
}
unittest {
void Bar() {
Foo f;
}
}
int main() {
return 0;
}
It invades the namespace of the module when compiling with -unittest though.
More information about the Digitalmars-d-learn
mailing list