Skip to content

C# -> VB: most pattern matching fails to convert #1281

Description

@gherards99

Input code

class Picture
{
}

class TestClass
{
    bool NegatedType(object o) => o is not string;

    bool NegatedNamedType(object o) => o is not Picture;

    int NegatedDeclaration(object o)
    {
        if (o is not string s) return -1;
        return s.Length;
    }

    bool Relational(int i) => i is > 0;

    bool ValueConstant(int i) => i is 5;
}

Erroneous output

Friend Class Picture
End Class

Friend Class TestClass
    Private Function NegatedType(o As Object) As Boolean
        ''' Cannot convert IsPatternExpressionSyntax, System.ArgumentOutOfRangeException: ... (Parameter 'node')
        ''' Actual value was not string.
    End Function

    Private Function NegatedNamedType(o As Object) As Boolean
        ''' Cannot convert IsPatternExpressionSyntax, System.ArgumentOutOfRangeException: ... (Parameter 'node')
        ''' Actual value was not Picture.
    End Function

    ''' Cannot convert MethodDeclarationSyntax, System.ArgumentOutOfRangeException: ... (Parameter 'node')
    ''' Actual value was not string s.

    Private Function Relational(i As Integer) As Boolean
        ''' Cannot convert IsPatternExpressionSyntax, System.ArgumentOutOfRangeException: ... (Parameter 'node')
        ''' Actual value was > 0.
    End Function

    Private Function ValueConstant(i As Integer) As Boolean
        Return i Is 5
    End Function
End Class

Stack traces trimmed for readability. NegatedDeclaration loses its whole body, because the throw comes from the hoisting path in CommonConversions.ConvertToVariableDeclaratorOrNull rather than from the visitor.

ValueConstant is the case worth separating: no error is reported, and Is compares references in VB, so i Is 5 does not compile against a value type. The others at least say something.

Expected output

Friend Class Picture
End Class

Friend Class TestClass
    Private Function NegatedType(o As Object) As Boolean
        Return TypeOf o IsNot String
    End Function

    Private Function NegatedNamedType(o As Object) As Boolean
        Return TypeOf o IsNot Picture
    End Function

    Private Function NegatedDeclaration(o As Object) As Integer
        Dim s As String = Nothing
        If CSharpImpl.__Assign(s, TryCast(o, String)) Is Nothing Then Return -1
        Return s.Length
    End Function

    Private Function Relational(i As Integer) As Boolean
        Return i > 0
    End Function

    Private Function ValueConstant(i As Integer) As Boolean
        Return i = 5
    End Function
End Class

VisitIsPatternExpression handles only DeclarationPatternSyntax and ConstantPatternSyntax and throws for anything else, so type patterns, negated patterns and relational patterns all fail. They have direct VB equivalents, so these read as gaps rather than language differences.

Details

  • Product in use: the NuGet library, called through ProjectConversion.ConvertSingleAsync.
  • Version in use: master at 2606077. Also reproduces on NuGet 10.0.1.923
  • Did you see it working in a previous version, which? No
  • Any other relevant information to the issue, or your interest in contributing a fix.
    I have a branch with the fix and six characterization tests, full suite green: https://github.com/gherards99/CodeConverter/tree/vb-pattern-matching

Two related gaps are deliberately left out of it, since both need the tested expression evaluated once rather than repeated, which means introducing a temporary: property patterns (o is T { P: true }, l is { Count: > 0 }, same ArgumentOutOfRangeException) and ??= (NotSupportedException: CoalesceAssignmentExpression is not supported!). I would rather raise those separately if you agree with the approach here.

Target-typed new is #1112 and the branch covers it too. The branch also makes #983 report the failure instead of silently returning nothing.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    C# -> VBSpecific to C# -> VB conversion

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions