Bug 18457 – betterC - struct destructor is always called at function return
Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2018-02-18T13:56:03Z
Last change time
2018-11-02T02:01:48Z
Keywords
betterC
Assigned to
No Owner
Creator
Johannes Nordhoff
Comments
Comment #0 by mephisto — 2018-02-18T13:56:03Z
D seems to have an optimization:
If a struct is initialized in a function and is returned from the same function, the destructor will not be called at return.
But:
With betterC this optimization doesn't appear.
Problem:
This produces a different behaviour of betterC and non-betterC.
an example code from file "source/app.d":
----
struct S {
~this() {}
}
S myFunction() {
S s = S();
return s;
}
S myOFunction( S s) {
return s;
}
extern( C) void main() {}
----
extraction from:
$ dmd -vcg-ast source/app.d
----
S myFunction()
{
S s = S();
try
return s;
catch(Throwable __o3)
{
s.~this();
throw __o3;
}
}
S myOFunction(S s)
{
try
{
return s;
}
finally
s.~this();
}
----
extraction from:
$ dmd -betterC -vcg-ast source/app.d
----
S myFunction()
{
S s = S();
try
{
return s;
}
finally
s.~this();
}
S myOFunction(S s)
{
try
{
return s;
}
finally
s.~this();
}
----