bug in compiles?

bauss jj_1337 at live.dk
Thu Apr 11 20:49:45 UTC 2019


On Thursday, 11 April 2019 at 18:13:48 UTC, Alex wrote:
> The following code works when I comment out the static if
>
> //static if (__traits(compiles, __traits(getAttributes, T)))
>    static foreach(a;  __traits(getAttributes, T)) Attributes ~=
>
>
> There seems to be absolutely no reason why this code would fail 
> with the static if but pass without it but in the first case I 
> get no attributes because the __traits compiles fails.
>
>
>
> __traits(compiles, __traits(getAttributes, T))
>
> vs
>
> __traits(getAttributes, T)
>
> How could it not compile in the first case and yet work in the 
> foreach?
>
> T is a local variable name passed to this code, which does 
> generally return an
> error when used in certain ways:
>
>
> int x;
>
> Code!x; // Basically the code at the top
>
> if I try to do too much with x I get `cannot use local `x` as a 
> parameter to a non-global template`. Usually when I try to pass 
> T to another template I get this error.
>
>
> But some things work. But I can't for the life of me understand 
> why compiles is failing but the foreach loop is working.
>
>
> The problem is that I am using compiles quite often and either 
> I'm misusing it or it's got some bugs in it that worry me 
> because maybe they are silently failing?
>
> Note that I get no errors in my code and I do get the 
> attributes so it is technically working.
>
> It would be really nice to know why the compiles is failing, 
> e.g.,
>
> pragma(compilesmsg, __traits(getAttributes, T)) spits out the 
> error.

Must be something else in your code because it works fine for me 
with this shortened example.

https://run.dlang.io/is/o8xESB


More information about the Digitalmars-d-learn mailing list