Skip to content

VB -> C#: Converter ignores the three-valued logic for nullable types and relational operators. #840

@Yozer

Description

@Yozer

VB.Net input code

Public Sub Main()
    Dim x As Integer? = Nothing
    If x <> 10 Then
        Console.WriteLine("Fail!")
    Else
        Console.WriteLine("Ok!")
    End If
End Sub

This will print OK because: x <> 10 results in Nothing with Boolean? type.
Nothing is False so code will execute Else statement.

Erroneous output

public static void Main()
{
    int? x = default;
    if (x != 10 == true) {
        Console.WriteLine("Fail!");
    }
    else {
        Console.WriteLine("Ok!");
    }
}

Here the code will incorrectly print Fail because: x != 10 results in true and true is equal to true.

Expected output

  public static void Main()
  {
      int? x = default;
      if ((x == null ? null : x != 10) == true) {
          Console.WriteLine("Fail!");
      }
      else {
          Console.WriteLine("Ok!");
      }
  }

VB uses three-valued logic for nullable types. Any relational comparison (=, <>, >, >=, <, <=) that involves nullable types results in Boolean?.
If lhs or rhs is null then the entire expression will be null.
The expected output produces ugly code but I'm not sure if it's possible to maintain the same logic using something simpler as we need to keep bool? as the result of the comparison.
I will take a look if there is something in Microsoft.VisualBasic.dll that could simplify the code.

Comparing Nullable Types

Details

  • Product in use: VS extension
  • Version in use: b5e798b
  • Did you see it working in a previous version, which?
  • Any other relevant information to the issue, or your interest in contributing a fix.

Metadata

Metadata

Assignees

Labels

VB -> C#Specific to VB -> C# conversionoutput logic errorA bug where the converted output behaves differently to the input code

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions