Bug 9091 – Using __traits(getMember) on template argument fails inside member function

Status
RESOLVED
Resolution
FIXED
Severity
major
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2012-11-28T04:08:00Z
Last change time
2013-03-01T06:28:17Z
Keywords
pull, rejects-valid
Assigned to
nobody
Creator
turkeyman
Depends on
9100

Comments

Comment #0 by turkeyman — 2012-11-28T04:08:31Z
I've created a new bug from this thread: http://d.puremagic.com/issues/show_bug.cgi?id=9065 That thread has forked into unrelated conversations, and I suspect this is a separate bug anyway... Kenji: I boiled this down as much as I could... class Test { void func() { void templateFunc( T )( ref const T obj ) { foreach( m; __traits( allMembers, T ) ) { pragma(msg, m); static if( isVariable!( __traits(getMember, T, m) ) ) { //... } } } templateFunc( this ); } // some class members int x; } isVariable throws lots of errors when considering the class members. Note: __traits(allMembers, T) and __traits(getMember, T, m) These should also work with class instances, not just types, eg: __traits(allMembers, obj) and __traits(getMember, obj, m) And these combinations should also work: __traits(allMembers, T) and __traits(getMember, obj, m) __traits(allMembers, obj) and __traits(getMember, T, m) All these configurations throw errors, and the errors are different for each configuration. Depends on: /** * Detect whether symbol or type $(D X) is a function. */ template isFunction(X...) if (X.length == 1) { static if (is(typeof(&X[0]) U : U*) && is(U == function) || is(typeof(&X[0]) U == delegate)) { // x is a (nested) function symbol. enum isFunction = true; } else static if (is(X[0] T)) { // x is a type. Take the type of it and examine. enum isFunction = is(T == function); } else enum isFunction = false; } /** * Detect whether symbol $(D X) is a run-time variable. */ template isVariable(X...) if (X.length == 1) { static if (!is(X[0]) && !is(typeof(X[0]) == void) && !isFunction!(X[0])) { enum isVariable = is(typeof({ auto ptr = &X[0]; })) || is(typeof({ enum off = X[0].offsetof; })); } else enum isVariable = false; }
Comment #1 by turkeyman — 2012-11-29T04:20:58Z
I have a hard deadline (like, proper hard) tomorrow, I'd really like to have this fix in there. Is there anyone who can possibly look into/comment on it? I bumped the priority to help it get noticed...
Comment #2 by k.hara.pg — 2012-11-30T20:04:31Z
Furthermore reduced case: template isVariable(X...) if (X.length == 1) { enum isVariable = true; } class C { int x; void func() { enum is_x = isVariable!(__traits(getMember, C, "x")); } } I posted a report bug 9100 to explain the current semantic analysis weird behavior on template argument. I think that it is the root cause of this bug.
Comment #3 by k.hara.pg — 2012-11-30T20:08:21Z
Changed summary.
Comment #4 by k.hara.pg — 2012-11-30T21:17:32Z
Comment #5 by github-bugzilla — 2012-12-09T12:52:00Z
Commit pushed to master at https://github.com/D-Programming-Language/dmd https://github.com/D-Programming-Language/dmd/commit/a4fb02fb3572c1616e7aad55fdd07b22b12242b8 fix Issue 9091 - Using __traits(getMember) on template argument fails inside member function
Comment #6 by yebblies — 2013-01-12T22:31:27Z
Fixed?
Comment #7 by turkeyman — 2013-01-13T00:54:56Z
(In reply to comment #6) > Fixed? Kenji was working on these, the prototypes he gave me worked for me, I'm not sure if it was accepted/merged though. @Kenji?
Comment #8 by k.hara.pg — 2013-01-17T20:14:14Z
Unfortunately, this still not be fixed completely. https://github.com/D-Programming-Language/dmd/pull/1406
Comment #9 by github-bugzilla — 2013-03-01T01:32:04Z
Commit pushed to master at https://github.com/D-Programming-Language/dmd https://github.com/D-Programming-Language/dmd/commit/8a781342c90c0fc93a52876ceb972500a1603d42 fix Issue 9091 - Using __traits(getMember) on template argument fails inside member function Use ThisExp instead of VarExp to avoid interpretation on template arguments.