Return value in BetterC mode.

meppl mephisto at nordhoff-online.de
Sat Feb 17 13:47:28 UTC 2018


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 :/


More information about the Digitalmars-d-learn mailing list