D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 21122 - __traits(getAttributes) wrong scope on enums
Summary: __traits(getAttributes) wrong scope on enums
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P1 normal
Assignee: No Owner
URL:
Keywords: pull, rejects-valid
Depends on:
Blocks:
 
Reported: 2020-08-05 12:09 UTC by SHOO
Modified: 2020-08-10 12:09 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description SHOO 2020-08-05 12:09:35 UTC
This code doesn't work:
--------------------------------------
import std.traits;
enum B;
void main()
{
    enum A;
    struct S { @A int a; }
    enum E { @A a, @B b }
    
    static assert(hasUDA!(S.a, A));
    // Error: undefined identifier A ... while evaluating:
    static assert(hasUDA!(E.a, A));
    static assert(hasUDA!(E.b, B));
    
    static assert(is(getUDAs!(S.a, A)[0] : A));
    // Error: static assert:  is(getUDAs!(cast(E)0, A)[0] : A) is false
    static assert(is(getUDAs!(E.a, A)[0] : A));
    static assert(is(getUDAs!(E.b, B)[0] : B));
    
    static assert(getSymbolsByUDA!(S, A).length == 1);
    // Error: static assert:  0LU == 1LU is false
    //static assert(getSymbolsByUDA!(E, A).length == 1);
    static assert(getSymbolsByUDA!(E, B).length == 1);
}
--------------------------------------

The following issue is similar, but the previous example succeeds in S, but fails in E.
https://issues.dlang.org/show_bug.cgi?id=15335
Comment 1 Simen Kjaeraas 2020-08-05 13:07:57 UTC
Reduced:

unittest {
    enum A;
    enum E { @A a }
    
    pragma(msg, getAllUDAs!(E.a));
}

alias getAllUDAs(A...) = __traits(getAttributes, A);

By introducting a symbol A at module scope we can even have getAllUDAs return the wrong thing. Obviously it's looking up things in the wrong scope.
Comment 2 Dlang Bot 2020-08-07 02:36:18 UTC
@BorisCarvajal created dlang/dmd pull request #11527 "Fix Issue 21122 - __traits(getAttributes) wrong scope on enums" fixing this issue:

- Fix Issue 21122 - __traits(getAttributes) wrong scope on enums

https://github.com/dlang/dmd/pull/11527
Comment 3 Dlang Bot 2020-08-07 03:36:51 UTC
dlang/dmd pull request #11527 "Fix Issue 21122 - __traits(getAttributes) wrong scope on enums" was merged into stable:

- 0ad9333f5dda9b857c51317c9be51039e8b91716 by Boris Carvajal:
  Fix Issue 21122 - __traits(getAttributes) wrong scope on enums

https://github.com/dlang/dmd/pull/11527
Comment 4 Dlang Bot 2020-08-10 12:09:40 UTC
dlang/dmd pull request #11542 "merge stable" was merged into master:

- 7d40b941b2b36ee84aa669f9c5310e1b98caa504 by Boris Carvajal:
  Fix Issue 21122 - __traits(getAttributes) wrong scope on enums

https://github.com/dlang/dmd/pull/11542