Skip to content

Fix converting Omitted/Optional parameters which could result in wrong method overload being called. - #907

Merged
GrahamTheCoder merged 8 commits into
icsharpcode:masterfrom
Yozer:fix-optional-methods
Jun 27, 2022
Merged

GrahamTheCoder merged 8 commits into
icsharpcode:masterfrom
Yozer:fix-optional-methods

Conversation

@Yozer

@Yozer Yozer commented Jun 27, 2022 •

Copy link
Copy Markdown
Member

Fixes #906

More complicated than I initially thought. The idea of just providing every omitted argument didn't work for all cases. Example:

Input sharplab.io:

Public Sub M(a As String)
    Console.WriteLine($"{a}")
End Sub
Public Sub M(a As String, b as string)
    Console.WriteLine($"{a} {b}")
End Sub
Public Sub M(Optional a As String = "1", Optional b as string = "2", optional c as string = "3")
    Console.WriteLine($"{a} {b} {c}")
End Sub

Public Sub Run()
   M("11", "22",) ' "11 22 3
   M(a:="11", ) ' 11 2 3
End Sub

Develop sharplab.io

M("11", "22"); // incorrect: 11 22
M(a: "11"); // incorrect: 11

Initial idea sharplab.io

M("11", "22", "3"); // correct
M(a: "11", "2"); // incorrect: 11 2

Final (?) solution sharplab.io

M("11", "22", c: "3"); // correct
M(a: "11", b: "2", c: "3"); // correct

The solution works more or less like this:

  1. Process named/positional args as usual. If the positional arg is omitted then pull ExplicitDefaultValue.
    a) Note that after named arg we can still have omitted args!
    b) If the omitted arg is required in C# (ref/out) then do the hoisting.
  2. Now GetAdditionalRequiredArgs will check what parameters we're still missing to match the invoked symbol.
    This includes cases where we need to provide Microsoft.VisualBasic.CompareMethod enum or do the hoisting for (ref/out) arg.

The downside of this approach is that we sometimes pull more arguments than it's necessary but I guess it's better than calling wrong method overload.

@Yozer
Yozer requested a review from GrahamTheCoder June 27, 2022 14:01
@Yozer

Yozer commented Jun 27, 2022 •

Copy link
Copy Markdown
Member Author

Idea: maybe the logic "pull every optional argument" could be only executed when the invocation symbol has overloads. Shouldn't be too hard to implement. I will take a look.

//edit
Done in 869bf88

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

VB -> C#: Converting Omitted/Optional parameters can result in wrong method overload being called.

2 participants