www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Why is align() on structures not allowed within unit-test code blocks?

reply GrahamC <grahamc001uk yahoo.co.uk> writes:
Is there a good reason why align() applied to structure definitions is not =
allowed on the struct definition line within unit-test code blocks?=0A=0AE.=
g.:=0A=0Aunittest {=0A=0A=A0 =A0 =A0align(1) struct Foo {=0A=A0 =A0 =A0 =A0=
 char=09c;=0A=A0=09=A0 =A0 =A0 int=09=09i;=0A=A0 =A0 }=0A=0A=A0 =A0 =A0void=
 Bar() {=0A=A0=09=A0 =A0 =A0 =A0 Foo f;=0A=A0 =A0 =A0}=0A=0A}=0A=0Aint main=
() {=0A=A0 =A0 =A0return 0;=0A}=0A=0A=0Afails to compile. The error message=
 is:=0A=A0 =A0 found 'align' instead of statement=0A=0AThe error message is=
 the same for both DMD and GDC.=0A=0AIf you take the align(1) out it compil=
es OK.=0AIf you move the structure definition out of the unit-test block bu=
t leave the align(1) in place it compiles OK.=0AIf you put the align(1) on =
each member variable definition instead of the struct line it compiles OK.=
=0A=0AIf the structure type is only used within the unit-test code then I w=
ould think it ought to be possible to define it (including it's alignment a=
ttribute) within that unit-test code block.
Dec 04 2011
parent reply Timon Gehr <timon.gehr gmx.ch> writes:
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.
Dec 04 2011
parent bearophile <bearophileHUGS lycos.com> writes:
Timon Gehr:

 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.

http://d.puremagic.com/issues/show_bug.cgi?id=7065 Bye, bearophile
Dec 04 2011