Skip to content

Commit

Permalink
[mcs] Lift any non-nullable operand under runtime binder for lifted b…
Browse files Browse the repository at this point in the history
…inary operation
  • Loading branch information
marek-safar committed Feb 13, 2014
1 parent da2fe71 commit 1451cd9
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
19 changes: 17 additions & 2 deletions mcs/mcs/nullable.cs
Expand Up @@ -659,10 +659,10 @@ protected override Expression DoResolve (ResolveContext rc)
{
if (rc.IsRuntimeBinder) {
if (UnwrapLeft == null && !Left.Type.IsNullableType)
Left = Wrap.Create (Left, rc.Module.PredefinedTypes.Nullable.TypeSpec.MakeGenericType (rc.Module, new[] { Left.Type }));
Left = LiftOperand (rc, Left);

if (UnwrapRight == null && !Right.Type.IsNullableType)
Right = Wrap.Create (Right, rc.Module.PredefinedTypes.Nullable.TypeSpec.MakeGenericType (rc.Module, new[] { Right.Type }));
Right = LiftOperand (rc, Right);
} else {
if (UnwrapLeft == null && Left != null && Left.Type.IsNullableType) {
Left = Unwrap.CreateUnwrapped (Left);
Expand All @@ -681,6 +681,21 @@ protected override Expression DoResolve (ResolveContext rc)
return this;
}

Expression LiftOperand (ResolveContext rc, Expression expr)
{
TypeSpec type;
if (expr.IsNull) {
type = Left.IsNull ? Right.Type : Left.Type;
} else {
type = expr.Type;
}

if (!type.IsNullableType)
type = rc.Module.PredefinedTypes.Nullable.TypeSpec.MakeGenericType (rc.Module, new[] { type });

return Wrap.Create (expr, type);
}

public override void Emit (EmitContext ec)
{
if (IsBitwiseBoolean && UserOperator == null) {
Expand Down
10 changes: 10 additions & 0 deletions mcs/tests/dtest-006.cs
Expand Up @@ -523,6 +523,11 @@ void AndTestNullable ()
dynamic d3 = new MyType? (new MyType (-7));
Assert (d3 & new MyType (6), new MyType (0), "#3a");
Assert<MyType?> (d3 + null, null, "#3b");

dynamic a = (bool?) true;
dynamic b = (bool?) null;
Assert (a & b, (bool?)null, "#4a");
Assert (b & a, (bool?)null, "#4b");
}

void AndAssignedTest ()
Expand Down Expand Up @@ -1608,6 +1613,11 @@ void NotEqualNullableTest ()
Assert (d != true, false, "#5");
Assert (d != null, true, "#5a");
Assert (d != false, true, "#5b");

d = null;
long? l = null;
Assert (d != l, false, "#6a");
Assert (l != d, false, "#6b");
}

void NotEqualEnumTest ()
Expand Down

0 comments on commit 1451cd9

Please sign in to comment.