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#: Add null if we have only space in function parameter #445

Closed
Saibamen opened this issue Nov 26, 2019 · 11 comments
Closed

VB -> C#: Add null if we have only space in function parameter #445

Saibamen opened this issue Nov 26, 2019 · 11 comments
Labels
VB -> C# Specific to VB -> C# conversion

Comments

@Saibamen
Copy link
Contributor

Input code

Call mySuperFunction(strSomething, , optionalSomething)

Erroneous output

{
    ;
#error Cannot convert CallStatementSyntax - see comment for details
    /* Cannot convert CallStatementSyntax, CONVERSION ERROR: Conversion for OmittedArgument not implemented, please report this issue in '' at character 81
       at ICSharpCode.CodeConverter.CSharp.ExpressionNodeVisitor.DefaultVisit(SyntaxNode node) in D:\GitWorkspace\CodeConverter\ICSharpCode.CodeConverter\CSharp\ExpressionNodeVisitor.cs:line 82
       at ICSharpCode.CodeConverter.CSharp.CommentConvertingVisitorWrapper`1.Visit(SyntaxNode node) in D:\GitWorkspace\CodeConverter\ICSharpCode.CodeConverter\CSharp\CommentConvertingVisitorWrapper.cs:line 23
       at ICSharpCode.CodeConverter.CSharp.SyntaxNodeVisitorExtensions.AcceptAsync[T](SyntaxNode node, CommentConvertingVisitorWrapper`1 visitorWrapper) in D:\GitWorkspace\CodeConverter\ICSharpCode.CodeConverter\CSharp\SyntaxNodeVisitorExtensions.cs:line 17
       at ICSharpCode.CodeConverter.CSharp.SyntaxNodeVisitorExtensions.<>c__DisplayClass4_0`2.<<AcceptAsync>b__0>d.MoveNext() in D:\GitWorkspace\CodeConverter\ICSharpCode.CodeConverter\CSharp\SyntaxNodeVisitorExtensions.cs:line 38
    --- End of stack trace from previous location where exception was thrown ---
       at ICSharpCode.CodeConverter.Shared.AsyncEnumerableTaskExtensions.SelectAsync[TArg,TResult](IEnumerable`1 source, Func`2 selector)
       at ICSharpCode.CodeConverter.CSharp.SyntaxNodeVisitorExtensions.AcceptAsync[TGeneral,TSpecific](IEnumerable`1 nodes, CommentConvertingVisitorWrapper`1 visitorWrapper) in D:\GitWorkspace\CodeConverter\ICSharpCode.CodeConverter\CSharp\SyntaxNodeVisitorExtensions.cs:line 39
       at ICSharpCode.CodeConverter.CSharp.ExpressionNodeVisitor.<>c__DisplayClass66_0.<<VisitInvocationExpression>g__CreateElementAccess|1>d.MoveNext() in D:\GitWorkspace\CodeConverter\ICSharpCode.CodeConverter\CSharp\ExpressionNodeVisitor.cs:line 725
    --- End of stack trace from previous location where exception was thrown ---
       at ICSharpCode.CodeConverter.CSharp.ExpressionNodeVisitor.VisitInvocationExpression(InvocationExpressionSyntax node) in D:\GitWorkspace\CodeConverter\ICSharpCode.CodeConverter\CSharp\ExpressionNodeVisitor.cs:line 726
       at ICSharpCode.CodeConverter.CSharp.CommentConvertingVisitorWrapper`1.Visit(SyntaxNode node) in D:\GitWorkspace\CodeConverter\ICSharpCode.CodeConverter\CSharp\CommentConvertingVisitorWrapper.cs:line 23
       at ICSharpCode.CodeConverter.CSharp.SyntaxNodeVisitorExtensions.AcceptAsync[T](SyntaxNode node, CommentConvertingVisitorWrapper`1 visitorWrapper) in D:\GitWorkspace\CodeConverter\ICSharpCode.CodeConverter\CSharp\SyntaxNodeVisitorExtensions.cs:line 17
       at ICSharpCode.CodeConverter.CSharp.MethodBodyExecutableStatementVisitor.VisitCallStatement(CallStatementSyntax node) in D:\GitWorkspace\CodeConverter\ICSharpCode.CodeConverter\CSharp\MethodBodyExecutableStatementVisitor.cs:line 744
       at ICSharpCode.CodeConverter.CSharp.ByRefParameterVisitor.CreateLocals(VisualBasicSyntaxNode node) in D:\GitWorkspace\CodeConverter\ICSharpCode.CodeConverter\CSharp\ByRefParameterVisitor.cs:line 53
       at ICSharpCode.CodeConverter.CSharp.ByRefParameterVisitor.AddLocalVariables(VisualBasicSyntaxNode node) in D:\GitWorkspace\CodeConverter\ICSharpCode.CodeConverter\CSharp\ByRefParameterVisitor.cs:line 49
       at ICSharpCode.CodeConverter.CSharp.CommentConvertingMethodBodyVisitor.ConvertWithTrivia(SyntaxNode node) in D:\GitWorkspace\CodeConverter\ICSharpCode.CodeConverter\CSharp\CommentConvertingMethodBodyVisitor.cs:line 44
       at ICSharpCode.CodeConverter.CSharp.CommentConvertingMethodBodyVisitor.DefaultVisit(SyntaxNode node) in D:\GitWorkspace\CodeConverter\ICSharpCode.CodeConverter\CSharp\CommentConvertingMethodBodyVisitor.cs:line 35

    Input:
    Call mySuperFunction(strSomething, , optionalSomething)

     */
}

