D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 19708 - Can't use __traits(getAttributes, ...)[...] as a type
Summary: Can't use __traits(getAttributes, ...)[...] as a type
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: 2019-02-28 14:56 UTC by moonlightsentinel
Modified: 2020-03-21 03:56 UTC (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description moonlightsentinel 2019-02-28 14:56:20 UTC
The result of __traits(getAttributes, ...)[..] can't be used as a type. Instead dmd produces some misleading error messages depending on the context.

Consider the following example:

---------------

module test;

struct Foo {}
@Foo int bar;

alias TR = __traits(getAttributes, bar);

/* Error: TR[0] is not a type
TR[0] a;
*/

/* Error: type Foo is not an expression
typeof(TR[0]) b;
*/

/* alias `test.c` cannot alias an expression Foo
alias c = TR[0];
*/

/* __traits(getAttributes, bar) does not give a valid type
alias d = __traits(getAttributes, bar)[0];
*/

// Possible workaround using a tuple
alias Tuple(T...) = T;
alias TP = Tuple!(__traits(getAttributes, bar));

TP[0] e;

---------------

Probably related to issue 16390
Comment 1 basile-z 2019-07-05 17:14:39 UTC
It was a dup of 7804, which is implemented now.
Comment 2 basile-z 2019-07-05 17:39:19 UTC
sorry i missed that most failing code is commented. There's indeed a problem with direct __trait aliasing. Tuples processing (as obtained by __trait(getAttributes) is incomplete.
Comment 3 basile-z 2019-07-06 10:42:43 UTC
Most of the fix is easy but the part involving an index is more complicated because there's a kind of precedence problem:

  alias d = __traits(getAttributes, bar)[0];

is more like (using parens to explain even if not valid)

  alias d = IndexedBy(__traits(getAttributes, bar), 0);

The internal wrapper is itself wrapped in the type that stores the index so a flag indicating if symbols are allowed when processing the trait is never set.
That's why there's the spurious error message about giving a type.
Comment 4 Dlang Bot 2019-07-06 11:30:53 UTC
@Basile-z created dlang/dmd pull request #10143 "fix issue 19708 - Can't use __traits(getAttributes, ...)[...] as a type" fixing this issue:

- fix issue 19708 - Can't use __traits(getAttributes, ...)[...] as a type

https://github.com/dlang/dmd/pull/10143
Comment 5 Dlang Bot 2019-07-06 11:39:41 UTC
@Basile-z created dlang/dmd pull request #10144 "fix issue 19708 - Can't use __traits(getAttributes, ...)[...] as a type" fixing this issue:

- fix issue 19708 - Can't use __traits(getAttributes, ...)[...] as a type

https://github.com/dlang/dmd/pull/10144
Comment 6 Dlang Bot 2019-07-06 14:29:33 UTC
dlang/dmd pull request #10144 "fix issue 19708 - Can't use __traits(getAttributes, ...)[...] as a type" was merged into stable:

- caf888bd949a119c2136be6ee7011eedbf3f5f0b by Basile Burg:
  fix issue 19708 - Can't use __traits(getAttributes, ...)[...] as a type

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