Skip to content
This repository has been archived by the owner on Jan 22, 2022. It is now read-only.

Commit

Permalink
Fix issue with compound assignment operators
Browse files Browse the repository at this point in the history
- Fix issue with compound assignment operators and potentially other things modifying a value before it is used in a binary expression. This was causing incorrect results in expressions like `float s = 5; s = s * (s -= 3);` because the s -= 3 would be executed before the multiply so the result would be 4 whereas the expected result was 10.
  • Loading branch information
MerlinVR committed Apr 7, 2020
1 parent 4b52882 commit e059f61
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions Assets/UdonSharp/Editor/UdonSharpASTVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1568,20 +1568,28 @@ public override void VisitBinaryExpression(BinaryExpressionSyntax node)
SymbolDefinition rhsValue = null;
SymbolDefinition lhsValue = null;

using (ExpressionCaptureScope rhsCapture = new ExpressionCaptureScope(visitorContext, null))
{
Visit(node.Right);

rhsValue = rhsCapture.ExecuteGet();
}

ExpressionCaptureScope outerScope = visitorContext.topCaptureScope;

using (ExpressionCaptureScope lhsCapture = new ExpressionCaptureScope(visitorContext, null))
{
Visit(node.Left);

lhsValue = lhsCapture.ExecuteGet();
// This needs to be copied because someone can do an in place assignment operator on the rhs that changes the lhs value
SymbolDefinition lhsCopy = visitorContext.topTable.CreateUnnamedSymbol(lhsCapture.GetReturnType(true), SymbolDeclTypeFlags.Internal);
using (ExpressionCaptureScope lhsCopySetter = new ExpressionCaptureScope(visitorContext, null))
{
lhsCopySetter.SetToLocalSymbol(lhsCopy);
lhsCopySetter.ExecuteSetDirect(lhsCapture);
}

lhsValue = lhsCopy;

using (ExpressionCaptureScope rhsCapture = new ExpressionCaptureScope(visitorContext, null))
{
Visit(node.Right);

rhsValue = rhsCapture.ExecuteGet();
}

System.Type lhsType = lhsValue.symbolCsType;
System.Type rhsType = rhsValue.symbolCsType;
Expand Down

0 comments on commit e059f61

Please sign in to comment.