www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Can't get UDAs of constants (enums) !

reply Johan Engelen <j j.nl> writes:
This code no longer compiles (regression since 2.072, [1]):
```
module example;
import std.meta: staticIndexOf;

struct Attrib {}

 Attrib enum TEST = 123;

void foo() {
     foreach(name; __traits(allMembers, example)) {
         pragma(msg, name);
         pragma(msg, __traits(getAttributes, __traits(getMember, 
example, name)));
     }
}
```

Does anybody know a workaround? (the intent is to loop over all 
module members that have a certain UDA applied)
It's currently blocking Weka from updating to a newer compiler. :(

You can play with code live here: https://godbolt.org/g/G8Yw59
Switch between LDC 1.1.0 (dlang 2.071) and LDC 1.2.0 (dlang 
2.072).

Thanks,
   Johan

[1]  https://issues.dlang.org/show_bug.cgi?id=17545
Jun 24 2017
next sibling parent Meta <jared771 gmail.com> writes:
On Saturday, 24 June 2017 at 17:42:48 UTC, Johan Engelen wrote:
 This code no longer compiles (regression since 2.072, [1]):
 ```
 module example;
 import std.meta: staticIndexOf;

 struct Attrib {}

  Attrib enum TEST = 123;

 void foo() {
     foreach(name; __traits(allMembers, example)) {
         pragma(msg, name);
         pragma(msg, __traits(getAttributes, __traits(getMember, 
 example, name)));
     }
 }
 ```

 Does anybody know a workaround? (the intent is to loop over all 
 module members that have a certain UDA applied)
 It's currently blocking Weka from updating to a newer compiler. 
 :(

 You can play with code live here: https://godbolt.org/g/G8Yw59
 Switch between LDC 1.1.0 (dlang 2.071) and LDC 1.2.0 (dlang 
 2.072).

 Thanks,
   Johan

 [1]  https://issues.dlang.org/show_bug.cgi?id=17545
Looks like I responded a bit too quickly; I didn't even read the title so I didn't notice that you already identified the actual cause. Sorry for the redundant post.
Jun 24 2017
prev sibling next sibling parent ketmar <ketmar ketmar.no-ip.org> writes:
Johan Engelen wrote:

 This code no longer compiles (regression since 2.072, [1]):
 ```
 module example;
 import std.meta: staticIndexOf;

 struct Attrib {}

  Attrib enum TEST = 123;

 void foo() {
      foreach(name; __traits(allMembers, example)) {
          pragma(msg, name);
          pragma(msg, __traits(getAttributes, __traits(getMember, example, 
 name)));
      }
 }
 ```

 Does anybody know a workaround? (the intent is to loop over all module 
 members that have a certain UDA applied)
 It's currently blocking Weka from updating to a newer compiler. :(

 You can play with code live here: https://godbolt.org/g/G8Yw59
 Switch between LDC 1.1.0 (dlang 2.071) and LDC 1.2.0 (dlang 2.072).

 Thanks,
    Johan

 [1]  https://issues.dlang.org/show_bug.cgi?id=17545
`std.traits.getSymbolsByUDA()` will allow to select all symbols with the given UDA. yet you'll still have `123` instead of a `TEST` as a result. i'm not sure that this is a real bug, tho: such enums doesn't meant to exist as separate entities, they meant to be macro-like, pasting their value. i.e. i myself wont complain, but i may be completely wrong here, and this may be a real bug.
Jun 24 2017
prev sibling next sibling parent Johan Engelen <j j.nl> writes:
On Saturday, 24 June 2017 at 17:42:48 UTC, Johan Engelen wrote:
 Does anybody know a workaround?
I got something with mixins now that seems to work. ``` void foo(string modname)() { mixin("static import mod = " ~ modname ~ ";"); foreach(name; __traits(allMembers, mod)) { pragma(msg, name); pragma(msg, mixin("__traits(getAttributes, " ~ modname ~ "." ~ name ~")")); } } ```
Jun 24 2017
prev sibling next sibling parent Timon Gehr <timon.gehr gmx.ch> writes:
On 24.06.2017 19:42, Johan Engelen wrote:
 This code no longer compiles (regression since 2.072, [1]):
 ```
 module example;
 import std.meta: staticIndexOf;
 
 struct Attrib {}
 
  Attrib enum TEST = 123;
 
 void foo() {
      foreach(name; __traits(allMembers, example)) {
          pragma(msg, name);
          pragma(msg, __traits(getAttributes, __traits(getMember, 
 example, name)));
      }
 }
 ```
 
 Does anybody know a workaround? (the intent is to loop over all module 
 members that have a certain UDA applied)
 It's currently blocking Weka from updating to a newer compiler. :(
 
 You can play with code live here: https://godbolt.org/g/G8Yw59
 Switch between LDC 1.1.0 (dlang 2.071) and LDC 1.2.0 (dlang 2.072).
 
 Thanks,
    Johan
 
 [1]  https://issues.dlang.org/show_bug.cgi?id=17545
 
 
FWIW: This seems to fix the problem: https://github.com/dlang/dmd/compare/master...tgehr:fix17545 I don't really understand why though.
Jun 24 2017
prev sibling parent Walter Bright <newshound2 digitalmars.com> writes:
On 6/24/2017 10:42 AM, Johan Engelen wrote:
 [1]  https://issues.dlang.org/show_bug.cgi?id=17545
https://github.com/dlang/dmd/pull/6949
Jun 29 2017