icsharpcode / CodeConverter

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

OR in combination with AndAlso may wrongly translate #1102

Closed ursvogel closed 3 weeks ago

ursvogel commented 1 month ago

VB.Net input code

If (tb.Job = Job Or tb.Key = Job) AndAlso tb.ID = ID Then

Erroneous output

if (tb.Job == Job || tb.Key == Job && tb.ID == ID)

Expected output

if ((tb.Job == Job || tb.Key == Job) && tb.ID == ID)

Details

GrahamTheCoder commented 1 month ago

Thanks, I'm surprised to see that, definitely needs fixing!

Yozer commented 1 month ago

Hmm, I just tried to convert this on website

Imports System
Public Class C
    Public Class Dto
        Public Job As Double
        Public ID As Integer
        Public Key as Double
    End Class
    Public Sub M(tb as Dto)
        Dim Job As Double = ""
        Dim ID As Integer = 1
        If (tb.Job = Job Or tb.Key = Job) AndAlso tb.ID = ID Then
            System.Console.WriteLine("1")
        End If
    End Sub
End Class

and the result is

if (tb.Job == Job | tb.Key == Job && tb.ID == ID)

Works as expected to me. | has higher precedence than && so simplifier skiped brackets.

GrahamTheCoder commented 1 month ago

I had actually expected bitwise to take precedence yeah. I did try comparing outputs for the whole truth table and found that it does behave differently, but only tried it in vb. So maybe c# is different.

GrahamTheCoder commented 3 weeks ago

C sharp operator precedence prioritises the pipe operator: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/#operator-precedence