www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 4714] New: Cannot return ref this when struct has invariant

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

           Summary: Cannot return ref this when struct has invariant
           Product: D
           Version: D2
          Platform: Other
        OS/Version: Linux
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: jmdavisProg gmail.com


--- Comment #0 from Jonathan M Davis <jmdavisProg gmail.com> 2010-08-22
14:41:29 PDT ---
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.

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


Don <clugdbug yahoo.com.au> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |clugdbug yahoo.com.au
           Severity|normal                      |critical


--- Comment #1 from Don <clugdbug yahoo.com.au> 2010-09-20 08:33:37 PDT ---
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.

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


Kenji Hara <k.hara.pg gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |k.hara.pg gmail.com
         Resolution|                            |DUPLICATE


--- Comment #2 from Kenji Hara <k.hara.pg gmail.com> 2011-06-18 06:46:17 PDT ---
*** This issue has been marked as a duplicate of issue 3273 ***

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