Bug 3744 – __traits getMember error in checking of second argument

Status
RESOLVED
Resolution
FIXED
Severity
minor
Priority
P4
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2010-01-26T15:50:00Z
Last change time
2014-02-15T02:46:13Z
Assigned to
nobody
Creator
denis.tomilin

Attachments

IDFilenameSummaryContent-TypeSize
557fail.dExample codetext/x-dsrc368

Comments

Comment #0 by denis.tomilin — 2010-01-26T15:50:48Z
Created attachment 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.
Comment #1 by denis.tomilin — 2010-01-27T00:14:04Z
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.
Comment #2 by clugdbug — 2010-01-27T00:53:18Z
(In reply to comment #1) > 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.
Comment #3 by denis.tomilin — 2010-01-27T01:41:07Z
(In reply to comment #2) > 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.
Comment #4 by clugdbug — 2010-01-27T02:20:05Z
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.
Comment #5 by denis.tomilin — 2010-01-27T03:05:12Z
(In reply to comment #4) > 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.
Comment #6 by clugdbug — 2010-01-28T11:09:25Z
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]); }
Comment #7 by denis.tomilin — 2010-01-28T21:35:03Z
(In reply to comment #6) > 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]. =/
Comment #8 by denis.tomilin — 2010-02-02T14:40:11Z
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
Comment #9 by denis.tomilin — 2010-02-02T14:43:13Z
Changed priority, cuz now i can use one way to solve my problem. See code in prev msg.
Comment #10 by hoganmeier — 2010-02-04T20:00:15Z
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
Comment #11 by braddr — 2011-02-06T15:39:45Z
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.
Comment #12 by yebblies — 2011-06-29T21:38:26Z
(In reply to comment #10) > 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.