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#: Error converting shared/static variable #260

Closed
mrmonday opened this Issue Apr 5, 2019 · 2 comments

Comments

Projects
None yet
2 participants
@mrmonday
Copy link
Contributor

mrmonday commented Apr 5, 2019

Please prefix the issue title with either "C# -> VB: " or "VB -> C#: " if relevant

Input code

Public Class Class1
    Sub Foo()
        MyClass.Val = 6
    End Sub

    Shared Val As Integer
End Class

Erroneous output

public class Class1
{
    public void Foo()
    {
        ;/* Cannot convert AssignmentStatementSyntax, CONVERSION ERROR: Conversion for MyClassExpression not implemented, please report this issue in 'MyClass' at character 46
   at ICSharpCode.CodeConverter.CSharp.VisualBasicConverter.NodesVisitor.DefaultVisit(SyntaxNode node)
   at Microsoft.CodeAnalysis.VisualBasic.VisualBasicSyntaxVisitor`1.VisitMyClassExpression(MyClassExpressionSyntax node)
   at Microsoft.CodeAnalysis.VisualBasic.Syntax.MyClassExpressionSyntax.Accept[TResult](VisualBasicSyntaxVisitor`1 visitor)
   at Microsoft.CodeAnalysis.VisualBasic.VisualBasicSyntaxVisitor`1.Visit(SyntaxNode node)
   at ICSharpCode.CodeConverter.CSharp.CommentConvertingNodesVisitor.DefaultVisit(SyntaxNode node)
   at Microsoft.CodeAnalysis.VisualBasic.VisualBasicSyntaxVisitor`1.VisitMyClassExpression(MyClassExpressionSyntax node)
   at Microsoft.CodeAnalysis.VisualBasic.Syntax.MyClassExpressionSyntax.Accept[TResult](VisualBasicSyntaxVisitor`1 visitor)
   at ICSharpCode.CodeConverter.CSharp.VisualBasicConverter.NodesVisitor.VisitMemberAccessExpression(MemberAccessExpressionSyntax node)
   at Microsoft.CodeAnalysis.VisualBasic.Syntax.MemberAccessExpressionSyntax.Accept[TResult](VisualBasicSyntaxVisitor`1 visitor)
   at Microsoft.CodeAnalysis.VisualBasic.VisualBasicSyntaxVisitor`1.Visit(SyntaxNode node)
   at ICSharpCode.CodeConverter.CSharp.CommentConvertingNodesVisitor.DefaultVisit(SyntaxNode node)
   at Microsoft.CodeAnalysis.VisualBasic.VisualBasicSyntaxVisitor`1.VisitMemberAccessExpression(MemberAccessExpressionSyntax node)
   at Microsoft.CodeAnalysis.VisualBasic.Syntax.MemberAccessExpressionSyntax.Accept[TResult](VisualBasicSyntaxVisitor`1 visitor)
   at ICSharpCode.CodeConverter.CSharp.VisualBasicConverter.MethodBodyVisitor.VisitAssignmentStatement(AssignmentStatementSyntax node)
   at Microsoft.CodeAnalysis.VisualBasic.Syntax.AssignmentStatementSyntax.Accept[TResult](VisualBasicSyntaxVisitor`1 visitor)
   at Microsoft.CodeAnalysis.VisualBasic.VisualBasicSyntaxVisitor`1.Visit(SyntaxNode node)
   at ICSharpCode.CodeConverter.CSharp.CommentConvertingMethodBodyVisitor.ConvertWithTrivia(SyntaxNode node)
   at ICSharpCode.CodeConverter.CSharp.CommentConvertingMethodBodyVisitor.DefaultVisit(SyntaxNode node)

Input: 
        MyClass.Val = 6

 */
    }

    private static int Val;
}

Expected output

public class Class1
{
    public void Foo()
    {
        Class1.Val = 6;
    }

    private static int Val;
}

Details

Product in use: e.g. VS extension

Version in use: e.g. 6.6.0

@mrmonday

This comment has been minimized.

Copy link
Contributor Author

mrmonday commented Apr 7, 2019

I started working on this, but it's more complicated than it initially seems:

    <Fact()>
    Public Sub MyClassInheritance()
        Dim x As A = New B
        Assert.True(x.TestMethod())
    End Sub
 
    Public Class A
        Overridable Function F1() As Integer
            Return 1
        End Function
 
        Function F3() As Integer
            Return 3
        End Function
 
        Shared Property P1 As Integer
 
        Public Function TestMethod() As Boolean
            Return MyClass.F1() = 1 AndAlso MyClass.F3() = 3 AndAlso MyClass.P1 = 1
        End Function
    End Class
 
 
    Public Class B
        Inherits A
        Public Overrides Function F1() As Integer
            Return 11
        End Function
    End Class

The MyClass.F3() is the simplest case, this can become this.F3() == 3.

MyClass.P1 is also simple, providing we have type info, it becomes A.P1 == 1. If we don't have type info I don't think we can convert it.

The MyClass.F1() case doesn't translate directly to C#, unless you introduce another non-virtual method to call.

@GrahamTheCoder

This comment has been minimized.

Copy link
Member

GrahamTheCoder commented Apr 8, 2019

I had never seen this syntax before. I think generating a non-virtual method seems like a sensible solution, though I'll happily accept a PR that handles the simpler cases and throws in the more complicated ones.
I'm assuming from that code block that you've discovered ExpressionTests.vb and the self verifying tests it creates, but if not, take a look.

mrmonday added a commit to mrmonday/CodeConverter that referenced this issue Apr 8, 2019

GrahamTheCoder added a commit to mrmonday/CodeConverter that referenced this issue Apr 8, 2019

mrmonday added a commit to mrmonday/CodeConverter that referenced this issue Apr 16, 2019

mrmonday added a commit to mrmonday/CodeConverter that referenced this issue Apr 18, 2019

mrmonday added a commit to mrmonday/CodeConverter that referenced this issue Apr 18, 2019

GrahamTheCoder added a commit to mrmonday/CodeConverter that referenced this issue Apr 19, 2019

GrahamTheCoder added a commit that referenced this issue Apr 19, 2019

Merge pull request #273 from mrmonday/260-vb-to-cs-myclass
Add initial support for VB MyClass expressions [#260]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.