www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Return value in BetterC mode.

reply ANtlord <antlord92 gmail.com> writes:
Hello!
Yesterday I found an interesting issue for myself while I was 
implementing unique pointer for my project in BetterC. When I was 
trying to return new instance from a `move` method I got calling 
of destructor the instance. When the instance is out of scope the 
calling is happens again. I also see that addresses of an 
instance that calls destructor are same.

That thing doesn't happen when I don't use BetterC mode. Is that 
bug or feature?
Please checkout code https://run.dlang.io/is/m1TRnT and try it in 
BetterC mode and in usual mode.

But if I return result of a constructor directly instead of 
saving the result to variable and returning the variable from the 
`move` method I don't see double calling of the same destructor.

I. e.
```
auto newObject = Box(this);
return newObject;
```
is replaced by
```
return Box(this);
```

So... is that bug or feature?
Feb 16 2018
parent reply meppl <mephisto nordhoff-online.de> writes:
On Saturday, 17 February 2018 at 07:58:40 UTC, ANtlord wrote:
 Hello!
 Yesterday I found an interesting issue for myself while I was 
 implementing unique pointer for my project in BetterC. When I 
 was trying to return new instance from a `move` method I got 
 calling of destructor the instance. When the instance is out of 
 scope the calling is happens again. I also see that addresses 
 of an instance that calls destructor are same.

 That thing doesn't happen when I don't use BetterC mode. Is 
 that bug or feature?
 Please checkout code https://run.dlang.io/is/m1TRnT and try it 
 in BetterC mode and in usual mode.

 But if I return result of a constructor directly instead of 
 saving the result to variable and returning the variable from 
 the `move` method I don't see double calling of the same 
 destructor.

 I. e.
 ```
 auto newObject = Box(this);
 return newObject;
 ```
 is replaced by
 ```
 return Box(this);
 ```

 So... is that bug or feature?
Okay, with the glorious help of the compiler-switch `-vcg-ast` I was able to see the source of that weirdness. See also: https://forum.dlang.org/thread/juxihybpqpjycmxiydns forum.dlang.org It seems that there is always a try-block when the struct has a destructor I reduced your code to: ---- struct S { ~this() {} } S myFunction() { S s = S(); return s; } extern( C) void main(){} ---- transformation of myFunction() with $ dmd -betterC -vcg-ast source/app.d ---- S myFunction() { S s = S(); try { return s; } finally s.~this(); } ---- transformation of myFunction() with $ dmd -vcg-ast source/app.d ---- S myFunction() { S s = S(); try return s; catch(Throwable __o3) { s.~this(); throw __o3; } } ---- sadly I have no good idea how to name the title of that issue :/
Feb 17 2018
parent meppl <mephisto nordhoff-online.de> writes:
On Saturday, 17 February 2018 at 13:47:28 UTC, meppl wrote:
 On Saturday, 17 February 2018 at 07:58:40 UTC, ANtlord wrote:
 ...
... sadly I have no good idea how to name the title of that issue :/
I looked at it again and came up with a title name: https://issues.dlang.org/show_bug.cgi?id=18457
Feb 18 2018