icsharpcode / CodeConverter

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

VB -> C#: Type conversions of integer values vanish in inferred generic types #1159

Open gaschd opened 2 weeks ago

gaschd commented 2 weeks ago

The array types are correct, but the necessary type casts are missing. This issue seems to only affect integer values, which need to be explicitly cast to the appropriate types.

VB.Net input code

Imports System
Imports System.Linq

Public Class TestClass
    Public Sub GenerateFromConstants
        Dim floatArr = Enumerable.Repeat(1.0F, 5).ToArray()        
        Dim doubleArr = Enumerable.Repeat(2.0, 5).ToArray()        
        Dim decimalArr = Enumerable.Repeat(3.0D, 5).ToArray()    
    End Sub

    Public Sub GenerateFromCasts
        Dim floatArr = Enumerable.Repeat(CSng(1), 5).ToArray()        
        Dim doubleArr = Enumerable.Repeat(CDbl(2), 5).ToArray()        
        Dim decimalArr = Enumerable.Repeat(CDec(3), 5).ToArray()  
    End Sub
End Class

Erroneous output

using System;
using System.Linq;
using Microsoft.VisualBasic.CompilerServices; // Install-Package Microsoft.VisualBasic

public partial class TestClass
{
    public void GenerateFromConstants()
    {
        float[] floatArr = Enumerable.Repeat(1.0f, 5).ToArray();
        double[] doubleArr = Enumerable.Repeat(2.0d, 5).ToArray();
        decimal[] decimalArr = Enumerable.Repeat(3.0m, 5).ToArray();
    }

    public void GenerateFromCasts()
    {
        float[] floatArr = Enumerable.Repeat(1, 5).ToArray();
        double[] doubleArr = Enumerable.Repeat(2, 5).ToArray();
        decimal[] decimalArr = Enumerable.Repeat(3, 5).ToArray();          
    }
}
3 target compilation errors:
CS0029: Cannot implicitly convert type 'int[]' to 'float[]'
CS0029: Cannot implicitly convert type 'int[]' to 'double[]'
CS0029: Cannot implicitly convert type 'int[]' to 'decimal[]'

Expected output

using System;
using System.Linq;
using Microsoft.VisualBasic.CompilerServices; // Install-Package Microsoft.VisualBasic

public partial class TestClass
{
    public void GenerateFromConstants()
    {
        float[] floatArr = Enumerable.Repeat(1.0f, 5).ToArray();
        double[] doubleArr = Enumerable.Repeat(2.0d, 5).ToArray();
        decimal[] decimalArr = Enumerable.Repeat(3.0m, 5).ToArray();
    }

    public void GenerateFromCasts()
    {
        float[] floatArr = Enumerable.Repeat((float)1, 5).ToArray();
        double[] doubleArr = Enumerable.Repeat((double)2, 5).ToArray();
        decimal[] decimalArr = Enumerable.Repeat((decimal)3, 5).ToArray();          
    }
}

Details