Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion src/Compilers/CSharp/Portable/Lowering/SpillSequenceSpiller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1130,7 +1130,7 @@ public override BoundNode VisitConditionalOperator(BoundConditionalOperator node

return conditionBuilder.Update(_F.Default(node.Type));
}
else
else if (!node.IsRef)
{
var tmp = _F.SynthesizedLocal(node.Type, kind: SynthesizedLocalKind.Spill, syntax: _F.Syntax);

Expand All @@ -1142,6 +1142,33 @@ public override BoundNode VisitConditionalOperator(BoundConditionalOperator node

return conditionBuilder.Update(_F.Local(tmp));
}
else
{
Debug.Assert(condition.Type.SpecialType == SpecialType.System_Boolean);

// 1. Capture the boolean value (the condition) in a temp
var tmp = _F.SynthesizedLocal(condition.Type, kind: SynthesizedLocalKind.Spill, syntax: _F.Syntax);

conditionBuilder.AddLocal(tmp);
conditionBuilder.AddStatement(_F.Assignment(_F.Local(tmp), condition));
condition = _F.Local(tmp);

// 2. Conditionally execute side-effects from the builders based on the temp
conditionBuilder.AddLocals(consequenceBuilder.GetLocals());
conditionBuilder.AddLocals(alternativeBuilder.GetLocals());

conditionBuilder.AddStatement(
_F.If(condition,
_F.StatementList(consequenceBuilder.GetStatements()),
_F.StatementList(alternativeBuilder.GetStatements())));

consequenceBuilder.Free();
alternativeBuilder.Free();

// 3. Use updated conditional operator as the result. Note, we are using the captured temp as its condition,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use updated conditional operator as the result

nit: consider calling out that we want to use the original node to maintain proper refness

// plus rewritten consequence and alternative.
return conditionBuilder.Update(node.Update(node.IsRef, condition, consequence, alternative, node.ConstantValueOpt, node.NaturalTypeOpt, node.WasTargetTyped, node.Type));
}
}

public override BoundNode VisitConversion(BoundConversion node)
Expand Down
Loading