Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

VB -> C#: Interop method invocations with OptionalAttribute aren't converted #1048

Open
TymurGubayev opened this issue Nov 29, 2023 · 2 comments
Labels
exception caught An exception is caught (and stacktrace provided) VB -> C# Specific to VB -> C# conversion

Comments

@TymurGubayev
Copy link
Contributor

VB.Net input code

    Sub S()
        Dim what = Microsoft.Office.Interop.Word.WdGoToItem.wdGoToLine

        Dim objRange As Microsoft.Office.Interop.Word.Range
        objRange = objRange.GoTo(what, , -1)
        objRange = objRange.GoTo(what, Nothing, -1)
    End Sub

where the signature of the GoTo method is

    [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
    [DispId(173)]
    [return: MarshalAs(UnmanagedType.Interface)]
    Range GoTo([Optional][In][MarshalAs(UnmanagedType.Struct)] ref object What, [Optional][In][MarshalAs(UnmanagedType.Struct)] ref object Which, [Optional][In][MarshalAs(UnmanagedType.Struct)] ref object Count, [Optional][In][MarshalAs(UnmanagedType.Struct)] ref object Name);

Erroneous output

        public void S()
        {
            var what = Microsoft.Office.Interop.Word.WdGoToItem.wdGoToLine;

            var objRange = default(Microsoft.Office.Interop.Word.Range);
            object argWhat = what;
            object argCount = -1;
            objRange = default
#error Cannot convert InvocationExpressionSyntax - see comment for details
   /* Cannot convert ArgumentListSyntax, System.InvalidOperationException: Operation is not valid due to the current state of the object.
      at Microsoft.CodeAnalysis.VisualBasic.Symbols.ParameterSymbol.get_ExplicitDefaultValue()
      at ICSharpCode.CodeConverter.CSharp.ExpressionNodeVisitor.CreateOptionalRefArg(IParameterSymbol p, RefKind refKind)
      at ICSharpCode.CodeConverter.CSharp.ExpressionNodeVisitor.<GetAdditionalRequiredArgs>d__132.MoveNext()
      at System.Linq.Enumerable.<ConcatIterator>d__59`1.MoveNext()
      at Microsoft.CodeAnalysis.CSharp.SyntaxFactory.SeparatedList[TNode](IEnumerable`1 nodes)
      at ICSharpCode.CodeConverter.CSharp.ExpressionNodeVisitor.<VisitArgumentList>d__55.MoveNext()
   --- End of stack trace from previous location where exception was thrown ---
      at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
      at ICSharpCode.CodeConverter.CSharp.CommentConvertingVisitorWrapper.<ConvertHandledAsync>d__12`1.MoveNext()

   Input: (what, , -1)

   Context:
   objRange.[GoTo](what, , -1)

    */
   ;
            what = (Microsoft.Office.Interop.Word.WdGoToItem)Conversions.ToInteger(argWhat);
            object argWhat1 = what;
            object argWhich = null;
            object argCount1 = -1;
            objRange = default
#error Cannot convert InvocationExpressionSyntax - see comment for details
   /* Cannot convert ArgumentListSyntax, System.InvalidOperationException: Operation is not valid due to the current state of the object.
      at Microsoft.CodeAnalysis.VisualBasic.Symbols.ParameterSymbol.get_ExplicitDefaultValue()
      at ICSharpCode.CodeConverter.CSharp.ExpressionNodeVisitor.CreateOptionalRefArg(IParameterSymbol p, RefKind refKind)
      at ICSharpCode.CodeConverter.CSharp.ExpressionNodeVisitor.<GetAdditionalRequiredArgs>d__132.MoveNext()
      at System.Linq.Enumerable.<ConcatIterator>d__59`1.MoveNext()
      at Microsoft.CodeAnalysis.CSharp.SyntaxFactory.SeparatedList[TNode](IEnumerable`1 nodes)
      at ICSharpCode.CodeConverter.CSharp.ExpressionNodeVisitor.<VisitArgumentList>d__55.MoveNext()
   --- End of stack trace from previous location where exception was thrown ---
      at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
      at ICSharpCode.CodeConverter.CSharp.CommentConvertingVisitorWrapper.<ConvertHandledAsync>d__12`1.MoveNext()

   Input: (what, Nothing, -1)

   Context:
   objRange.[GoTo](what, Nothing, -1)

    */
   ;
            what = (Microsoft.Office.Interop.Word.WdGoToItem)Conversions.ToInteger(argWhat1);
        }
    }

Expected output

public void S()
{
    var what = Microsoft.Office.Interop.Word.WdGoToItem.wdGoToLine;

    var objRange = default(Microsoft.Office.Interop.Word.Range);
    object argWhat = what;
    object argWhich = null;
    object argCount = -1;
    objRange = default
    objRange.[GoTo](what, argWhich, -1);
    what = (Microsoft.Office.Interop.Word.WdGoToItem)Conversions.ToInteger(argWhat);
    object argWhat1 = what;
    object argWhich1 = null;
    object argCount1 = -1;
    objRange = default
    objRange.[GoTo](what, argWhich1, -1);
    what = (Microsoft.Office.Interop.Word.WdGoToItem)Conversions.ToInteger(argWhat1);
}

Details

  • Product in use: VS extension
  • Version in use: ae25cdc
  • Did you see it working in a previous version, which? No
  • Any other relevant information to the issue, or your interest in contributing a fix.
@TymurGubayev TymurGubayev added the VB -> C# Specific to VB -> C# conversion label Nov 29, 2023
@GrahamTheCoder
Copy link
Member

Thanks for raising all these issues. I'm hoping I'll get a chance to solve a few in the next week or so, but am very happy to receive even draft PRs for any of them too

@GrahamTheCoder GrahamTheCoder added the exception caught An exception is caught (and stacktrace provided) label Dec 18, 2023
@GrahamTheCoder
Copy link
Member

GrahamTheCoder commented Dec 21, 2023

It's not quite compilable code due to the missing arg, but here's a simple way to make the library throw in the same way

Public Partial Class Foo
    Public Shared Sub Main()
        Console.WriteLine(New Foo().ExplicitFunc())
    End Sub

    Private Function ExplicitFunc(ByRef str As String) As Integer
        Return 1
    End Function
End Class

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
exception caught An exception is caught (and stacktrace provided) VB -> C# Specific to VB -> C# conversion
Projects
None yet
Development

No branches or pull requests

2 participants