Skip to content

Commit 34b7e87

Browse files
craiggwilsonrstam
authored andcommitted
CSHARP-542: normalized a vb expression related to nullable comparisons.
1 parent f81e9d9 commit 34b7e87

File tree

3 files changed

+81
-4
lines changed

3 files changed

+81
-4
lines changed

Driver/Linq/Expressions/ExpressionNormalizer.cs

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,27 @@ protected override Expression VisitBinary(BinaryExpression node)
6161
{
6262
node = EnsureConstantIsOnRight(node);
6363

64+
if (node.Type == typeof(Nullable<bool>))
65+
{
66+
switch (node.NodeType)
67+
{
68+
case ExpressionType.Equal:
69+
case ExpressionType.GreaterThan:
70+
case ExpressionType.GreaterThanOrEqual:
71+
case ExpressionType.LessThan:
72+
case ExpressionType.LessThanOrEqual:
73+
case ExpressionType.NotEqual:
74+
node = Expression.MakeBinary(
75+
node.NodeType,
76+
node.Left,
77+
node.Right,
78+
false,
79+
null,
80+
null);
81+
break;
82+
}
83+
}
84+
6485
Expression result = null;
6586
if (node.Left.NodeType == ExpressionType.Call && node.Right.NodeType == ExpressionType.Constant)
6687
{
@@ -118,16 +139,19 @@ protected override Expression VisitBinary(BinaryExpression node)
118139
/// <returns>The UnaryExpression (possibly modified).</returns>
119140
protected override Expression VisitUnary(UnaryExpression node)
120141
{
121-
if (node.NodeType == ExpressionType.Convert || node.NodeType == ExpressionType.ConvertChecked)
142+
var newNode = base.VisitUnary(node);
143+
144+
if (newNode.NodeType == ExpressionType.Convert || newNode.NodeType == ExpressionType.ConvertChecked)
122145
{
123-
if (node.Type.IsAssignableFrom(node.Operand.Type))
146+
var newUnaryNode = (UnaryExpression)newNode;
147+
if (newUnaryNode.Type.IsAssignableFrom(newUnaryNode.Operand.Type))
124148
{
125149
// ignore the unnecessary conversion added by VB
126-
return Visit(node.Operand);
150+
newNode = newUnaryNode.Operand;
127151
}
128152
}
129153

130-
return base.VisitUnary(node);
154+
return newNode;
131155
}
132156

133157
private BinaryExpression EnsureConstantIsOnRight(BinaryExpression node)

DriverUnitTestsVB/DriverUnitTestsVB.vbproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
<Import Include="System.Xml.Linq" />
6868
</ItemGroup>
6969
<ItemGroup>
70+
<Compile Include="Jira\CSharp542.vb" />
7071
<Compile Include="Linq\MongoQueryableTests.vb" />
7172
<Compile Include="Linq\MongoQueryProviderTests.vb" />
7273
<Compile Include="Linq\SelectDictionaryTests.vb" />
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
' Copyright 2010-2012 10gen Inc.
2+
'*
3+
'* Licensed under the Apache License, Version 2.0 (the "License");
4+
'* you may not use this file except in compliance with the License.
5+
'* You may obtain a copy of the License at
6+
'*
7+
'* http://www.apache.org/licenses/LICENSE-2.0
8+
'*
9+
'* Unless required by applicable law or agreed to in writing, software
10+
'* distributed under the License is distributed on an "AS IS" BASIS,
11+
'* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
'* See the License for the specific language governing permissions and
13+
'* limitations under the License.
14+
'
15+
16+
17+
Imports System.Collections.Generic
18+
Imports System.Linq
19+
Imports System.Text
20+
Imports NUnit.Framework
21+
22+
Imports MongoDB.Bson
23+
Imports MongoDB.Bson.Serialization.Attributes
24+
Imports MongoDB.Driver
25+
Imports MongoDB.Driver.Linq
26+
27+
Namespace MongoDB.DriverUnitTests.Jira
28+
29+
<TestFixture()>
30+
Public Class CSharp542
31+
32+
Public Class Test
33+
Public Id As ObjectId
34+
35+
Public MyNullableInt As Nullable(Of Integer)
36+
End Class
37+
38+
<Test()>
39+
Public Sub TestNullableComparison()
40+
41+
Dim server = MongoServer.Create()
42+
Dim db = server.GetDatabase("test")
43+
Dim col = db.GetCollection(Of Test)("foos")
44+
45+
Dim query = col.AsQueryable.Where(Function(p) p.MyNullableInt = 3)
46+
47+
Dim list = query.ToList()
48+
49+
End Sub
50+
End Class
51+
52+
End Namespace

0 commit comments

Comments
 (0)