Bug 1748 – Wrong stringof for templated classes

Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2007-12-23T22:13:00Z
Last change time
2015-01-06T08:34:39Z
Keywords
patch, pull, wrong-code
Assigned to
bugzilla
Creator
casantander1

Attachments

IDFilenameSummaryContent-TypeSize
279patchPatch to add template parameters to class .stringoftext/plain506

Comments

Comment #0 by casantander1 — 2007-12-23T22:13:00Z
I understand that "class A(T)" is syntactic sugar for "template A(T){class A...", but I still think this is wrong behavior: --- class A(T) {} alias A!(int) Aint; A!(int) a; pragma(msg, typeof(a).stringof); pragma(msg, Aint.stringof); static assert(typeof(a).stringof != "A", "typeof(a).stringof shouldn't be A"); --- Both pragmas print A, when I think it should be A!(int). Tested with GDC rev 198, but this is part of the front end.
Comment #1 by dhasenan — 2008-11-10T10:02:32Z
This is really hurting me with CTFE + mixins. I can't use templated classes without creating a subclass for each template instantiation, and I can't use templated structs, if I want to use some of my testing code for mock objects. (That is, these objects cannot have methods that reference templated classes or templated structs.)
Comment #2 by dhasenan — 2008-11-10T13:35:28Z
It looks like fixing this will require giving Dsymbol a reference to a TemplateInstance, eliminating the nesting part (or giving an option to omit it), then modifying *::toChars to check if it's in a template instance and, if so, prepend the template instance's string representation. You can add the reference at template.c:3026 Only types (typedefs, classes, structs) need modification in their toChars methods. Expected time required to make this change: 30-60 minutes. I'll make it tonight, if I can get llvm to compile.
Comment #3 by dhasenan — 2008-11-15T22:04:22Z
This isn't a straightforward fix after all. I've seen some situations in which a string representation closer to what we would need is shown... With structs, I'm seeing: typeof(a).stringof prints the correct result A!(int).stringof prints the wrong result This will give at least a workaround with a minimum of effort.
Comment #4 by dhasenan — 2008-11-15T22:52:41Z
Created attachment 279 Patch to add template parameters to class .stringof
Comment #5 by dhasenan — 2008-11-15T22:54:08Z
If you have a templated struct, it partially works already: --- struct S(T) {} S!(int) s; pragma (msg, typeof(s).stringof); // prints S!(int) pragma (msg, S!(int).stringof); // prints S pragma (msg, (S!(int)).stringof); // prints S!(int) --- The patch I just added gives the same output if you change 'struct' to 'class'.
Comment #6 by dhasenan — 2008-11-23T08:43:09Z
The patch I submitted fixes the case of: class S(T){} I don't think it fixes any other cases: class S(T) { class B {} } S!(int).B.stringof == B template Template(T) { struct S {} } Template!(int).S.stringof == S However, it does cover the basic case, which is something of an improvement. I'll see what I can do about everything else. Additionally, I think there might be something going on with nested template .stringof that I have to look into.
Comment #7 by k.hara.pg — 2013-11-22T01:03:32Z
Comment #8 by github-bugzilla — 2013-11-23T12:43:16Z
Commits pushed to master at https://github.com/D-Programming-Language/dmd https://github.com/D-Programming-Language/dmd/commit/ceb3bf6370a4d7e3688cce2bda8b8df6467a2f25 fix Issue 1748 - Wrong stringof for templated classes https://github.com/D-Programming-Language/dmd/commit/9eba2f5e2785b0f85a428d7242b9af18aa683887 Merge pull request #2854 from 9rnsr/fix1748 Issue 1748 - Wrong stringof for templated classes
Comment #9 by k.hara.pg — 2015-01-06T08:34:39Z
*** Issue 9460 has been marked as a duplicate of this issue. ***