www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 5769] New: struct elaborate constructor should make rvalue

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

           Summary: struct elaborate constructor should make rvalue
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: k.hara.pg gmail.com



Struct literal/constructor call should make rvalue.
Related issue is issue5178.

Test code:
----
struct S    // has default parameterized constructor
{
    int n;
}
struct T    // has normal constructor
{
    this(int n){}
//  ref T __ctor(int n){ return this; }  // dmd generates internally like this
}
struct T2
{
    template __ctor()   // workaround
    {
        T2 __ctor(int n){ return this; } // return copied rvalue
    }
}

// lvalue checker
bool checker(T...)(auto ref T t){ return __traits(isRef, t[0]); }

import std.stdio;
void main()
{
    int n =    20;
    S   s =  S(20);
    T   t =  T(20);
    T2 t2 = T2(20);

    writefln("%s / %s", checker(   10) , checker( n));  // false/true, ok
    writefln("%s / %s", checker( S(10)), checker( s));  // true /true, NG  
(Fixed by issue5178 patch)
    writefln("%s / %s", checker( T(10)), checker( t));  // true /true, NG!!!
    writefln("%s / %s", checker(T2(10)), checker(t2));  // false/true, ok
}
----

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




Fixing thi bug is difficult for me.

DMD internally behavior is...
1.Elaborate constructor returns this reference internally.
  -> see CtorDeclaration::semantic()
2.Elaborate constructor call T(args) is translated like (T __ctmp,
__ctmp.__ctor(args)) and __ctmp.__ctor returns ref T.
  -> see CallExp::semantic

Trivial fix patch is follwing, but this causes copying value.
----
 src/func.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)

diff --git a/src/func.c b/src/func.c
index c3fd455..ea48670 100644
--- a/src/func.c
+++ b/src/func.c
   -2953,7 +2953,7    void CtorDeclaration::semantic(Scope *sc)

 #if STRUCTTHISREF
     if (ad && ad->isStructDeclaration())
-    {   ((TypeFunction *)type)->isref = 1;
+    {   //((TypeFunction *)type)->isref = 1;
         if (!originalType)
             // Leave off the "ref"
             originalType = new TypeFunction(arguments, tret, varargs, LINKd,
storage_class | sc->stc);
----

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


SomeDude <lovelydear mailmetrash.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |lovelydear mailmetrash.com



PDT ---
On 2.059, 

PS E:\DigitalMars\dmd2\samples> rdmd bug.d
false / true
false / true
false / true
false / true

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


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

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|                            |DUPLICATE



*** This issue has been marked as a duplicate of issue 5889 ***

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