www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 10146] New: ref on return is ignored

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

           Summary: ref on return is ignored
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: major
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: rswhite4 googlemail.com


--- Comment #0 from rswhite4 googlemail.com 2013-05-23 03:42:34 PDT ---
The 'ref' by 'getA' is ignored.

[code]
import std.stdio;

struct A {
public:
    int id;

    this(int id) {
        writeln("CTor ", id);

        this.id = id;
    }

    this(this) {
        writeln("Postblit ", this.id);

        this.id *= 2;
    }

    ~this() {
        writeln("DTor ", this.id);
    }
}

class B {
private:
    A _a;

public:
    this() {
        this._a = A(42);
    }

    ref A getA() {
        writeln("Return A");
        return this._a;
    }
}

void main() {
    B b = new B();
    A a = b.getA();
}
[/code]

Expected Output:
----
CTor 42
DTor 0
Return A
DTor 42
----

Current Output:
----
CTor 42
DTor 0
Return A
Postblit 42
DTor 84
DTor 42
----

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
May 23 2013
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=10146


Maxim Fomin <maxim maxim-fomin.ru> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |maxim maxim-fomin.ru
         Resolution|                            |INVALID
           Severity|major                       |normal


--- Comment #1 from Maxim Fomin <maxim maxim-fomin.ru> 2013-05-23 04:03:04 PDT
---
D does not have references like C++. Each time you assign return value to a
struct variable, refness is wiped out. The only cases when returning by ref
matter, is when returned value is also returned by caller by ref or casted to
pointer.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
May 23 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=10146



--- Comment #2 from rswhite4 googlemail.com 2013-05-23 04:08:43 PDT ---
I expected that in this case opAssign is called, not the postblit, beause I
return by ref and assign then the ref to a new value.

But if you change A to this:

[code]
struct A {
public:
    int id;

    this(int id) {
        writeln("CTor ", id);

        this.id = id;
    }

    this(this) {
        writeln("Postblit ", this.id);

        this.id *= 2;
    }

    void opAssign(ref const A a) {
        writeln("opAssign L: ", a.id);
        this.id = a.id;
    }

    void opAssign(const A a) {
        writeln("opAssign R ", a.id);
        memcpy(&this, &a, A.sizeof);
    }

    ~this() {
        writeln("DTor ", this.id);
    }
}
[/code]

You see still the same output, no call of opAssign by 'getA':

----
CTor 42
opAssign R 42
DTor 42
Return A
Postblit 42
DTor 84
DTor 42
----

Or did I miss something important here?

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
May 23 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=10146



--- Comment #3 from Maxim Fomin <maxim maxim-fomin.ru> 2013-05-23 04:14:52 PDT
---
(In reply to comment #2)
 I expected that in this case opAssign is called, not the postblit, beause I
 return by ref and assign then the ref to a new value.
Actually no, because refness here does not matter. You return by ref, but get not a ref in the caller. You don't assign ref to a new value either. You have A a = Initializer; which calls copy constructor. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
May 23 2013
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=10146



--- Comment #4 from rswhite4 googlemail.com 2013-05-23 04:18:16 PDT ---
That is absolute strange, but thanks for the explanation.

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