Expected output

{
    mySuperFunction(strSomething, null, optionalSomething);
}

Details

  • Product in use: e.g. codeconverter.icsharpcode.net
  • Version in use: 7.3.0.0
  • 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.
@Saibamen Saibamen added the VB -> C# Specific to VB -> C# conversion label Nov 26, 2019
@GrahamTheCoder
Copy link
Member

Ah, haven't seen that before, thanks for the bug report!

@GrahamTheCoder GrahamTheCoder added the Small Estimated less than 4 hours work label Mar 8, 2020
@Saibamen
Copy link
Contributor Author

Saibamen commented Mar 14, 2020

@GrahamTheCoder This is not fixed.
Try to convert this:

Dim strArtikelNummer As String
Dim strMengenEinheitAs String
Dim strKommentar As String

Function mySuperFunction(ArtikelNummer As String, MengenEinheit As String, Optional ist_die_Nummer_bereitsVorhanden As Boolean, Optional Kommentar As String)
End Function

Call mySuperFunction(strArtikelNummer, strMengenEinheit, , strKommentar)

Error:

Cannot convert CallStatementSyntax - see comment for details
/* Cannot convert CallStatementSyntax, System.InvalidCastException:
Unable to cast object of type 'Microsoft.CodeAnalysis.CSharp.Syntax.EmptyStatementSyntax' to type 'Microsoft.CodeAnalysis.CSharp.Syntax.MemberDeclarationSyntax'.

@GrahamTheCoder
Copy link
Member

@Saibamen I can't figure out what context you are in where that code is valid, is it some kind of VB scripting command line? The code converter only makes token efforts at converting malformed VB. Could you find an example that will compile in SharpLab perhaps: Example

@Saibamen
Copy link
Contributor Author

Saibamen commented Mar 15, 2020

Please: correct example

@GrahamTheCoder
Copy link
Member

GrahamTheCoder commented Mar 15, 2020

Input

Public Class CompilingClass
    Dim strArtikelNummer As String
    Dim strMengenEinheit As String
    Dim strKommentar As String

    Function mySuperFunction(ArtikelNummer As String, MengenEinheit As String, Optional ist_die_Nummer_bereitsVorhanden As Boolean = False, Optional Kommentar As String = "")
        mySuperFunction = ""
    End Function
    
    Function someMainFunction()
        Call mySuperFunction(strArtikelNummer, strMengenEinheit, , strKommentar)
        someMainFunction = ""
    End Function

End Class

Actual output

public partial class CompilingClass
{
    private string strArtikelNummer;
    private string strMengenEinheit;
    private string strKommentar;

    public object mySuperFunction(string ArtikelNummer, string MengenEinheit, bool ist_die_Nummer_bereitsVorhanden = false, string Kommentar = "")
    {
        object mySuperFunctionRet = default;
        mySuperFunctionRet = "";
        return mySuperFunctionRet;
    }

    public object someMainFunction()
    {
        object someMainFunctionRet = default;
        mySuperFunction(strArtikelNummer, strMengenEinheit, Kommentar: strKommentar);
        someMainFunctionRet = "";
        return someMainFunctionRet;
    }
}

@Saibamen
Copy link
Contributor Author

Saibamen commented Mar 16, 2020

Ok, but why it must be in class? Sometimes I need a quick convert some code.

Why it can not be just simple like it is now with:
Input:
Call mySuperFunction(strArtikelNummer, strMengenEinheit)
Output:

{
    mySuperFunction[strArtikelNummer, strMengenEinheit];
}

While I trying this same with this input, it has error:
Input:
Call mySuperFunction(strArtikelNummer, strMengenEinheit, , strKommentar)
Buggy output:

{
    ;
#error Cannot convert CallStatementSyntax - see comment for details
    /* Cannot convert CallStatementSyntax, System.InvalidCastException: Unable to cast object of type 'Microsoft.CodeAnalysis.CSharp.Syntax.EmptyStatementSyntax' to type 'Microsoft.CodeAnalysis.CSharp.Syntax.ExpressionSyntax'.
       at ICSharpCode.CodeConverter.CSharp.MethodBodyExecutableStatementVisitor.VisitCallStatement(CallStatementSyntax node) in D:\GitWorkspace\CodeConverter\CodeConverter\CSharp\MethodBodyExecutableStatementVisitor.cs:line 763
       at ICSharpCode.CodeConverter.CSharp.ByRefParameterVisitor.CreateLocals(VisualBasicSyntaxNode node) in D:\GitWorkspace\CodeConverter\CodeConverter\CSharp\ByRefParameterVisitor.cs:line 52
       at ICSharpCode.CodeConverter.CSharp.ByRefParameterVisitor.AddLocalVariables(VisualBasicSyntaxNode node) in D:\GitWorkspace\CodeConverter\CodeConverter\CSharp\ByRefParameterVisitor.cs:line 42
       at ICSharpCode.CodeConverter.CSharp.CommentConvertingMethodBodyVisitor.DefaultVisitInnerAsync(SyntaxNode node) in D:\GitWorkspace\CodeConverter\CodeConverter\CSharp\CommentConvertingMethodBodyVisitor.cs:line 29

    Input:
    Call mySuperFunction(strArtikelNummer, strMengenEinheit, , strKommentar)

     */
}

