Bug 9897 – Wrong context for nested class inside virtual or final function with contract which uses `this`

Status
RESOLVED
Resolution
DUPLICATE
Severity
major
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2013-04-07T06:49:00Z
Last change time
2015-07-01T08:03:00Z
Keywords
contracts, wrong-code
Assigned to
nobody
Creator
verylonglogin.reg

Comments

Comment #0 by verylonglogin.reg — 2013-04-07T06:49:19Z
--- 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 ---
Comment #1 by k.hara.pg — 2015-07-01T08:03:00Z
The class D in C.foo captures 'this' variable on stack, due to access C.ch from D, then it makes C.foo a closure. *** This issue has been marked as a duplicate of issue 9383 ***