Bug 14740 – __traits(allMembers) returns erroneous 'this' member for types declared in functions.

Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2015-06-27T07:25:52Z
Last change time
2021-04-18T05:57:44Z
Keywords
pull
Assigned to
No Owner
Creator
Mihail.K

Comments

Comment #0 by Mihail.K — 2015-06-27T07:25:52Z
__traits(allMembers) returns an additional non-existent member named 'this' for types declared inside of functions. ----- struct Foo { this(int x, int y) { } } void main() { struct Bar { this(int x, int y) { } } pragma(msg, __traits(allMembers, Foo)); // => __ctor pragma(msg, __traits(allMembers, Bar)); // => __ctor, this foreach(name; __traits(allMembers, Bar)) { import std.stdio : writeln; typeof(__traits(getMember, Bar, name)).stringof.writeln; } } ----- $ dmd -run demo.d > tuple("__ctor") > tuple("__ctor", "this") > demo.d(24): Error: no property 'this' for type 'Bar' Note the extra member produced by __traits(allMembers, Bar), eventually leading to a compilation error when referred to by __traits(getMember). This is likely an oversight or error and should be removed as it serves no purpose, and can cause problems when iterating over members of a type.
Comment #1 by k.hara.pg — 2015-07-06T07:26:14Z
(In reply to Mihail.K from comment #0) > __traits(allMembers) returns an additional non-existent member named 'this' > for types declared inside of functions. That's actually exists. It's an instance field which implicitly inserted by compiler, in order to capture the outer context of nested structs and classes.
Comment #2 by Mihail.K — 2015-07-06T07:51:53Z
(In reply to Kenji Hara from comment #1) > (In reply to Mihail.K from comment #0) > > __traits(allMembers) returns an additional non-existent member named 'this' > > for types declared inside of functions. > > That's actually exists. It's an instance field which implicitly inserted by > compiler, in order to capture the outer context of nested structs and > classes. Well, it's not accessible in any way, and trying to reference it through the getMember trait fails. It should either not be returned in the list, or should be accessible. As it is, all it does is cause a compiler error, unless you add special handling for that specific case.
Comment #3 by boris2.9 — 2021-04-17T23:19:31Z
*** Issue 19410 has been marked as a duplicate of this issue. ***
Comment #4 by dlang-bot — 2021-04-18T00:15:18Z
@BorisCarvajal created dlang/dmd pull request #12449 "Fix Issue 14740 - __traits(allMembers) returns erroneous 'this' member for types declared in functions" fixing this issue: - Fix Issue 14740 - __traits(allMembers) returns erroneous 'this' member for types declared in functions https://github.com/dlang/dmd/pull/12449
Comment #5 by dlang-bot — 2021-04-18T05:57:44Z
dlang/dmd pull request #12449 "Fix Issue 14740 - __traits(allMembers) returns erroneous 'this' member for types declared in functions" was merged into master: - dc9fd8754c197f222667fba62c07564fd3bedfcf by Boris Carvajal: Fix Issue 14740 - __traits(allMembers) returns erroneous 'this' member for types declared in functions https://github.com/dlang/dmd/pull/12449