Skip to content

Commit 3d55536

Browse files
committed
Fix orphaned CondExp __cond variable refs in codegen AST
This fixes dmd-testsuite's compilable/b16598.d by making sure to (partially) constant-fold dtor expressions of VarDeclarations too. dlang/dmd#12176 made these __cond temporaries const, triggering constant-folding and potentially getting rid of it altogether when optimizing a CondExp.
1 parent 5b6aff6 commit 3d55536

File tree

1 file changed

+24
-13
lines changed

1 file changed

+24
-13
lines changed

dmd/optimize.d

+24-13
Original file line numberDiff line numberDiff line change
@@ -320,13 +320,23 @@ Expression Expression_optimize(Expression e, int result, bool keepLvalue)
320320

321321
override void visit(VarExp e)
322322
{
323-
if (keepLvalue)
323+
VarDeclaration v = e.var.isVarDeclaration();
324+
325+
if (!(keepLvalue && v && !(v.storage_class & STC.manifest)))
326+
ret = fromConstInitializer(result, e);
327+
328+
// if unoptimized, try to optimize the dtor expression
329+
// (e.g., might be a LogicalExp with constant lhs)
330+
if (ret == e && v && v.edtor)
324331
{
325-
VarDeclaration v = e.var.isVarDeclaration();
326-
if (v && !(v.storage_class & STC.manifest))
327-
return;
332+
// prevent infinite recursion (`<var>.~this()`)
333+
if (!v.inuse)
334+
{
335+
v.inuse++;
336+
expOptimize(v.edtor, WANTvalue);
337+
v.inuse--;
338+
}
328339
}
329-
ret = fromConstInitializer(result, e);
330340
}
331341

332342
override void visit(TupleExp e)
@@ -604,13 +614,15 @@ Expression Expression_optimize(Expression e, int result, bool keepLvalue)
604614
Type t1 = e.e1.type.toBasetype();
605615
if (t1.ty == Tdelegate)
606616
t1 = t1.nextOf();
607-
assert(t1.ty == Tfunction);
608-
TypeFunction tf = cast(TypeFunction)t1;
609-
for (size_t i = 0; i < e.arguments.dim; i++)
617+
// t1 can apparently be void for __ArrayDtor(T) calls
618+
if (auto tf = t1.isTypeFunction())
610619
{
611-
Parameter p = tf.parameterList[i];
612-
bool keep = p && p.isReference();
613-
expOptimize((*e.arguments)[i], WANTvalue, keep);
620+
for (size_t i = 0; i < e.arguments.dim; i++)
621+
{
622+
Parameter p = tf.parameterList[i];
623+
bool keep = p && p.isReference();
624+
expOptimize((*e.arguments)[i], WANTvalue, keep);
625+
}
614626
}
615627
}
616628
}
@@ -1066,8 +1078,7 @@ Expression Expression_optimize(Expression e, int result, bool keepLvalue)
10661078
ret = Expression_optimize(ret, result, false);
10671079
return;
10681080
}
1069-
if (expOptimize(e.e2, WANTvalue))
1070-
return;
1081+
expOptimize(e.e2, WANTvalue);
10711082
if (e.e1.isConst())
10721083
{
10731084
if (e.e2.isConst())

0 commit comments

Comments
 (0)