Skip to content

Commit 7a6c6cc

Browse files
committed
Do proper construction when initializing __result
1 parent 720f0e0 commit 7a6c6cc

4 files changed

Lines changed: 9 additions & 23 deletions

File tree

compiler/src/dmd/funcsem.d

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2769,6 +2769,7 @@ void buildResultVar(FuncDeclaration fd, Scope* sc, Type tret)
27692769
* fbody.dsymbolSemantic() running, vresult.type might be modified.
27702770
*/
27712771
fd.vresult = new VarDeclaration(loc, tret, Id.result, null);
2772+
fd.vresult._init = new VoidInitializer(loc); // hdrgen requires _init
27722773
fd.vresult.storage_class |= STC.nodtor | STC.temp;
27732774
if (!fd.isVirtual())
27742775
fd.vresult.storage_class |= STC.const_;

compiler/src/dmd/glue/s2ir.d

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -661,22 +661,7 @@ void Statement_toIR(Statement s, ref IRState irs, StmtState* stmtstate)
661661
{
662662
// Reference return, so convert to a pointer
663663
e = toElemDtor(s.exp, irs);
664-
665-
/* already taken care of for vresult in buildResultVar() and semantic3.d
666-
* https://issues.dlang.org/show_bug.cgi?id=19384
667-
*/
668-
if (func.vresult)
669-
if (BlitExp be = s.exp.isBlitExp())
670-
{
671-
if (VarExp ve = be.e1.isVarExp())
672-
{
673-
if (ve.var == func.vresult)
674-
goto Lskip;
675-
}
676-
}
677-
678664
e = addressElem(e, s.exp.type.pointerTo());
679-
Lskip:
680665
}
681666
else
682667
{

compiler/src/dmd/semantic3.d

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -961,8 +961,10 @@ private extern(C++) final class Semantic3Visitor : Visitor
961961

962962
/* https://issues.dlang.org/show_bug.cgi?id=10789
963963
* If NRVO is not possible, all returned lvalues should call their postblits.
964+
* For functions with a __result variable, postblits will be called later
965+
* during initialization of __result.
964966
*/
965-
if (!funcdecl.isNRVO)
967+
if (!funcdecl.isNRVO && !funcdecl.vresult)
966968
exp = doCopyOrMove(sc2, exp, f.next, true, true);
967969

968970
if (tret.hasPointers())
@@ -973,13 +975,11 @@ private extern(C++) final class Semantic3Visitor : Visitor
973975

974976
if (funcdecl.vresult)
975977
{
976-
// Create: return vresult = exp;
977-
if (canElideCopy(exp, funcdecl.vresult.type))
978-
exp = new ConstructExp(rs.loc, funcdecl.vresult, exp);
979-
else
980-
exp = new BlitExp(rs.loc, funcdecl.vresult, exp);
981-
978+
// Create: return (vresult = exp, vresult);
979+
exp = new ConstructExp(rs.loc, funcdecl.vresult, exp);
982980
exp.type = funcdecl.vresult.type;
981+
exp = Expression.combine(exp, new VarExp(rs.loc, funcdecl.vresult));
982+
exp = exp.expressionSemantic(sc2);
983983

984984
if (rs.caseDim)
985985
exp = Expression.combine(exp, new IntegerExp(rs.caseDim));

compiler/test/compilable/extra-files/vcg-ast.d.cg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ class C : Object
6161
}
6262
__require();
6363
this.__invariant();
64-
__result = 2;
64+
__result = 2 , __result;
6565
goto __returnLabel;
6666
__returnLabel:
6767
this.__invariant();

0 commit comments

Comments
 (0)