Skip to content

Commit

Permalink
Extend arithmetic operators on all lower precision types to return int
Browse files Browse the repository at this point in the history
- All C# arithmetic operators +, *, /, etc return an integer when used on lower precision types such as byte and short. Fix that this was only being handled for left shift an right shift operators.
  • Loading branch information
MerlinVR committed Feb 25, 2020
1 parent 1f3a004 commit c443c68
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 11 deletions.
4 changes: 2 additions & 2 deletions Assets/UdonSharp/Editor/UdonSharpASTVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -762,9 +762,9 @@ public override void VisitAssignmentExpression(AssignmentExpressionSyntax node)
SymbolDefinition resultSymbol = operatorMethodCapture.Invoke(new SymbolDefinition[] { lhsCapture.ExecuteGet(), rhsValue });

BuiltinOperatorType operatorType = SyntaxKindToBuiltinOperator(node.Kind());
bool explicitSet = operatorType == BuiltinOperatorType.LeftShift || operatorType == BuiltinOperatorType.RightShift;

lhsCapture.ExecuteSet(resultSymbol, explicitSet);
// In place arithmetic operators for lower precision types will return int, but C# will normaally cast the result back to the target type
lhsCapture.ExecuteSet(resultSymbol, true);
}
}
}
Expand Down
16 changes: 7 additions & 9 deletions Assets/UdonSharp/Editor/UdonSharpOperatorMethodInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,16 +91,14 @@ public override Type ReturnType
operatorType == BuiltinOperatorType.Inequality)
return typeof(bool);

if (operatorType == BuiltinOperatorType.LeftShift || operatorType == BuiltinOperatorType.RightShift)

if (operatorSourceType == typeof(byte) ||
operatorSourceType == typeof(sbyte) ||
operatorSourceType == typeof(char) ||
operatorSourceType == typeof(short) ||
operatorSourceType == typeof(ushort))
{
if (operatorSourceType == typeof(byte) ||
operatorSourceType == typeof(sbyte) ||
operatorSourceType == typeof(char) ||
operatorSourceType == typeof(short) ||
operatorSourceType == typeof(ushort))
{
return typeof(int);
}
return typeof(int);
}

return operatorSourceType;
Expand Down

0 comments on commit c443c68

Please sign in to comment.