Skip to content

Commit

Permalink
[c] Return local object storage with a destructor.
Browse files Browse the repository at this point in the history
  • Loading branch information
pfusik committed Mar 13, 2024
1 parent c092b6d commit 501536b
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 6 deletions.
2 changes: 1 addition & 1 deletion GenC.fu
Original file line number Diff line number Diff line change
Expand Up @@ -2951,7 +2951,7 @@ public class GenC : GenCCpp
// Optimization: avoid copy
WriteDestructAll(local);
Write("return ");
if (this.CurrentMethod.Type is FuClassType resultPtr)
if (this.CurrentMethod.Type is FuClassType resultPtr && !(resultPtr is FuStorageType))
WriteClassPtr(resultPtr.Class, symbol, FuPriority.Argument); // upcast, but don't AddRef
else
symbol.Accept(this, FuPriority.Argument);
Expand Down
3 changes: 2 additions & 1 deletion libfut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11869,7 +11869,8 @@ void GenC::visitReturn(const FuReturn * statement)
if (std::find(this->varsToDestruct.begin(), this->varsToDestruct.end(), local) != this->varsToDestruct.end()) {
writeDestructAll(local);
write("return ");
if (const FuClassType *resultPtr = dynamic_cast<const FuClassType *>(this->currentMethod->type.get()))
const FuClassType * resultPtr;
if ((resultPtr = dynamic_cast<const FuClassType *>(this->currentMethod->type.get())) && !dynamic_cast<const FuStorageType *>(resultPtr))
writeClassPtr(resultPtr->class_, symbol, FuPriority::argument);
else
symbol->accept(this, FuPriority::argument);
Expand Down
2 changes: 1 addition & 1 deletion libfut.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12139,7 +12139,7 @@ internal override void VisitReturn(FuReturn statement)
if (this.VarsToDestruct.Contains(local)) {
WriteDestructAll(local);
Write("return ");
if (this.CurrentMethod.Type is FuClassType resultPtr)
if (this.CurrentMethod.Type is FuClassType resultPtr && !(resultPtr is FuStorageType))
WriteClassPtr(resultPtr.Class, symbol, FuPriority.Argument);
else
symbol.Accept(this, FuPriority.Argument);
Expand Down
2 changes: 1 addition & 1 deletion libfut.js
Original file line number Diff line number Diff line change
Expand Up @@ -12539,7 +12539,7 @@ export class GenC extends GenCCpp
this.#writeDestructAll(local);
this.write("return ");
let resultPtr;
if ((resultPtr = this.currentMethod.type) instanceof FuClassType)
if ((resultPtr = this.currentMethod.type) instanceof FuClassType && !(resultPtr instanceof FuStorageType))
this.#writeClassPtr(resultPtr.class, symbol, FuPriority.ARGUMENT);
else
symbol.accept(this, FuPriority.ARGUMENT);
Expand Down
4 changes: 2 additions & 2 deletions test/MethodArgTempObjectStgDestructor.fu
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public static class Test
{
Data() o;
o.S = "foo";
return o; //FAIL: c TODO
return o;
}

static string AcceptPtr(Data d)
Expand All @@ -19,6 +19,6 @@ public static class Test

public static bool Run()
{
return AcceptPtr(ReturnStorage()) == "foo";
return AcceptPtr(ReturnStorage()) == "foo"; //FAIL: c TODO
}
}

0 comments on commit 501536b

Please sign in to comment.