Skip to content

Commit

Permalink
Handle null == null, null != null (#30)
Browse files Browse the repository at this point in the history
* Handle `null == null`, `null != null`

Handle `null == null`, `null != null`.

Co-authored-by: Merlin <36685500+MerlinVR@users.noreply.github.com>
  • Loading branch information
ureishi and MerlinVR committed Dec 1, 2022
1 parent baaa203 commit a89fc5e
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
Expand Up @@ -674,6 +674,26 @@ private BoundExpression HandleNullCoalescingExpression(BinaryExpressionSyntax no

return new BoundCoalesceExpression(node, lhs, rhs);
}

private BoundExpression HandleNullEqualsExpression(BinaryExpressionSyntax node)
{
TypeSymbol booleanType = Context.GetTypeSymbol(SpecialType.System_Boolean);
IConstantValue booleanValue;

switch (node.Kind())
{
case SyntaxKind.EqualsExpression:
booleanValue = new ConstantValue<bool>(true);
break;
case SyntaxKind.NotEqualsExpression:
booleanValue = new ConstantValue<bool>(false);
break;
default:
throw new InvalidOperationException("Invalid equals expression");
}

return new BoundConstantExpression(booleanValue, booleanType, node);
}

public override BoundNode VisitBinaryExpression(BinaryExpressionSyntax node)
{
Expand All @@ -683,9 +703,14 @@ public override BoundNode VisitBinaryExpression(BinaryExpressionSyntax node)

if (node.Kind() == SyntaxKind.CoalesceExpression)
return HandleNullCoalescingExpression(node);

MethodSymbol binaryMethodSymbol = (MethodSymbol)GetSymbol(node);


if (binaryMethodSymbol == null &&
(node.Kind() == SyntaxKind.EqualsExpression ||
node.Kind() == SyntaxKind.NotEqualsExpression))
return HandleNullEqualsExpression(node);

BoundExpression lhs = VisitExpression(node.Left, binaryMethodSymbol.Parameters[0].Type);
BoundExpression rhs = VisitExpression(node.Right, binaryMethodSymbol.Parameters[1].Type);

Expand Down
Expand Up @@ -32,6 +32,7 @@ public void ExecuteTests()
DecimalOps();
BitwiseNot();
UdonBehaviourFieldCompoundAssignment();
NullEquals();
CastCharToFloat();
}

Expand Down Expand Up @@ -459,7 +460,13 @@ void UdonBehaviourFieldCompoundAssignment()

tester.TestAssertion("Field struct compound assignment", _testVec.x == 4);
}


void NullEquals()
{
tester.TestAssertion("Null equals null", null == null);
tester.TestAssertion("Null doesn't equal null", !(null != null));
}

void CastCharToFloat()
{
float af = 97.5f;
Expand Down

0 comments on commit a89fc5e

Please sign in to comment.