Skip to content

Commit b8c386d

Browse files
Merge pull request #1121 from TymurGubayev/fix/ExplicitelyOmittedArguments/1
Convert explicitely omitted ByRef arguments into temporary variables
2 parents 0658a1b + d85f6b3 commit b8c386d

2 files changed

Lines changed: 37 additions & 37 deletions

File tree

CodeConverter/CSharp/ExpressionNodeVisitor.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1731,17 +1731,22 @@ async Task<ArgumentSyntax> ConvertArg(VBSyntax.ArgumentSyntax arg, int argIndex)
17311731
{
17321732
var argName = arg is VBSyntax.SimpleArgumentSyntax { IsNamed: true } namedArg ? namedArg.NameColonEquals.Name.Identifier.Text : null;
17331733
var parameterSymbol = invocationSymbol?.GetParameters().GetArgument(argName, argIndex);
1734+
var convertedArg = await ConvertArgForParameter(arg, parameterSymbol);
17341735

1735-
if (parameterSymbol != null) {
1736+
if (convertedArg is not null && parameterSymbol is not null) {
17361737
processedParameters.Add(parameterSymbol.Name);
17371738
}
17381739

1740+
return convertedArg;
1741+
}
1742+
1743+
async Task<ArgumentSyntax> ConvertArgForParameter(VBSyntax.ArgumentSyntax arg, IParameterSymbol parameterSymbol)
1744+
{
17391745
if (arg.IsOmitted) {
17401746
if (invocationSymbol != null && !invocationHasOverloads) {
17411747
forceNamedParameters = true;
17421748
return null; //Prefer to skip omitted and use named parameters when the symbol has only one overload
17431749
}
1744-
17451750
return ConvertOmittedArgument(parameterSymbol);
17461751
}
17471752

Tests/CSharp/MemberTests/MemberTests.cs

Lines changed: 30 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -4051,50 +4051,45 @@ public static int StaticTestProperty
40514051
}
40524052

40534053
[Fact]
4054-
public async Task TestMissingByRefArgumentWithNoExplicitDefaultValueAsync()
4054+
public async Task TestOmittedArgumentsAsync()
40554055
{
40564056
await TestConversionVisualBasicToCSharpAsync(
4057-
@"Imports System.Runtime.InteropServices
4057+
@"Class OmittedArguments
4058+
Sub M(Optional a As String = ""a"", ByRef Optional b As String = ""b"")
4059+
Dim s As String = """"
40584060
4059-
Class MissingByRefArgumentWithNoExplicitDefaultValue
4060-
Sub S()
4061-
ByRefNoDefault()
4062-
OptionalByRefNoDefault()
4063-
OptionalByRefWithDefault()
4064-
End Sub
4061+
M() 'omitted implicitly
4062+
M(,) 'omitted explicitly
4063+
4064+
M(s) 'omitted implicitly
4065+
M(s,) 'omitted explicitly
40654066
4066-
Private Sub ByRefNoDefault(ByRef str1 As String) : End Sub
4067-
Private Sub OptionalByRefNoDefault(<[Optional]> ByRef str2 As String) : End Sub
4068-
Private Sub OptionalByRefWithDefault(<[Optional], DefaultParameterValue(""a"")> ByRef str3 As String) : End Sub
4067+
M(a:=s) 'omitted implicitly
4068+
M(a:=s, ) 'omitted explicitly
4069+
End Sub
40694070
End Class", @"using System.Runtime.InteropServices;
40704071
4071-
internal partial class MissingByRefArgumentWithNoExplicitDefaultValue
4072+
internal partial class OmittedArguments
40724073
{
4073-
public void S()
4074+
public void M([Optional, DefaultParameterValue(""a"")] string a, [Optional, DefaultParameterValue(""b"")] ref string b)
40744075
{
4075-
ByRefNoDefault();
4076-
string argstr2 = default;
4077-
OptionalByRefNoDefault(str2: ref argstr2);
4078-
string argstr3 = ""a"";
4079-
OptionalByRefWithDefault(str3: ref argstr3);
4080-
}
4076+
string s = """";
40814077
4082-
private void ByRefNoDefault(ref string str1)
4083-
{
4084-
}
4085-
private void OptionalByRefNoDefault([Optional] ref string str2)
4086-
{
4087-
}
4088-
private void OptionalByRefWithDefault([Optional][DefaultParameterValue(""a"")] ref string str3)
4089-
{
4078+
string argb = ""b"";
4079+
M(b: ref argb); // omitted implicitly
4080+
string argb1 = ""b"";
4081+
M(b: ref argb1); // omitted explicitly
4082+
4083+
string argb2 = ""b"";
4084+
M(s, b: ref argb2); // omitted implicitly
4085+
string argb3 = ""b"";
4086+
M(s, b: ref argb3); // omitted explicitly
4087+
4088+
string argb4 = ""b"";
4089+
M(a: s, b: ref argb4); // omitted implicitly
4090+
string argb5 = ""b"";
4091+
M(a: s, b: ref argb5); // omitted explicitly
40904092
}
4091-
}
4092-
3 source compilation errors:
4093-
BC30455: Argument not specified for parameter 'str1' of 'Private Sub ByRefNoDefault(ByRef str1 As String)'.
4094-
BC30455: Argument not specified for parameter 'str2' of 'Private Sub OptionalByRefNoDefault(ByRef str2 As String)'.
4095-
BC30455: Argument not specified for parameter 'str3' of 'Private Sub OptionalByRefWithDefault(ByRef str3 As String)'.
4096-
1 target compilation errors:
4097-
CS7036: There is no argument given that corresponds to the required formal parameter 'str1' of 'MissingByRefArgumentWithNoExplicitDefaultValue.ByRefNoDefault(ref string)'
4098-
");
4093+
}");
40994094
}
41004095
}

0 commit comments

Comments
 (0)