Thread overview
[Issue 2206] New: unnamed template mixin of class inside function or class has incorrect classinfo and mangleof
Jul 08, 2008
d-bugmail
Jul 08, 2008
d-bugmail
Jul 09, 2008
d-bugmail
Dec 05, 2010
Walter Bright
July 08, 2008
http://d.puremagic.com/issues/show_bug.cgi?id=2206

           Summary: unnamed template mixin of class inside function or class
                    has incorrect classinfo and mangleof
           Product: D
           Version: 1.029
          Platform: PC
        OS/Version: Linux
            Status: NEW
          Keywords: wrong-code
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: bugzilla@digitalmars.com
        ReportedBy: kamm-removethis@incasoftware.de


Test:
--
template T() {
  class C {}
}

void main()
{
  mixin T!(); // using a named mixin here fixes it

  pragma(msg, T!().C.mangleof);
  pragma(msg, C.mangleof); // incorrectly outputs the same as above

  assert(T!().C.classinfo !is C.classinfo); // fails
  assert(T!().C.mangleof != C.mangleof); // fails
}
--
The types of T!().C and main.C are clearly different as the nested one carries an additional context pointer. This means that allocation relying on classinfo.init.length should be wrong too.

In the frontend the types are initially different and only merged into a single type during semantic. To be specific, Type::mangle unifies them as toDecoBuffer yields the same result for both.

Maybe this could be fixed by adding a TemplateMixin::mangle overload. This frontend bug causes some template-mixin related bugs in LLVMDC that seem to be worked around somehow in DMD.


-- 

July 08, 2008
http://d.puremagic.com/issues/show_bug.cgi?id=2206





------- Comment #1 from kamm-removethis@incasoftware.de  2008-07-08 14:37 -------
To illustrate the resulting problem with class allocation:

--
import std.stdio;

class D {}

template T() {
  class C { this() { } }
}

void main()
{
  mixin T!();

  // all print 8
  writefln(T!().C.classinfo.init.length);
  writefln(C.classinfo.init.length);
  writefln(D.classinfo.init.length);

  auto c = new C; // segfault in _d_newclass
}


-- 

July 09, 2008
http://d.puremagic.com/issues/show_bug.cgi?id=2206


kamm-removethis@incasoftware.de changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |patch




------- Comment #2 from kamm-removethis@incasoftware.de  2008-07-09 09:58 -------
Adding the following override for TemplateMixin::mangle seems to have fixed the issue in LLVMDC:

--
char *TemplateMixin::mangle()
{
    OutBuffer buf;
    char *id;

#if 0
    printf("TemplateMixin::mangle() %s", toChars());
    if (parent)
        printf("  parent = %s %s", parent->kind(), parent->toChars());
    printf("\n");
#endif
    id = ident ? ident->toChars() : toChars();
// use parent instead of tempdecl->parent here
    if (parent)
    {
        char *p = parent->mangle();
        if (p[0] == '_' && p[1] == 'D')
            p += 2;
        buf.writestring(p);
    }
    buf.printf("%"PRIuSIZE"%s", strlen(id), id);
    id = buf.toChars();
    buf.data = NULL;
    //printf("TemplateMixin::mangle() %s = %s\n", toChars(), id);
    return id;
}
--


-- 

December 05, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=2206


Walter Bright <bugzilla@digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|                            |FIXED


--- Comment #3 from Walter Bright <bugzilla@digitalmars.com> 2010-12-04 21:23:55 PST ---
http://www.dsource.org/projects/dmd/changeset/777

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------