C# -> VB: most pattern matching fails to convert
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 35/100
Research direction
Start with VisitIsPatternExpression and the hoisting path in CommonConversions.ConvertToVariableDeclaratorOrNull, then review the six characterization tests on the linked branch. Done means type, negated, relational, and constant patterns convert to the shown VB equivalents without errors or invalid Is comparisons, with the full suite remaining green.
Written by the indexing model from the issue text.
Description
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 26060772. 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.
- Dominant language
- C#
- Stars
- 913
- Forks
- 244
- PR merge metrics
- No merged PRs in 30d
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from icsharpcode/CodeConverter
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
icsharpcode/CodeConverter#1271 · 1 comment ·
-
Difficulty 3/5 1-2 days Newbie friendliness 55/100
icsharpcode/CodeConverter#1275 ·
-
VB -> C#
Difficulty 3/5 1-2 days Newbie friendliness 62/100
icsharpcode/CodeConverter#1273 ·
-
VB -> C#: "Group By Into" Linq queries supressing aggregations ("Into" part) in the converted code OpenDifficult area VB -> C#
Difficulty 3/5 1-2 days Newbie friendliness 74/100
icsharpcode/CodeConverter#1272 ·
-
Difficulty 4/5 3-5 days Newbie friendliness 35/100
icsharpcode/CodeConverter#1270 ·
All issues in icsharpcode/CodeConverter
Similar issues
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
nightscout/nocturne#1425 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 70/100
RayWangQvQ/BiliBiliToolPro#1137 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 70/100