Bug 4714 – Cannot return ref this when struct has invariant

Status
RESOLVED
Resolution
DUPLICATE
Severity
critical
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
Other
OS
Linux
Creation time
2010-08-22T14:41:00Z
Last change time
2011-06-18T06:46:17Z
Keywords
rejects-valid
Assigned to
nobody
Creator
issues.dlang

Comments

Comment #0 by issues.dlang — 2010-08-22T14:41:29Z
The following code import std.stdio; struct S { ref S hello() { writeln("hello"); return this; } invariant() { } } void main() { S().hello().hello().hello(); } results in the error prog.d(9): Error: __result = this is not mutable prog.d(9): Error: __result = this is not an lvalue If I remove the invariant, it compiles and runs just fine. Also, if I compile with -release, it compiles and runs just fine. But if that invariant is there and compiled in (as it would be without -release), it won't compile. The same goes for out if I use it instead of invariant. This makes it very difficulty to use the idiom where you have member functions returning references to the object so that you can chain function calls. That is, it's impossible to use that idiom and use invariants or out contracts. I assume that something in the invariant and out code dealing with the return value does not take into account the fact that you could return a reference to this from a struct's member function.
Comment #1 by clugdbug — 2010-09-20T08:33:37Z
This looks very similar to bug 3273 (struct invariant + dtor fails to compile (no line number)). But my patch for 3273 doesn't fix this one. Suggests that my patch to 3273 is probably wrong.
Comment #2 by k.hara.pg — 2011-06-18T06:46:17Z
*** This issue has been marked as a duplicate of issue 3273 ***