Bug 2881 – x.stringof returns typeof(x).stringof when x is an enum
Status
RESOLVED
Resolution
FIXED
Severity
major
Priority
P2
Component
dmd
Product
D
Version
D1 (retired)
Platform
x86
OS
All
Creation time
2009-04-22T16:26:00Z
Last change time
2014-02-16T15:23:53Z
Keywords
patch, wrong-code
Assigned to
nobody
Creator
bus_dbugzilla
Comments
Comment #0 by bus_dbugzilla — 2009-04-22T16:26:16Z
-------------
enum Shapes
{
Circle, Square
}
void main()
{
int i;
Shapes s;
pragma(msg, i.stringof);
pragma(msg, s.stringof);
}
-------------
Expected Output:
i
s
Actual Output:
i
Shapes
I'm not sure, but this might be related to the fix for #1610
Comment #1 by nfxjfg — 2010-01-13T08:38:27Z
This bug is a blocker for me. Here's a trivial patch that fixes it.
Note that the stringof functionality in getProperty() isn't called; it just isn't needed and produces the wrong result.
Dear Walter, if you don't accept this patch, pretty please state this clearly (instead of just not commenting it).
It's against dmd 1.053 (1.054/55 don't work for me because of other bugs).
--- a/mtype.c
+++ b/mtype.c
@@ -3888,7 +3888,7 @@ Expression *TypeEnum::dotExp(Scope *sc, Expression *e, Identifier *ident)
if (ident == Id::max ||
ident == Id::min ||
ident == Id::init ||
- ident == Id::stringof ||
+ //ident == Id::stringof ||
!sym->memtype
)
{
Comment #2 by dlang-bugzilla — 2010-03-14T05:04:34Z
Wow, what an ugly bug. This bug obliterates the entire concept of iterating over all the fields of a struct/class using .tupleof, if there's an enum field in it.
Also, I just checked and it's not a regression - this bug existed ever since .stringof appeared in DMD 1.005.
Comment #3 by nfxjfg — 2010-03-14T10:06:36Z
Vladimir, did the patch I posted fix the problem for you, without regressions in other parts of the code?
(Really nice that someone else cares about this problem. Maybe I can stop requiring a patched compiler for my code in a far, distant, feature?)
Just noting: bug 3651 is similar but unrelated.
Comment #4 by nfxjfg — 2010-04-29T01:42:30Z
If you just want to get the member names of a struct, there's an easy work around: use SomeStructType.tupleof.stringof and parse the result. Some strange inconsistency makes dmd use the actual member name even if the member's type is an enum.
Sadly, this makes parsing the .stringof result even more hacky and non-trivial than without bug 2881.
(If you use SomeStructType.tupleof[idx].stringof, when idx is the index of a member of an enum type, bug 2881 will make dmd to return the type name instead of the member name.)