Bug 15094 – __traits(getMember) fails when the source is a struct/class field

Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2015-09-21T15:49:24Z
Last change time
2021-01-03T22:57:06Z
Keywords
industry, pull, rejects-valid
Assigned to
No Owner
Creator
Alex Parrill

Comments

Comment #0 by initrd.gz — 2015-09-21T15:49:24Z
Attempting to use `__traits(getMember)` with a sub-struct/field fails with a `need 'this'` error. Example code: import std.stdio; struct Foo { int i; } struct Bar { Foo foo; } void main() { Bar bar; writeln(__traits(getMember, bar.foo, "i")); } Output: $ rdmd ~/test.d /home/col/test.d(14): Error: need 'this' for 'i' of type 'int' Failed: ["dmd", "-v", "-o-", "/home/col/test.d", "-I/home/col"] It's possible, but annoying, to work around this by storing a local pointer to the field first. auto foo = &bar.foo; writeln(__traits(getMember, foo, "i"));
Comment #1 by dlang-bugzilla — 2015-10-15T17:22:38Z
This appears to be a regression. Introduced in https://github.com/D-Programming-Language/dmd/pull/1687
Comment #2 by k.hara.pg — 2015-10-21T01:25:45Z
(In reply to Vladimir Panteleev from comment #1) > This appears to be a regression. > > Introduced in https://github.com/D-Programming-Language/dmd/pull/1687 Not correct. With 2.062, compiling example code with `dmd -c` will make: test.d(16): Error: need 'this' to access member foo PR 1687 had moved many "need this" errors in glue layer to front-end, but such the field access is not yet supported. I think it's a reject-valid bug.
Comment #3 by dlang-bugzilla — 2015-10-21T03:30:09Z
(In reply to Kenji Hara from comment #2) > PR 1687 had moved many "need this" errors in glue layer to front-end, but > such the field access is not yet supported. I think it's a reject-valid bug. Oh, OK.
Comment #4 by k.hara.pg — 2015-10-21T08:43:19Z
Comment #5 by eyal.lotem — 2016-05-31T14:32:38Z
Even after applying the PR (https://github.com/D-Programming-Language/dmd/pull/5215): ``` struct S { int i; } S s; unittest { import std.meta : Alias; alias GetMember1 = Alias!(__traits(getMember, mixin(__MODULE__), "s")); alias GetMember2 = Alias!(__traits(getMember, GetMember1, "i")); alias GetMember3 = Alias!(__traits(getMember, __traits(getMember, mixin(__MODULE__), "s"), "i")); // Works: auto a1 = &GetMember1; // Fails: auto a2 = &GetMember2; // Fails: auto a3 = &GetMember3; // Works: auto a4 = &__traits(getMember, __traits(getMember, mixin(__MODULE__), "s"), "i"); } ``` fails (in a2, a3).
Comment #6 by eyal.lotem — 2016-05-31T14:33:21Z
Even after applying the PR (https://github.com/D-Programming-Language/dmd/pull/5215): ``` struct S { int i; } S s; unittest { import std.meta : Alias; alias GetMember1 = Alias!(__traits(getMember, mixin(__MODULE__), "s")); alias GetMember2 = Alias!(__traits(getMember, GetMember1, "i")); alias GetMember3 = Alias!(__traits(getMember, __traits(getMember, mixin(__MODULE__), "s"), "i")); // Works: auto a1 = &GetMember1; // Fails: auto a2 = &GetMember2; // Fails: auto a3 = &GetMember3; // Works: auto a4 = &__traits(getMember, __traits(getMember, mixin(__MODULE__), "s"), "i"); } ``` fails (in a2, a3).
Comment #7 by eyal — 2017-05-23T21:36:55Z
This bit us in several places in the weka codebase.
Comment #8 by github-bugzilla — 2017-11-26T14:41:32Z
Commits pushed to master at https://github.com/dlang/dmd https://github.com/dlang/dmd/commit/89c900316c02d7e845b151e02a460d89631a9d07 Fix Issue 15094 - __traits(getMember) fails when the source is a struct/class field https://github.com/dlang/dmd/commit/c9d988ebc0eca6137551eaacb961400acb2e7752 Merge pull request #7341 from JinShil/fix_15094 Fix Issue 15094 - __traits(getMember) fails when the source is a struct/class field
Comment #9 by github-bugzilla — 2017-12-18T22:57:33Z
Commits pushed to stable at https://github.com/dlang/dmd https://github.com/dlang/dmd/commit/89c900316c02d7e845b151e02a460d89631a9d07 Fix Issue 15094 - __traits(getMember) fails when the source is a struct/class field https://github.com/dlang/dmd/commit/c9d988ebc0eca6137551eaacb961400acb2e7752 Merge pull request #7341 from JinShil/fix_15094
Comment #10 by dlang-bot — 2021-01-03T22:57:06Z
dlang/dmd pull request #12093 "[dmd-cxx] Backport more recent traits to the C++ port" was merged into dmd-cxx: - 542f9a47d165eb27b2d3b937cadd119c779b07fe by JinShil: [dmd-cxx] Fix Issue 15094 - __traits(getMember) fails when the source is a struct/class field https://github.com/dlang/dmd/pull/12093