www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 4860] New: Taking delegates to a member function broken if method is also aliased in from a base class

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

           Summary: Taking delegates to a member function broken if method
                    is also aliased in from a base class
           Product: D
           Version: D2
          Platform: x86_64
        OS/Version: Mac OS X
            Status: NEW
          Severity: critical
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: code klickverbot.at



---
Consider the following example (the alias directive in there might seem
strange, but is needed if there would be another, non-overridden overload of
foo (see http://www.digitalmars.com/d/2.0/function.html#function-inheritance):

---
import std.stdio;

class Base {
  void foo() {
  }
}

class Derived : Base {
  alias Base.foo foo;
  override void foo() {
  }
}

void main() {
  auto d = new Derived();
  void delegate() dg = &d.foo;
  writefln("dg: (%s, %s)", dg.ptr, dg.funcptr);
}
---

As long as the alias is present, the delegate created via »&d.foo« is invalid
–
its funcptr part is null (and indeed, trying to call the delegate yields an
access violation). If the alias directive is removed, everything works as
expected, but as explained above, this is not an option.

What may be also relevant is that DMD fails to infer the type for »&d.foo« –
replacing »void delegate() dg« with »auto dg« produces »cannot infer type
from
overloaded function symbol &d.foo«.

I would also be happy to learn about any workarounds, since this blocks the
release of my D SWIG module.

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




---
Iain pointed out on #d that the example above works if you put the alias
directive *after* the last overload:

---
import std.stdio;

class Base {
  void foo() {
  }
}

class Derived : Base {
  override void foo() {
  }
  alias Base.foo foo;
}

void main() {
  auto d = new Derived();
  void delegate() dg = &d.foo;
  writefln("dg: (%s, %s)", dg.ptr, dg.funcptr);
}
---

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


Steven Schveighoffer <schveiguy yahoo.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |schveiguy yahoo.com



14:31:07 PDT ---
In order to make this bug report valid, you should cite a better example.  In
your example, you are overriding the base function, and then also aliasing it. 
Yes, in the case you reference, it's valid, but your trivial example is
nonsensical -- you get nothing by aliasing Base.foo in this case.

I am pretty sure you have a better example :)

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




---
Steven, I am not quite sure if I see why a non-minimal code snippet in a bug
report would be useful, but here you go:

---
import std.stdio;

class Base {
  void foo( int i ) {}
  void foo( string s ) {}
}

class Derived : Base {
  alias Base.foo foo;
  override void foo( int i ) {}
}

void main() {
  auto d = new Derived();
  void delegate( int ) dg = &d.foo;
  writefln("dg: (%s, %s)", dg.ptr, dg.funcptr);
}
---

Feel free to reduce that to the above test case again. ;)

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




14:46:31 PDT ---

 Steven, I am not quite sure if I see why a non-minimal code snippet in a bug
 report would be useful
Because it avoids an argument against fixing the bug because the use case is completely useless. Your new example is perfect, thanks! -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Sep 13 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4860


nfxjfg gmail.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |nfxjfg gmail.com



What? A bug is a bug, it doesn't matter if the code causing it is nonsensical.
A code snippet reproducing a bug should be as minimal as possible.

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


Don <clugdbug yahoo.com.au> changed:

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



Wrong-code bugs are always important.
Here's a mitigation patch to turn it into a rejects-valid bug.
Haven't tracked down the root cause yet, but there certainly should be an
assert in this function -- if it's a virtual function, it should have a
non-negative vtable index, not -1.

e2ir.c, line 3275.  DelegateExp::toElem()

    Symbol *sfunc;
    int directcall = 0;

    printf("DelegateExp::toElem() '%s'\n", toChars());
+    if (func->isVirtual() && func->vtblIndex < 0)
+        error("Internal compiler error: malformed delegate. See Bugzilla
4860");
    sfunc = func->toSymbol();
    if (func->isNested())

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




That patch was a bit too early in the function. Should be e2ir.c, line 3308.
Still in DelegateExp::toElem().

        {
            // Get pointer to function out of virtual table
            unsigned vindex;

            assert(ethis);
            ep = el_same(&ethis);
            ep = el_una(OPind, TYnptr, ep);
            vindex = func->vtblIndex;
+    if (vindex < 0)
+        error("Internal compiler error: malformed delegate. See Bugzilla
4860");

            // Build *(ep + vindex * 4)
            ep = el_bin(OPadd,TYnptr,ep,el_long(TYsize_t, vindex * 4));
            ep = el_una(OPind,TYnptr,ep);
        }

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


Walter Bright <bugzilla digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |bugzilla digitalmars.com



18:00:27 PST ---
Don's mitigation patch:

http://www.dsource.org/projects/dmd/changeset/824

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


Don <clugdbug yahoo.com.au> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|wrong-code                  |rejects-valid
           Platform|x86_64                      |All
         OS/Version|Mac OS X                    |All



Downgrading to rejects-valid, now that the patch is in place.

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


yebblies <yebblies gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |yebblies gmail.com
         Resolution|                            |DUPLICATE



This is a symptom of issue 3359 - overload sets are not searched properly in
some cases.

*** This issue has been marked as a duplicate of issue 3359 ***

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




This is a symptom of issue 3559 - overload sets are not searched properly in
some cases.

*** This issue has been marked as a duplicate of issue 3559 ***

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