Bug 24720 – Can't alias __traits(getMember)

Status
RESOLVED
Resolution
DUPLICATE
Severity
critical
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2024-08-25T07:06:39Z
Last change time
2024-08-28T08:24:24Z
Assigned to
No Owner
Creator
Manu

Comments

Comment #0 by turkeyman — 2024-08-25T07:06:39Z
This is unexpected and extremely inconvenient: struct T { void myFun(int x) {} } T instance; void test() { __traits(getMember, instance, "myFun")(10); // no problem alias fun = __traits(getMember, instance, "myFun"); fun(10); // error: please god, fix me }
Comment #1 by razvan.nitu1305 — 2024-08-26T08:19:16Z
The issue has nothing to do with __traits(getMember). It's the alias that's the problem: struct T { void myFun(int x) {} } T instance; void test() { alias fun = instance.myFun; fun(10); // error: instance required } The problem is that you cannot alias an expression, only symbols or types. The problem here seems to be that the fun alias is accepted, but the compiler actually aliases to the T.myFun symbol which is not attached to any particular symbol. Looking at the docs [1] I see that the last runnable line in the example is commented (alias c = s.j) is commented out with a "scheduled for deprecation" comment, so I assume that this was discussed at some point. PS: this has been reported in the past multiple times: https://issues.dlang.org/show_bug.cgi?id=6842 . [1] https://dlang.org/spec/declaration.html#alias-variable *** This issue has been marked as a duplicate of issue 6842 ***
Comment #2 by razvan.nitu1305 — 2024-08-26T08:20:48Z
(In reply to RazvanN from comment #1) > The issue has nothing to do with __traits(getMember). It's the alias that's > the problem: > > struct T > { > void myFun(int x) {} > } > T instance; > > void test() > { > alias fun = instance.myFun; > fun(10); // error: instance required > } > > The problem is that you cannot alias an expression, only symbols or types. > The problem here seems to be that the fun alias is accepted, but the > compiler actually aliases to the T.myFun symbol which is not attached to any > particular symbol. Looking at the docs [1] I see that the last runnable line > in the example is commented (alias c = s.j) is commented out with a > "scheduled for deprecation" comment, so I assume that this was discussed at > some point. > > PS: this has been reported in the past multiple times: > https://issues.dlang.org/show_bug.cgi?id=6842 . > > > [1] https://dlang.org/spec/declaration.html#alias-variable > > *** This issue has been marked as a duplicate of issue 6842 *** I meant: "but the compiler actually aliases to the T.myFun symbol which is not attached to any particular instance". Sorry for the confusion.
Comment #3 by nick — 2024-08-26T11:06:25Z
> alias fun = instance.myFun; Thanks, should get detected with https://github.com/dlang/dmd/pull/16813. > Looking at the docs [1] I see that the last runnable line in the example is commented (alias c = s.j) is commented out with a "scheduled for deprecation" comment Walter said we shouldn't add a deprecation for this (probably because if people understood it, it might deprecate correct code): https://github.com/dlang/dmd/pull/15863#issuecomment-1902711081 I should update that comment though.
Comment #4 by turkeyman — 2024-08-28T07:50:47Z
Sorry, but what is this a duplicate of? It's definitely not fixed...
Comment #5 by razvan.nitu1305 — 2024-08-28T07:53:47Z
(In reply to Manu from comment #4) > Sorry, but what is this a duplicate of? > It's definitely not fixed... It's a duplicate of issue 6842: https://issues.dlang.org/show_bug.cgi?id=6842 which indeed is not fixed.
Comment #6 by turkeyman — 2024-08-28T08:03:49Z
Oh sorry, I thought I saw that it was closed too. My mistake!
Comment #7 by razvan.nitu1305 — 2024-08-28T08:24:24Z
No worries!