www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 3744] New: __traits getMember error in checking of second argument

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3744

           Summary: __traits getMember error in checking of second
                    argument
           Product: D
           Version: 2.040
          Platform: x86_64
        OS/Version: Linux
            Status: NEW
          Severity: blocker
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: denis.tomilin gmail.com



Created an attachment (id=557)
Example code

I've trying to get text representation of class, that can be changed in
development time.
For this problem i've used __traits functions "allModules",
"isVirtualFunction"(for filtering) and "getMember" for getting a type by member
name(from "allModules").
But i've stuck in "getMember".

Error: string expected as second argument of __traits getMember instead of m
(see code in attachment) in
if (!__traits(isVirtualFunction, __traits(getMember, Check, m))){
As said in references:
Takes two arguments, the second must be a string.
Ofc, m is normal string. I've tried to use "writeln(typeid(typeof(m)));" for determinating a type of m-variable.
immutable(char)[]
as printed in console. That means all ok. But __traits still throws error. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Jan 26 2010
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3744




I've tried to fix this problem and I've found this part of code, what can say
about this problem:
Object *o = (Object *)args->data[0];
Expression *e = isExpression((Object *)args->data[1]);
if (!e)
{   error("expression expected as second argument of __traits %s",
ident->toChars());
    goto Lfalse;
}
e = e->optimize(WANTvalue | WANTinterpret);
if (e->op != TOKstring)
{   error("string expected as second argument of __traits %s instead of %s =
%d", ident->toChars(), e->toChars(), e->op);
    goto Lfalse;
}
(traits.c) Problem in this code. This code want to see const string expression(every const string in the code). Program fails in this string:
if (e->op != TOKstring)
Cuz e->op == TOKvar. As i think, need to get an string from var and send it into StringExp instance for next operations, but i donts know how. Now i trying to find similar code in other sources. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Jan 27 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3744


Don <clugdbug yahoo.com.au> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |clugdbug yahoo.com.au




 I've tried to fix this problem and I've found this part of code, what can say
 about this problem:
e = e->optimize(WANTvalue | WANTinterpret);
if (e->op != TOKstring)
(traits.c)
 Problem in this code. This code want to see const string expression(every const
 string in the code). Program fails in this string:
if (e->op != TOKstring)
Cuz e->op == TOKvar. As i think, need to get an string from var and send it into StringExp instance for next operations, but i donts know how.
e->optimize(WANTvalue|WANTinterpret) should have turned it into a TOKstring. If it's still a TOKvar, then the problem is in e->optimize or earlier. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Jan 27 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3744





 e->optimize(WANTvalue|WANTinterpret) should have turned it into a TOKstring. If
 it's still a TOKvar, then the problem is in e->optimize or earlier.
Hm. Huh, why Walter don't wrote a comments for sly functions like this... interesting, I dunno how can miss word "WANT" in "WANTvalue", but I did it +_+. Ok, good logic, but i cant find an implementation of this function. It must be predefined in expressions.h. And i found this:
struct VarExp : SymbolExp{
 ...
 Expression *optimize(int result);
 ...
}
Aha! But implementation there's in another file - optimize.c:
Expression *VarExp::optimize(int result)
{
    return fromConstInitializer(result, this);
}
Yeah, good. This func found in this file:
Expression *fromConstInitializer(int result, Expression *e1)
Only this part interesting for me, cuz it returns 0, that means returns original argument("this", with TOKvar):
e = expandVar(result, v);
Next...
Expression *expandVar(int result, VarDeclaration *v)
if (v->isConst() || v->isImmutable() || v->storage_class & STCmanifest)
Yeah! It fails ;_; But, stop! WTF? This function dont checks a WANTvalue flag and... i dunno. I want to cry.. really. I want to solve this problem cuz its blocked my work. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Jan 27 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3744




Just had a look at your test code.
The problem lies here:

    foreach(string m; a){
        __traits(getMember, Check, m)
    }
m is not a compile-time constant. Of course it _should_ be, but it's not.

One thing you could try in traits.c is to replace:
e = e->optimize(WANTvalue);
with  e = e->interpret(NULL);
and that'll probably get it working.

But the real problem is in tuple foreach: the iteration variable should be a
compile-time constant.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jan 27 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3744





 Just had a look at your test code.
 The problem lies here:
 
     foreach(string m; a){
         __traits(getMember, Check, m)
     }
 m is not a compile-time constant. Of course it _should_ be, but it's not.
Yeah, i understand this, but i think this function(has/getMember and some other) useless if it can't be used in runtime.
 One thing you could try in traits.c is to replace:
 e = e->optimize(WANTvalue);
 with  e = e->interpret(NULL);
 and that'll probably get it working.
With this fix compiles ok, but if i want to get string representation of type from variable(typeid(typeof(__traits(getMember, Check, m)))), i've get always one string: fail.Check(). But if i try ti get this from Check.a, i've get a "int". I dunno why, cuz i think this is bad fix.
 But the real problem is in tuple foreach: the iteration variable should be a
 compile-time constant.
This is problem why it doesn't compiles, ofc. But i need a functionality rather then perfectly compiles "hello world", lol. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Jan 27 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3744




Actually, there's nothing at all wrong with __traits(getMember).
Really, you are asking for static foreach: given a compile-time constant array,
iterate over each of its members.
Since static foreach is unfortunately not going to happen (it was on Andrei's
list, but Walter rejected it), the other possibilities are to make
__traits(allMembers) into a tuple, or to use the static foreach workaround:

Something like:

enum a = __traits(allMembers, ...)'
foreach(T, int indx; ForEachTuple!(a.length))
{
   a = __traits(getMember, a[indx]);
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jan 28 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3744





 Actually, there's nothing at all wrong with __traits(getMember).
 Really, you are asking for static foreach: given a compile-time constant array,
 iterate over each of its members.
 Since static foreach is unfortunately not going to happen (it was on Andrei's
 list, but Walter rejected it), the other possibilities are to make
 __traits(allMembers) into a tuple, or to use the static foreach workaround:
 
 Something like:
 
 enum a = __traits(allMembers, ...)'
 foreach(T, int indx; ForEachTuple!(a.length))
 {
    a = __traits(getMember, a[indx]);
 }
Ok, good idea. But i cannot find a "ForEachTuple" template. Static version of foreach might be help in this sitiation, but i dunno how to do it. Cuz if i use normal foreach with this hack(using enum and index for getting a name of member) a have one same error: Error: string expected as second argument of __traits getMember instead of a[indx]. =/ -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Jan 28 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3744




Maybe not a pure, but works(also, thx to eldar, qtd-developer):

template Alias(T...){ //small hack, cuz directly alias dont want to work
    alias T Alias;
}
alias Alias!(__traits(allMembers, abc)) ABCMEMBS; //alias creates data as
compile-time var
void main(){
    foreach(m;ABCMEMBS){ //now we can use it in compile-time
        writeln(m, ": ", typeid(typeof(__traits(getMember, abc, m)))); //all
ok, cuz it's in compile-time
    }
}

class abc{
    void foo(){
    }
    int a,b,c;
}

We have this output:
foo: void()
a: int
b: int
c: int

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Feb 02 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3744


iorlas <denis.tomilin gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Priority|P2                          |P4
            Version|2.040                       |2.041
           Severity|blocker                     |minor



Changed priority, cuz now i can use one way to solve my problem. See code in
prev msg.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Feb 02 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3744


Hoenir <mrmocool gmx.de> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |mrmocool gmx.de



This is correlated with that bug:
http://d.puremagic.com/issues/show_bug.cgi?id=1386

Interestingly it still doesn't work even though allMembers returns a tuple
since r360:

4: foreach(m; __traits(allMembers, Check)){
5:    if (!__traits(isVirtualFunction, __traits(getMember, Check, m))){
6:        writefln("Var: s%, Type: %s", m, typeid(typeof(m)));
    }
}

yields:

main.d(5): Error: 'this' is only defined in non-static member functions, not
main
main.d(5): Error: this for a needs to be type Check not type int

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Feb 04 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3744


Brad Roberts <braddr puremagic.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Platform|x86_64                      |x86



PST ---
Mass migration of bugs marked as x86-64 to just x86.  The platform run on isn't
what's relevant, it's if the app is a 32 or 64 bit app.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Feb 06 2011
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3744


yebblies <yebblies gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |yebblies gmail.com
           Platform|x86                         |All
         Resolution|                            |FIXED
         OS/Version|Linux                       |All




 Interestingly it still doesn't work even though allMembers returns a tuple
 since r360:
 
 4: foreach(m; __traits(allMembers, Check)){
 5:    if (!__traits(isVirtualFunction, __traits(getMember, Check, m))){
 6:        writefln("Var: s%, Type: %s", m, typeid(typeof(m)));
     }
 }
 
 yields:
 
 main.d(5): Error: 'this' is only defined in non-static member functions, not
 main
 main.d(5): Error: this for a needs to be type Check not type int
It actually works correctly now (dmd 2.053). The errors in the sample above are because it's not possible to get a member variable of from the class type, you need an instance. The following works: -- import std.stdio; class Check { void foo() {} int bar() { return 0; } invariant() {} int x; } void main() { Check check; foreach(m; __traits(allMembers, Check)) { if (!__traits(isVirtualFunction, __traits(getMember, check, m))){ writefln("Var: %s, Type: %s", m, typeid(typeof(m))); } } } -- The bug was fixed when __traits(allMembers) started returning a tuple. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Jun 29 2011