Skip to content

Commit

Permalink
Use the last effect as the result of the sequence, when lowering
Browse files Browse the repository at this point in the history
deconstruction
  • Loading branch information
jcouv committed Jun 27, 2017
1 parent 996fae2 commit 11f2783
Show file tree
Hide file tree
Showing 2 changed files with 219 additions and 95 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,31 @@ private BoundExpression RewriteDeconstruction(BoundTupleExpression left, Convers
ArrayBuilder<Binder.DeconstructionVariable> lhsTargets = GetAssignmentTargetsAndSideEffects(left, temps, effects.init);

BoundExpression returnValue = ApplyDeconstructionConversion(lhsTargets, right, conversion, temps, effects, isUsed, inInit: true);
if (!returnValue.HasErrors)

BoundExpression result;
if (!isUsed)
{
// When a deconstruction is not used, we use the last effect is used as return value
var last = effects.PopLast();
if (last is null)
{
result = null;
}
else
{
result = _factory.Sequence(temps.ToImmutableAndFree(), effects.ToImmutableAndFree(), last);
}
}
else
{
returnValue = VisitExpression(returnValue);
if (!returnValue.HasErrors && isUsed)
{
returnValue = VisitExpression(returnValue);
}
result = _factory.Sequence(temps.ToImmutableAndFree(), effects.ToImmutableAndFree(), returnValue);
}
BoundExpression result = _factory.Sequence(temps.ToImmutableAndFree(), effects.ToImmutableAndFree(), returnValue);
Binder.DeconstructionVariable.FreeDeconstructionVariables(lhsTargets);

Binder.DeconstructionVariable.FreeDeconstructionVariables(lhsTargets);
return result;
}

Expand All @@ -68,7 +86,7 @@ private BoundExpression RewriteDeconstruction(BoundTupleExpression left, Convers
Debug.Assert(!underlyingConversions.IsDefault);
Debug.Assert(leftTargets.Count == rightParts.Length && leftTargets.Count == conversion.UnderlyingConversions.Length);

var builder = ArrayBuilder<BoundExpression>.GetInstance(leftTargets.Count);
var builder = isUsed ? ArrayBuilder<BoundExpression>.GetInstance(leftTargets.Count) : null;
for (int i = 0; i < leftTargets.Count; i++)
{
BoundExpression resultPart;
Expand All @@ -95,7 +113,7 @@ private BoundExpression RewriteDeconstruction(BoundTupleExpression left, Convers
used: true, isChecked: false, isCompoundAssignment: false));
}
}
builder.Add(resultPart);
builder?.Add(resultPart);
}

if (isUsed)
Expand All @@ -109,8 +127,7 @@ private BoundExpression RewriteDeconstruction(BoundTupleExpression left, Convers
}
else
{
// When a deconstruction is not used, we use the last value of the LHS as the return value for the lowered form
return builder.ToImmutableAndFree().Last();
return null;
}
}

Expand Down Expand Up @@ -329,6 +346,35 @@ internal static DeconstructionSideEffects GetInstance()
return result;
}

internal BoundExpression PopLast()
{
if (assignments.Count != 0)
{
var last = assignments.Last();
assignments.RemoveLast();
return last;
}
if (conversions.Count != 0)
{
var last = conversions.Last();
conversions.RemoveLast();
return last;
}
if (deconstructions.Count != 0)
{
var last = deconstructions.Last();
deconstructions.RemoveLast();
return last;
}
if (init.Count != 0)
{
var last = init.Last();
init.RemoveLast();
return last;
}
return null;
}

internal ImmutableArray<BoundExpression> ToImmutableAndFree()
{
init.AddRange(deconstructions);
Expand Down
Loading

0 comments on commit 11f2783

Please sign in to comment.