icsharpcode / CodeConverter

Convert code from C# to VB.NET and vice versa using Roslyn
https://icsharpcode.github.io/CodeConverter/
MIT License
836 stars 218 forks source link

C# -> VB: unchecked can cause compile error #981

Open Walkman100 opened 1 year ago

Walkman100 commented 1 year ago

Input code

using System.Drawing;
using System.Windows.Forms;

class ConverterTests {
    public Color[] test1() {
        var textColor = Color.FromArgb(unchecked((int)0xFFCFCFCF));
        var backColor = Color.FromArgb(unchecked((int)0xFF303030));
        var altTextColor = Color.FromArgb(unchecked((int)0xFFFFFFFF));
        var altBackColor = Color.FromArgb(unchecked((int)0xFF515259));

        return new Color[] { textColor, backColor, altTextColor, altBackColor };
    }
}

Erroneous output

Imports System.Drawing
Imports System.Windows.Forms

Friend Class ConverterTests
    Public Function test1() As Color()
                ''' Cannot convert LocalDeclarationStatementSyntax, System.InvalidCastException: Unable to cast object of type 'Microsoft.CodeAnalysis.VisualBasic.Syntax.EmptyStatementSyntax' to type 'Microsoft.CodeAnalysis.VisualBasic.Syntax.ExpressionSyntax'.
'''    at ICSharpCode.CodeConverter.VB.CommonConversions.RemodelVariableDeclaration(VariableDeclarationSyntax declaration)
'''    at ICSharpCode.CodeConverter.VB.MethodBodyExecutableStatementVisitor.VisitLocalDeclarationStatement(LocalDeclarationStatementSyntax node)
'''    at Microsoft.CodeAnalysis.CSharp.Syntax.LocalDeclarationStatementSyntax.Accept[TResult](CSharpSyntaxVisitor`1 visitor)
'''    at Microsoft.CodeAnalysis.CSharp.CSharpSyntaxVisitor`1.Visit(SyntaxNode node)
'''    at ICSharpCode.CodeConverter.VB.CommentConvertingMethodBodyVisitor.DefaultVisit(SyntaxNode node)
''' 
''' Input:
'''         var textColor = System.Drawing.Color.FromArgb(unchecked((int)0xFFCFCFCF));
''' 
''' 
                ''' Cannot convert LocalDeclarationStatementSyntax, System.InvalidCastException: Unable to cast object of type 'Microsoft.CodeAnalysis.VisualBasic.Syntax.EmptyStatementSyntax' to type 'Microsoft.CodeAnalysis.VisualBasic.Syntax.ExpressionSyntax'.
'''    at ICSharpCode.CodeConverter.VB.CommonConversions.RemodelVariableDeclaration(VariableDeclarationSyntax declaration)
'''    at ICSharpCode.CodeConverter.VB.MethodBodyExecutableStatementVisitor.VisitLocalDeclarationStatement(LocalDeclarationStatementSyntax node)
'''    at Microsoft.CodeAnalysis.CSharp.Syntax.LocalDeclarationStatementSyntax.Accept[TResult](CSharpSyntaxVisitor`1 visitor)
'''    at Microsoft.CodeAnalysis.CSharp.CSharpSyntaxVisitor`1.Visit(SyntaxNode node)
'''    at ICSharpCode.CodeConverter.VB.CommentConvertingMethodBodyVisitor.DefaultVisit(SyntaxNode node)
''' 
''' Input:
'''         var backColor = System.Drawing.Color.FromArgb(unchecked((int)0xFF303030));
''' 
''' 
                ''' Cannot convert LocalDeclarationStatementSyntax, System.InvalidCastException: Unable to cast object of type 'Microsoft.CodeAnalysis.VisualBasic.Syntax.EmptyStatementSyntax' to type 'Microsoft.CodeAnalysis.VisualBasic.Syntax.ExpressionSyntax'.
'''    at ICSharpCode.CodeConverter.VB.CommonConversions.RemodelVariableDeclaration(VariableDeclarationSyntax declaration)
'''    at ICSharpCode.CodeConverter.VB.MethodBodyExecutableStatementVisitor.VisitLocalDeclarationStatement(LocalDeclarationStatementSyntax node)
'''    at Microsoft.CodeAnalysis.CSharp.Syntax.LocalDeclarationStatementSyntax.Accept[TResult](CSharpSyntaxVisitor`1 visitor)
'''    at Microsoft.CodeAnalysis.CSharp.CSharpSyntaxVisitor`1.Visit(SyntaxNode node)
'''    at ICSharpCode.CodeConverter.VB.CommentConvertingMethodBodyVisitor.DefaultVisit(SyntaxNode node)
''' 
''' Input:
'''         var altTextColor = System.Drawing.Color.FromArgb(unchecked((int)0xFFFFFFFF));
''' 
''' 
                ''' Cannot convert LocalDeclarationStatementSyntax, System.InvalidCastException: Unable to cast object of type 'Microsoft.CodeAnalysis.VisualBasic.Syntax.EmptyStatementSyntax' to type 'Microsoft.CodeAnalysis.VisualBasic.Syntax.ExpressionSyntax'.
'''    at ICSharpCode.CodeConverter.VB.CommonConversions.RemodelVariableDeclaration(VariableDeclarationSyntax declaration)
'''    at ICSharpCode.CodeConverter.VB.MethodBodyExecutableStatementVisitor.VisitLocalDeclarationStatement(LocalDeclarationStatementSyntax node)
'''    at Microsoft.CodeAnalysis.CSharp.Syntax.LocalDeclarationStatementSyntax.Accept[TResult](CSharpSyntaxVisitor`1 visitor)
'''    at Microsoft.CodeAnalysis.CSharp.CSharpSyntaxVisitor`1.Visit(SyntaxNode node)
'''    at ICSharpCode.CodeConverter.VB.CommentConvertingMethodBodyVisitor.DefaultVisit(SyntaxNode node)
''' 
''' Input:
'''         var altBackColor = System.Drawing.Color.FromArgb(unchecked((int)0xFF515259));
''' 
''' 

        Return New Color() {textColor, backColor, altTextColor, altBackColor}
    End Function
End Class

Expected output

Imports System.Drawing
Imports System.Windows.Forms

Friend Class ConverterTests
    Public Function test1() As Color()
        Dim textColor As Color = Color.FromArgb(&HFFCFCFCF)
        Dim backColor As Color = Color.FromArgb(&HFF303030)
        Dim altTextColor As Color = Color.FromArgb(&HFFFFFFFF)
        Dim altBackColor As Color = Color.FromArgb(&HFF515259)

        Return New Color() {textColor, backColor, altTextColor, altBackColor}
    End Function
End Class

Details

GrahamTheCoder commented 1 year ago

Hi, thanks for the report. This should be fixed by 9.1.2 shipping imminently.

Though when I say fixed, I specifically mean that it shouldn't error like that any more. It may still create a compile error but should be closer. Recognising with overflowing constants in VB needs some extra logic still

GrahamTheCoder commented 1 year ago

The output is now:

Imports System.Drawing
Imports System.Windows.Forms

Friend Class ConverterTests
    Public Function test1() As Color()
        Dim textColor = Color.FromArgb(CInt(&HFFCFCFCFUI))
        Dim backColor = Color.FromArgb(CInt(&HFF303030UI))
        Dim altTextColor = Color.FromArgb(CInt(&HFFFFFFFFUI))
        Dim altBackColor = Color.FromArgb(CInt(&HFF515259UI))

        Return New Color() {textColor, backColor, altTextColor, altBackColor}
    End Function
End Class

If you remove the "UI" from the end of the constants (which means Unsigned Integer) then the output compiles. This is the line of code and PR that causes this to be added if anyone wants to look into it: https://github.com/icsharpcode/CodeConverter/pull/699/files#diff-4e0c26a555453c9fe21dbf2cccad6591d246e008b243026c679c0f3224070076R653-R654