@GrahamTheCoder
Copy link
Member

GrahamTheCoder commented Mar 16, 2020

I think I see what you're getting at. There are a few different areas involved here.

  1. Incomplete input: The inner part of the converter only deals with whole files. There's a thin layer around it (which I call snippet detection), which tries to identify when something else has been entered, and create a valid file containing it. The error in this case is caused by a failure in that snippet detection and wrapping code. It'd be possible to either target just this case, or use it as a test case for a more general effort at improving the code (e.g. Find methods in roslyn relating to scripting that may deal with this already). Let's use this github issue to track improving the case you mentioned last: a single call statement should convert to some kind of invocation.

  2. Invalid input: I don't believe declaring a function and calling it is ever a valid thing to do at the same scope in VB. Without further context as to why that case is special (e.g If it's valid in some scripting window), I'm unlikely to fix that specific case. If this was a common request, my first reaction would be to make the input box closer to a full editor with syntax highlighting, to help people understand what is likely to work. I think this would be a great feature request anyway, so I'll open it as a separate issue - Improve web snippet conversion experience #546

@GrahamTheCoder GrahamTheCoder removed the Small Estimated less than 4 hours work label Mar 16, 2020
@Saibamen
Copy link
Contributor Author

Ok, thank you

@GrahamTheCoder GrahamTheCoder modified the milestones: 8.1.0, 8.1.0 candidate Mar 19, 2020
@Saibamen
Copy link
Contributor Author

Retested: OK 👍

Better default (should be null) than exception ;)

@GrahamTheCoder
Copy link
Member

Your example posted above was a good example of why default is a better conversion of Nothing than null. If I tried to pass null in CSharp, it wouldn't compile

@Saibamen
Copy link
Contributor Author

Ah, ok ;)
The parameter in my code was optional string, not optional bool like in this issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
VB -> C# Specific to VB -> C# conversion
Projects
None yet
Development

No branches or pull requests

2 participants