forked from icsharpcode/CodeConverter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathByRefTests.cs
More file actions
998 lines (850 loc) · 25.5 KB
/
ByRefTests.cs
File metadata and controls
998 lines (850 loc) · 25.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
using System.Threading.Tasks;
using ICSharpCode.CodeConverter.Common;
using ICSharpCode.CodeConverter.CSharp;
using ICSharpCode.CodeConverter.Tests.TestRunners;
using Xunit;
namespace ICSharpCode.CodeConverter.Tests.CSharp.ExpressionTests;
public class ByRefTests : ConverterTestBase
{
[Fact]
public async Task OptionalRefDateConstsWithOmittedArgListAsync()
{
await TestConversionVisualBasicToCSharpAsync(@"Public Class Issue213
Const x As Date = #1990-1-1#
Private Sub Y(Optional ByRef opt As Date = x)
End Sub
Private Sub CallsY()
Y
End Sub
End Class", @"using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
public partial class Issue213
{
private static DateTime x = DateTime.Parse(""1990-01-01"");
private void Y([Optional, DateTimeConstant(627667488000000000L/* Global.Issue213.x */)] ref DateTime opt)
{
}
private void CallsY()
{
DateTime argopt = DateTime.Parse(""1990-01-01"");
Y(opt: ref argopt);
}
}");
}
[Fact]
public async Task NullInlineRefArgumentAsync()
{
await TestConversionVisualBasicToCSharpAsync(@"Public Class VisualBasicClass
Public Sub UseStuff()
Stuff(Nothing)
End Sub
Public Sub Stuff(ByRef strs As String())
End Sub
End Class", @"
public partial class VisualBasicClass
{
public void UseStuff()
{
string[] argstrs = null;
Stuff(ref argstrs);
}
public void Stuff(ref string[] strs)
{
}
}");
}
[Fact]
public async Task RefArgumentRValueAsync()
{
await TestConversionVisualBasicToCSharpAsync(@"Public Class Class1
Private Property C1 As Class1
Private _c2 As Class1
Private _o1 As Object
Sub Foo()
Bar(New Class1)
Bar(C1)
Bar(Me.C1)
Bar(_c2)
Bar(Me._c2)
Bar(_o1)
Bar(Me._o1)
End Sub
Sub Bar(ByRef class1)
End Sub
End Class", @"
public partial class Class1
{
private Class1 C1 { get; set; }
private Class1 _c2;
private object _o1;
public void Foo()
{
object argclass1 = new Class1();
Bar(ref argclass1);
object argclass11 = C1;
Bar(ref argclass11);
C1 = (Class1)argclass11;
object argclass12 = C1;
Bar(ref argclass12);
C1 = (Class1)argclass12;
object argclass13 = _c2;
Bar(ref argclass13);
_c2 = (Class1)argclass13;
object argclass14 = _c2;
Bar(ref argclass14);
_c2 = (Class1)argclass14;
Bar(ref _o1);
Bar(ref _o1);
}
public void Bar(ref object class1)
{
}
}");
}
[Fact]
public async Task NestedRefArgumentRValueIssue876Async()
{
await TestConversionVisualBasicToCSharpAsync(@"Public Class Issue876
Property SomeProperty As Integer
Property SomeProperty2 As Integer
Property SomeProperty3 As Integer
Public Function InlineAssignHelper(Of T)(ByRef lhs As T, ByVal rhs As T) As T
lhs = rhs
Return lhs
End Function
Public Sub Main()
Dim result = InlineAssignHelper(SomeProperty, InlineAssignHelper(SomeProperty2, InlineAssignHelper(SomeProperty3, 1)))
End Sub
End Class", @"
public partial class Issue876
{
public int SomeProperty { get; set; }
public int SomeProperty2 { get; set; }
public int SomeProperty3 { get; set; }
public T InlineAssignHelper<T>(ref T lhs, T rhs)
{
lhs = rhs;
return lhs;
}
public void Main()
{
int localInlineAssignHelper() { int arglhs = SomeProperty3; var ret = InlineAssignHelper(ref arglhs, 1); SomeProperty3 = arglhs; return ret; }
int localInlineAssignHelper1() { int arglhs1 = SomeProperty2; var ret = InlineAssignHelper(ref arglhs1, localInlineAssignHelper()); SomeProperty2 = arglhs1; return ret; }
int arglhs = SomeProperty;
int result = InlineAssignHelper(ref arglhs, localInlineAssignHelper1());
SomeProperty = arglhs;
}
}");
}
[Fact]
public async Task RefArgumentRValue2Async()
{
await TestConversionVisualBasicToCSharpAsync(@"Public Class Class1
Sub Foo()
Dim x = True
Bar(x = True)
End Sub
Function Foo2()
Return Bar(True = False)
End Function
Sub Foo3()
If Bar(True = False) Then Bar(True = False)
End Sub
Sub Foo4()
If Bar(True = False) Then
Bar(True = False)
ElseIf Bar(True = False) Then
Bar(True = False)
Else
Bar(True = False)
End If
End Sub
Sub Foo5()
Bar(Nothing)
End Sub
Function Bar(ByRef b As Boolean) As Boolean
Return True
End Function
Function Bar2(ByRef c1 As Class1) As Integer
If c1 IsNot Nothing AndAlso Len(Bar3(Me)) <> 0 Then
Return 1
End If
Return 0
End Function
Function Bar3(ByRef c1 As Class1) As String
Return """"
End Function
End Class", @"using Microsoft.VisualBasic; // Install-Package Microsoft.VisualBasic
public partial class Class1
{
public void Foo()
{
bool x = true;
bool argb = x == true;
Bar(ref argb);
}
public object Foo2()
{
bool argb = true == false;
return Bar(ref argb);
}
public void Foo3()
{
bool argb1 = true == false;
if (Bar(ref argb1))
{
bool argb = true == false;
Bar(ref argb);
}
}
public void Foo4()
{
bool argb3 = true == false;
bool argb4 = true == false;
if (Bar(ref argb3))
{
bool argb = true == false;
Bar(ref argb);
}
else if (Bar(ref argb4))
{
bool argb2 = true == false;
Bar(ref argb2);
}
else
{
bool argb1 = true == false;
Bar(ref argb1);
}
}
public void Foo5()
{
bool argb = default;
Bar(ref argb);
}
public bool Bar(ref bool b)
{
return true;
}
public int Bar2(ref Class1 c1)
{
var argc1 = this;
if (c1 is not null && Strings.Len(Bar3(ref argc1)) != 0)
{
return 1;
}
return 0;
}
public string Bar3(ref Class1 c1)
{
return """";
}
}");
}
[Fact]
public async Task RefArgumentUsingAsync()
{
await TestConversionVisualBasicToCSharpAsync(@"Imports System.Data.SqlClient
Public Class Class1
Sub Foo()
Using x = New SqlConnection
Bar(x)
End Using
End Sub
Sub Bar(ByRef x As SqlConnection)
End Sub
End Class", @"using System.Data.SqlClient;
public partial class Class1
{
public void Foo()
{
using (var x = new SqlConnection())
{
var argx = x;
Bar(ref argx);
}
}
public void Bar(ref SqlConnection x)
{
}
}");
}
[Fact]
public async Task RefOptionalArgumentAsync()
{
await TestConversionVisualBasicToCSharpAsync(@"Public Class OptionalRefIssue91
Public Shared Function TestSub(Optional ByRef IsDefault As Boolean = False) As Boolean
End Function
Public Shared Function CallingFunc() As Boolean
Return TestSub() AndAlso TestSub(True)
End Function
End Class", @"using System.Runtime.InteropServices;
public partial class OptionalRefIssue91
{
public static bool TestSub([Optional, DefaultParameterValue(false)] ref bool IsDefault)
{
return default;
}
public static bool CallingFunc()
{
bool argIsDefault = false;
bool argIsDefault1 = true;
return TestSub(IsDefault: ref argIsDefault) && TestSub(ref argIsDefault1);
}
}");
}
[Fact]
public async Task RefAfterOptionalArgumentAsync()
{
await TestConversionVisualBasicToCSharpAsync(@"
Sub S(Optional a As Integer = 0, Optional ByRef b As Integer = 0)
S()
End Sub
", @"
public void S([Optional, DefaultParameterValue(0)] int a, [Optional, DefaultParameterValue(0)] ref int b)
{
int argb = 0;
S(b: ref argb);
}");
}
[Fact]
public async Task DateRefAfterOptionalArgumentAsync()
{
await TestConversionVisualBasicToCSharpAsync(@"
Sub S(Optional ByRef dt As Date = Nothing)
End Sub
", @"
public void S([Optional] ref DateTime dt)
{
}");
}
[Fact]
public async Task ParenthesizedArgShouldNotBeAssignedBackAsync()
{
await TestConversionVisualBasicToCSharpAsync(@"
Public Class C
Public Sub S()
Dim i As Integer = 0
Modify(i)
System.Diagnostics.Debug.Assert(i = 1)
Modify((i))
System.Diagnostics.Debug.Assert(i = 1)
End Sub
Sub Modify(ByRef i As Integer)
i = i + 1
End Sub
End Class
", @"using System.Diagnostics;
public partial class C
{
public void S()
{
int i = 0;
Modify(ref i);
Debug.Assert(i == 1);
int argi = i;
Modify(ref argi);
Debug.Assert(i == 1);
}
public void Modify(ref int i)
{
i = i + 1;
}
}");
}
[Fact]
public async Task OutOptionalArgumentAsync()
{
await TestConversionVisualBasicToCSharpAsync(@"
Public Class OptionalOutIssue882
Private Sub TestSub(<Out> ByRef a As Integer, <Out> Optional ByRef b As Integer = Nothing)
a = 42
b = 23
End Sub
Public Sub CallingFunc()
Dim a As Integer
Dim b As Integer
TestSub(a:=a, b:=b)
TestSub(a:=a)
End Sub
End Class", @"using System.Runtime.InteropServices;
public partial class OptionalOutIssue882
{
private void TestSub(out int a, [Optional] out int b)
{
a = 42;
b = 23;
}
public void CallingFunc()
{
int a;
int b;
TestSub(a: out a, b: out b);
int argb = 0;
TestSub(a: out a, b: out argb);
}
}");
}
[Fact]
public async Task ExplicitInterfaceImplementationOptionalRefParametersAsync()
{
await TestConversionVisualBasicToCSharpAsync(
@"Public Interface IFoo
Function ExplicitFunc(Optional ByRef str2 As String = """") As Integer
End Interface
Public Class Foo
Implements IFoo
Private Function ExplicitFunc(Optional ByRef str As String = """") As Integer Implements IFoo.ExplicitFunc
Return 5
End Function
End Class", @"using System.Runtime.InteropServices;
public partial interface IFoo
{
int ExplicitFunc([Optional, DefaultParameterValue("""")] ref string str2);
}
public partial class Foo : IFoo
{
private int ExplicitFunc([Optional, DefaultParameterValue("""")] ref string str)
{
return 5;
}
int IFoo.ExplicitFunc([Optional, DefaultParameterValue("""")] ref string str) => ExplicitFunc(ref str);
}");
}
[Fact]
public async Task RefArgumentPropertyInitializerAsync()
{
await TestConversionVisualBasicToCSharpAsync(@"Public Class Class1
Private _p1 As Class1 = Foo(New Class1)
Public Shared Function Foo(ByRef c1 As Class1) As Class1
Return c1
End Function
End Class", @"
public partial class Class1
{
static Class1 Foo__p1()
{
var argc1 = new Class1();
return Foo(ref argc1);
}
private Class1 _p1 = Foo__p1();
public static Class1 Foo(ref Class1 c1)
{
return c1;
}
}");
}
[Fact]
public async Task ReadOnlyPropertyRef_Issue843Async()
{
await TestConversionVisualBasicToCSharpAsync(@"Module Module1
Public Class TestClass
Public ReadOnly Property Foo As String
Public Sub New()
Foo = ""abc""
End Sub
End Class
Sub Main()
Test02()
End Sub
Private Sub Test02()
Dim t As New TestClass
Test02Sub(t.Foo)
End Sub
Private Sub Test02Sub(ByRef value As String)
Console.WriteLine(value)
End Sub
End Module", @"using System;
internal static partial class Module1
{
public partial class TestClass
{
public string Foo { get; private set; }
public TestClass()
{
Foo = ""abc"";
}
}
public static void Main()
{
Test02();
}
private static void Test02()
{
var t = new TestClass();
string argvalue = t.Foo;
Test02Sub(ref argvalue);
}
private static void Test02Sub(ref string value)
{
Console.WriteLine(value);
}
}");
}
[Fact]
public async Task AssignsBackToPropertyAsync()
{
await TestConversionVisualBasicToCSharpAsync(@"Imports System
Public Class MyTestClass
Private Property Prop As Integer
Private Property Prop2 As Integer
Private Function TakesRef(ByRef vrbTst As Integer) As Boolean
vrbTst = Prop + 1
Return vrbTst > 3
End Function
Private Sub TakesRefVoid(ByRef vrbTst As Integer)
vrbTst = vrbTst + 1
End Sub
Public Sub UsesRef(someBool As Boolean, someInt As Integer)
TakesRefVoid(someInt) ' Convert directly
TakesRefVoid(1) 'Requires variable before
TakesRefVoid(Prop2) ' Requires variable before, and to assign back after
Dim a = TakesRef(someInt) ' Convert directly
Dim b = TakesRef(2) 'Requires variable before
Dim c = TakesRef(Prop) ' Requires variable before, and to assign back after
If 16 > someInt OrElse TakesRef(someInt) ' Convert directly
Console.WriteLine(1)
Else If someBool AndAlso TakesRef(3 * a) 'Requires variable before (in local function)
someInt += 1
Else If TakesRef(Prop) ' Requires variable before, and to assign back after (in local function)
someInt -=2
End If
Console.WriteLine(someInt)
End Sub
End Class", @"using System;
using Microsoft.VisualBasic.CompilerServices; // Install-Package Microsoft.VisualBasic
public partial class MyTestClass
{
private int Prop { get; set; }
private int Prop2 { get; set; }
private bool TakesRef(ref int vrbTst)
{
vrbTst = Prop + 1;
return vrbTst > 3;
}
private void TakesRefVoid(ref int vrbTst)
{
vrbTst = vrbTst + 1;
}
public void UsesRef(bool someBool, int someInt)
{
TakesRefVoid(ref someInt); // Convert directly
int argvrbTst = 1;
TakesRefVoid(ref argvrbTst); // Requires variable before
int argvrbTst1 = Prop2;
TakesRefVoid(ref argvrbTst1);
Prop2 = argvrbTst1; // Requires variable before, and to assign back after
bool a = TakesRef(ref someInt); // Convert directly
int argvrbTst2 = 2;
bool b = TakesRef(ref argvrbTst2); // Requires variable before
int argvrbTst3 = Prop;
bool c = TakesRef(ref argvrbTst3);
Prop = argvrbTst3; // Requires variable before, and to assign back after
bool localTakesRef() { int argvrbTst = 3 * Conversions.ToInteger(a); var ret = TakesRef(ref argvrbTst); return ret; }
bool localTakesRef1() { int argvrbTst1 = Prop; var ret = TakesRef(ref argvrbTst1); Prop = argvrbTst1; return ret; }
if (16 > someInt || TakesRef(ref someInt)) // Convert directly
{
Console.WriteLine(1);
}
else if (someBool && localTakesRef()) // Requires variable before (in local function)
{
someInt += 1;
}
else if (localTakesRef1()) // Requires variable before, and to assign back after (in local function)
{
someInt -= 2;
}
Console.WriteLine(someInt);
}
}");
}
[Fact]
public async Task Issue567Async()
{
await TestConversionVisualBasicToCSharpAsync(@"Public Class Issue567
Dim arr() As String
Dim arr2(,) As String
Sub DoSomething(ByRef str As String)
str = ""test""
End Sub
Sub Main()
DoSomething(arr(1))
Debug.Assert(arr(1) = ""test"")
DoSomething(arr2(2, 2))
Debug.Assert(arr2(2, 2) = ""test"")
End Sub
End Class", @"using System.Diagnostics;
public partial class Issue567
{
private string[] arr;
private string[,] arr2;
public void DoSomething(ref string str)
{
str = ""test"";
}
public void Main()
{
DoSomething(ref arr[1]);
Debug.Assert(arr[1] == ""test"");
DoSomething(ref arr2[2, 2]);
Debug.Assert(arr2[2, 2] == ""test"");
}
}");
}
[Fact]
public async Task Issue567ExtendedAsync()
{
await TestConversionVisualBasicToCSharpAsync(@"Public Class Issue567
Sub DoSomething(ByRef str As String)
lst = New List(Of String)({4.ToString(), 5.ToString(), 6.ToString()})
lst2 = New List(Of Object)({4.ToString(), 5.ToString(), 6.ToString()})
str = 999.ToString()
End Sub
Sub Main()
DoSomething(lst(1))
Debug.Assert(lst(1) = 4.ToString())
DoSomething(lst2(1))
Debug.Assert(lst2(1) = 5.ToString())
End Sub
End Class
Friend Module Other
Public lst As List(Of String) = New List(Of String)({ 1.ToString(), 2.ToString(), 3.ToString()})
Public lst2 As List(Of Object) = New List(Of Object)({ 1.ToString(), 2.ToString(), 3.ToString()})
End Module", @"using System.Collections.Generic;
using System.Diagnostics;
using Microsoft.VisualBasic.CompilerServices; // Install-Package Microsoft.VisualBasic
public partial class Issue567
{
public void DoSomething(ref string str)
{
Other.lst = new List<string>(new[] { 4.ToString(), 5.ToString(), 6.ToString() });
Other.lst2 = new List<object>(new[] { 4.ToString(), 5.ToString(), 6.ToString() });
str = 999.ToString();
}
public void Main()
{
var tmp = Other.lst;
string argstr = tmp[1];
DoSomething(ref argstr);
tmp[1] = argstr;
Debug.Assert((Other.lst[1] ?? """") == (4.ToString() ?? """"));
var tmp1 = Other.lst2;
string argstr1 = Conversions.ToString(tmp1[1]);
DoSomething(ref argstr1);
tmp1[1] = argstr1;
Debug.Assert(Conversions.ToBoolean(Operators.ConditionalCompareObjectEqual(Other.lst2[1], 5.ToString(), false)));
}
}
internal static partial class Other
{
public static List<string> lst = new List<string>(new[] { 1.ToString(), 2.ToString(), 3.ToString() });
public static List<object> lst2 = new List<object>(new[] { 1.ToString(), 2.ToString(), 3.ToString() });
}");
}
[Fact]
public async Task Issue856Async()
{
await TestConversionVisualBasicToCSharpAsync(@"Public Class Issue856
Sub Main()
Dim decimalTarget As Decimal
Double.TryParse(""123"".AsSpan(), decimalTarget)
Dim longTarget As Long
Integer.TryParse(""123"".AsSpan(), longTarget)
Dim intTarget As Integer
Long.TryParse(""123"".AsSpan(), intTarget)
End Sub
End Class", @"using System;
public partial class Issue856
{
public void Main()
{
var decimalTarget = default(decimal);
double argresult = (double)decimalTarget;
double.TryParse(""123"".AsSpan(), out argresult);
decimalTarget = (decimal)argresult;
var longTarget = default(long);
int argresult1 = (int)longTarget;
int.TryParse(""123"".AsSpan(), out argresult1);
longTarget = argresult1;
var intTarget = default(int);
long argresult2 = intTarget;
long.TryParse(""123"".AsSpan(), out argresult2);
intTarget = (int)argresult2;
}
}");
}
[Fact]
public async Task OutParameterIsEnforcedByCSharpCompileErrorAsync()
{
await TestConversionVisualBasicToCSharpAsync(@"Imports System.Runtime.InteropServices ' Statement removed so comment removed too
Public Class OutParameterIsEnforcedByCSharpCompileError
Shared Sub LogAndReset(<Out> ByRef arg As Integer)
System.Console.WriteLine(arg)
End Sub
End Class", @"using System;
public partial class OutParameterIsEnforcedByCSharpCompileError
{
public static void LogAndReset(out int arg)
{
Console.WriteLine(arg);
}
}
2 target compilation errors:
CS0269: Use of unassigned out parameter 'arg'
CS0177: The out parameter 'arg' must be assigned to before control leaves the current method");
// These compile errors are the correct conversion - VB doesn't enforce out parameters not being used for input, or being assigned before output
}
[Fact]
public async Task BinaryExpressionOutParameterAsync()
{
await TestConversionVisualBasicToCSharpAsync(@"Imports System.Runtime.InteropServices ' BUG: Comment lost because overwritten
Public Class BinaryExpressionOutParameter
Shared Sub Main()
Dim wide As Object = 7
Zero(wide)
Dim narrow As Short = 3
Zero(narrow)
Zero(7 + 3)
End Sub
Shared Sub Zero(<Out> ByRef arg As Integer)
arg = 0
End Sub
End Class", @"using Microsoft.VisualBasic.CompilerServices; // Install-Package Microsoft.VisualBasic
public partial class BinaryExpressionOutParameter
{
public static void Main()
{
object wide = 7;
int argarg = Conversions.ToInteger(wide);
Zero(out argarg);
wide = argarg;
short narrow = 3;
int argarg1 = narrow;
Zero(out argarg1);
narrow = (short)argarg1;
int argarg2 = 7 + 3;
Zero(out argarg2);
}
public static void Zero(out int arg)
{
arg = 0;
}
}");
}
[Fact]
public async Task BinaryExpressionRefParameterAsync()
{
await TestConversionVisualBasicToCSharpAsync(@"Public Class BinaryExpressionRefParameter
Shared Sub Main()
Dim wide As Object = 7
LogAndReset(wide)
Dim wideArray() As Object = {3,4,4}
LogAndReset(wideArray(1))
Dim narrow As Short = 3
LogAndReset(narrow)
LogAndReset(7 + 3)
End Sub
Shared Sub LogAndReset(ByRef arg As Integer)
System.Console.WriteLine(arg)
arg = 0
End Sub
End Class", @"using System;
using Microsoft.VisualBasic.CompilerServices; // Install-Package Microsoft.VisualBasic
public partial class BinaryExpressionRefParameter
{
public static void Main()
{
object wide = 7;
int argarg = Conversions.ToInteger(wide);
LogAndReset(ref argarg);
wide = argarg;
object[] wideArray = new object[] { 3, 4, 4 };
var tmp = wideArray;
int argarg1 = Conversions.ToInteger(tmp[1]);
LogAndReset(ref argarg1);
tmp[1] = argarg1;
short narrow = 3;
int argarg2 = narrow;
LogAndReset(ref argarg2);
narrow = (short)argarg2;
int argarg3 = 7 + 3;
LogAndReset(ref argarg3);
}
public static void LogAndReset(ref int arg)
{
Console.WriteLine(arg);
arg = 0;
}
}");
}
[Fact]
public async Task Issue1225_OutAttributeOnParameterFromOtherFileDoesNotCrashAsync()
{
// Issue #1225: IsOutAttribute called SemanticModel.GetTypeInfo(attribute) on an attribute node
// that belongs to a different syntax tree (parameter declared in a separate source file).
// This caused ArgumentException "Knoten ist nicht innerhalb Syntaxbaum" /
// "Node is not within syntax tree". The fix falls back to name-based checking
// when the attribute's syntax tree differs from the current semantic model's tree.
var serviceFileContent = @"Imports System.Runtime.InteropServices
Public Class LicenseService
Public Shared ReadOnly Property Instance As LicenseService
Get
Return New LicenseService()
End Get
End Property
Public Function GetLicenseMaybe(<Out> ByRef licenseName As String) As Boolean
licenseName = Nothing
Return False
End Function
End Class";
var callerFileContent = @"Public Class Caller
Public Sub Test(licenseName As String)
Dim res = LicenseService.Instance.GetLicenseMaybe(licenseName)
End Sub
End Class";
var options = new TextConversionOptions(DefaultReferences.With()) { ShowCompilationErrors = true };
var languageConversion = new VBToCSConversion { ConversionOptions = options };
var serviceTree = languageConversion.CreateTree(serviceFileContent);
var callerTree = languageConversion.CreateTree(callerFileContent);
// Add both files to the same project so that the VB compilation sees both
var serviceDoc = await languageConversion.CreateProjectDocumentFromTreeAsync(serviceTree, options.References);
var callerDoc = serviceDoc.Project.AddDocumentFromTree(callerTree);
// Convert only the caller document; its parameter info comes from the service file's syntax tree
var result = await ProjectConversion.ConvertSingleAsync<VBToCSConversion>(callerDoc, options);
var output = (result.ConvertedCode ?? "") + (result.GetExceptionsAsString() ?? "");
Assert.DoesNotContain("#error", output);
Assert.Contains("GetLicenseMaybe", output);
}
[Fact]
public async Task OptionalStructRefParameterUsesDefaultNotNullIssue886Async()
{
// Issue #886: When a VB method has an optional ByRef struct parameter with Nothing as the default,
// the generated C# should use `default` rather than `null` (null is not valid for value types).
await TestConversionVisualBasicToCSharpAsync(@"
Structure S
End Structure
Class C
Sub Foo(Optional ByRef s As S = Nothing)
End Sub
Sub Bar()
Foo()
End Sub
End Class
", @"using System.Runtime.InteropServices;
internal partial struct S
{
}
internal partial class C
{
public void Foo([Optional] ref S s)
{
}
public void Bar()
{
S args = default;
Foo(s: ref args);
}
}");
}
}