Skip to content

Commit c443c68

Browse files
committed
Extend arithmetic operators on all lower precision types to return int
- 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.
1 parent 1f3a004 commit c443c68

File tree

2 files changed

+9
-11
lines changed

2 files changed

+9
-11
lines changed

Assets/UdonSharp/Editor/UdonSharpASTVisitor.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -762,9 +762,9 @@ public override void VisitAssignmentExpression(AssignmentExpressionSyntax node)
762762
SymbolDefinition resultSymbol = operatorMethodCapture.Invoke(new SymbolDefinition[] { lhsCapture.ExecuteGet(), rhsValue });
763763

764764
BuiltinOperatorType operatorType = SyntaxKindToBuiltinOperator(node.Kind());
765-
bool explicitSet = operatorType == BuiltinOperatorType.LeftShift || operatorType == BuiltinOperatorType.RightShift;
766765

767-
lhsCapture.ExecuteSet(resultSymbol, explicitSet);
766+
// In place arithmetic operators for lower precision types will return int, but C# will normaally cast the result back to the target type
767+
lhsCapture.ExecuteSet(resultSymbol, true);
768768
}
769769
}
770770
}

Assets/UdonSharp/Editor/UdonSharpOperatorMethodInfo.cs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -91,16 +91,14 @@ public override Type ReturnType
9191
operatorType == BuiltinOperatorType.Inequality)
9292
return typeof(bool);
9393

94-
if (operatorType == BuiltinOperatorType.LeftShift || operatorType == BuiltinOperatorType.RightShift)
94+
95+
if (operatorSourceType == typeof(byte) ||
96+
operatorSourceType == typeof(sbyte) ||
97+
operatorSourceType == typeof(char) ||
98+
operatorSourceType == typeof(short) ||
99+
operatorSourceType == typeof(ushort))
95100
{
96-
if (operatorSourceType == typeof(byte) ||
97-
operatorSourceType == typeof(sbyte) ||
98-
operatorSourceType == typeof(char) ||
99-
operatorSourceType == typeof(short) ||
100-
operatorSourceType == typeof(ushort))
101-
{
102-
return typeof(int);
103-
}
101+
return typeof(int);
104102
}
105103

106104
return operatorSourceType;

0 commit comments

Comments
 (0)