[Issue 9897] New: Wrong context for nested class inside virtual or final function with contract which uses `this`

d-bugmail at puremagic.com d-bugmail at puremagic.com
Sun Apr 7 06:49:20 PDT 2013


http://d.puremagic.com/issues/show_bug.cgi?id=9897

           Summary: Wrong context for nested class inside virtual or final
                    function with contract which uses `this`
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Keywords: contracts, wrong-code
          Severity: major
          Priority: P2
         Component: DMD
        AssignedTo: nobody at puremagic.com
        ReportedBy: verylonglogin.reg at gmail.com


--- Comment #0 from Denis Shelomovskij <verylonglogin.reg at gmail.com> 2013-04-07 17:49:19 MSD ---
---
import std.stdio;

abstract class B
{
    abstract void g();
}

char* chPtr;
B b;

class C
{
    char ch;

    final void f()
    in { assert(ch != 7); }
    body
    {
        class D: B
        {
            this()
            {
                writefln("D: %s", cast(void*[]) (cast(void*) this)[0 ..
__traits(classInstanceSize, D)]);
                writefln("&c: %8s in C.f.D.this", &ch - ch.offsetof);
                assert(&ch == chPtr);
            }

            override void g()
            {
                writefln("&c: %8s in C.f.D.g", &ch - ch.offsetof);
                assert(&ch == chPtr); // Line 31
            }
        }
        b = new D;
    }
}

void main()
{
    auto c = new C();
    chPtr = &c.ch;
    writefln("&c: %8s in main", cast(void*) c);
    c.f();
    b.g();
}
---

Output:
---
&c:   A11F90 in main
D: [43F1E0, null, 12FD44]
&c:   A11F90 in C.f.D.this
&c:   440150 in C.f.D.g     <-- wrong c`'s address
<Assertion failure at line 31>
---

If `assert(ch != 7);` is replaced with `assert(chPtr);` or if `f` is `package`,
`private` or templated it works fine:
---
&c:   A11F90 in main
D: [43F1E0, null, A11F90]    <-- now the last field refers to `c`
&c:   A11F90 in C.f.D.this
&c:   A11F90 in C.f.D.g
---

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


More information about the Digitalmars-d-bugs mailing list