www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Strange behavior when concatenating array

reply Jose Armando Garcia <jsancio gmail.com> writes:
It looks like the rt is not calling the postblit constructor when
concatenating arrays. For example, the following code:

import std.stdio;

struct Test
{
   this(this) { writeln("copy done"); }
   void opAssign(Test rhs) { writeln("assignment done"); }
   ~this() { writeln("destructor called"); }
}

void main()
{
   Test[] tests = new Test[1];
   {
      Test test;
      tests ~= test;
   }
   writeln("done");
}

Gives the following output:

destructor called
done

The dtr for 'Test test;' is getting call after the scope exits but the
postblit ctr for 'tests[0]' is never called. I believe the output of
this code should either be:

done

or:

copy done
destructor called
done

Thanks!
-Jose
Jun 16 2011
next sibling parent reply "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Fri, 17 Jun 2011 00:05:56 -0400, Jose Armando Garcia  
<jsancio gmail.com> wrote:

 It looks like the rt is not calling the postblit constructor when
 concatenating arrays. For example, the following code:

 import std.stdio;

 struct Test
 {
    this(this) { writeln("copy done"); }
    void opAssign(Test rhs) { writeln("assignment done"); }
    ~this() { writeln("destructor called"); }
 }

 void main()
 {
    Test[] tests = new Test[1];
    {
       Test test;
       tests ~= test;
    }
    writeln("done");
 }

 Gives the following output:

 destructor called
 done

 The dtr for 'Test test;' is getting call after the scope exits but the
 postblit ctr for 'tests[0]' is never called. I believe the output of
 this code should either be:

 done

 or:

 copy done
 destructor called
 done

You are right, postblit should be called. http://d.puremagic.com/issues/show_bug.cgi?id=5272 I'll look at doing a fix. -Steve
Jun 17 2011
parent Timon Gehr <timon.gehr gmx.ch> writes:
Steven Schveighoffer wrote:
 https://github.com/D-Programming-Language/druntime/pull/29

 See if that helps.  On my system, your code now results in your expected
 output.

 -Steve

If the compiler optimizes well enough the output given could be valid (it might construct the struct directly where it belongs). This needs some investigation. Anyways, is druntime the correct place to fix this? If it _is_ a bug, isn't it a compiler bug and not a bug in druntime? After a quick glimpse over your patch I think the output will never be just "done" now. Timon
Jun 17 2011
prev sibling next sibling parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Fri, 17 Jun 2011 06:42:51 -0400, Steven Schveighoffer  
<schveiguy yahoo.com> wrote:

 On Fri, 17 Jun 2011 00:05:56 -0400, Jose Armando Garcia  
 <jsancio gmail.com> wrote:

 It looks like the rt is not calling the postblit constructor when
 concatenating arrays. For example, the following code:

 import std.stdio;

 struct Test
 {
    this(this) { writeln("copy done"); }
    void opAssign(Test rhs) { writeln("assignment done"); }
    ~this() { writeln("destructor called"); }
 }

 void main()
 {
    Test[] tests = new Test[1];
    {
       Test test;
       tests ~= test;
    }
    writeln("done");
 }

 Gives the following output:

 destructor called
 done

 The dtr for 'Test test;' is getting call after the scope exits but the
 postblit ctr for 'tests[0]' is never called. I believe the output of
 this code should either be:

 done

 or:

 copy done
 destructor called
 done

You are right, postblit should be called. http://d.puremagic.com/issues/show_bug.cgi?id=5272 I'll look at doing a fix.

https://github.com/D-Programming-Language/druntime/pull/29 See if that helps. On my system, your code now results in your expected output. -Steve
Jun 17 2011
prev sibling parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Fri, 17 Jun 2011 18:52:05 -0400, Timon Gehr <timon.gehr gmx.ch> wrote:

 Steven Schveighoffer wrote:
 https://github.com/D-Programming-Language/druntime/pull/29

 See if that helps.  On my system, your code now results in your expected
 output.

 -Steve

If the compiler optimizes well enough the output given could be valid (it might construct the struct directly where it belongs). This needs some investigation. Anyways, is druntime the correct place to fix this? If it _is_ a bug, isn't it a compiler bug and not a bug in druntime? After a quick glimpse over your patch I think the output will never be just "done" now.

The patch is still necessary in cases where the value being appended is not a temporary. For example, if in the OP's code the test variable was used after appending it to the array. Michel Fortin also brought up the point in a comment on the pull request. But the reality is, druntime cannot do any better without help from the compiler (the runtime needs to be told when to refrain from running a postblit). It's a separate bug. However, I think there is nothing invalid with doing both the postblit and the destructor on the temporary. So at least the code is correctly calling the postblit. -Steve
Jun 17 2011