Browse Source

Add VBPretty fixtures for VB anonymous types and queries

VBPretty had no coverage of VB's anonymous types, so nothing caught that their
use sites decompiled to the raw metadata names while their definitions were
hidden from the output. The expected C# is written as it should read once the
generated-name predicates agree with each other; it fails until then.

Roslyn 2.10 targeting .NET Core 2.2 is branched off with #if: there the query
operator calls are not restored to extension-method syntax, so no query
expression is formed and the lowered form survives.

Assisted-by: Claude:claude-fable-5:Claude Code
pull/3964/head
Siegfried Pammer 1 month ago committed by Siegfried Pammer
parent
commit
0f10ee1b9b
  1. 1
      ICSharpCode.Decompiler.Tests/ICSharpCode.Decompiler.Tests.csproj
  2. 89
      ICSharpCode.Decompiler.Tests/TestCases/VBPretty/VBAnonymousTypes.cs
  3. 62
      ICSharpCode.Decompiler.Tests/TestCases/VBPretty/VBAnonymousTypes.vb
  4. 7
      ICSharpCode.Decompiler.Tests/VBPrettyTestRunner.cs

1
ICSharpCode.Decompiler.Tests/ICSharpCode.Decompiler.Tests.csproj

@ -149,6 +149,7 @@ @@ -149,6 +149,7 @@
<None Include="TestCases\VBPretty\Issue1906.vb" />
<None Include="TestCases\VBPretty\Issue2192.vb" />
<None Include="TestCases\VBPretty\Select.vb" />
<None Include="TestCases\VBPretty\VBAnonymousTypes.vb" />
<None Include="TestCases\VBPretty\ParameterizedProperties.vb" />
<None Include="TestCases\ILPretty\Unsafe.il" />
<None Include="TestCases\ILPretty\Issue1389.il" />

89
ICSharpCode.Decompiler.Tests/TestCases/VBPretty/VBAnonymousTypes.cs

@ -0,0 +1,89 @@ @@ -0,0 +1,89 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using Microsoft.VisualBasic.CompilerServices;
[StandardModule]
public sealed class VBAnonymousTypes
{
public static void MutableAnonymousType()
{
var anon = new {
Value = 1,
Name = "test"
};
Console.WriteLine(anon.Value);
Console.WriteLine(anon.Name);
}
public static void KeyAnonymousType()
{
var anon = new {
Value = 1,
Name = "test"
};
Console.WriteLine(anon.Value);
Console.WriteLine(anon.Name);
}
public static void AnonymousTypeAsArgument()
{
Console.WriteLine(new {
Value = 1,
Name = "test"
}.ToString());
}
public static void SelectAnonymousType(IEnumerable<int> items)
{
var enumerable = items.Select([SpecialName] (int i) => new {
Value = i,
Square = checked(i * i)
});
foreach (var item in enumerable)
{
Console.WriteLine(item.Value);
Console.WriteLine(item.Square);
}
}
public static void LetWhereSelect(IEnumerable<int> items)
{
var enumerable = from i in items
let square = checked(i * i)
where square > 4
select new { i, square };
foreach (var item in enumerable)
{
Console.WriteLine(item.i);
Console.WriteLine(item.square);
}
}
public static void JoinSelect(IEnumerable<int> left, IEnumerable<int> right)
{
var enumerable = from x in left
join y in right on x equals y
select new { x, y };
foreach (var item in enumerable)
{
Console.WriteLine(item.x);
Console.WriteLine(item.y);
}
}
public static void OrderBySelect(IEnumerable<int> items)
{
var enumerable = from i in items
let doubled = checked(i * 2)
orderby doubled
select new { i, doubled };
foreach (var item in enumerable)
{
Console.WriteLine(item.i);
Console.WriteLine(item.doubled);
}
}
}

62
ICSharpCode.Decompiler.Tests/TestCases/VBPretty/VBAnonymousTypes.vb

@ -0,0 +1,62 @@ @@ -0,0 +1,62 @@
Imports System
Imports System.Collections.Generic
Imports System.Linq
Public Module VBAnonymousTypes
Public Sub MutableAnonymousType()
Dim value = New With {.Value = 1, .Name = "test"}
Console.WriteLine(value.Value)
Console.WriteLine(value.Name)
End Sub
Public Sub KeyAnonymousType()
Dim value = New With {Key .Value = 1, Key .Name = "test"}
Console.WriteLine(value.Value)
Console.WriteLine(value.Name)
End Sub
Public Sub AnonymousTypeAsArgument()
Console.WriteLine(New With {Key .Value = 1, Key .Name = "test"}.ToString())
End Sub
Public Sub SelectAnonymousType(items As IEnumerable(Of Integer))
Dim query = From i In items
Select New With {Key .Value = i, Key .Square = i * i}
For Each item In query
Console.WriteLine(item.Value)
Console.WriteLine(item.Square)
Next
End Sub
Public Sub LetWhereSelect(items As IEnumerable(Of Integer))
Dim query = From i In items
Let square = i * i
Where square > 4
Select i, square
For Each item In query
Console.WriteLine(item.i)
Console.WriteLine(item.square)
Next
End Sub
Public Sub JoinSelect(left As IEnumerable(Of Integer), right As IEnumerable(Of Integer))
Dim query = From x In left
Join y In right On x Equals y
Select x, y
For Each item In query
Console.WriteLine(item.x)
Console.WriteLine(item.y)
Next
End Sub
Public Sub OrderBySelect(items As IEnumerable(Of Integer))
Dim query = From i In items
Let doubled = i * 2
Order By doubled
Select i, doubled
For Each item In query
Console.WriteLine(item.i)
Console.WriteLine(item.doubled)
Next
End Sub
End Module

7
ICSharpCode.Decompiler.Tests/VBPrettyTestRunner.cs

@ -125,6 +125,13 @@ namespace ICSharpCode.Decompiler.Tests @@ -125,6 +125,13 @@ namespace ICSharpCode.Decompiler.Tests
await Run(options: options | CompilerOptions.Library);
}
[Test]
public async Task VBAnonymousTypes([ValueSource(nameof(defaultOptions))] CompilerOptions options)
{
IgnoreIfVbRuntimeSubstituted(options);
await Run(options: options | CompilerOptions.Library);
}
[Test]
public async Task Issue1906([ValueSource(nameof(defaultOptions))] CompilerOptions options)
{

Loading…
Cancel
Save