Bug 1723 – __traits(getVirtualFunctions) on a non-function fails badly

Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
x86
OS
Linux
Creation time
2007-12-10T10:45:00Z
Last change time
2015-06-09T01:14:22Z
Keywords
diagnostic, rejects-valid
Assigned to
bugzilla
Creator
dhasenan

Comments

Comment #0 by dhasenan — 2007-12-10T10:45:28Z
--- class Foo { int i; void f() {} } void main () { // Succeeds: foreach (m; __traits(getVirtualFunctions, Foo, "f")) {} // Fails: foreach (m; __traits(getVirtualFunctions, Foo, "i")) {} } --- The error message is: virtualfunctions_notfunction.d(9): Error: 'this' is only allowed in non-static member functions, not main virtualfunctions_notfunction.d(9): Error: this for i needs to be type Foo not type int I also get this when trying to access the .length property of the resulting tuple. If there are no virtual methods with the given name, an empty tuple should be returned. At the very least, it should have a decent error message.
Comment #1 by dhasenan — 2008-02-27T07:28:52Z
*** Bug 1872 has been marked as a duplicate of this bug. ***
Comment #2 by dhasenan — 2008-02-27T07:35:10Z
The obvious workaround: static if (__traits(compiles, (__traits(getVirtualFunctions, T, name)))) { foreach (i, method; __traits(getVirtualFunctions, T, name)) {} } It's an ugly way to work around compiler bugs, though.
Comment #3 by bugzilla — 2008-03-12T00:05:27Z
Since "i" is not a virtual function, the error message, while unclear, is justified as the code is not valid. I'll improve the error message.
Comment #4 by dhasenan — 2008-03-12T07:50:29Z
Then what should happen if I call it on a final function? The same thing? At any rate, I'm left with the same ugly workaround. If you're not going to make that workaround unnecessary, please add it to std.traits.
Comment #5 by bugzilla — 2008-05-11T04:13:20Z
Fixed dmd 2.013