Browse Source

Fix #1906: RRs for default expressions of primitive types should use the correct constant value.

VB's `New Long()` is emitted as `initobj` whereas C#'s `default(long)` is emitted as `ldc.i4.0; conv.i8`.
pull/1914/head
Siegfried Pammer 5 years ago
parent
commit
6973dec0ef
  1. 2
      ICSharpCode.Decompiler.Tests/ICSharpCode.Decompiler.Tests.csproj
  2. 9
      ICSharpCode.Decompiler.Tests/TestCases/VBPretty/Issue1906.cs
  3. 6
      ICSharpCode.Decompiler.Tests/TestCases/VBPretty/Issue1906.vb
  4. 6
      ICSharpCode.Decompiler.Tests/VBPrettyTestRunner.cs
  5. 5
      ICSharpCode.Decompiler/CSharp/ExpressionBuilder.cs

2
ICSharpCode.Decompiler.Tests/ICSharpCode.Decompiler.Tests.csproj

@ -82,6 +82,7 @@ @@ -82,6 +82,7 @@
<Compile Include="DisassemblerPrettyTestRunner.cs" />
<Compile Include="TestCases\Correctness\StringConcat.cs" />
<Compile Include="TestCases\Ugly\NoLocalFunctions.cs" />
<Compile Include="TestCases\VBPretty\Issue1906.cs" />
<Compile Include="TestCases\VBPretty\Select.cs" />
<None Include="TestCases\ILPretty\WeirdEnums.cs" />
<Compile Include="TestCases\ILPretty\ConstantBlobs.cs" />
@ -93,6 +94,7 @@ @@ -93,6 +94,7 @@
<Compile Include="TestCases\Ugly\NoForEachStatement.cs" />
<None Include="TestCases\Ugly\NoExtensionMethods.Expected.cs" />
<Compile Include="TestCases\Ugly\NoExtensionMethods.cs" />
<None Include="TestCases\VBPretty\Issue1906.vb" />
<None Include="TestCases\VBPretty\Select.vb" />
<None Include="TestCases\VBPretty\VBCompoundAssign.cs" />
<Compile Include="TestCases\Pretty\ThrowExpressions.cs" />

9
ICSharpCode.Decompiler.Tests/TestCases/VBPretty/Issue1906.cs

@ -0,0 +1,9 @@ @@ -0,0 +1,9 @@
using System;
public class Issue1906
{
public void M()
{
Console.WriteLine(Math.Min(Math.Max(long.MinValue, default(long)), long.MaxValue));
}
}

6
ICSharpCode.Decompiler.Tests/TestCases/VBPretty/Issue1906.vb

@ -0,0 +1,6 @@ @@ -0,0 +1,6 @@
Imports System
Public Class Issue1906
Public Sub M()
Console.WriteLine(Math.Min(Math.Max(Int64.MinValue, New Long), Int64.MaxValue))
End Sub
End Class

6
ICSharpCode.Decompiler.Tests/VBPrettyTestRunner.cs

@ -78,6 +78,12 @@ namespace ICSharpCode.Decompiler.Tests @@ -78,6 +78,12 @@ namespace ICSharpCode.Decompiler.Tests
Run(options: options | CompilerOptions.Library);
}
[Test]
public void Issue1906([ValueSource(nameof(defaultOptions))] CompilerOptions options)
{
Run(options: options | CompilerOptions.Library);
}
void Run([CallerMemberName] string testName = null, CompilerOptions options = CompilerOptions.UseDebug, DecompilerSettings settings = null)
{
var vbFile = Path.Combine(TestCasePath, testName + ".vb");

5
ICSharpCode.Decompiler/CSharp/ExpressionBuilder.cs

@ -536,14 +536,17 @@ namespace ICSharpCode.Decompiler.CSharp @@ -536,14 +536,17 @@ namespace ICSharpCode.Decompiler.CSharp
{
Expression expr;
IType constantType;
object constantValue;
if (type.IsReferenceType == true || type.IsKnownType(KnownTypeCode.NullableOfT)) {
expr = new NullReferenceExpression();
constantType = SpecialType.NullType;
constantValue = null;
} else {
expr = new DefaultValueExpression(ConvertType(type));
constantType = type;
constantValue = CSharpResolver.GetDefaultValue(type);
}
return expr.WithRR(new ConstantResolveResult(constantType, null));
return expr.WithRR(new ConstantResolveResult(constantType, constantValue));
}
protected internal override TranslatedExpression VisitSizeOf(SizeOf inst, TranslationContext context)

Loading…
Cancel
Save