Skip to content

Commit aae9226

Browse files
Subtle behaviour change: Use bool version rather than late bind
1 parent 6a01789 commit aae9226

File tree

10 files changed

+68
-19
lines changed

10 files changed

+68
-19
lines changed

CodeConverter/CSharp/BuiltInVisualBasicOperatorSubsitutions.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ private async Task<ExpressionSyntax> ConvertToObjectBinaryOperatorAsync(VBSyntax
108108
private async Task<ExpressionSyntax> ConvertToObjectComparisonOperatorAsync(VBSyntax.BinaryExpressionSyntax node, KnownMethod member)
109109
{
110110
var (lhs, rhs) = await AcceptSidesAsync(node);
111+
member = (member.Import, member.TypeName, "Conditional" + member.MethodName); //The VB compiler would late bind, but this should provide identical results in most cases I think
111112
var compareTextKind = _visualBasicEqualityComparison.OptionCompareTextCaseInsensitive ? SyntaxKind.TrueLiteralExpression : SyntaxKind.FalseLiteralExpression;
112113
return member.Invoke(_visualBasicEqualityComparison.ExtraUsingDirectives, lhs, rhs, SyntaxFactory.LiteralExpression(compareTextKind));
113114
}

CodeConverter/CSharp/ExpressionNodeVisitor.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -696,7 +696,9 @@ private CSharpSyntaxNode ConvertAddressOf(VBSyntax.UnaryExpressionSyntax node, E
696696

697697
public override async Task<CSharpSyntaxNode> VisitBinaryExpression(VBasic.Syntax.BinaryExpressionSyntax node)
698698
{
699-
if (await _operatorConverter.ConvertRewrittenBinaryOperatorOrNullAsync(node) is CSharpSyntaxNode nothingComparison) return nothingComparison;
699+
if (await _operatorConverter.ConvertRewrittenBinaryOperatorOrNullAsync(node) is ExpressionSyntax operatorNode) {
700+
return operatorNode;
701+
}
700702

701703
var lhsTypeInfo = _semanticModel.GetTypeInfo(node.Left);
702704
var rhsTypeInfo = _semanticModel.GetTypeInfo(node.Right);

CodeConverter/Shared/DefaultReferences.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace ICSharpCode.CodeConverter.Shared
1212
/// <summary>
1313
/// This file requires net standard 2.0 or above. Therefore it should be linked into projects referencing the converter to get a wider range of references.
1414
/// </summary>
15-
public class DefaultReferences
15+
public static class DefaultReferences
1616
{
1717
private static readonly Assembly[] DefaultAssemblies = new []{
1818
typeof(object),

Tests/CSharp/ExpressionTests/ByRefTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -509,7 +509,7 @@ public void Main()
509509
string argstr1 = Conversions.ToString(tmp1[1]);
510510
DoSomething(ref argstr1);
511511
tmp1[1] = argstr1;
512-
Debug.Assert(Conversions.ToBoolean(Operators.CompareObjectEqual(Other.lst2[1], 5.ToString(), false)));
512+
Debug.Assert(Conversions.ToBoolean(Operators.ConditionalCompareObjectEqual(Other.lst2[1], 5.ToString(), false)));
513513
}
514514
}
515515

Tests/CSharp/ExpressionTests/ExpressionTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1023,7 +1023,7 @@ private void TestMethod()
10231023
object test(object a) => Operators.MultiplyObject(a, 2);
10241024
object test2(object a, object b)
10251025
{
1026-
if (Conversions.ToBoolean(Operators.CompareObjectGreater(b, 0, false)))
1026+
if (Conversions.ToBoolean(Operators.ConditionalCompareObjectGreater(b, 0, false)))
10271027
return Operators.DivideObject(a, b);
10281028
return 0;
10291029
};

Tests/CSharp/ExpressionTests/StringExpressionTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ public partial class Issue396ComparisonOperatorForStringsAsync
358358
{
359359
public Issue396ComparisonOperatorForStringsAsync()
360360
{
361-
b = Operators.CompareObjectGreater(str, """", false);
361+
b = Operators.ConditionalCompareObjectGreater(str, """", false);
362362
}
363363
364364
private object str = 1.ToString();

Tests/CSharp/TypeCastTests.cs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,52 @@ public static T ToGenericParameter<T>(object Value)
401401
return (T)Value;
402402
}
403403
}
404+
}");
405+
}
406+
407+
/// <summary>
408+
/// We just use ConditionalCompareObjectEqual to make it a bool, but VB emits a late binding call something like this:
409+
/// array[0] = Operators.CompareObjectEqual(left, right, false);
410+
/// array[1] = "Identical values stored in objects should be equal";
411+
/// NewLateBinding.LateCall(this, null, "AssertTrue", array, null, null, null, true);
412+
/// This will likely be the same in the vast majority of cases
413+
/// </summary>
414+
[Fact]
415+
public async Task ObjectComparisonIsConvertedToBoolRatherThanLateBoundAsync()
416+
{
417+
await TestConversionVisualBasicToCSharpAsync(
418+
@"Imports System
419+
420+
Public Class CopiedFromTheSelfVerifyingBooleanTests
421+
Public Sub VisualBasicEqualityOfNormalObjectsNotSubjectToSpecialStringConversionRules()
422+
Dim a1 As Object = 3
423+
Dim a2 As Object = 3
424+
AssertTrue(a1 = a2, ""Identical values stored in objects should be equal"")
425+
End Sub
426+
427+
Private Sub AssertTrue(v1 As Nullable(Of Boolean), v2 As String)
428+
End Sub
429+
430+
Private Sub AssertTrue(v1 As Boolean, v2 As String)
431+
End Sub
432+
End Class", @"using Microsoft.VisualBasic.CompilerServices; // Install-Package Microsoft.VisualBasic
433+
434+
public partial class CopiedFromTheSelfVerifyingBooleanTests
435+
{
436+
public void VisualBasicEqualityOfNormalObjectsNotSubjectToSpecialStringConversionRules()
437+
{
438+
object a1 = 3;
439+
object a2 = 3;
440+
AssertTrue(Operators.ConditionalCompareObjectEqual(a1, a2, false), ""Identical values stored in objects should be equal"");
441+
}
442+
443+
private void AssertTrue(bool? v1, string v2)
444+
{
445+
}
446+
447+
private void AssertTrue(bool v1, string v2)
448+
{
449+
}
404450
}");
405451
}
406452
}

Tests/TestData/SelfVerifyingTests/VBToCS/BooleanTests.vb

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,24 +43,33 @@ Public Class BooleanTests
4343
End Sub
4444

4545
<Fact>
46-
Public Sub NullableBoolsTrue()
46+
Public Sub NullableBoolsTrue()
4747
Dim x As Object = 4
4848
Dim res = 1
4949

50-
If (Not x?.Equals(4))' x != 4
50+
If (Not x?.Equals(4)) Then ' x != 4
5151
res *= 2
5252
Else
5353
res *= 3 'Branch taken
5454
End If
5555

5656

57-
If (x?.Equals(4))' x == 4
57+
If (x?.Equals(4)) Then ' x == 4
5858
res *= 5 'Branch taken
5959
Else
6060
res *= 7
6161
End If
6262

6363
Assert.Equal(15, res)
6464
End Sub
65+
66+
<Fact>
67+
Public Sub VisualBasicEqualityOfNormalObjectsNotSubjectToSpecialStringConversionRules()
68+
Dim a1 As Object = 3
69+
Dim a2 As Object = 3
70+
Dim b As Object = 4
71+
Assert.True(a1 = a2, "Identical values stored in objects should be equal")
72+
Assert.False(a1 = b, "Different values stored in objects should not be equal")
73+
End Sub
6574
End Class
6675

Tests/TestData/SelfVerifyingTests/VBToCS/StringEqualityTests.vb

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,7 @@ Public Class StringEqualityTests
1414

1515
<Fact>
1616
Public Sub VisualBasicEqualityOfCharArrays()
17-
Assert.True(New Char(){} = New Char(){}, "Char arrays should be compared as strings because that's what happens in VB")
18-
End Sub
19-
20-
<Fact>
21-
Public Sub VisualBasicEqualityOfNormalObjectsNotSubjectToSpecialStringConversionRules()
22-
Dim a1 As Object = 3
23-
Dim a2 As Object = 3
24-
Dim b As Object = 4
25-
Assert.True(a1 = a2, "Identical values stored in objects should be equal")
26-
Assert.False(a1 = b, "Different values stored in objects should not be equal")
17+
Assert.True(New Char() {} = New Char() {}, "Char arrays should be compared as strings because that's what happens in VB")
2718
End Sub
2819

2920
Private nullObject As Object = Nothing

Web/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
namespace ICSharpCode.CodeConverter.Web
55
{
6-
public class Program
6+
public static class Program
77
{
88
public static void Main(string[] args)
99
{

0 commit comments

Comments
 (0)