D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 13687 - Virtual call in the interface's precondition is dispatched to the wrong vtbl entry
Summary: Virtual call in the interface's precondition is dispatched to the wrong vtbl ...
Status: RESOLVED WORKSFORME
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: x86_64 Linux
: P1 major
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2014-11-04 22:40 UTC by Ali Cehreli
Modified: 2018-10-11 17:45 UTC (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Ali Cehreli 2014-11-04 22:40:35 UTC
If an interface makes a virtual function call in its precondition block then the call gets dispatched to the wrong vtbl entry. (Probably to toHash() in the following program).

For the bug to occur, the derived class must have a precondition block as well.

Also see the following discussion:

  http://forum.dlang.org/thread/m3bbbb$lgi$1@digitalmars.

import std.stdio;

interface I
{
    void foo()
    in
    {
        int result = virtualCall();
        writeln(result);
    }

    int virtualCall();
}

class C : I
{
    void foo()
    in
    {}
    body
    {}

    int virtualCall()
    {
        return 42;
    }
}

void main()
{
    new C().foo();
}

The output of the program is not the expected 42 from virtualCall():

-226009216

Ali
Comment 1 Nathan S. 2018-10-10 22:35:03 UTC
Tested with DMD 2.082.0. Program output was "42" as desired.
Comment 2 Steven Schveighoffer 2018-10-11 17:45:14 UTC
According to run.dlang.io, happened around 2.071